📄 ds18b20.c
字号:
#include "at89x52.h"
#include <intrins.h>
#define SCAN_CYCLE 300
#define uchar unsigned char
#define uint unsigned int
uchar num1[8]={0x99,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//,0x00
uchar num2[8]={0x99,0x00,0x07,0x00,0x07,0x00,0x70,0x00}; //,0x07
uchar get_num,getflag,flag=0;
uint keyflag;
uchar UARTKeyFlag=0xaa;
static const char c_keyCode[16]={0x01,0x02,0x03,0x0a, /*键盘1,2,3,A*/
0x04,0x05,0x06,0x0b, /*键盘4,5,6,B*/
0x07,0x08,0x09,0x0c, /*键盘7,8,9,C*/
0x0e,0x00,0x0f,0x0d}; /*键盘*,0,#,D*/
uchar num3[8]={0xAA,0x01,0xfa,0xfb,0xfc,0xfd,0xfe,0xfa}; //,0xff
sbit One_Wire_Bus=P3^7;
//function define
/********************************************************************/
void One_Wire_Delay(unsigned char delay_time);
void One_Wire_Write_Byte(unsigned char oww_dat);
unsigned char One_Wire_Read_Byte(void);
void Read_18B20(void);
void Initize_One_Wire_Bus(void);
/********************************************************************/
data unsigned char GetScratchpad[2];
code unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
code unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
unsigned char ResultTemperatureH=0x00;
unsigned char ResultTemperatureLH=0x00;
unsigned char ResultTemperatureLL=0x00;
data unsigned char ResultSignal;
//The signal of temperature;
/********************************************************************/
//ds18b20 part
#define SkipRom 0xcc
#define ConvertTemperature 0x44
#define ReadScratchpad 0xbe
void keyhandle();
void scankey();
void delay(uint value);
void scrshift(uchar keyindex);
void numjudge();
void clearscr();
void statussend(uchar num[8]);
void statussend_one(uint num);
uchar keyjudge();
void One_Wire_Delay(unsigned char delay_time);
void One_Wire_Write_Byte(unsigned char oww_dat);
unsigned char One_Wire_Read_Byte(void);
void Read_18B20(void);
void Initize_One_Wire_Bus(void);
/********************************************************************/
void Initize_One_Wire_Bus(void)
{
One_Wire_Bus=0;
One_Wire_Delay(80);//Bus master pulling low 488us
One_Wire_Bus=1;
One_Wire_Delay(25);//Resister pull up 158us;
}//Intialization the 1-wire devices;
/***********************************************************************/
/******************* ds 18b20 **********************************/
void One_Wire_Delay(unsigned char delay_time)
{
while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
}
/***********************************************************************/
unsigned char One_Wire_Read_Byte(void)
{
bit temp_bit;
unsigned char i,result=0;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
One_Wire_Bus=1;
temp_bit=One_Wire_Bus;
One_Wire_Delay(11);//delay 62 us
if(temp_bit)
result|=0x01<<i;
}
return(result);
//return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
/***********************************************************************/
void One_Wire_Write_Byte(unsigned char oww_dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
if(oww_dat&0x01)One_Wire_Bus=1;
One_Wire_Delay(25);//delay 128 us
One_Wire_Bus=1;
oww_dat>>=1;
}
One_Wire_Delay(10);
}//Write a byte to the 1-wire bus;
/***********************************************************************/
void Read_18B20(void)
{
unsigned char tempH,tempL;
Initize_One_Wire_Bus();
num3[7] = 0x11;
One_Wire_Write_Byte(SkipRom);
num3[5] = 0x11;
_nop_();
//There is just one DS1820 on the bus;
One_Wire_Write_Byte(ConvertTemperature);
num3[5] = 0x22;
One_Wire_Delay(5);
//Start to convert temperature;
Initize_One_Wire_Bus();
num3[7] = 0x22;
One_Wire_Write_Byte(SkipRom);
num3[5] = 0x33;
_nop_();
One_Wire_Write_Byte(ReadScratchpad);
num3[5] = 0x44;
GetScratchpad[0]=One_Wire_Read_Byte();
num3[6] = 0x11;
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=One_Wire_Read_Byte();
num3[6] = 0x22;
//Master samples the MSB temperature from the scratchpad;
One_Wire_Delay(120);
tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);
tempL=(GetScratchpad[0]&0x0f);
Initize_One_Wire_Bus();
//Issue a reset to terminate left parts;
if(tempH&0x80)
{
tempH=~tempH;
tempL=~tempL+1;
ResultSignal=1;
//Negative temperature;
}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad;
/************************************************************************/
void main()
{
TMOD=0x11; /*Timer0工作在案MODE1,16位定时器,Timer1工作在MODE2,自动重新装载模式*/
/*| 定时器1 | 定时器1 |*/
/*| GATE|C/T| M1 | M0 | GATE|C/T| M1 | M0 |*/
TH0=(65536-SCAN_CYCLE)/256; /*设定Timer0每隔SCAN_CYCLEu中断一次*/
TL0=(65536-SCAN_CYCLE)%256;
TR0=1; /*启动TEMER0*/
T2CON=0x38; /*令定时器2作为串口波特率发生器*/
/*|TF2|EXF2|RCLK|TCLK|EXEN2|TR2|C/T2|CP/RL2|*/
TL2=0xd9; /*设定串口波特率为9600,晶振为12MHz*/
TH2=0xff;
RCAP2L=0xd9; /*根据串口波特率9600设定自动重载寄存器*/
RCAP2H=0xff;
TR2=1; /*启动定时器2*/
SCON=0x50; /*设定UART工作在MODE3模式,可传送和接收数据,REN=1*/
/*| SM0| SM1| SM2| REN| TB8| RB8| TI | RI |*/
IE=0x9a; /*使能总中断、Timer0,1中断、外部中断1,2*/
/*| EA| - | ET2| ES | ET1| EX1| ET0| EX0|*/
IP=0x12; /*优先Timer0中断*/
/*| - | - | PT2| PS | PT1| PX1| PT0| PX0|*/
// P3=0xFF;
// p33 =0 ;
// p33 = 1;
// ds = 0 ;
GetScratchpad[0] = 0x00;
GetScratchpad[1] = 0x00;
while(1)
{
keyhandle();
// time_delay(1000000);
}
}
void keyhandle() //按键处理
{
uchar temp1,key;
uint aa=0,judge=0;
key=keyjudge();
if(key!=0x55)
{
temp1=c_keyCode[key];
switch(temp1)
{
case 0x0a: statussend(num1);
break;
case 0x0b: statussend(num2);
break;
case 0x0c:
/*{
Initize_One_Wire_Bus();
statussend(num3);
break;
} */
/*
statussend(num3);
break;
*/
{
Read_18B20();
num3[0]=GetScratchpad[1];
num3[1]=GetScratchpad[0];
statussend(num3);
break;
}
default: break;
}
UARTKeyFlag=0xaa; /*清串口键盘命令标志位*/
}
}
void statussend(uchar num[8])
{
uchar i;
IE&=0xef;
for(i=0;i<8;i++)
{
SBUF=num[i];
while(TI!=1);
TI=0;
}
IE|=0x10;
}
void statussend_one()
{
uchar i;
IE&=0xef;
for(i=0;i<8;i++)
{
//SBUF=ResultTemperatureH;
SBUF=0x45;
while(TI!=1);
TI=0;
}
IE|=0x10;
}
uchar keyjudge(void) //键盘处理
{
uchar j,counterKeyPressedNum;
uchar keyret=0x55;
uint keynum;
scankey();
if(keyflag!=0)
{
delay(1000);
scankey();
P1=0x0f;
while((P1&0x0f)!=0x0f);
counterKeyPressedNum=0;
for(j=0;j<16;j++)
{
keynum=((uint)0x0001)<<j;
if((keyflag&keynum)==keynum)
{
counterKeyPressedNum++;
keyret=j;
}
}
if(counterKeyPressedNum>1)
{
return(0x55);
}
else
{
return(keyret);
}
}
if(UARTKeyFlag!=0xaa)
{
return(UARTKeyFlag);
}
return(0x55);
}
void scankey() //键盘扫描
{
uchar scanvalue=0xef,scan,scantemp;
for(scan=0;scan<4;scan++)
{
P1=scanvalue;
scantemp=P1;
if((scantemp&0x01)==0x00)
keyflag|=(0x0001<<(scan*4+0));
else
keyflag&=~(0x0001<<(scan*4+0));
if((scantemp&0x02)==0x00)
keyflag|=(0x0001<<(scan*4+1));
else
keyflag&=~(0x0001<<(scan*4+1));
if((scantemp&0x04)==0x00)
keyflag|=(0x0001<<(scan*4+2));
else
keyflag&=~(0x0001<<(scan*4+2));
if((scantemp&0x08)==0x00)
keyflag|=(0x0001<<(scan*4+3));
else
keyflag&=~(0x0001<<(scan*4+3));
scanvalue=scanvalue<<1|0x01;
}
}
void delay(uint value) //延时
{
while(value!=0)
{
value--;
}
}
void Timer0Int() interrupt 1 using 2 //中断Timer0
{
static uchar temp;
uint temp1;
temp1=65536-SCAN_CYCLE;
TH0=temp1>>8;
TL0=temp1-((temp1>>8)<<8);
if(++temp>=10)
temp=1;
get_num=temp;
}
void UARTInt() interrupt 4 using 1 //串口中断服务程序
{
if(TI==1) //是发送中断则返回//
{
TI=0;
return;
}
else //接收到数据//
{
uchar rcv;
while(RI!=1);
{
RI=0; //请接收标志位以激活下次串口中断//
rcv=SBUF;
switch(rcv)
{
case 48:
UARTKeyFlag=0x0d; //收到'0'//
//数码管左移
break;
case 49: UARTKeyFlag=0x00; //收到'1'//
//数码管左移
break;
case 50:
UARTKeyFlag=0x01; //收到'2'//
//数码管左移
break;
case 51:
UARTKeyFlag=0x02; //收到'3'//
break;
case 52:
UARTKeyFlag=0x04; //收到'4'//
break;
case 53: UARTKeyFlag=0x05; //收到'5'//
break;
case 54: UARTKeyFlag=0x06; //收到'6'//
break;
case 55: UARTKeyFlag=0x08; //收到'7'//
break;
case 56: UARTKeyFlag=0x09; //收到'8'//
break;
case 57: UARTKeyFlag=0x0a; //收到'9'//
break;
case 65: UARTKeyFlag=0x03; //收到'A'//
break;
case 66: UARTKeyFlag=0x07; //收到'B//
break;
case 67: UARTKeyFlag=0x0b; //收到'C'//
break;
case 68: UARTKeyFlag=0x0f; //收到'D'*/
break;
case 69: UARTKeyFlag=0x0c; //收到'E'//
break;
case 70: UARTKeyFlag=0x0e; //收到'F'*/
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -