#include <ioCC2530.h> #include <string.h> #include <stdio.h> #include "DHT11.h" #include "OLED.h" uchar ExternFlag=0; void clock_setup(void) { CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振 while(CLKCONSTA & 0x40); //等待晶振稳定 CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ } void init_sensor_io(void) { //1.设置IO为普通IO P0SEL |= 0xC0; P1SEL |= 0x07; //2.设置编码口为输入 P0DIR &= ~0xC0; P1DIR &= ~0x07; } uchar get_sensor_id(void) { volatile uchar temp = P0_6<<4|P0_7<<3|P1_0<<2|P1_1<<1|P1_2; return temp; } void set_p0_input(uint bit) { APCFG &= ~(1<<bit); //1.设置IO为普通IO P0SEL &= ~(1<<bit); //2.设置编码口为输入 P0DIR &= ~(1<<bit); } uint16 get_aout_value(void) { uint16 reading=0; //1.使能AD通道P0.0 ADCCFG |= 0x01; //2.设置转换方式,启动转换 ADCCON3 = 0x80; //3.等待转换完成 while (!(ADCCON1 & 0x80)); //4.关闭转换 ADCCFG &= (0x01 ^ 0xFF); //5.读取结果 reading = ADCH; reading |= (uint16) (ADCH << 8); reading >>= 8; return (reading); } void main(void) { uchar temp8; uchar buf[16]; uchar* sensor[]={"光照强度","雨滴大小","空气质量","烟雾浓度"}; clock_setup(); //系统时钟设置,精确延时必须要设置 Delay_ms(1000); //让设备稳定 port_init(); initial_lcd(); clear_screen(); //清屏 disp_string_8x16_16x16(1,1,"武汉唯众智创科技"); //显示字符串,括号里的参数分别为(PAGE,列,字符串指针) disp_string_8x16_16x16(3,1,"----实验2.7-----"); Delay_ms(1000); while(1) { clear_screen(); temp8 = get_sensor_id(); switch(temp8){ case 4: //雨滴大小 sprintf(buf,"SID:%d %s",temp8,sensor[1]); break; case 8: //空气质量 sprintf(buf,"SID:%d %s",temp8,sensor[2]); break; case 25: //烟雾浓度 sprintf(buf,"SID:%d %s",temp8,sensor[3]); break; case 26: //光照强度 sprintf(buf,"SID:%d %s",temp8,sensor[0]); break; default: disp_string_8x16_16x16(1,1,"请插入传感器"); disp_string_8x16_16x16(3,1,"----实验2.7-----"); Delay_ms(1000); continue; } disp_string_8x16_16x16(1,1,buf); //显示字符串,括号里的参数分别为(PAGE,列,字符串指针) sprintf(buf,"模拟量:%d",get_aout_value()); disp_string_8x16_16x16(3,1,buf); Delay_ms(1000); //延时,1S读取1次 } } |