单片机源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- #include <math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- sbit LED=P1^7;
- sbit LED1=p3^7;
- uint ad;
- uint ad1;
- uint ad2;
- uint ad3;
- uint ad4;
- uint ad5;
- uint ad6;
- uint adH2;
- uint adCH4;
- uint adC0;
- uint adC2H2;
- uint adC2H4;
- uint adC2H6;
- sbit key=p1^0;
- sbit key1=p1^1;
- sbit key2=p1^2;
- sbit key3=p1^3;
- sbit key4=p1^4;
- sbit key5=p1^5;
- sbit key6=p1^6;
- uchar b;
- uchar b1;
- uchar b2;
- uchar b3;
- uint hc;
- uint u=0;
- uint i=0;
- uint ux=300;
- uint ix=10;
- uint wendu=80;
- uchar code zifu[]="0123456789abcdef";
- uchar code zifu0[]="H2:000 CH4:000";
- uchar code zifu1[]="CO:000 C2H2:000";
- uchar code zifu2[]="C2H4:000 C2H6:000";
- uchar code zifu3[]="B:0-0-0";
- uchar code zifu4[]="dianya:000";
- uchar code zifu5[]="dianliu:000";
- uchar code zifu6[]="wendu:000";
- uchar moshi=0;
- #includ <ad.h>
- #include <ds18.h>
- #include <lcd.h>
- sbit beer=P2^6;
- void beerc()
- {
- beer=0; _delay_ms(60);
- beer=1;_delay_ms(1);
- }
- void led()
- {
- LED=0;
- LED1=0;
- delay();
- }
- viod read()
- {
- readds();
- ad1=get_data(0);
- ad2=get_data(1);
- ad3=get_data(2);
- ad4=get_data(3);
- ad5=get_data(4);
- ad6=get_data(5);
- //电压电流
- u=get_data(6);
- i=get_data(7); _delay_ms(10);
- //赋值
- adh2=ad1;
- adch4=ad2;
- adco=ad3;
- adc2h2=ad4;
- adc2h4=ad5;
- adc2h6=ad6;
- //比值 1
- hc=adc2h2*10/adc2h4;
- if(hc<1) b1=0;
- if((hc>=1)&&(hc<10)) b1=1;
- if((hc>=10)&&(hc<30)) b1=1;
- if(hc>=30) b1=2;
- //比值 2
- hc=adch4*10/adh2;
- if(hc<1) b2=1;
- if((hc>=1)&&(hc<10)) b2=0;
- if((hc>=10)&&(hc<30)) b2=2;
- if(hc>=30) b2=2;
- //比值 3
- hc=adc2h2*10/adc2h6;
- if(hc<1) b3=0;
- if((hc>=1)&&(hc<10)) b3=0;
- if((hc>=10)&&(hc<30)) b3=1;
- if(hc>=30) b3=2;
- if((b1==0)&&(b2==0)&&(b3==0)) b=0;
- if((b1==0)&&(b2==1)&&(b3==0)) b=1;
- if((b1==0)&&(b2==0)&&(b3==1)) b=2;
- if((b1==0)&&(b2==2)&&(b3==0)) b=3;
- if((b1==0)&&(b2==2)&&(b3==1)) b=4;
- if((b1==0)&&(b2==0)&&(b3==2)) b=5;
- if((b1==0)&&(b2==1)&&(b3==2)) b=5;
- if((b1==0)&&(b2==2)&&(b3==2)) b=5;
- if((b1==2)&&(b2==0)) b=6;
- if((b1==2)&&(b2==1)) b=6;
- if((b1==2)&&(b2==2)) b=6;
- if((b1==1)&&(b2==0)) b=7;
- if((b1==1)&&(b2==1)) b=7;
- if((b1==1)&&(b2==2)) b=7;
- if(b>0)beerc();LED=0;LED1=1;
- if(u>ux)beerc();LED=0;LED1=1;
- if(i>ix)beerc();LED=0;LED1=1;
- if(wendu>wendux)beerc();LED=0;LED1=1;
- }
- void xianshi()
- {
- lcd1602_adr(0x08);
- lcd1602_writenumber(0x30+u%1000/100)
- lcd1602_writenumber(0x30+u%100/10);
- lcd1602_writenumber(0x30+u%10);
- lcd1602_adr(0x0d);
- lcd1602_writenumber(0x30+ux%1000/100);
- lcd1602_writenumber(0x30+ux%100/10);
- lcd1602_writenumber(0x30+ux%10);
- //当前 i
- lcd1602_adr(0x48);
- lcd1602_writenumber(0x30+i%1000/100);
- lcd1602_writenumber(0x30+i%100/10);
- lcd1602_writenumber(0x30+i%10);
- lcd1602_adr(0x4D);
- lcd1602_writenumber(0x30+ix%1000/100);
- lcd1602_writenumber(0x30+ix%100/10);
- lcd1602_writenumber(0x30+ix%10);
- //当前 w
- lcd1602_adr(0x18);
- lcd1602_writenumber(0x30+wendu%1000/100);
- lcd1602_writenumber(0x30+wendu%100/10);
- lcd1602_writenumber(0x30+wendu%10);
- lcd1602_adr(0x1D);
- lcd1602_writenumber(0x30+wendux%1000/100);
- lcd1602_writenumber(0x30+wendux%100/10);
- lcd1602_writenumber(0x30+wendux%10);
- lcd1602_adr(0x53);
- lcd1602_writenumber(0x30+b1%10);
- lcd1602_adr(0x55);
- lcd1602_writenumber(0x30+b2%10);
- lcd1602_adr(0x57);
- lcd1602_writenumber(0x30+b3%10);
- lcd1602_adr(0x5f);
- lcd1602_writenumber(0x30+b%10); //----------------------------------
- //切换
- if(key==0)
- {
- moshi=0;
- LCD1602_string(1,1,zifu0);
- LCD1602_string(2,1,zifu1);
- LCD1602_string(3,1,zifu2);
- LCD1602_string(4,1,zifu3);
- LED=0;LED1=1;
- while(key==0);
- }
- //SET
- if(key1==0){if(ux<999)ux=ux+1; while(key1==0); }
- if(key2==0){if(ux> 0)ux=ux-1; while(key2==0); }
- //SET
- if(key3==0){if(ix<999)ix=ix+1; while(key3==0); }
- if(key4==0){if(ix> 0)ix=ix-1; while(key4==0); }
- //SET
- if(key5==0){if(wendux<199)wendux=wendux+1; while(key5==0); }
- if(key6==0){if(wendux> 0)wendux=wendux-1; while(key6==0); }
- }
- void xianshi0()
- {
- //当前 H2
- lcd1602_adr(0x04);
- lcd1602_writenumber(0x30+adh2%1000/100);
- lcd1602_writenumber(0x30+adh2%100/10);
- lcd1602_writenumber(0x30+adh2%10);
- //当前 CH4
- lcd1602_adr(0x44);
- lcd1602_writenumber(0x30+adch4%1000/100);
- lcd1602_writenumber(0x30+adch4%100/10);
- lcd1602_writenumber(0x30+adch4%10);
- //当前 H2
- lcd1602_adr(0x14);
- lcd1602_writenumber(0x30+adco%1000/100);
- lcd1602_writenumber(0x30+adco%100/10);
- lcd1602_writenumber(0x30+adco%10);
- //当前 C2H2
- lcd1602_adr(0x0D);
- lcd1602_writenumber(0x30+adc2h2%1000/100);
- lcd1602_writenumber(0x30+adc2h2%100/10);
- lcd1602_writenumber(0x30+adc2h2%10);
- //当前 C2H4
- lcd1602_adr(0x4D);
- lcd1602_writenumber(0x30+adc2h4%1000/100);
- lcd1602_writenumber(0x30+adc2h4%100/10);
- lcd1602_writenumber(0x30+adc2h4%10);
- //当前 C2H2
- lcd1602_adr(0x1D);
- lcd1602_writenumber(0x30+adc2h6%1000/100);
- lcd1602_writenumber(0x30+adc2h6%100/10);
- lcd1602_writenumber(0x30+adc2h6%10);
- lcd1602_adr(0x53);
- lcd1602_writenumber(0x30+b1%10);
- lcd1602_adr(0x55);
- lcd1602_writenumber(0x30+b2%10);
- lcd1602_adr(0x57);
- lcd1602_writenumber(0x30+b3%10);
- lcd1602_adr(0x5f);
- lcd1602_writenumber(0x30+b%10);
- //切换
- if(key==0)
- {
- moshi=1;
- LCD1602_string(1,1,zifu4);
- LCD1602_string(2,1,zifu5);
- LCD1602_string(3,1,zifu6);
- LED1=0;LED=1;
- while(key==0);
- }
- }
- void main()
- {
- _delay_ms(1);
- //初始化
- lcd1602_init();
- LCD1602_string(1,1,zifu0);
- LCD1602_string(1,1,zifu0);
- LCD1602_string(2,1,zifu1);
- LCD1602_string(3,1,zifu2);
- LCD1602_string(4,1,zifu3);
- readds18();_delay_ms(300);
- readds18();_delay_ms(300);
- readds18();_delay_ms(300);
- readds18();_delay_ms(300);
- readds18();_delay_ms(300);
- //进入循环
- while(1)
- {
- read();
- if(moshi==0)xianshi0();
- if(moshi==1)xianshi1();
- }
- }
- void _delay_ms(uint t)
- {
- uint i,j;
- for(i=0;i<t;i++)
- for(j=0;j<120;j++);
- }
- //延时函数 us
- void _delay_us(uchar t)
- {
- while(t>0)t--;
- }
- //接口定义
- sbit SDO = P2^0;//数据输出口
- sbit ADD = P2^1;//通道选择
- sbit CS = P2^2;//启动
- sbit CLK = P2^3;//时钟时序
- sbit EOC = P2^4;//为高时轮换结束
- unsigned int adhc1=0;
- unsigned int adhc2=0;
- //读 AD 中的数据
- unsigned int get_data2(unsigned char ch)
- {
- unsigned char i;
- unsigned int temp=0;
- //4 有效地址左对齐
- ch <<= 4;
- CLK=0;_delay_us(1);
- CS=0;
- //地址-4
- for (i=0;i<4;i++)
- {
- if(ch&0X80)ADD=1; else ADD=0;_delay_us(1);
- CLK=1;_delay_us(1);
- CLK=0;_delay_us(1);
- ch <<= 1;
- }
- //CLK-8
- for (i=0;i<8;i++)
- { ADD=0;
- CLK=1;_delay_us(1);
- CLK=0;_delay_us(1);
- }
- //片选 信号
- CS=1;_delay_us(20);
- CS=0;_delay_us(10);
- //读取 10 位数据
- for (i=0;i<10;i++)
- {
- temp <<= 1;
- if(SDO)temp|=0x01; _delay_us(1);
- CLK=1;_delay_us(1);
- CLK=0;_delay_us(1);
- }
- CS=1;
- return(temp*9/10);
- }
- //读 AD 中的数据
- unsigned int get_data(unsigned char ch)
- {
- unsigned int temp=0;
- adhc1=get_data2(ch);
- adhc2=get_data2(ch);
- temp=(adhc1+adhc2)/2;
- return(temp);
- }
- //传感器的数据线
- sbit DQ=P2^7;
- int wendu=0;
- uchar z1;
- //延时函数
- void delay3(unsigned int i)
- {
- while(i--);
- }
- //初始化
- void Init_DS18B20(void)
- {
- unsigned char x=0;
- DQ = 1; //DQ 复位
- delay3(8); //稍做延时
- DQ = 0; //单片机将 DQ 拉低
- delay3(80); //精确延时 大于 480us
- DQ = 1; //拉高总线
- delay3(10);
- x=DQ; //稍做延时后 如果 x=0 则初始化成功 x=1 则初始化失败
- delay3(5);
- }
- //读一个字节
- unsigned char ReadOneChar(void)
- {
- unsigned char i=0;
- unsigned char dat = 0;
- for (i=8;i>0;i--)
- {
- DQ = 0; // 给脉冲信号
- dat>>=1;
- DQ = 1; // 给脉冲信号
- if(DQ)
- dat|=0x80;
- delay3(5);
- }
- return(dat);
- }
- //写一个字节
- void WriteOneChar(unsigned char dat)
- {
- unsigned char i=0;
- for (i=8; i>0; i--)
- {
- DQ = 0;
- DQ = dat&0x01;
- delay3(5);
- DQ = 1;
- dat>>=1;
- }
- delay3(5);
- }
- //读取温度
- unsigned int ReadTemperature(void)
- {
- unsigned char a=0;
- unsigned int b=0;
- unsigned int t=0;
- Init_DS18B20();
- WriteOneChar(0xCC); // 跳过读序号列号的操作
- WriteOneChar(0x44); // 启动温度转换
- delay3(10);
- Init_DS18B20();
- WriteOneChar(0xCC); //跳过读序号列号的操作
- WriteOneChar(0xBE); //读取温度寄存器等(共可读 9 个寄存器) 前两个就是温度
- a=ReadOneChar(); //低位
- b=ReadOneChar(); //高位
- b<<=8;
- t=a+b;
- return(t);
- }
- //数据处理
- void readds18()
- {
- unsigned int y=0;
- unsigned long x=0;
- y=ReadTemperature();
- //正
- if(!(y&0xf000))
- {
- z1=0;
- x=y;
- x=x*625/10000;
- wendu=x;
- }
- if(y&0xf000)
- {
- z1=1;
- y=-y;
- x=y;
- x=x*625/10000;
- wendu=0-x;
- }
- }
- sbit RS=P3^2; //写信号
- sbit E=P3^3; //使能信号
- #define e1 E=1
- #define rs1 RS=1
- #define e0 E=0
- #define rs0 RS=0
- #define PP P1
- //写 1602 控制字
- void lcd1602_writecrtl(uchar dat)
- {
- rs0;//写信号置 0
- _delay_us(5);
- PP=dat;
- e1;//使能信号置 1
- _delay_us(5);
- e0;//使能信号置 0
- }
- //写 1602 数据
- void lcd1602_writenumber(uchar dat)
- {
- rs1;//写信号置 1
- _delay_us(5);
- PP=dat;
- e1;//使能信号置 1
- _delay_us(5);
- e0;//使能信号置 0
- }
- //1602 初始化
- void lcd1602_init()
- {
- lcd1602_writecrtl(0x38); //显示模式
- lcd1602_writecrtl(0x06); //显示光标移动位置
- lcd1602_writecrtl(0x0c); //显示开及光标设置
- lcd1602_writecrtl(0x01); //显示清屏
- }
- //显示地址
- void lcd1602_adr(uchar dat)
- {
- lcd1602_writecrtl(0x80 | dat);
- }
- //行显示- void LCD1602_string(uchar hang,uchar lie,uchar const *p)
- {
- uchar a;
- if(hang == 1) a = 0x00;
- if(hang == 2) a = 0x40;
- if(hang == 3) a = 0x10;
- if(hang == 4) a = 0x50;
- a = a + lie - 1;
- lcd1602_adr(a);
- while(1)
- {
- if(*p == '\0') break;
- lcd1602_writenumber(*p);
- p++;
- }
- }
- //uchar code zifux[]="1234567812345678"
- //LCD1602_string(1,1,zifux);
- //LCD1602_string(2,1,zifux);
复制代码
|