临近毕业,有朋友找我帮忙做个无线温度变送器,想到大学四年就这么过去了,到最后其实没学到什么真正的看家手段,有些遗憾,但是做点简单的制作还是没有问题的。于是就动手,顺便发个帖。
系统的功能是,利用pt100热电阻采集温度数据,利用电阻电桥来获取准确的电压值,利用AD转换芯片tlc549进行AD转换,然后将采集的温度数据通过ZigBee模块发送到主机部分,利用LCD1602显示,这里我是每分钟对发送数据存储一次,一个小时更新一次,也可以选择片外存储器,能够容纳更多的数据。
制作出来的实物图如下:
无线温度数据传输
温度测试
整体外观
看温度采集部分的程序。
- #include <STC89C5xRC.H>
- #include <intrins.h>
- #define uchar unsigned char //无符路字符型 宏定义 变量范围0~255
- #define uint unsigned int //无符路整型 宏定义 变量范围0~65535
- sbit CS=P0^0; //tlc549片选
- sbit CLK=P0^2; //tlc549时钟
- sbit DAT=P0^1; //tlc549数据
- uchar bdata ADCdata;
- sbit ADbit=ADCdata^0; //定义可为选址变量
- uchar n,AD_DATA;
- uchar temper[3]; //一次温度存储数组
- uchar Data_start[]={0xFE,0X08,0x91,0x90,0xBB,0x37};
- uchar Data_end[]={0xFF};
- /*·····················1毫秒延时函数···························*/
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /************************读取温度电压函数*********************************/
- uchar TLC549ADC(void)
- {
- uchar i;
- CS=1;
- CLK=0;
- DAT=1;
- CS=0;
- for(i=0;i<8;i++)
- {
- CLK=1;
- // Delay_MS(5);
- _nop_(); _nop_();_nop_();
- _nop_();
- ADbit=DAT;
- ADCdata<<=1; //AD转换
- CLK=0;
- // Delay_MS(5);
- _nop_(); _nop_();_nop_();
- }
- return (ADCdata);
- }
- /*························串口发送初始化函数·····················*/
- void InitUART(void)
- {
- SCON=0x50; //串口工作方式1,8位UART,波特率可变
- TH2=0xFF;
- TL2=0xFD; //波特率:115200 晶振=11.0592MHz
- RCAP2H=0xFF;
- RCAP2L=0xFD; //16位自动再装入值
- TCLK=1;
- RCLK=1;
- C_T2=0;
- EXEN2=0; //波特率发生器工作方式
- /*****************/
- TR2=1 ; //定时器2开始
- ES=1;
- EA=1;
-
- }
- /****************串行口1发送****************/
- void UART_1SendOneByte(unsigned char c)
- {
- if(c=='\n') //如果遇到\n就换行
- {
- //发送CR(carriage return)
- SBUF=0x0D;
- while(!TI); //等待发送完成
- TI=0;
-
- //发送 LF(NL line feed,new line)
- SBUF=0x0A;
- while(!TI); //等待发送完成
- TI=0;
- }
- else
- {
- SBUF=c;
- while(!TI); //等待发送完成
- TI=0;
- }
- }
- /*·································温度电压值发送函数·························································*/
- void send()
- { uchar n;
- temper[0]=AD_DATA/100;
- temper[1]=AD_DATA%100/10;
- temper[2]=AD_DATA%100%10;
- ES=0;
- for(n=0;n<6;n++)
- {
- UART_1SendOneByte(Data_start[n]);
- }
- UART_1SendOneByte(temper[0]);
- UART_1SendOneByte(temper[1]);
- UART_1SendOneByte(temper[2]);
- UART_1SendOneByte('
- );
- UART_1SendOneByte(0xFF);
- ES=1;
- }
- /*································主函数··············································*/
- void main()
- {
- InitUART(); //串行口初始化
- while(1)
- {
-
- AD_DATA=TLC549ADC(); //读取当前电压值A/D转换数据
- send(); //数据发送
-
- for(n=10;n>0;n--) //10秒发送延时
- {
- delay(1000);
- }
- }
- }
- 下面是主机部分程序。
- #include <STC89C5xRC.H>
- #include <intrins.h>
- #define uchar unsigned char //无符路字符型 宏定义 变量范围0~255
- #define uint unsigned int //无符路整型 宏定义 变量范围0~65535
- sbit E=P2^5;
- sbit RW=P2^6;
- sbit RS=P2^7;
- sbit key1=P1^1;
- sbit key2=P1^2;
- sbit beep=P1^3;
- uchar code table[] ="C_temper: "; //当前温度显示
- uchar code table1[]="P_temper: "; //上一时刻温度显示
- uchar temper[10],temper1[10]; //显示温度数据临时存放
- uchar num1=0,flag=0,dat[70]; //计数值,按键标志位,历史数据存储数组
- uint time,AD_value,flag2,rev; //每分钟计数
- uint temp1=0,temp2=0,count1=0;
- uchar Receive_Buff1[12]; //接受数据缓存数组
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void key_scan()
- {
- if(key1==0)
- {
- delay(1);
- if(key1==0)
- {
- num1++;
- flag=1;
- while(!key1);
- }
- }
- if(key2==0)
- {
- delay(1);
- if(key2==0)
- {
- num1=0;
- flag=0;
- while(!key2);
- }
- }
- if(num1>100)
- num1=0;
- }
- void wr_com(uchar com)
- {
- RS=0;
- RW=0;
- P0=com;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void wr_dat(uchar date)
- {
- RS=1;
- P0=date;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void init()
- {
- E=0;
- wr_com(0x38);
- wr_com(0x0c);
- wr_com(0x06);
- wr_com(0x01);
- }
- void display(unsigned char *p)//显示//
- {
- while(*p!='\0')
- {
- wr_dat(*p);
- p++;
- delay(1);
- }
- }
- void init_play()//初始化显示
- {
- wr_com(0x80);
- display(table);
- wr_com(0xc0);
- display(table1);
- }
- /*························串口发送初始化函数·····················*/
- void InitUART(void)
- {
- SCON=0x50; //串口工作方式1,8位UART,波特率可变
- TH2=0xFF;
- TL2=0xFD; //波特率:115200 晶振=11.0592MHz
- RCAP2H=0xFF;
- RCAP2L=0xFD; //16位自动再装入值
- TCLK=1;
- RCLK=1;
- C_T2=0;
- EXEN2=0; //波特率发生器工作方式
- /*****************/
- TR2=1 ; //定时器2开始
- ES=1;
- EA=1;
- }
- void init_count()
- {
- TMOD=0x61;
- TCON|=0X01;
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void AD_to_temper()
- { uint i;
-
- temp1=AD_value*100/25.6+32;//把热电阻上的电压值转为温度值,加补偿
- temper[0]=temp1/1000+0x30;
- temper[1]=(temp1/100)%10+0x30;
- temper[2]=(temp1%100)/10+0x30;
- temper[3]='.';
- temper[4]=(temp1%100)%10+0x30;
- temper[5]='C';
- if(flag==0)
- {
- temper1[0]=rev/1000+0x30;
- temper1[1]=(rev/100)%10+0x30;
- temper1[2]=(rev%100)/10+0x30;
- temper1[3]='.';
- temper1[4]=(rev%100)%10+0x30;
- temper1[5]='C';
- }
- if(flag==1)
- {
- temper1[0]=dat[num1-1]/1000+0x30;
- temper1[1]=(dat[num1-1]/100)%10+0x30;
- temper1[2]=(dat[num1-1]%100)/10+0x30;
- temper1[3]='.';
- temper1[4]=(dat[num1-1]%100)%10+0x30;
- temper1[5]='C';
- }
- for(i=0;i<6;i++)
- {
- wr_com(0xca+i);
- wr_dat(temper1[ i]); [ i]
- }
-
- for(i=0;i<6;i++)
- {
- wr_com(0x8a+i);
- wr_dat(temper[ i]); [ i]
- temper1[ i]=temper[ i];[ i][ i]
- }
- }
- /***************主函数*****************/
- void main()
- {
- init();
- init_play();//初始化显示
- InitUART(); //串行口初始化
- init_count();//初始化定时器
- while(1)
- {
- key_scan();
-
- AD_to_temper();
- if(time>=6000)
- {
- beep=0;
- time=0;
- temp2++;
- dat[temp2]=temp1;
- if(temp2>70)temp2=0;
- }
- }
- }
- /************串行口1中断处理函数*************/
- void UART_1Interrupt(void) interrupt 4
- {
- if(RI==1)
- {
- RI=0;
- ES=0;
- Receive_Buff1[count1++]=SBUF;
- if(flag2==2&&SBUF==0xFF)
- {
- count1=0;
- AD_value=Receive_Buff1[6]*100+Receive_Buff1[7]*10+Receive_Buff1[8];
- rev=temp1;
- }
- if(SBUF=='
- )
- {
- flag2=2;
- }
-
- ES=1;
- }
- }
- void timer0_isr(void) interrupt 1
- {
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- time++;
- }
复制代码
|