DS18B20温度检测器
程序代码:
#include <REGX51.H> #include <intrins.h> #include "display.h" void delay5us(unsigned char x) { unsigned char i; while(x--){ _nop_(); i = 11; while (--i);} } sbit DQ=P3^7; bit DS18B20chushihua() { bit i; DQ=0; delay5us(96); DQ=1; delay5us(16); i=DQ; delay5us(80); return i; } void writeDS18B20(unsigned char xdat) { unsigned char i; for(i=0;i<8;i++){ DQ=0; delay5us(1); DQ=xdat&0x01; delay5us(12); DQ=1; xdat=xdat>>1; } } unsigned char readDS18B20() { unsigned char i,rdat; for(i=0;i<8;i++){ rdat=rdat>>1; DQ=0; delay5us(1); DQ=1; delay5us(1); if(DQ==1){ rdat=rdat|0x80; delay5us(12); DQ=1;} } return rdat; } unsigned char L; unsigned int H; unsigned int temp; void main() { while(1){ DS18B20chushihua(); writeDS18B20(0xcc); writeDS18B20(0x44); DS18B20chushihua(); writeDS18B20(0xcc); writeDS18B20(0xbe); L=readDS18B20(); H=readDS18B20(); H=(H<<8)|L; if((H&0xf800)==0xf800) H=!H+1; temp=H*0.0625; display_s((double)temp); } }
调用函数:#include <REGX51.H>
#include "delay.h"
sbit wei1=P2^1;
sbit wei2=P2^3;
sbit wei3=P2^5;
sbit wei4=P2^7;
//********????,????,???????********
unsigned char code table[]={
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
//********????,????,???????********
unsigned char code table1[]={
//0., 1., 2., 3., 4., 5., 6., 7., 8., 9., A., B., C., D., E., F.
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
void display_s(double x)
{
unsigned int yi,er,san,si;
yi=(unsigned int)(x)/10;
er=(unsigned int)(x)%10;
san=(unsigned int)(x*10)%10;
si=12;
while(1){
P0=table[yi];
wei1=0;
delay(2);
wei1=1;
P0=table1[er];
wei2=0;
delay(2);
wei2=1;
P0=table[san];
wei3=0;
delay(2);
wei3=1;
P0=table[si];
wei4=0;
delay(2);
wei4=1;}
}
protuse仿真图:
求解:为什么返回0
[此贴子已经被作者于2023-6-14 21:37编辑过]