📄 text1.c
字号:
#include<reg52.h>
sbit a_0=ACC^0;
bit flag=0;
sbit DQ=P2^2;
#define T_CONVER 0x44
#define T_CONFIG 0x5f
#define T_READ 0xbe
#define T_WRITE 0x4e
#define T_ROM_JMP 0xcc
#define T_C0PY_E2 0x48
#define T_RECALL_E2 0xb8
#define T_cont 0.0625
bit T_sign;
unsigned char t_bai,t_shi,t_ge,t_fen,t_miao;
#define LCM1602_DB0_DB7 P0
sbit LCM1602_RS=P3^5;
sbit LCM1602_RW=P2^0;
sbit LCM1602_E=P3^4;
sbit LCM1602_Busy=P0^7;
unsigned char code DS18B20[]={"DS18B20 OK!"};
unsigned char code welcome[]={"High Sensor Temp"};
unsigned char code welcome1[]={"MADE By:ZhangHu"};
unsigned char code Temp[]={"Temp: "};
void delay(unsigned int delay_time)
{ while(delay_time--);}
void DS18B20_INIT()
{unsigned char i=1;
while(flag==0)
{DQ=1;
delay(8);
DQ=0;
delay(52);
DQ=1;
delay(2);
while(1)
{i=DQ;
if(i==0)
{flag=1;
delay(10);
break;
}}}
flag=0;}
void DS18B20_send(unsigned char out_data)
{
unsigned char i=0;
for(i=8;i>0;i--)
{DQ=0;
ACC=out_data&0x01;
DQ=a_0;
delay(5);
DQ=1;
out_data>>=1;
}}
unsigned char DS18B20_read(void)
{
unsigned char i,in_data;
in_data=0;
for(i=8;i>0;i--)
{ DQ=0;
in_data>>=1;
DQ=1;
if(DQ)
in_data|=0x80;
delay(4);}
return(in_data);}
void ReadTemperature()
{ float temp;
unsigned int y;
unsigned char T_L=0;
unsigned char T_H=0;
unsigned char k;
DS18B20_INIT();
DS18B20_send(T_ROM_JMP);
DS18B20_send(T_CONVER);
DS18B20_INIT();
DS18B20_send(T_ROM_JMP);
DS18B20_send(T_READ);
T_L=DS18B20_read();
T_H=DS18B20_read();
k=T_H&0xf8;
if(k==0xf8)
T_sign=1;
else
T_sign=0;
T_H=T_H&0x07;
temp=(T_H*256+T_L)*T_cont;
temp=temp*100;
t_bai=(signed int)temp/10000;
y=(signed int)temp%10000;
t_shi=y/1000;
y=(signed int)y%1000;
t_ge=y/100;
y=(signed int)y%100;
t_fen=y/10;
t_miao=(signed int)y%10;
}
void LCM1602_Delay(unsigned char count)
{ unsigned char i;
while(--count!=0)
{for(i=0;i<125;i++);}}
void LCM1602_TestBusy(void)
{do{LCM1602_DB0_DB7=0xff;
LCM1602_RS=0;
LCM1602_RW=1;
LCM1602_E=0;
LCM1602_Delay(2);
LCM1602_E=1;
}
while(LCM1602_Busy);}
void LCM1602_WriteCmd(unsigned char LCM1602_command)
{LCM1602_DB0_DB7=LCM1602_command;
LCM1602_RS=0;
LCM1602_RW=0;
LCM1602_E=0;
//LCM1602_Delay(2);
LCM1602_TestBusy();
LCM1602_E=1;}
void LCM1602_WriteData(unsigned char LCM1602_data)
{LCM1602_DB0_DB7=LCM1602_data;
LCM1602_RS=1;
LCM1602_RW=0;
LCM1602_E=0;
//LCM1602_Delay(2);
LCM1602_TestBusy();
LCM1602_E=1;
}
void LCM1602_Init(void)
{LCM1602_WriteCmd(0x01);
//LCM1602_Delay(2);
LCM1602_WriteCmd(0x38);
//LCM1602_Delay(2);
LCM1602_WriteCmd(0x06);
// LCM1602_Delay(2);
LCM1602_WriteCmd(0x0c);
//LCM1602_Delay(2);
}
void print(unsigned char a,unsigned char *str)
{
LCM1602_WriteCmd(a);
while(*str!='\0')
{ LCM1602_WriteData(*str++);}
*str=0;}
void print1(unsigned char B,unsigned char t)
{
LCM1602_WriteCmd(B);
LCM1602_WriteData(t);}
void main(void)
{
LCM1602_Init();
print(0x80,welcome);
print(0xc0,welcome1);
//print(0xC0,DS18B20);
LCM1602_Init();
while(1)
{
ReadTemperature();
print(0x80,DS18B20);
print(0xc0,Temp);
if(T_sign==1)
print1(0xc7,0x2b);
if(t_bai!=0)
print1(0xc8,t_bai+0x30);
else
print1(0xc8,0x20);
print1(0xc9,t_shi+0x30);
print1(0xca,t_ge+0x30);
print1(0xcb,0x2e);
print1(0xcc,t_fen+0x30);
print1(0xcd,t_miao+0x30);
print1(0xce,0x43);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -