|
硬件设计包括如下模块: n 最小系统模块: STC89C52RC正常工作的最小系统; n 模拟输入模块:一路光照、一路电压; n 数字输入模块:两路温度、一路湿度、多个按键; n 模拟输出模块:一路光照; n 数字输出模块:一路PWM波控制直流电机、蜂鸣器、多个工作状态指示灯; n 液晶显示模块:一块12864LCD显示输出; n 串口通信模块:CH340T模块电路 软件设计包括如下模块:
n C代码:AD采集、数字传感器驱动、DA输出、按键与液晶控制、串口收发处理等
- #include<reg52.h>
- #include<stdio.h>
-
- #define uchar unsigned char
- #define uint unsigned int
- #include"12864.c"
- #include"PCF8591ADDA.c"
- #include"DS18B20.C"
- #include"DTH11.C"
- sbit led0=P1^0;//通信测试灯
- sbit led1=P1^1;
- sbit led2=P1^2;
- sbit led3=P1^3;
- sbit MOTOR=P1^4;
- uchar ren_flag,ren_buff;
- uchar buff[8];
- char *get="Get it.";
- char *sendflag="sendflag";
- unsigned char num; //DA数模输出变量
- unsigned char ADtemp; //定义中间变量
- void serial_init(void) //串口初始化函数
- {
- TMOD=0x20;//00100000
- PCON=0x00;//波特率不加倍
- SCON=0x50;//01010000 方式1,允许接收
- TH1=0xfd; //波特率9600
- TL1=0xfd;
- EA=1;
- ES=1;
- TR1=1;
- ET1=0;
-
- }
- void serial_send(char*parr) //串口数据发送函数
- {
- do
- {
- SBUF=*parr++; //发送数据
- while(!TI); //等待发送完成标志为1
- TI=0; //标志清0
- }while(*parr); //保持循环直到字符为‘\0’
- }
- void DAQ_display_sender()
- {
- //DS18B20
-
- show_char(0,0,"Data acquisition");
- tempchange(); //温度转换函数
- get_temp();
- show_char(1,2," ");
- show_char(1,0,"TEM:");
-
- sprintf(buff,"%.8s",sendflag);
- serial_send(buff);
- sprintf(buff,"%.3f",f_temp);//将浮点型温度格式化为字符型
- show_char(1,2,buff); //获取温度并显示
- show_char(1,5,"℃");
- f_temp+=0x64;
- sprintf(buff,"%.3f",f_temp);
- serial_send(buff); //串口发送数据
-
- /*DA
- DAC(num); //DA输出,可以用LED模拟电压变化
- num++; //累加,到256后溢出变为0,往复循环。显示在LED上亮度逐渐变化
- */
-
- //DTH11
- RH();
- sprintf(buff,"%.1f",f_temp_T);//将浮点型温度格式化为字符型
- show_char(2,0,"T: ℃");
- show_char(2,1,buff);
- serial_send(buff);
- sprintf(buff,"%.1f",f_temp_RH);//将浮点型温度格式化为字符型
- show_char(2,4,"R: %");
- show_char(2,5,buff);
- serial_send(buff);
- //AD
- if(ADFlag) //定时采集输入模拟量
- {
- ADFlag=0;
- ADtemp=ReadADC(0);
- TempData[0]=(ReadADC(0))/50;//处理0通道电压显示
- TempData[1]=((ReadADC(0))%50)/10;
-
- ADtemp=ReadADC(2);
- TempData[4]=(ReadADC(2))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
- TempData[5]=((ReadADC(2))%50)/10;
-
- /* ADC通道2、通道4采集
- ADtemp=ReadADC(1);
- TempData[2]=(ReadADC(1))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
- TempData[3]=((ReadADC(1))%50)/10;
-
-
- ADtemp=ReadADC(3);
- TempData[6]=(ReadADC(3))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
- TempData[7]=((ReadADC(4))%50)/10;
- */
- }
- ADFlag=1;
-
- f_temp=TempData[4]+TempData[5]*0.1;//通道3光敏电阻电压值
- sprintf(buff,"%.1f",f_temp);//
- show_char(3,0,"O: V");
- show_char(3,1,buff);
- serial_send(buff);
-
- f_temp=TempData[0]+TempData[1]*0.1;
- sprintf(buff,"%.1f",f_temp);//将浮点型温度格式化为字符型
- show_char(3,4,"V: V");
- show_char(3,5,buff);
- serial_send(buff);//串口发送数据
-
- /* ADC通道3、通道4处理显示信息
- f_temp=TempData[4]+TempData[5]*0.1;
- sprintf(buff,"%.1f",f_temp);//将浮点型温度格式化为字符型
- show_char(3,0,"3: V");
- show_char(3,1,buff);
- serial_send(buff); //串口发送数据
-
- f_temp=TempData[6]+TempData[7]*0.1;
- sprintf(buff,"%.1f",f_temp);//将浮点型温度格式化为字符型
- show_char(3,4,"4: V");
- show_char(3,5,buff);
- serial_send(buff); //串口发送数据
- */
-
-
- }
- void main() //主函数
- {
-
-
- cmg();// 数码管锁存函数 关时钟DS1302
- wela=0;
- dula=0;
- lcd_init(); //初始化LCD
- serial_init();
- MOTOR=0;
- while(1)
- {
- if(ren_flag==0)
- {
- led0=1;
- MOTOR=0;
- show_char(0,0,"Wait for command");
- show_char(1,0," ");
- show_char(2,0," ");
- show_char(3,0," ");
- }
- if(ren_flag==1) //测试通信
- {
- led0=0;
- delay_ms(10);
- led0=1;
- MOTOR=0;
- show_char(0,0,"Connection Succe");
- show_char(1,0,"ssful! ");
- show_char(2,0," ");
- show_char(3,0," ");
- sprintf(buff,"%.8s",get);
- serial_send(buff);
- }
- if(ren_flag==2)//
- {
- MOTOR=0;
- DAQ_display_sender();
- }
-
- if(ren_flag==3)
- {
- MOTOR=1;
- DAQ_display_sender();
- }
- if(ren_flag==4)
- {
- MOTOR=0;
- DAQ_display_sender();
- }
-
- if(ren_flag==5)
- {
- show_char(0,0,"Stop DAQ and sen");
- show_char(1,0,"ding! ");
- show_char(2,0," ");
- show_char(3,0," ");
- MOTOR=0;
- led0=1;
- }
- }
- }
- void ser() interrupt 4
- {
-
- if(TI)
- ;
- else
- {
- if(RI)
- lcd_clean();
- ES=0;
- RI=0;
- ren_buff=SBUF;
- switch(ren_buff)
- {
- case 0x31: ren_flag=1; break; //接受到1,第一个LED亮
- case 0x32: ren_flag=2; break; //接受到2,第二个LED亮
- case 0x33: ren_flag=3; break; //接受到3,第三个LED亮
- case 0x34: ren_flag=4;break; //接受到4,第四个LED亮
- case 0x35: ren_flag=5;break; //接受到5,第五个LED亮
- case 0x36: ren_flag=6;break; //接受到5,第六个LED亮
- case 0x37: ren_flag=7;break; //接受到5,第七个LED亮
- case 0x38: ren_flag=8;break; //接受到5,第八个LED亮
- default: ren_flag=0;break; //接受到其它数据,蜂鸣器响
- led1=~led1;
- }
-
- ES=1;
- }
- }
复制代码
|
-
-
C源码.rar
59.77 KB, 下载次数: 13, 下载积分: 黑币 -5
下位机源代码
评分
-
查看全部评分
|