当前位置:首页 > #基于单片机的数字温度计设计60543 - 图文
单片机课程设计报告
{
j=RdBit(); d=(j<<7)|(d>>1); }
return d; }
/*---------------- 写入字节数据 ----------------*/ void WrByte(uchar d) {
uint i; uchar j; bit btmp;
for(j=0;j<8;j++) //各位由低向高写入DS18B20 {
btmp=d&0x01; d=d>>1;
if(btmp) //写1 {
DQ=0; //延时15us i++; i++;
DQ=1; //写1时隙不低于60u i=8;
while(i>0) i--; }
else //写0 {
DQ=0; i=8;
20
单片机课程设计报告
while(i>0) i--; //保持低电平60us到120us DQ=1; //释放总线 i++; i++; } } }
/*---------------- 启动温度转换 ----------------*/ void Convert() {
uint i;
Delay(125); //延时1ms TxReset(); //初始化DS18B20 RxWait(); //等待DS18B20应答 Delay(125); //延时
WrByte(0xcc); //跳过ROM命令 WrByte(0x44); //温度转换命令 for(i=0;i<250;i++) //延时1s Display(); }
/*----------------- 读取温度值 -----------------*/ void RdTemp(void) {
short int temp; uint x; bit flag = 1;
TxReset(); //初始化DS18B20 RxWait(); //等待DS18B20应答 Delay(125); //延时
21
单片机课程设计报告
WrByte(0xcc); //跳过ROM命令 WrByte(0xbe); //读暂存存储器命令
templow = RdByte(); //温度值低字节,低4位为小数 temphigh = RdByte(); //温度值高字节
temp = (temphigh << 8) | templow; //温度为16位补码 if(temp<0) //负温度 {
flag = 0; temp = ~temp + 1; }
tempzheng = temp >> 4; //温度值整数 x = temp & 0x000f;
tempxiao = (x*10)/16; //温度值小数 if(flag) //正温度
disp[0] = tempzheng/100; //百位 else //负温度
disp[0] = 16; //负号
disp[1] = (tempzheng0)/10; //十位 disp[2] = tempzheng; //个位 disp[3] = tempxiao; //小数 }
/*-------------------- 延时 -------------------*/ void Delay(uint x) {
uint i;
for(i=x;i>0;i--); }
/*---------------- 数码管显示 -----------------*/ void Display()
22
单片机课程设计报告
{
uchar i,j; j=0x08;
for(i=0;i<4;i++) {
P1=j; //数码管位码输出 LOCK=1; //打开锁存 if(i==2)
P0=led[disp[i]]|0x01; //显示小数点
else P0=led[disp[i]]; //数码管段码输出
LOCK=0; //关闭锁存 j=j>>1; //调整位码 Delay(125); P0=0x00; } }
23
共分享92篇相关文档