当前位置:文档之家› 单片机实验程序温度测量实验

单片机实验程序温度测量实验


程序分析2-动态显示
P1_buff=wei[disp_cnt]; P1=P1_buff; P2=duan[disp_buff[disp_cnt]]; if(disp_cnt==1) P2=P2&0x7f; /*显示小数点*/ disp_cnt=disp_cnt+1; if (disp_cnt==5) disp_cnt=0;
实验20 温度测量
功能
• 驱动DS18D20,读取温度值 • 在数码管上显示温度 • 每秒测量一次温度
程序设计
• 利用定时器中断进行动态显示 • 利用定时器生成秒脉冲 • 编写1wire总线协议读取DS18B20中的温度
程序分析1-1-wire协议
• 读取温度ReadTemperature
– – – – – – – – – – – – – – DS18B20_Reset(); //设备复位 DS18B20_WriteByte(0xCC); //跳过ROM命令 DS18B20_WriteByte(0x44); //开始转换命令 DS18B20_Reset(); //设备复位 DS18B20_WriteByte(0xCC); //跳过ROM命令 DS18B20_WriteByte(0xBE); //读暂存存储器命令 TPL = DS18B20_ReadByte(); //读温度低字节 TPH = DS18B20_ReadByte(); //读温度高字节 t=TPH; t<<=8; t = t | TPL; tt =t * 0.0625; //将温度的高位与低位合并 t = tt * 10+0.5; //对结果进行4舍5入 return(t)
code unsigned char duan[]= //数码管显示字型表 {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90, //0..9 0x88,0x83,0xA7,0xA1,0x86,0x8E,0xff}; //A b c d E F blank (10~16)

unsigned char code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; // 数码管位的选择
程序分析3-全局变量
• • • • • • • • #include <stc12c5410ad.h> #include <intrins.h> #include <math.h> #define MAIN_Fosc 11059200UL //定义主时钟 #define Timer0_Reload (MAIN_Fosc / 12000) typedef unsigned char BYTE; BYTE TPH; //存放温度值的高字节 BYTE TPL; //存放温度值的低字节
程序分析2-刷新显示缓冲区
void ChangeTime(void) { secflag=0; tempbuf=ReadTemperature(); disp_buff[0]=tempbuf/1000; disp_buff[1]=tempbuf%100/10; disp_buff[2]=tempbuf%10; disp_buff[3]=16; disp_buff[4]=16; disp_buff[5]=16; }
• • • • • • •
• • • •
sbit DQ =P1^7; //定义DS18B20通信端口 bit secflag=0; unsigned char disp_buff[6]; unsigned int Countms; unsigned int tempbuf; unsigned char disp_cnt; unsigned char P1_buff;
相关主题