|
DS18B20温度计代码
- #include<reg51.h>
- extern unsigned char code NumberData[12];
- typedef unsigned char uchar;
- typedef unsigned int uint;
- sbit BUS=P2^6;//温度传感器接口
- sbit LED_ONE = P2^0;//第一位数码管位选端口
- sbit LED_TWO = P2^1;//第一位数码管位选端口
- sbit LED_THREE = P2^2;//第一位数码管位选端口
- sbit LED_FOUR = P2^3;//第一位数码管位选端口
- volatile uchar GeWei,ShiWei,BaiWei,_Geiwei,_ShiWei;
- volatile bit Convert_yes_not=0;
- volatile char Negative=0;
- /**********************************
- 数码管编码
- **********************************/
- code unsigned char NumberData[12] ={
- 0x3f,//0
- 0x06,//1
- 0x5b,//2
- 0x4f,//3
- 0x66,//4
- 0x6d,//5
- 0x7d,//6
- 0x07,//7
- 0x7f,//8
- 0x6f,//9
- 0x80,//DP
- 0x40 //negative
- };
- //延时子函数
- void Delay_4xN_US(uchar us)
- {
- while(--us);
- }
- //初始化子函数
- void Timer0_Init()
- {
- TMOD = 0x01;//定时器0模式1-16位定时器-
- TCON = 0x00;//清零TCON
- ET0 = 1; //允许定时器0中断
- TH0 = 0x3c;//定时100ms
- TL0 = 0xb0;
- TR0 = 1; //启动定时器0
- }
- void DS18B20_Recover()
- {
- BUS = 1;
- Delay_4xN_US(200);//4us
- BUS = 0;
- Delay_4xN_US(200);//800us
- BUS = 1;
- Delay_4xN_US(200);//800us
- BUS = 1;
- }
- void SendProc(uchar proc)
- {
- bit T_bit;
- uchar i=0;
- for(;i<8;i++)
- {
- T_bit = (proc>>i)&0x01;//单位数据
- BUS = 1;
- Delay_4xN_US(1);//4us
- BUS = 0;
- BUS = T_bit;//单位数据
- Delay_4xN_US(15);//60us等待时间
- BUS = 1;
- }
- }
- uchar ReadOneByte()
- {
- uchar i;
- bit T_bit;
- uchar dat=0;
- for(i=0;i<8;i++)
- {
- BUS = 1;
- Delay_4xN_US(1);//4us
- BUS = 0;
- BUS = 1;
- T_bit = BUS;
- Delay_4xN_US(15);//60us
- BUS = 1;
- dat |= ((uchar)T_bit)<<i;
- }
- return dat;
- }
- void LED_DATE_Convert(uchar T_L,uchar T_H)
- {
- uchar ZhengShu,MiddleShu;
- uint XiaoShu;
- if((T_H>>4)>0)//负数
- {
- Negative = 0xff;
- MiddleShu =((~(T_L&0x0f))&0x0f)+1;
- if(MiddleShu>>4>0)//有进位
- {
- ZhengShu = (~T_H)*16+((~T_L)>>4)+1;
- XiaoShu =(MiddleShu&0x0f)*625;
- }
- else
- {
- ZhengShu = (~T_H)*16+((~T_L)>>4);
- XiaoShu =(MiddleShu&0x0f)*625;
- }
- GeWei = ZhengShu%10;
- ShiWei = (ZhengShu/10)%10;
- _Geiwei= XiaoShu/1000;
- _ShiWei= (XiaoShu/100)%10;
-
- }
- else//正数
- {
- Negative = 0x00;
- ZhengShu = T_H*16+(T_L>>4);
- XiaoShu =(T_L&0x0f)*625;
- GeWei = ZhengShu%10;
- ShiWei = (ZhengShu/10)%10;
- BaiWei = ZhengShu/100;
- _Geiwei= XiaoShu/1000;
- _ShiWei= (XiaoShu/100)%10;
- }
- }
- void LED_Show()
- {
- switch(Negative)
- {
- case 0x00: //正数
- if(BaiWei!=0)
- {
- P0 = NumberData[BaiWei];
- LED_ONE = 0;
- Delay_4xN_US(100);//400us
- LED_ONE = 1;
- P0 = NumberData[ShiWei];
- LED_TWO = 0;
- Delay_4xN_US(100);//400us
- LED_TWO = 1;
- P0 = NumberData[GeWei]|NumberData[10];
- LED_THREE = 0;
- Delay_4xN_US(100);//400us
- LED_THREE = 1;
- P0 = NumberData[_Geiwei];
- LED_FOUR = 0;
- Delay_4xN_US(100);//400us
- LED_FOUR = 1;
- }
- else
- {
- P0 = NumberData[ShiWei];
- LED_ONE = 0;
- Delay_4xN_US(100);//400us
- LED_ONE = 1;
- P0 = NumberData[GeWei]|NumberData[10];
- LED_TWO = 0;
- Delay_4xN_US(100);//400us
- LED_TWO = 1;
- P0 = NumberData[_Geiwei];
- LED_THREE = 0;
- Delay_4xN_US(100);//400us
- LED_THREE = 1;
- P0 = NumberData[_ShiWei];
- LED_FOUR = 0;
- Delay_4xN_US(100);//400us
- LED_FOUR = 1;
- }
- break;
- case 0xff: //负数
- if(ShiWei!=0)
- {
- P0 = NumberData[11];
- LED_ONE = 0;
- Delay_4xN_US(100);//400us
- LED_ONE = 1;
- P0 = NumberData[ShiWei];
- LED_TWO = 0;
- Delay_4xN_US(100);//400us
- LED_TWO = 1;
- P0 = NumberData[GeWei]|NumberData[10];
- LED_THREE = 0;
- Delay_4xN_US(100);//400us
- LED_THREE = 1;
- P0 = NumberData[_Geiwei];
- LED_FOUR = 0;
- Delay_4xN_US(100);//400us
- LED_FOUR = 1;
- }
- else
- {
- P0 = NumberData[11];
- LED_ONE = 0;
- Delay_4xN_US(100);//400us
- LED_ONE = 1;
- P0 = NumberData[GeWei]|NumberData[10];
- LED_TWO = 0;
- Delay_4xN_US(100);//400us
- LED_TWO = 1;
- P0 = NumberData[_Geiwei];
- LED_THREE = 0;
- Delay_4xN_US(100);//400us
- LED_THREE = 1;
- P0 = NumberData[_ShiWei];
- LED_FOUR = 0;
- Delay_4xN_US(100);//400us
- LED_FOUR = 1;
- }
- break;
- }
- }
- void Timer_Interrupt() interrupt 1
- {
- uchar T_dat_L,T_dat_H;
- P0=0x00;
- TH0 = 0x3c;//定时100ms
- TL0 = 0xb0;
- if(Convert_yes_not==0)
- {
- DS18B20_Recover();//复位
- SendProc(0xcc);//ROM跳过指令
- SendProc(0x44);//数据转换指令
- Convert_yes_not=1;
- }
- else
- {
- DS18B20_Recover();//复位
- SendProc(0xcc);//ROM跳过指令
- SendProc(0xbe);//数据读取指令
- T_dat_L = ReadOneByte();//读取数据
- T_dat_H = ReadOneByte();//读取数据
- LED_DATE_Convert(T_dat_L,T_dat_H);
- Convert_yes_not=0;
- }
- }
- //主函数
- void main()
- {
- P0=0x00;
- P2=0xff;
- DS18B20_Recover();//复位
- Timer0_Init();
- EA =1;//开放中断
- while(1)
- {
- LED_Show();
- }
- }
-
复制代码
|
-
-
温度计.rar
46.36 KB, 下载次数: 38, 下载积分: 黑币 -5
|