为什么DS18B20显示0度,用示波器也用波形出来。pcb板子应该也没有问题。DQ引脚接的是PE7,谢谢!!!
#include "stm32f4xx.h"
#include "delay.h"
#include "timer.h"
#include "exti.h"
#include "display.h"
#include "oled.h"
#include "stdio.h"
#include "ds18b20.h"
int main(void)
{
Delay_Init();
DS18B20_Init();
OLED_Init();
OLED_Show_Str(16,0,"DS18B20_Test",16);
Delay_ms(500);
Delay_ms(500);
while(1)
{
DS18B20_Test();
}
}
#include "ds18b20.h"
#include <stdio.h>
extern const unsigned char Text16[][32];
/**************************************************************
*1| Äü£o¸′λDS18B20
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PA0 OUTPUT
DS18B20_DQ_OUT=0; //à-μíDQ
Delay_us(750); //à-μí750us
DS18B20_DQ_OUT=1; //DQ=1
Delay_us(15); //15US
}
/**************************************************************
*1| Äü£oμè′yDS18B20μÄ»Øó|
*2Î êy: ÎT
*·μ»ØÖμ: ·μ»Ø1:Î′¼ì2aμ½DS18B20μÄ′æÔú ·μ»Ø0:′æÔú
**************************************************************/
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PA0 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
Delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
Delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
/**************************************************************
*1| Äü£o′óDS18B20¶áè¡ò»¸öλ
*2Î êy: ÎT
*·μ»ØÖμ: 1/0
**************************************************************/
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT(); //SET PA0 OUTPUT
DS18B20_DQ_OUT=0;
Delay_us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN(); //SET PA0 INPUT
Delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
Delay_us(50);
return data;
}
/**************************************************************
*1| Äü£o′óDS18B20¶áè¡ò»¸ö×Ö½ú
*2Î êy: ÎT
*·μ»ØÖμ: ¶áμ½μÄêy¾Y
**************************************************************/
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
/**************************************************************
*1| Äü£oD′ò»¸ö×Ö½úμ½DS18B20
*2Î êy: dat£oòaD′èëμÄ×Ö½ú
*·μ»ØÖμ: ÎT
**************************************************************/
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT(); //SET PA0 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0; // Write 1
Delay_us(2);
DS18B20_DQ_OUT=1;
Delay_us(60);
}
else
{
DS18B20_DQ_OUT=0; // Write 0
Delay_us(60);
DS18B20_DQ_OUT=1;
Delay_us(2);
}
}
}
/**************************************************************
*1| Äü£o¿aê¼Î¶è×a»»
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
void DS18B20_Start(void) // ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc); // skip rom
DS18B20_Write_Byte(0x44); // convert
}
/**************************************************************
*1| Äü£o3õê¼»ˉDS18B20μÄIO¿ú DQ í¬ê±¼ì2aDSμÄ′æÔú
*2Î êy: ÎT
*·μ»ØÖμ: ·μ»Ø1:2»′æÔú ·μ»Ø0:′æÔú
**************************************************************/
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // ê1ÄüPA¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // PA4¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // ÆÕí¨êä3öÄ£ê½
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 100MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); // 3õê¼»ˉIO¿ú
GPIO_SetBits(GPIOC,GPIO_Pin_1); //PA4êä3ö¸ß
DS18B20_Rst();
return DS18B20_Check();
}
/**************************************************************
*1| Äü£oÅäÖÃêä3ö¶Ë¿ú
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
void DQ1820_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // ÆÕí¨êä3öÄ£ê½
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // ¿a©êä3ö
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/**************************************************************
*1| Äü£oÅäÖÃêäèë¶Ë¿ú
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
void DQ1820_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // êäèëÄ£ê½
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // ¸¡¿Õêäèë
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
short DS18B20_Get_Temp(void)
{
uint8_t temp;
uint8_t TL,TH;
short tem;
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc); // skip rom
DS18B20_Write_Byte(0xbe); // convert
TL = DS18B20_Read_Byte(); // LSB
TH = DS18B20_Read_Byte(); // MSB
if(TH>7)
{
TH = ~TH;
TL = ~TL;
temp = 0;
}else temp = 1; // ζèÎaÕy
tem = TH; // »ñμø߰Ëλ
tem <<= 8;
tem += TL; // »ñμÃμí°Ëλ
tem = (float)tem * 0.625;
if(temp)return tem; // ·μ»ØζèÖμ
else return -tem;
}
/**************************************************************
*1| Äü£oDS18B20′«¸DÆ÷¼ì2a
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
void DS18B20_WHILE(void)
{
unsigned char init_0 = 0;
while(DS18B20_Init())
{
init_0 ++;
if(init_0 == 240);
//SoftReset();
OLED_Show_Str(16*1,2,"DS18B20 Error",16);
}
OLED_Show_Str(16*1,2,"DS18B20 Ok ",16);
}
/**************************************************************
*1| Äü£oOLEDÏÔê¾Î¶èÖμ
*2Î êy: ÎT
*·μ»ØÖμ: ÎT
**************************************************************/
extern unsigned char tmp_buf[16];
void DS18B20_Test(void)
{
short temperature;
char date[20] ;
temperature = DS18B20_Get_Temp();
if(temperature<0)
{
sprintf(date,"Temp:-%3.1f ¡æ",temperature/10.0);
OLED_Show_Str(16*1,3,date,16);
}
else
{
sprintf(date,"Temp:%3.1f ¡æ",temperature/10.0);
OLED_Show_Str(16*1,3,date,16);
}
}
|