一、系统方案 本设计采用STC15单片机作为主控器,液晶1602显示,DS18B20采集温度,光敏电阻采集光照、按键设置温度上下限,测量温度小于下限,启动加热,测量温度大于上限,启动降温。
二、硬件设计 原理图如下: 三、单片机软件设计 1、首先是系统初始化 uint count=0; uint16 TempPhoto; uint8 strPhoto[6]; uint wendu=0; P3M0 = 0x00; P3M1 = 0x00; lcd_init(); //LCD1602初始化 delay_ms(10); //初始化后延时 DisplayListChar(0, 0, table1); //在LCD1602屏第一行显示table1内容 DisplayListChar(0, 1, table2); //在LCD1602屏第二行显示table2内容
InitADC(); InitUart(); //初始化串口 2、液晶显示程序 /************************************** 功能描述:LCD1602写命令函数 入口参数int8 com 返回值:无 ***************************************/ void lcd_wcom(uint8 com) { lcd1602_rs=0; //选择指令寄存器 lcd1602_rw=0; //选择写 P0=com; //把命令字送入P0 delay_ms(1); //延时一小会儿,让1602准备接收数据 lcd1602_en=1; //使能线电平变化,命令送入1602的8位数据口 lcd1602_en=0; }
/************************************** 功能描述:LCD1602写数据函数 入口参数:uint8 dat 返回值:无 ***************************************/ void lcd_wdat(uint8 dat) { lcd1602_rs=1; //选择数据寄存器 lcd1602_rw=0; //选择写 P0=dat; //把要显示的数据送入P0 delay_ms(1); //延时一小会儿,让1602准备接收数据 lcd1602_en=1; //使能线电平变化,数据送入1602的8位数据口 lcd1602_en=0; }
/************************************** 功能描述:LCD1602初始化函数 入口参数:无 返回值:无 ***************************************/ void lcd_init(void) { lcd_wcom(0x38); //8位数据,双列,5*7字形 lcd_wcom(0x0c); //开启显示屏,关光标,光标不闪烁 lcd_wcom(0x06); //显示地址递增,即写一个数据后,显示位置右移一位 lcd_wcom(0x01); //清屏 }
3、按键程序 void keyscan() //按键扫描 { if(K1==0) //功能键 { delayms(5); if(K1==0) { keyflag_1++; //键一按下,标志位加1 if(keyflag_1>3) keyflag_1=0; lcd_init(); if( keyflag_1==0) { DisplayListChar(0, 0, table1); //在LCD1602屏第一行显示table1内容 DisplayListChar(0, 1, table2); //在LCD1602屏第二行显示table2内容 } if( keyflag_1==1) { DisplayListChar(0, 0, " SET TEMP HIGH");
} if( keyflag_1==2) { DisplayListChar(0, 0, " SET TEMP LOW");
} while(!K1); } }
if(keyflag_1!=0) { if(K2==0) //限值加键 { delayms(5); //按键消除抖动判断 if(K2==0) {
if(keyflag_1==1) //进入设置模式 { max++; if(max==100) max=0;
} if(keyflag_1==2) //进入设置模式 { min++; if(min==100) min=0;
} while(!K2); //松手检测 } } if(K3==0) //限值减键 { delayms(5); //按键消除抖动判断 if(K3==0) {
if(keyflag_1==1) //进入设置模式 { max--; if(max==0) max=99;
} if(keyflag_1==2) //进入设置模式 { min--; if(min==0) min=99;
}
while(!K3); //松手检测 } } } } /*** 4、核心算法程序 int main(void) { uint count=0; uint16 TempPhoto; uint8 strPhoto[6]; uint wendu=0; P3M0 = 0x00; P3M1 = 0x00; lcd_init(); //LCD1602初始化 delay_ms(10); //初始化后延时 DisplayListChar(0, 0, table1); //在LCD1602屏第一行显示table1内容 DisplayListChar(0, 1, table2); //在LCD1602屏第二行显示table2内容
InitADC(); InitUart(); //初始化串口 Init_DS18B20();
while (1) { keyscan(); if(keyflag_1==0) { count++; if(count>10)//读取温度值 {
wendu=ReadTemperature(); count=0; } Disp_Temperature(wendu) ; memset(strPhoto, 0, sizeof(strPhoto)); //strTemp数组清零 TempPhoto =GetADCResultint(1); //实时读取P1.1通道的AD转换结果 TempPhoto=TempPhoto*5.0*20/1023;
strPhoto[0] = TempPhoto/1000+48; //千位 strPhoto[1] = (TempPhoto%1000)/100+48; //百位 strPhoto[2] = (TempPhoto%1000)%100/10+48; //十位
strPhoto[3] = (TempPhoto%10)+48; //个位
//在LCD1602上显示 DisplayOneChar(6, 0, strPhoto[0]); //在LCD1602屏第二行显示千位值 DisplayOneChar(7,0, strPhoto[1]); //在LCD1602屏第二行显示百位值 DisplayOneChar(8, 0, strPhoto[2]); //在LCD1602屏第二行显示十位值 DisplayOneChar(9, 0, strPhoto[3]); //在LCD1602屏第二行显示个位值 DisplayOneChar(10,0,'l'); DisplayOneChar(11,0,'u'); DisplayOneChar(12,0,'x');
} if(keyflag_1==1) { DisplayOneChar(6, 1, max/100+0x30); //在LCD1602屏第二行显示百位值 DisplayOneChar(7,1, max%100/10+0x30); //在LCD1602屏第二行显示十位值 DisplayOneChar(8, 1, max%10+0x30); //在LCD1602屏第二行显示个位值
} if(keyflag_1==2) { DisplayOneChar(6, 1, min/100+0x30); //在LCD1602屏第二行显示百位值 DisplayOneChar(7,1, min%100/10+0x30); //在LCD1602屏第二行显示十位值 DisplayOneChar(8, 1, min%10+0x30); //在LCD1602屏第二行显示个位值 }
SendString("guangz:"); SendData( strPhoto[0]);//显示湿度值 SendData( strPhoto[1]); SendData( strPhoto[2]); SendData( strPhoto[3]); SendData( 'l'); SendData( 'u'); SendData( 'x'); SendData( 0x0d); SendData( 0x0a);
} } 四、 proteus仿真设计 Proteus软件是一款应用比较广泛的工具,它可以在没有硬件平台的基础上通过自身的软件仿真出硬件平台的运行情况,这样就可以通过软件仿真来验证我们设计的方案有没有问题,如果有问题,可以重新选择器件,连接器件,直到达到我们设定的目的,避免我们搭建实物的时候,如果当初选择的方案有问题,我们器件都已经焊接好了,再去卸载下去,再去焊接新的方案的器件,测试,这样会浪费人力和物力,也给开发者带来一定困惑,Proteus仿真软件就很好的解决这个问题,我们在设计之初,就使用该软件进行模拟仿真,测试,选择满足我们设计的最优方案。最后根据测试没问题的仿真图纸,焊接实物,调试,最终完成本设计的作品。
|