仿真截图:
//仿真文件网盘地址:http://pan.http://www.wodefanwen.com//s/1qW8sGQK
//程序:
#include
#define uchar unsigned char #define uint unsigned int
sbit P00 = P0^0; sbit P01 = P0^1; sbit P02 = P0^2; sbit P03 = P0^3; sbit P04 = P0^4; sbit P05 = P0^5; sbit P06 = P0^6; sbit P07 = P0^7;
sbit P10 = P1^0; sbit P11 = P1^1; sbit P12 = P1^2; sbit P13 = P1^3; sbit P14 = P1^4;
sbit P15 = P1^5; sbit P16 = P1^6; sbit P17 = P1^7;
sbit P20 = P2^0; sbit P21 = P2^1; sbit P22 = P2^2; sbit P23 = P2^3; sbit P24 = P2^4; sbit P25 = P2^5; sbit P26 = P2^6; sbit P27 = P2^7;
sbit P30 = P3^0; sbit P31 = P3^1; sbit P32 = P3^2; sbit P33 = P3^3; sbit P34 = P3^4; sbit P35 = P3^5; sbit P36 = P3^6; sbit P37 = P3^7;
//****** DS18B20 ****** #define DQ P17
/*************精确延时函数*****************/ void delay10us(void) //误差 0us {
unsigned char a,b; for(b=1;b>0;b--)
for(a=2;a>0;a--); }
void delay20us(void) //误差 0us {
unsigned char a,b; for(b=1;b>0;b--)
for(a=7;a>0;a--); }
void delay30us() //误差 0us {
unsigned char a,b; for(b=3;b>0;b--)
for(a=3;a>0;a--); }
void delay100us() //误差 0us {
unsigned char a,b; for(b=1;b>0;b--)
for(a=47;a>0;a--); }
void delay200us(void) //误差 0us {
unsigned char a,b; for(b=1;b>0;b--)
for(a=97;a>0;a--); }
void delay500us() //误差 0us {
unsigned char a,b; for(b=71;b>0;b--) for(a=2;a>0;a--); }
void DS18B20_init() //DS18B20初始化 复位 { DQ = 1; _nop_(); _nop_(); _nop_(); _nop_(); //延时几个时钟周期 保证DQ引脚稳定在高电平 DQ = 0; delay500us(); //最短为480us的低电平信号 复位 DQ = 1; //拉高总线 15-60us delay30us(); delay200us(); //延时足够时间 复位基本上都会成功 因此不必再判断是否复位成功 DQ = 1; //释放总线 }
uchar Read_One_Byte() { uchar i; uchar byte = 0; for(i = 0;i < 8;i++) { DQ = 1;
_nop_(); _nop_(); _nop_(); _nop_(); //延时几个时钟周期 保证DQ引脚稳定在高电平 DQ = 0; byte >>= 1; delay20us(); DQ = 1; //给脉冲 产生读时间间隙 delay10us(); //延时一定时间后,读DQ的值 if(DQ) { byte |= 0x80;} //读得DQ为1 将1写到dat最高位 ;读得DQ为0 不必处理 delay100us(); DQ = 1; } return(byte); }
void Write_One_Byte(uchar byte) { uchar i = 0; for(i = 0;i < 8;i++) { DQ = 1; _nop_(); _nop_(); _nop_(); _nop_(); //延时几个时钟周期 保证DQ引脚稳定在高电平 DQ = 0; DQ = byte & 0x01; //写所给数据最低位 delay30us(); byte >>= 1; } }
int Read_Temp() ////////***读取温度值***********///// 每次读写均要先复位 { int t; float tep; uchar a,b; DS18B20_init(); Write_One_Byte(0xcc); //跳过ROM命令 单个传感器所以不必读取ROM里的序列号 Write_One_Byte(0x44); //开始转换 DS18B20_init(); Write_One_Byte(0xcc); //跳过ROM命令 Write_One_Byte(0xbe); //读寄存器,共九字节,前两字节为转换值
a = Read_One_Byte(); //a存低字节 b = Read_One_Byte(); //b存高字节 t = b; t <<= 8;//高字节转换为10进制 t = t|a; tep = t*0.0625;//转换精度为0.0625/LSB t = tep*10 + 0.5;//保留1位小数并四舍五入****后面除10还原正确温度值) return(t); }
/*********** LCD ************/ #define RS P22 #define RW P21 #define LCDEN P20 #define LCD_DATA P0 //P0口接LCD数据口 #define LCD_BUSY P07 //lcd1602忙碌标志位
uchar idata lcd_code[10];//用来标记lcd1602 什么时候清显示 每个页面都设一个code,code不想同时清显示
//*****************************************延时函数***************************************// void delayms(uint ms)//延时?个 ms {
uchar a,b,c; while(ms--) { for(c=1;c>0;c--)
for(b=142;b>0;b--) for(a=2;a>0;a--); } } /*
//**********字符串复制函数**********
void string_copy(uchar *target,uchar *source)//字符串复制 target:目标 source:源 { uchar i = 0; for(i = 0;source[i] != '\\0';i++)//注意target的长度 无保护措施! { target[i] = source[i]; } target[i] = '\\0'; }
//**********字符串比较函数**********
uchar string_cmp(uchar *target,uchar *source)//字符串比较 target:目标 source:源