1、.仿真截图:/仿真文件网盘地址:pan.baidu./s/1qW8sGQK/程序:#include#include #define uchar unsigned char #define uint unsigned intsbit P00 = P00;sbit P01 = P01;sbit P02 = P02;sbit P03 = P03;sbit P04 = P04;sbit P05 = P05;sbit P06 = P06;sbit P07 = P07;sbit P10 = P10;sbit P11 = P11;sbit P12 = P12;sbit P13 = P13;sbit P14
2、= P14;sbit P15 = P15;sbit P16 = P16;sbit P17 = P17;sbit P20 = P20;sbit P21 = P21;sbit P22 = P22;sbit P23 = P23;sbit P24 = P24;sbit P25 = P25;sbit P26 = P26;sbit P27 = P27;sbit P30 = P30;sbit P31 = P31;sbit P32 = P32;sbit P33 = P33;sbit P34 = P34;sbit P35 = P35;sbit P36 = P36;sbit P37 = P37;/* DS18B2
3、0 *#define DQ P17/*精确延时函数*/void delay10us(void) /误差 0us unsigned char a,b; for(b=1;b0;b-) for(a=2;a0;a-);void delay20us(void) /误差 0us unsigned char a,b; for(b=1;b0;b-) for(a=7;a0;a-);void delay30us() /误差 0us unsigned char a,b; for(b=3;b0;b-) for(a=3;a0;a-);void delay100us() /误差 0us unsigned char a,b
4、; for(b=1;b0;b-) for(a=47;a0;a-);void delay200us(void) /误差 0us unsigned char a,b; for(b=1;b0;b-) for(a=97;a0;a-);void delay500us() /误差 0us unsigned char a,b; for(b=71;b0;b-) for(a=2;a0;a-);void DS18B20_init() /DS18B20初始化 复位DQ = 1;_nop_();_nop_();_nop_();_nop_();/延时几个时钟周期 保证DQ引脚稳定在高电平DQ = 0;delay500u
5、s();/最短为480us的低电平信号 复位DQ = 1; /拉高总线 15-60us delay30us();delay200us();/延时足够时间 复位基本上都会成功 因此不必再判断是否复位成功DQ = 1;/释放总线uchar Read_One_Byte()uchar i;uchar byte = 0;for(i = 0;i = 1;delay20us();DQ = 1;/给脉冲 产生读时间间隙delay10us();/延时一定时间后,读DQ的值if(DQ) byte |= 0x80;/读得DQ为1 将1写到dat最高位 ;读得DQ为0 不必处理delay100us();DQ = 1;
6、return(byte);void Write_One_Byte(uchar byte)uchar i = 0;for(i = 0;i = 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);/读寄存器,共九字节
7、,前两字节为转换值a = Read_One_Byte(); /a存低字节b = Read_One_Byte(); /b存高字节t = b;t 0;c-) for(b=142;b0;b-) for(a=2;a0;a-);/*/*字符串复制函数*void string_copy(uchar *target,uchar *source)/字符串复制 target:目标 source:源uchar i = 0;for(i = 0;sourcei != 0;i+)/注意target的长度 无保护措施!targeti = sourcei;targeti = 0;/*字符串比较函数*uchar string
8、_cmp(uchar *target,uchar *source)/字符串比较 target:目标 source:源uchar revalue;uchar i = 0;for(i = 0;targeti != 0 & sourcei != 0;i+) /两个都不等于0才执行 出现一个等于0就跳出if(targeti = sourcei)revalue = 1;elserevalue = 0;break;if(revalue = 1)if(targeti = 0 & sourcei = 0)revalue = 1;elserevalue = 0;return(revalue);*/* LCD *
9、/LCD基本函数:void busy_check() /忙碌检测/*RW = 1; /读RS = 0;/指令寄存器LCD_DATA = 0xFF;/实验证明读数时要将I/O口要置1 LCDEN = 0;_nop_();_nop_();_nop_();_nop_();LCDEN = 1;/ EN高电平读信息 负跳变执行指令_nop_();_nop_();_nop_();_nop_();while(1)if(LCD_BUSY = 0)/P07 = 0跳出循环break; */delayms(2);/仿真时用延时法 下载到真实单片机上时,将这句注释掉,采用上面的语句。void lcdwrcom(uc
10、har command)/写指令 busy_check();RW = 0;/写 RS = 0;/指令寄存器 LCD_DATA = command;LCDEN = 1;/负跳变写入指令_nop_();_nop_();_nop_();_nop_();LCDEN = 0;void lcdwrdata(uchar lcd_data)/写数据数字、字母、标点符号都是数据busy_check();RW = 0;/写 RS = 1;/数据寄存器 LCD_DATA = lcd_data; LCDEN = 1;/负跳变写入指令_nop_();_nop_();_nop_();_nop_();LCDEN = 0;v
11、oid lcd_init()delayms(15);/必要 lcd1602上电到电压稳定需要时间 RW = 0;/写RS = 0;/指令寄存器 LCD_DATA = 0x38;/ 0x38设置显示模式为:16X2 显示,5X7 点阵,8 位数据接口 LCDEN = 1;_nop_();_nop_();_nop_();_nop_();LCDEN = 0;delayms(5);lcdwrcom(0x0c);/打开显示 无光标 不闪烁lcdwrcom(0x06);/指令3 光标右移 屏幕所有文字移动无效lcdwrcom(0x01);/ 清显示,光标复位到地址00H位置。/LCD扩展函数:void a
12、ddress(uchar x,uchar y) /定位下一步要写数的地址uchar location;if(x = 0) location = 0x80|y;elselocation = 0xC0|y; lcdwrcom(location);void printchar(uchar x,uchar y,uchar letter)/显示字母、单个字符address(x,y);lcdwrdata(letter);void printword(uchar x,uchar y,uchar *word) /显示单词(字符数组)uchar i = 0;for(i = 0;wordi != 0;i+)add
13、ress(x,y + i);lcdwrdata(wordi);/*void printuint(uchar x,uchar y,uchar num_ws_max,uint num)/显示无符号整形 065535 x:行 y:列 num_ws_max 变量的最大位数uchar i = 0; uchar str5 = 0x20,0x20,0x20,0x20,0x20;if(num = 10000)str0 = num/10000 + 0;str1 = num%10000/1000 + 0;str2 = num%1000/100 + 0;str3 = num%100/10 + 0;str4 = nu
14、m%10 + 0; /str5 = 0; /手动加字符串结束标志else if(num = 1000)str0 = num/1000 + 0;str1 = num%1000/100 + 0;str2 = num%100/10 + 0;str3 = num%10 + 0;str4 =0;else if(num = 100)str0 = num/100 + 0;str1 = num%100/10 + 0;str2 = num%10 + 0;str3 = 0;else if(num =10)str0 = num/10 + 0;str1 = num%10 + 0;str2 = 0;else if(nu
15、m = 0)str0 = num + 0;str1 = 0;for(i = 0;i = 5;i+)/uint类型 最大值65535 为5位数if(stri != 0 & i num_ws_max)address(x,y + i);lcdwrdata(stri);else if(stri = 0 & i num_ws_max)address(x,y+i);lcdwrdata( );/空格 / 实现功能:在此变量的位数围,把没数字的位存0x20(空格) /例如:最大有3位:999 当变为99时,存为9+0 9+0 0x20*/void printtemp(uchar x,uchar y ,uint
16、 temp)/显示温度 显示一位小数 【显示效果相当于将一个数除以10并保存一位小数】if(temp 100)address(x,y);lcdwrdata(temp/10 + 0);address(x,y + 1);lcdwrdata(.);address(x,y + 2);lcdwrdata(temp%10 + 0);address(x,y + 3);lcdwrdata( );address(x,y + 4);lcdwrdata( );else if(temp 1000)address(x,y);lcdwrdata(temp/100 + 0);address(x,y + 1);lcdwrdata(temp%100/10 + 0);address(x,y + 2);lcdwrdata(.);address(x,y + 3);lcdwrdata(temp%10 + 0);address(x,y + 4);lcdwrdata( );else if(temp 0 )printchar(0,5,+);printtemp(0,6,temp);else if(temp = 0)printchar(0,5, );printchar(0,6,0);else temp = -temp;printchar(0,5,-);printtemp(0,6,temp); 22 / 22