|
跟著b站上學(xué)習(xí)的單片機(jī),在溫度傳感器碰到了瓶頸,訪問溫度返回的數(shù)據(jù)一直顯示255看到本論壇有相關(guān)的帖子說是時(shí)序問題,我就用軟件又重新生成了一遍相關(guān)的延時(shí)
結(jié)果還是不行,然后又屢著視頻代碼一遍遍的看還是不行
希望大佬可以幫忙看看是那邊錯(cuò)的
下面附上代碼
單總線的代碼
- #include <REGX52.H>
- sbit OneWire_DQ=P3^7;
- unsigned char OneWire_Init()
- {
- unsigned char i,r;
- OneWire_DQ=1;
- OneWire_DQ=0;
- i = 247;while (--i);//延時(shí)500us 229
- OneWire_DQ=1;
- i = 32;while (--i);//延遲70us 31
- i=OneWire_DQ;
- i = 247;while (--i);//延時(shí)500us 229
- return r;
- }
- void OneWire_Write(unsigned char Bit)
- {
- unsigned char i;
- OneWire_DQ=0;
- i = 4;while (--i);//延遲10us 3
- OneWire_DQ=Bit;
- i = 25;while (--i);//延遲60us 4(60)
- OneWire_DQ=1;
- }
- unsigned char OneWire_Read()
- {
- unsigned char i;
- unsigned char Bit;
- OneWire_DQ=0;
- i = 2;while (--i);//延遲5us
- OneWire_DQ=1;
- i = 2;while (--i);//延遲5us (9us)
- Bit=OneWire_DQ;
- i = 24;while (--i);//延遲50us 24
- return Bit;
- }
- void OneWire_SendByte(unsigned char Data)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- OneWire_Write(Data&(0x01<<i));
- }
- }
- unsigned char OneWire_ReadByte()
- {
- unsigned char i;
- unsigned char Byte=0x00;
- for(i=0;i<8;i++)
- {
- if(OneWire_Read()){Byte|=(0x01<<i);}
- }
- return Byte;
- }
復(fù)制代碼 返回?cái)?shù)據(jù)的代碼
- #include <REGX52.H>
- #include "OneWire.h"
- #include "LCD1602.h"
- void Delay1ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 1;
- j = 110;
- do
- {
- while (--j);
- } while (--i);
- }
- void DS18B20_ConvertT()
- {
- OneWire_Init();
- Delay1ms();
- OneWire_Write(0xCC);
- OneWire_Write(0x44);
- }
- float DS18B20_Read()
- {
- unsigned char TLSB,TMSB;
- int temp;
- float T;
- OneWire_Init();
- Delay1ms();
- OneWire_Write(0xCC);
- OneWire_Write(0xBE);
- TLSB=OneWire_ReadByte();
- TMSB=OneWire_ReadByte();// 用的LCD1602顯示的數(shù)據(jù)
- LCD_ShowNum(1,1,TMSB,8);//
- LCD_ShowNum(1,9,TLSB,8);
- temp=(TMSB<<8)|TLSB;
- T=temp/16.0;
- return T;
- }
復(fù)制代碼
|
|