1、多点测温系统#include#include#include#include#define uchar unsigned char#define uint unsigned int/*定义变量区*/*sbit beep=P33;/*TLC1543端口定义*sbit CS=P34; /AD片选信号sbit DOUT=P25; /AD输出sbit ADIN=P26; /AD通道选择sbit CLOCK=P27; /AD时钟信号/*AT24C64存储器端口定义*/sbit scl=P24;sbit sda=P23;/*LCD1602端口定义*sbit lcden=P35; /LCD使能信号sbit
2、lcdrw=P36; /LCD读写选择信号sbit lcdrs=P37; /LCD数据/命令选择信号/*液晶显示内容*uchar code tab=“0123456789.“; /TLC1543显示字符串uchar code table2=“00-00 00:00:00“;uchar code table3=“00.0“;/*变量定义*uchar tlc15434;uint readTLC1543(uchar); /AD转换 quint a; /a为全局变量float average=0,sum=0;uchar ddd=0;float panduan,wendu;uint gaogao=0,g
3、aodi=0,digao=0,didi=0;float GA=99,DI=0;/*DS1302端口定义*sbit sclk=P22;sbit io=P21;sbit reset=P20;uchar disp_buf12=0x00; /定义显示缓冲区uchar time_buf7=0,0,0x12,0,0,0,0; /DS1302时间缓冲区,存放秒、分、时、日、月、星期、年uchar temp_2=0; /用来存放设置时的时、分的中间值uchar code tabshu=0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x1
4、2,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x70,0x71,0x7
5、2,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99;uchar code tabshu1=0,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0;/*uchar code tabshu2=0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0
6、x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F;*/*uchar code tabshu1=0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
7、,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B
8、,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97
9、,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3
10、,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,0xE0,0xE1,0xE2,0x43,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,;*/uchar *p=tabshu1;/uchar *q=tabshu2;/时间设置函数的全局变量uint yuegao=0,yuedi=0,rigao=0
11、,ridi=0,shigao=0,shidi=0,fengao=0,fendi=0,miaogao=0,miaodi=0;uchar yue,ri,shi,fen,miao;uchar idata z1,z2,z3,z4,z5,z6,z7,z8,z9,z10,z11,z12,z13;/*函数声明区*void lcd_disp(); /LCD显示函数void lcd_init(); /LCD初始化函数void write_com(uchar); /写命令函数void write_data(uchar); /写数据函数void key_set(uchar); /初始设置温度上下限uchar key
12、_scan(); /键盘扫描函数/void cunchu();void cunchu_shineng(uchar);void lishicx(uchar);/*延时函数*void delay(uint k)uint i,j;for(i=k;i0;i-)for(j=50;j0;j-);/*AT24C64*/*void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);*/void delay_c() /存储延时 ; void start() /开始信号sda=1;delay_c();scl=1;delay_c();sda=0;delay_c
13、();void stop() /停止sda=0;delay_c();scl=1;delay_c();sda=1;delay_c();void respons() /应答uchar i;scl=1;delay_c();while(sda=1)a-)for(b=100;b0;b-);*/void write_add(uchar address,uchar address1,uchar date) /给定的地址写一个字节 start();write_byte_c(0xa0);respons();write_byte_c(address);respons();write_byte_c(address1
14、);respons();write_byte_c(date);respons();stop();uchar read_add(uchar address,uchar address1)/给定的地址读一个字节uchar date;start();write_byte_c(0xa0);respons();write_byte_c(address);respons();write_byte_c(address1);respons();start();write_byte_c(0xa1);respons();date=read_byte_c();stop();return date;/*/*LCD时间
15、显示*void lcd_disp_sj()/write_data(disp_buf0);/write_data(disp_buf1);/write_data( );write_data(disp_buf2);write_data(disp_buf3);write_data(-);write_data(disp_buf4);write_data(disp_buf5);write_data( );write_data(disp_buf6);write_data(disp_buf7);write_data(:);write_data(disp_buf8);write_data(disp_buf9);
16、write_data(:);write_data(disp_buf10);write_data(disp_buf11);z1=disp_buf2;z2=disp_buf3;z3=disp_buf4;z4=disp_buf5;z5=disp_buf6;z6=disp_buf7;z7=disp_buf8;z8=disp_buf9;z9=disp_buf10;z10=disp_buf11;/*DS1302子程序*/ void write_byte(uchar inbyte) /写 1字节数据函数uchar i;for(i=0;i1;uchar read_byte( ) /读 1字节数据函数uchar
17、 i,temp=0;io=1;for(i=0;i1;sclk=0;if(io=1)temp=temp|0x80;elsetemp=tempsclk=1;_nop_( );return(temp);void write_ds1302(uchar cmd,uchar indata) /写 DS1302函数,往 DS1302的某个地址写入数据sclk=0;reset=1;write_byte(cmd);write_byte(indata);sclk=0;reset=0;uchar read_ds1302(uchar addr) /读 DS1302函数,读 DS1302某地址的数据uchar back
18、data;sclk=0;reset=1;write_byte(addr);backdata=read_byte( );sclk=0;reset=0;return(backdata);void init_ds1302() /初始化 DS1302函数reset=0;sclk=0;write_ds1302(0x8e,0x00);/写保护控制字,开启写write_ds1302(0x90,0x06);/禁止充电write_ds1302(0x80,0x56);/写秒寄存器write_ds1302(0x82,0x34);/写分寄存器write_ds1302(0x84,0x21);/写时寄存器write_ds
19、1302(0x86,0x07);/写日寄存器write_ds1302(0x88,0x03);/写月寄存器write_ds1302(0x8C,0x99);/写年寄存器write_ds1302(0x8e,0x80);/写保护控制字,禁止写void get_time() /读取时间函数,负责读取当前的时间,并将读取到的时间转换为十进制数uchar sec,min,hour,day,mon,year;write_ds1302(0x8e,0x00); /控制命令,WP=0,允许写操作write_ds1302(0x90,0xab); /涓流充电控制sec=read_ds1302(0x81); /读取秒mi
20、n=read_ds1302(0x83); /读取分hour=read_ds1302(0x85); /读取时day=read_ds1302(0x87); /读取日mon=read_ds1302(0x89); /读取月year=read_ds1302(0x8d); /读取年time_buf0=sec/16*10+sec%16; /将读取到的十六进制转化为十进制time_buf1=min/16*10+min%16; /将读取到的十六进制转化为十进制time_buf2=hour/16*10+hour%16; /将读取到的十六进制转化为十进制time_buf3=day/16*10+day%16; /将读
21、取到的十六进制转化为十进制time_buf4=mon/16*10+mon%16; /将读取到的十六进制转化为十进制time_buf5=year/16*10+year%16; /将读取到的十六进制转化为十进制void lcd_conv(uchar in1,in2,in3,in4,in5,in6) /转换函数,负责将始终运行数据转换为适合 LCD显示的数据disp_buf0=in1/10+0x30; /年十位数据disp_buf1=in1%10+0x30; /年个位数据disp_buf2=in2/10+0x30; /月十位数据disp_buf3=in2%10+0x30; /月个位数据disp_bu
22、f4=in3/10+0x30; /日十位数据disp_buf5=in3%10+0x30; /日个位数据disp_buf6=in4/10+0x30; /时十位数据disp_buf7=in4%10+0x30; /时个位数据disp_buf8=in5/10+0x30; /分十位数据disp_buf9=in5%10+0x30; /分个位数据disp_buf10=in6/10+0x30; /秒十位数据disp_buf11=in6%10+0x30; /秒个位数据 /*时间设置函数开始*/*void time_set(uchar _key) uchar flag=0,y,i1=0,i2=0,i3=0,i4=
23、0,i5=0,s1num=0,z11,z12,z21,z22,z31,z32,z41,z42,z51,z52;if(_key=E)flag=1;write_ds1302(0x8e,0x00); /控制命令,WP=0,允许写操作/write_ds1302(0x80,0x80); /时钟振荡器停止write_com(0x80+0x40+4);write_data( );write_data( );write_data( );write_data( );write_data( );write_data( );write_data( );write_com(0x80);while(flag)y=key
24、_scan();while(y=B)|(y=F)if(y=B) y=0;if(s1num=1)write_com(0x0f);write_com(0x80+0x00);if(s1num=2)write_com(0x80+0x03);if(s1num=3)write_com(0x80+0x06);if(s1num=4)write_com(0x80+0x09);if(s1num=5)write_com(0x80+0x0C);if(s1num=1)/月调节if(i1=0)if(y=0)if(panduanDI)i+;s=key_scan();key_set(s);time_set(s);cunchu
25、_shineng(s);/ cunchu();lishicx(s);/*LCD初始化函数开始*/*void lcd_init()lcden=0;lcdrw=0;write_com(0x01); /LCD清屏write_com(0x38); /设置显示模式:16X2,5X7,8 位数据接口write_com(0x0c); /开显示,显示光标,光标闪烁write_com(0x06); /读写一个字符后,地址指针及光标加一,且光标加一整屏显示不移动write_com(0x80); /设置光标指针/*液晶写命令*/*void write_com(uchar com)lcdrs=0;P1=com;del
26、ay(5);lcden=1;delay(5);lcden=0;/*液晶写数据*/*void write_data(uchar date)lcdrs=1;P1=date;delay(5);lcden=1;delay(5);lcden=0;/*键盘扫描函数开始*/*uchar key_scan()uchar temp,key=16;/*扫描第一行*P0=0xfe;temp=P0;temp=tempwhile(temp!=0xf0)delay(5);temp=P0;temp=tempwhile(temp!=0xf0)temp=P0;switch(temp)case 0xee:key=4;break;
27、case 0xde:key=3;break;case 0xbe:key=2;break;case 0x7e:key=1;break;while(temp!=0xf0)temp=P0;temp=tempP0=0xfd;temp=P0;temp=tempwhile(temp!=0xf0)delay(5);temp=P0;temp=tempwhile(temp!=0xf0)temp=P0;switch(temp)case 0xed:key=8;break;case 0xdd:key=7;break;case 0xbd:key=6;break;case 0x7d:key=5;break;while(temp!=0xf0)