123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #include "ds18b20.h"
- #include "delay.h"
- void DS18B20_Rst(void)
- {
-
- DS18B20_IO_OUT();
- DS18B20_DQ_OUT=0;
- delay_us(750);
- DS18B20_DQ_OUT=1;
- delay_us(15);
- }
- u8 DS18B20_Check(void)
- {
- u8 retry=0;
- DS18B20_IO_IN();
- 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;
- }
- u8 DS18B20_Read_Bit(void)
- {
- u8 data;
- DS18B20_IO_OUT();
- DS18B20_DQ_OUT=0;
- delay_us(2);
- DS18B20_DQ_OUT=1;
- DS18B20_IO_IN();
- delay_us(12);
- if(DS18B20_DQ_IN)data=1;
- else data=0;
- delay_us(50);
- return data;
- }
- u8 DS18B20_Read_Byte(void)
- {
- u8 i,j,dat;
- dat=0;
- for (i=1;i<=8;i++)
- {
- j=DS18B20_Read_Bit();
- dat=(j<<7)|(dat>>1);
- }
- return dat;
- }
- void DS18B20_Write_Byte(u8 dat)
- {
- u8 j;
- u8 testb;
- DS18B20_IO_OUT();
- for (j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1;
- if (testb)
- {
- DS18B20_DQ_OUT=0;
- delay_us(2);
- DS18B20_DQ_OUT=1;
- delay_us(60);
- }
- else
- {
- DS18B20_DQ_OUT=0;
- delay_us(60);
- DS18B20_DQ_OUT=1;
- delay_us(2);
- }
- }
- }
- void DS18B20_Start(void)
- {
- DS18B20_Rst();
- DS18B20_Check();
- DS18B20_Write_Byte(0xcc);
- DS18B20_Write_Byte(0x44);
- }
- u8 DS18B20_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
-
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- DS18B20_Rst();
- return DS18B20_Check();
- }
- void Input_Mode(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void Output_Mode(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- short DS18B20_Get_Temp(void)
- {
- u8 temp;
- u8 TL,TH;
- short tem;
- DS18B20_Start ();
- DS18B20_Rst();
- DS18B20_Check();
- DS18B20_Write_Byte(0xcc);
- DS18B20_Write_Byte(0xbe);
- TL=DS18B20_Read_Byte();
- TH=DS18B20_Read_Byte();
-
- if(TH>7)
- {
- TH=~TH;
- TL=~TL;
- temp=0;
- }else temp=1;
- tem=TH;
- tem<<=8;
- tem+=TL;
- tem=(float)tem*0.625;
- if(temp)return tem;
- else return -tem;
- }
-
|