标题:
单片机多路温度采集、 显示与远程控制系统程序设计
[打印本页]
作者:
学习单片机啊丶
时间:
2020-2-18 22:50
标题:
单片机多路温度采集、 显示与远程控制系统程序设计
A8.JPG
(212.96 KB, 下载次数: 17)
下载附件
2020-2-18 22:49 上传
单片机源程序如下:
#include <reg52.h>//头文件
#include <intrins.h>
#include <math.h>
#define uchar unsigned char//宏定义
#define uint unsigned short int//宏定义
#define sint short int
#define nop _nop_()
void delay(uint z);//延时1MS
void delay1(uchar x2);//延时7us左右;
void write_Directive(uchar a);//写LCD指令
void write_Data(uchar a);//写LCD数据
void write_lcd_com_date(uchar addr,uchar date);//实现LCD写指令及数据
void LCD_init();//LCD初始化
void start(); //AT24C02开始程序
void send(uchar send_data);//AT24C02发送一个字节
uchar receive();//AT24C02接收一个程序
void ack();//AT24C02是否应答
void yack();//AT24C02应答
void nack();//AT24C02无应答
void stop();//AT24C02停止
void write(uchar xaddress,sint temp);//AT24C02写入温度
uint read(uchar daddress);//AT24C02读出温度
void send_str(sint cc,uchar chflag); // 传送字串
void DB18B20_init_1();//DB18B20_1复位
void write_DB18B20_1(uchar dat);//写DB18B20_1
uchar read_DB18B20_1();//读DB18B20_1
void DB18B20_init_1();//DB18B20_2复位
void write_DB18B20_1(uchar dat);//写DB18B20_2
uchar read_DB18B20_1();//读DB18B20_2
void tempzh_1(); //温度转换1
void tempzh_2(); //温度转换2
void tempread_1(); //读取温度1
void tempread_2(); //读取温度2
void temp(sint TEMPS,uchar t,bit fuflag);//显示温度函数
void readcc(); //读取存储芯片中最高及最低温度
void keyscan(); //键扫描程序
void write_ds1302(uchar addr, uchar date);//写DS1302
uchar read_ds1302(uchar addr);//读DS1302
void init_ds1302();//DS1302初始化
void time();//时间显示
void timekey();//时间部分功能键
void tempkey();//温度部分功能键
void shuma(uchar addr,uchar xz,bit flag);//1302调节函数及转换
void flash(uchar bz);//字符闪烁函数
void tempbj(); //温度比较
uchar bdata ch;//定义可位寻址空间,用于24c02的读取
sbit RS=P1^4;//1602数据/命令选择
sbit RW=P1^5;//1602读写命令选择
sbit E=P1^6;//1602使能端
sbit led=P0^1;//发光二极管
sbit beep=P0^0;//蜂鸣器
sbit SDA=P0^2;//24c02数据总线
sbit SCL=P0^3;//24c02时钟总线
sbit DQ1=P1^0;//温度传感器1
sbit DQ2=P3^2;//温度传感器2
sbit DSIO=P1^2;//1302输入输出口
sbit DSCLK=P1^1;//1302脉冲
sbit DSRST=P1^3;//1302复位
sbit key1=P3^7;//五个按键及其标志
sbit key2=P0^7;
sbit key3=P0^6;
sbit key4=P0^5;
sbit key5=P0^4;
bit k1flag=0;
bit k2flag=0;
bit k3flag=0;
bit k4flag=0;
bit k5flag=0;
sbit a0=ch^0;//定义ch各位
sbit a1=ch^1;
sbit a2=ch^2;
sbit a3=ch^3;
sbit a4=ch^4;
sbit a5=ch^5;
sbit a6=ch^6;
sbit a7=ch^7;
bit fuflag1=0;//负温度标志1
bit fuflag2=0;//负温度标志2
bit ackflag=0;//24c02应答是否标志
bit sendflag=0;//串口发送标志
sint temps1,temps2,tempd1,tempd2,tempx1,tempx2,uptem1,downtem1,uptem2,downtem2;//温度参数
uchar count=0,tcount=0,scount=0,t=0;
uchar secondh,secondl,minuteh,minutel,hourh,hourl,second,minute,hour;//时钟参数
uchar monthl,monthh,month,dayl,dayh,day,year,yearl,yearh,week;
uchar code DispTab_1[12]={'0','1','2','3','4','5','6','7','8','9',' ','-'};//1602:0-9 数字
uchar DispBuf[5]; //5字节的显示缓冲区
uchar code zzz[] = {"Made In Gao J C"};//定义字符串
uchar code cc1[4] = {"DQ1:"};
uchar code cc2[4] = {"DQ2:"};
uchar code cc3[14] = {"DQ1 is so high"};
uchar code cc4[13] = {"DQ1 is so low"};
uchar code cc5[14] = {"DQ2 is so high"};
uchar code cc6[13] = {"DQ2 is so low"};
void delay(uint z)//1ms延时
{
uchar i;
while(z--)
for(i = 120;i > 0;i--);
}
void delay1(uchar x2)//短延时,由于18b20
{
while(x2--);
}
void write_Directive(uchar a)//写LCD指令
{
RS = 0;
RW = 0;
delay(1);
E = 0;
P2 = a;
delay(1);
E = 1;
delay(1);
E = 0;
delay(1);
}
void write_Data(uchar a)//写LCD数据
{
RS = 1;
RW = 0;
delay(1);
E = 0;
P2 = a;
delay(1);
E = 1;
delay(1);
E = 0;
delay(1);
}
void write_lcd_com_date(uchar addr,uchar date)//实现LCD写指令及数据
{
write_Directive(addr);
delay(1);
write_Data(date);
}
void LCD_init()//LCD初始化
{
uchar i;
delay(15);
write_Directive(0x38);
delay(5);
write_Directive(0x38);
delay(5);
write_Directive(0x38);
write_Directive(0x01);
write_Directive(0x02);//光标复位
write_Directive(0x0c);//不显示光标
//write_Directive(0x0f);//显示光标
write_Directive(0x84);//显示字符串
for(i = 0;i < 7;i++)
write_Data(zzz[i]);
write_Directive(0xc3);
for(i = 8;i < 15;i++)
write_Data(zzz[i]);
}
void start() //AT24C02开始程序
{
SCL = 1;
SDA = 1;
nop;
nop;
SDA = 0;
nop;
nop;
SCL = 0;
}
void send(uchar send_data)//AT24C02发送一个字节
{
ch = send_data;
SDA = a7; SCL = 1; nop;nop; SCL = 0;
SDA = a6; SCL = 1; nop;nop; SCL = 0;
SDA = a5; SCL = 1; nop;nop; SCL = 0;
SDA = a4; SCL = 1; nop;nop; SCL = 0;
SDA = a3; SCL = 1; nop;nop; SCL = 0;
SDA = a2; SCL = 1; nop;nop; SCL = 0;
SDA = a1; SCL = 1; nop;nop; SCL = 0;
SDA = a0; SCL = 1; nop;nop; SCL = 0;
}
uchar receive()//AT24C02接收一个程序
{
SDA = 1;
SCL = 1; nop;nop; a7 = SDA; SCL = 0;
SCL = 1; nop;nop; a6 = SDA; SCL = 0;
SCL = 1; nop;nop; a5 = SDA; SCL = 0;
SCL = 1; nop;nop; a4 = SDA; SCL = 0;
SCL = 1; nop;nop; a3 = SDA; SCL = 0;
SCL = 1; nop;nop; a2 = SDA; SCL = 0;
SCL = 1; nop;nop; a1 = SDA; SCL = 0;
SCL = 1; nop;nop; a0 = SDA; SCL = 0;
return(ch);
}
void ack()//AT24C02是否应答
{
SDA = 1;
SCL = 1;
nop;
nop;
if(SDA)
ackflag = 1;
else
ackflag = 0;
SCL=0;
}
void yack()//AT24C02应答
{
SDA = 0;
SCL = 1;
nop;
nop;
SCL = 0;
SDA = 1;
}
void nack()//AT24C02无应答
{
SDA = 1;
SCL = 1;
nop;
nop;
SCL = 0;
SDA = 0;
}
void stop()//AT24C02停止
{
SDA = 0;
SCL = 1;
nop;
nop;
SDA = 1;
nop;
nop;
SCL = 0;
SDA = 0;
}
void write(uchar xaddress,sint temp)//AT24C02写入温度
{
uchar a,b;
do{
start();
send(0xa0);
ack();
} while(ackflag);
do{
send(xaddress);
ack();
}while(ackflag);
a = temp;
temp >>= 8;
b = temp;
do{
send(a);
ack();
}while(ackflag);
do{
send(b);
ack();
}while(ackflag);
stop();
}
uint read(uchar daddress)//AT24C02读出温度
{
uchar a,b;
sint temp;
do{
start();
send(0XA0);
ack();
}while(ackflag);
do{
send(daddress);
ack();
}while(ackflag);
do{
start();
send(0XA1);
ack();
}while(ackflag);
a = receive();
yack();
b = receive();
yack();
nack();
stop();
temp = b;
temp <<= 8;
temp |= a;
return temp;
}
void send_str(sint cc,uchar chflag) // 传送字串
{
uchar i,a,b,ch[4];
ES=0;
cc = abs(cc);
a = cc / 100;
b = cc % 100;
ch[1] = b;
ch[2] = a;
ch[3] = chflag;
for(i = 3;i > 0;i--){
SBUF = ch[i];
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
ES=1;
}
}
/*void serial() interrupt 4
{a=SBUF;
RI=0;
flag=1;
}
*/
void DB18B20_init_1()//DB18B20复位
{
DQ1 = 1;
delay1(20);
DQ1 = 0;
delay1(80);
DQ1 = 1;
delay1(15);
}
void write_DB18B20_1(uchar dat)//写DB18B20
{
uchar i;
for(i = 8;i > 0;i--){
DQ1 = 1;
delay1(10);
DQ1 = 0;
nop;
DQ1 = dat&0x01;
delay1(10);
DQ1 = 1;
dat >>= 1;
}
delay1(7);
}
uchar read_DB18B20_1()//读DB18B20
{
uchar i,date;
for(i = 8;i > 0;i--){
DQ1 = 0;
date >>= 1;
DQ1 = 1;
if(DQ1)
date |= 0x80;
delay(4);
}
return date;//返回温度值
}
void DB18B20_init_2()//DB18B20复位
{
DQ2=1;
delay1(20);
DQ2=0;
delay1(80);
DQ2=1;
delay1(15);
}
void write_DB18B20_2(uchar dat)//写DB18B20
{
uchar i;
for(i = 8;i > 0;i--){
DQ2 = 1;
delay1(10);
DQ2 = 0;
nop;
DQ2 = dat&0x01;
delay1(10);
DQ2 = 1;
dat >>= 1;
}
delay1(7);
}
uchar read_DB18B20_2()//读DB18B20
{
uchar i,date;
for(i = 8;i > 0;i--){
DQ2 = 0;
date >>= 1;
DQ2 = 1;
if(DQ2)
date |= 0x80;
delay(4);
}
return date;//返回温度值
}
void tempzh_1() //温度转换
{
DB18B20_init_1();//DB18B20复位
write_DB18B20_1(0xcc);//写DB18B20;0xcc 跳过 ROM 指令
write_DB18B20_1(0x44);//写DB18B20;0x44 启动温度转换指令
}
void tempzh_2() //温度转换
{
DB18B20_init_2();//DB18B20复位
write_DB18B20_2(0xcc);//写DB18B20;0xcc 跳过 ROM 指令
write_DB18B20_2(0x44);//写DB18B20;0x44 启动温度转换指令
}
void tempread_1()//温度读取
{
uint temp;
uchar a,b;
fuflag1 = 0;
DB18B20_init_1();//DB18B20复位
write_DB18B20_1(0xcc);//写DB18B20;0xcc 跳过 ROM 指令
write_DB18B20_1(0xbe);//写DB18B20;0xbe 读温度指令
a = read_DB18B20_1(); //a为低字节8位
b = read_DB18B20_1(); //b为高字节8位
temp = b; //temp为温度值UINT 16bit
temp <<= 8; //两个字节组合到一起
temp |= a;//此处可以直接把补码转换过来,只是关于负数标志的问题需要考虑
if(b > 7){ //判断正负,因为负温度高五位是1,故而可得
fuflag1 = 1;
temps1 = (temp*0.0625-4096)*10-0.5;
}
else{
fuflag1 = 0;
temps1 = temp*0.0625*10+0.5;
}
if(tempd1 < temps1){
tempd1 = temps1;
write(0x00,tempd1);
}
else if(tempx1 > temps1){
tempx1 = temps1;
write(0x04,tempx1);
}
if(sendflag){
t++;
if(t == 5){
send_str(temps1,1);
send_str(tempd1,2);
send_str(tempx1,3);
}
}
}
void tempread_2()
{
uint temp;
uchar a,b;
fuflag2 = 0;
DB18B20_init_2();//DB18B20复位
write_DB18B20_2(0xcc);//写DB18B20;0xcc 跳过 ROM 指令
write_DB18B20_2(0xbe);//写DB18B20;0xbe 读温度指令
a = read_DB18B20_2(); //a为低字节8位
b = read_DB18B20_2(); //b为高字节8位
temp = b; //temp为温度值UINT 16bit
temp <<= 8; //两个字节组合到一起
temp |= a;//此处可以直接把补码转换过来,只是关于负数标志的问题需要考虑
if(b>7){
fuflag2 = 1;
temps2=(temp*0.0625-4096)*10-0.5;
}
else{
fuflag2=0;
temps2=temp*0.0625*10+0.5;
}
if(tempd2<temps2){
tempd2=temps2;
write(0x08,tempd2);
}
else if(tempx2>temps2){
tempx2=temps2;
write(0x0c,tempx2);
}
if(sendflag){
if(t == 5){
t = 0;
send_str(temps2,4);
send_str(tempd2,5);
send_str(tempx2,6);
}
}
}
void temp(sint temps,uchar t,bit fuflag)//显示温度函数
{
uchar i;
uint mm;
temps = abs(temps);
DispBuf[0] = temps / 1000;//显示百位
mm = temps % 1000;
DispBuf[1] = mm / 100;//显示十位
mm = mm % 100;
DispBuf[2] = mm / 10;//显示个位
DispBuf[3] = mm % 10;//显示小数点:0.1
i = DispBuf[0];//百位判断,如果为0,则消隐
if(i == 0) i = 10;
if(fuflag)//负温度判断,如果为0,则温度为负的,显示负号
i = 11;
DispBuf[0] = DispTab_1[i];//查表,取字符
i = DispBuf[1];
DispBuf[1] = DispTab_1[i];
i = DispBuf[2];
DispBuf[2] = DispTab_1[i];
i = DispBuf[3];
DispBuf[3] = DispTab_1[i];
i = DispBuf[4];
DispBuf[4] = DispTab_1[i];
write_Directive(0x84+t);//第一行,第五个位置地址
write_Data(DispBuf[0]);//第一行,第五个位置
write_Data(DispBuf[1]);//第一行,第六个位置
write_Data(DispBuf[2]);//第一行,第八个位置
write_Data('.');//第一行,第七个位置
write_Data(DispBuf[3]);//第一行,第九个位置
}
void readcc()//读24c02中最高和最低温度并显示
{
bit flag;
tempd1 = read(0x00);
if(tempd1 > 0) flag = 0;
else flag = 1;
temp(tempd1,0x00,flag);
tempx1 = read(0x04);
if(tempx1 > 0) flag = 0;
else flag=1;
temp(tempx1,0x06,flag);
tempd2 = read(0x08);
if(tempd2 > 0) flag = 0;
else flag = 1;
temp(tempd2,0x40,flag);
tempx2 = read(0x0c);
if(tempx2 > 0) flag = 0;
else flag = 1;
temp(tempx2,0x46,flag);
}
void keyscan() //键扫描程序
{
bit flag = 0;
if(!key1){ //按键1
delay(30);
if(!key1){
while(!key1);
k1flag = 1;
flag = 1;
}
}
if(!key2){ //按键2
delay(30);
if(!key2){
while(!key2);
k2flag = 1;
flag = 1;
}
}
if(!key3){ //按键3
delay(30);
if(!key3){
while(!key3);
k3flag = 1;
flag = 1;
}
}
if(!key4){ //按键4
delay(30);
if(!key4){
while(!key4);
k4flag = !k4flag;
flag = 1;
}
}
if(!key5){ //按键5
delay(30);
if(!key5){
while(!key5);
k5flag=!k5flag;
flag=1;
write_Directive(0x01);
}
}
if(flag){
beep=0;
delay(100);
beep=1;
}
}
uchar read_ds1302(uchar addr)//读DS1302
{
uchar i,temp = 0x00;
DSRST = 0;
DSIO = 1;
DSCLK = 0;
DSRST = 1;
for(i = 8;i > 0;i--){ //循环8次 写入地址数据
DSCLK = 0;
DSIO = addr&0x01; //每次传输低字节
addr >>= 1; //右移一位
DSCLK = 1;
}
delay(1);
if(DSIO)
temp |= 0x80; //每次传输低字节
DSCLK = 0;
temp >>= 1;
for(i = 7;i > 0;i--){ //循环7次 读取数据此处如果读8次就会出现乱码
DSCLK = 0;
if(DSIO)
temp |= 0x80; //每次传输低字节
DSCLK = 1;
temp >>= 1; //右移一位
}
DSCLK = 1;
DSRST = 0;
DSIO = 0;
return temp;
}
void write_ds1302(uchar addr, uchar date)//写DS1302
{
uchar i;
DSRST = 0;
DSCLK = 0;
DSRST = 1;
for (i = 8;i > 0;i--){ //循环8次 写入地址数据
DSCLK = 0;
nop;
DSIO = addr&0x01; //每次传输低字节
addr >>= 1; //右移一位
DSCLK = 1;
nop;
}
for (i = 8;i > 0;i--){ //循环8次 写入数据
DSCLK = 0;
nop;
DSIO = date&0x01; //每次传输低字节
date >>= 1; //右移一位 DSCLK=1
DSCLK = 1;
nop;
}
DSRST = 0;
delay(1);
}
void init_ds1302()//DS1302初始化
{
write_ds1302(0x8e,0x00);
write_ds1302(0x90,0xa5);
/*write_ds1302(0x80,0x00); //00秒
write_ds1302(0x82,0x54); //54分
write_ds1302(0x84,0x17); //17点
write_ds1302(0x86,0x13); //13日
write_ds1302(0x88,0x03); //3月
write_ds1302(0x8a,0x05); //星期五
write_ds1302(0x8c,0x12); //2012年*/
}
void time()//时间显示
{
write_lcd_com_date(0x81,'2');
write_lcd_com_date(0x82,'0');
second = read_ds1302(0x81);
secondl = second & 0x0f;
secondh = second >> 4;
minute = read_ds1302(0x83);
minutel = minute & 0x0f;
minuteh = minute >> 4;
hour = read_ds1302(0x85);
hourl = hour & 0x0f;
hourh = hour >> 4;
month = read_ds1302(0x89);
monthl = month & 0x0f;
monthh = month >> 4;
year = read_ds1302(0x8d);
yearl = year & 0x0f;
yearh = year >> 4;
day = read_ds1302(0x87);
dayl = day & 0x0f;
dayh = day >> 4;
week = read_ds1302(0x8b);
week = week&0x0f;
write_lcd_com_date(0xca,0x30 + secondl);
write_lcd_com_date(0xc9,0x30 + secondh);
write_lcd_com_date(0xc8,':');
write_lcd_com_date(0xc7,0x30 + minutel);
write_lcd_com_date(0xc6,0x30 + minuteh);
write_lcd_com_date(0xc5,':');
write_lcd_com_date(0xc4,0x30 + hourl);
write_lcd_com_date(0xc3,0x30 + hourh);
write_lcd_com_date(0x84,0x30 + yearl);
write_lcd_com_date(0x83,0x30 + yearh);
write_lcd_com_date(0x85,'-');
write_lcd_com_date(0x87,0x30 + monthl);
write_lcd_com_date(0x86,0x30 + monthh);
write_lcd_com_date(0x88,'-');
write_lcd_com_date(0x8a,0x30 + dayl);
write_lcd_com_date(0x89,0x30 + dayh);
write_lcd_com_date(0x8c,0x30 + week);
}
void shuma(uchar addr,uchar xz,bit flag)
{
char date;
uchar day=0;
date = read_ds1302(addr + 1); //读分寄存器上的数据
date = ((date >> 4) * 10) + (date & 0x0f); //BCD Convert to shi
if(flag == 0) date++;
else date--;
switch(xz)
{
case 0:if(date > 59) date=0;if(date < 0) date = 59;break;//分秒
case 1:if(date > 23) date=0;if(date < 0) date = 23;break;//时
case 2:if(date > 12) date=0;if(date < 0) date = 12;break;//月
case 3:if(date > 99) date=0;if(date < 0) date = 99;break;//年
case 4:if(date > 7) date=1;if(date < 1) date = 7;break;//星期
case 5:
day = read_ds1302(0X87);
day = ((day >> 4) * 10) + (day & 0x0f);
if(day != 2){
if(day % 2 == 1){
if(date > 31)
date = 0;
else if(date < 0)
date = 31;
}
else{
if(date > 30)
date = 0;
else if(date < 0)
date = 30;
}
}
else{
if(date > 29)
date = 0;
else if(date < 0)
date = 29;
}
break;
default:break;
}
date = ((date / 10) << 4) + (date % 10);//Hex Convert to BCD
write_ds1302(addr,date);//将变量里的数据(BCD码)写回到分寄存器上
}
void flash(uchar bz)//字符闪烁可以采用亮灭的方式
{
bit flag = 0;
uchar sj = 0;
while(1){
sj++;
if(sj == 25){
sj = 0;
flag = !flag;
}
keyscan();
if(!flag){
second = read_ds1302(0x81);
secondl = second & 0x0f;
secondh = second >> 4;
write_lcd_com_date(0xca,0x30 + secondl);
write_lcd_com_date(0xc9,0x30 + secondh);
}
switch(bz)
{
case 0:
if(flag){
write_lcd_com_date(0xca,' ');
write_lcd_com_date(0xc9,' ');
}
break;
case 1:
if(flag){
write_lcd_com_date(0xc7,' ');
write_lcd_com_date(0xc6,' ');
}
else{
write_lcd_com_date(0xc7,0x30+minutel);
write_lcd_com_date(0xc6,0x30+minuteh);
}
break;
case 2:
if(flag){
write_lcd_com_date(0xc4,' ');
write_lcd_com_date(0xc3,' ');
}
else{
write_lcd_com_date(0xc4,0x30+hourl);
write_lcd_com_date(0xc3,0x30+hourh);
}
break;
case 3:
if(flag){
write_lcd_com_date(0x84,' ');
write_lcd_com_date(0x83,' ');
}
else{
write_lcd_com_date(0x84,0x30+yearl);
write_lcd_com_date(0x83,0x30+yearh);
}
break;
case 4:
if(flag){
write_lcd_com_date(0x87,' ');
write_lcd_com_date(0x86,' ');
}
else{
write_lcd_com_date(0x87,0x30+monthl);
write_lcd_com_date(0x86,0x30+monthh);
}
break;
case 5:
if(flag){
write_lcd_com_date(0x8a,' ');
write_lcd_com_date(0x89,' ');
}
else{
write_lcd_com_date(0x8a,0x30+dayl);
write_lcd_com_date(0x89,0x30+dayh);
}
break;
case 6:
if(flag){
write_lcd_com_date(0x8c,' ');
delay(10);
}
else{
write_lcd_com_date(0x8c,0x30+week);
delay(10);
}
break;
default:break;
}
if(k5flag || !k4flag || k3flag || k2flag || k1flag)
{
if(k5flag){
k3flag = 0;
k4flag = 0;
write_Directive(0x01);
}
write_Directive(0x0c);
break;
}
}
}
void timekey()//时间部分功能键
{
keyscan();
if(!k5flag){
if(k4flag){
if(k3flag){
k3flag = 0;
count++;
}
switch(count)
{
case 0:
flash(6);
if(k2flag){
shuma(0x8a,4,1);//week
k2flag = 0;
}
else if(k1flag){
shuma(0x8a,4,0);
k1flag = 0;
}
break;
case 1:
flash(5);
if(k2flag){
shuma(0x86,5,1);//day
k2flag = 0;
}
else if(k1flag){
shuma(0x86,5,0);
k1flag = 0;
}
break;
case 2:
flash(4);
if(k2flag){
shuma(0x88,2,1);//month
k2flag = 0;
}
else if(k1flag){
shuma(0x88,2,0);
k1flag = 0;
}
break;
case 3:
flash(3);
if(k2flag){
shuma(0x8c,3,1);//year
k2flag = 0;
}
else if(k1flag){
shuma(0x8c,3,0);
k1flag = 0;
}
break;
case 4:
flash(0);
if(k2flag){
shuma(0x80,0,1);//second
k2flag=0;
}
else if(k1flag){
shuma(0x80,0,0);
k1flag = 0;
}
break;
case 5:
flash(1);
if(k2flag){
shuma(0x82,0,1);//minute
k2flag = 0;
}
else if(k1flag){
shuma(0x82,0,0);
k1flag = 0;
}
break;
case 6:
flash(2);
if(k2flag){
shuma(0x84,1,1);//hour
k2flag=0;
}
else if(k1flag){
shuma(0x84,1,0);
k1flag=0;
}
break;
default : break;
}
if(count > 6)
count = 0;
}
}
}
void tempkey()//温度部分功能键
{
uchar i;
bit flag;
keyscan();
if(k5flag){
write_Directive(0x80);
for(i = 0;i < 4;i++){
write_Data(cc1[i]);//显示字符串
}
write_Directive(0xc0);
for(i = 0;i < 4;i++){
write_Data(cc2[i]);//显示字符串
}
if(k2flag && k1flag){
readcc();
delay(5000);
write_Directive(0x01);
k2flag = 0;
k1flag = 0;
}
if(k2flag && k3flag){
write(0x00,-550);
write(0x04,1250);
write(0x08,-550);
write(0x0c,1250);
k2flag = 0;
k3flag = 0;
}
if(k4flag){
temp(uptem1,0x00,flag);
temp(downtem1,0x06,flag);
temp(uptem2,0x40,flag);
temp(downtem2,0x46,flag);
while(1){
keyscan();
if(k3flag){
k3flag = 0;
tcount++;
}
switch(tcount)
{
case 0:
write_Directive(0x89);
write_Directive(0x0f);
if(k1flag){
uptem1 += 10;
k1flag = 0;
}
else if(k2flag){
uptem1 -= 10;
k2flag = 0;
}
if(uptem1 > 0)
flag = 0;
else
flag = 1;
temp(uptem1,0x00,flag);
write(0x10,uptem1);
break;
case 1:
write_Directive(0x8f);
write_Directive(0x0f);
if(k1flag){
downtem1 += 10;
k1flag = 0;
}
else if(k2flag){
downtem1 -= 10;
k2flag=0;
}
if(downtem1 > 0)
flag = 0;
else
flag = 1;
temp(downtem1,0x06,flag);
write(0x14,downtem1);
break;
case 2:
write_Directive(0xc9);
write_Directive(0x0f);
if(k1flag){
uptem2 += 10;
k1flag=0;
}
if(k2flag){
uptem2 -= 10;
k2flag = 0;
}
if(uptem2 > 0)
flag = 0;
else
flag = 1;
temp(uptem2,0x40,flag);
write(0x18,uptem2);
break;
case 3:
write_Directive(0xcf);
write_Directive(0x0f);
if(k1flag){
downtem2 += 10;
k1flag=0;
}
else if(k2flag){
downtem2 -= 10;
k2flag = 0;
}
if(downtem2 > 0)
flag = 0;
else
flag = 1;
temp(downtem2,0x46,flag);
write(0x1c,downtem2);
break;
default: break;
}
if(tcount > 3)
tcount = 0;
if(!k4flag || !k5flag){
if(!k5flag)
k4flag=0;
write_Directive(0x01);
write_Directive(0x0c);
break;
}
}
}
}
}
void tempbj() //温度比较
{
uchar i;
if(temps1 > uptem1){
beep = 0;
led = 0;
write_Directive(0x01);
write_Directive(0x81);//第二行第一位地址
for(i = 0;i < 14;i++){
write_Data(cc3[i]);//显示字符串
}
delay(3000);
beep = 1;
led = 1;
write_Directive(0x01);
}
else if(temps1<downtem1){
beep=0;
led=0;
write_Directive(0x01);
write_Directive(0x81);//第二行第一位地址
for(i=0;i<13;i++){
write_Data(cc4[i]);//显示字符串
}
delay(3000);
beep=1;
led=1;
write_Directive(0x01);
}
if(temps2>uptem2){
beep=0;
led=0;
write_Directive(0x01);
write_Directive(0x81);//第二行第一位地址
for(i=0;i<14;i++){
write_Data(cc5[i]);//显示字符串
}
delay(3000);
beep=1;
led=1;
write_Directive(0x01);
}
else if(temps2<downtem2){
beep=0;
led=0;
write_Directive(0x01);
write_Directive(0x81);//第二行第一位地址
for(i=0;i<13;i++){
write_Data(cc6[i]);//显示字符串
}
delay(3000);
beep=1;
led=1;
write_Directive(0x01);
}
}
/*void adjust_res2() ///res 分别等于 0x1f, 0x3f, 0x5f 温度读数分辨率分别对应
// 0.5, 0.25, 0.125
{ DB18B20_init_2();
write_DB18B20_2(0xcc); //跳过Rom
write_DB18B20_2(0x4e); //写暂存器
write_DB18B20_2(0x70); //写TH
write_DB18B20_2(0x01); //写TL
write_DB18B20_2(0x3f); //写结构寄存器
write_DB18B20_2(0xcc); //跳过Rom
write_DB18B20_2(0x48); //把暂存器内容写到EPRam中
}
void adjust_res1() ///res 分别等于 0x1f, 0x3f, 0x5f 温度读数分辨率分别对应
// 0.5, 0.25, 0.125
{ DB18B20_init_1();
write_DB18B20_1(0xcc); //跳过Rom
write_DB18B20_1(0x4e); //写暂存器
write_DB18B20_1(0x70); //写TH
write_DB18B20_1(0x01); //写TL
write_DB18B20_1(0x3f); //写结构寄存器
write_DB18B20_1(0xcc); //跳过Rom
write_DB18B20_1(0x48); //把暂存器内容写到EPRam中
} */
void main()
{
uchar i;
/*write(0x00,250);
write(0x04,150);
write(0x08,250);
write(0x0c,150);
write(0x10,500);
write(0x14,50);
write(0x18,500);
write(0x1c,50);*/
TMOD = 0x20; // 定时器1工作于8位自动重载模式, 用于产生波特率
TH1 = 0xfd; // 波特率9600
TL1 = 0xfd;
EA=1; //开中断
ES=1; //允许串口中断
SM0=0;
SM1=1;
REN=1;
TR1=1;
uptem1 = read(0x10);
downtem1 = read(0x14);
uptem2 = read(0x18);
downtem2 = read(0x1c);
LCD_init();//LCD复位
tempzh_1();
tempzh_2();
delay(1000);//延时
tempread_1();
tempread_2();
tempzh_1();
tempzh_2();
delay(1000);//延时
tempread_1();
tempread_2();
delay(3000);//延时
readcc();
write_Directive(0x01);
init_ds1302();
delay(100);
/*adjust_res1();
adjust_res2();*/
while(1){
if(RI){ // 是否有数据到来
RI = 0;
i = SBUF; // 暂存接收到的数据
if(i){
sendflag = 1;
send_str(0x0000,0);
}
else{
sendflag = 0;
}
}
tempzh_1();
tempzh_2();
keyscan();
if(!k5flag){
time();
timekey();
}
else{
temp(temps1,0x00,fuflag1);
temp(temps2,0x40,fuflag2);
tempkey();
tempbj();
}
delay(35);
keyscan();
delay(35);
keyscan();
delay(35);
keyscan();
delay(35);
tempread_1();
tempread_2();
}
}
复制代码
作者:
夜小明
时间:
2020-4-2 19:58
请问楼主,你的作品有实现温度滚动显示和选择显示的功能吗?一千多行代码码到一起真心看到头皮发麻。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1