📄 main_1820_8515.c
字号:
#include "init_1820_8515.h"
/********************/
/****1820初始化****/
/********************/
void init_1820(void)
{
SEI_DP;
CLR_DP;
delayus(600);
SEI_DP;
IN_DP;
delayus(40);
while(CHECK_DP);
SEI_DP;
delayus(200);
}
/********************/
/**1820写一个字节****/
/********************/
void write_1820(uchar WD_data)
{
for(uchar i=0;i<8;i++)
{
CLR_DP;
if(WD_data&(1<<i)) {SEI_DP; }
else { CLR_DP; }
delayus(40);
SEI_DP;
}
SEI_DP;//8位都发完,再释放总线
}
/********************/
/**1820读一个字节****/
/********************/
uchar read_1820()
{
uchar temp=0;
for(uchar n=0;n<8;n++)
{
CLR_DP;
SEI_DP;
IN_DP;
if(CHECK_DP) temp|=(1<<n);//读数据,从低位开始
else temp &= ~(1<<n);
delayus(50);
OUT_DP;
}
return temp;
}
/********************/
/**1820读温度程序****/
/********************/
long get_temperature()
{
uchar teml,temh;
init_1820(); //复位18b20
write_1820(0xcc); //跳过读序号列号的操作
write_1820(0x44);// 发出转换命令
delayms(1);
init_1820();
write_1820(0xcc); //发出读命令
write_1820(0xbe);
teml=read_1820(); //读数据
temh=read_1820();
//temh的高5位为1时, 负温度.为0时为正温度
if((temh&0xF8)==0xF8){ tem=(((0xFF-temh)*256)+(0xFF-teml))*0.625; }//负值方法:取反正*6. 25
else {tem=((temh*256)+teml)*0.625; }//正 //计算具体温度 这里*10
return tem;
}
void display_tem()
{
PORTA=0xC6;//"C"
PORTC=0xe0;//第4位
delayus(10);
PORTA=discode[tem/100];
PORTC=0xd0;//第3位
delayus(10);
PORTA=discode_dot[((tem%100)/10)];//带小数点
PORTC=0xb0;//第2位
delayus(10);
PORTA=discode[(tem%100)%10];
PORTC=0x70;//第1位
delayus(10);
}
void main()
{
Init_devices();
while(1)
{
if(count_ms==0)
{
count_ms=5;
get_temperature();
}
display_tem();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -