#define DS18B20_GPIO_PORT (GPIOC)
#define DS18B20_GPIO_PIN (GPIO_Pin_2)
#define DS18B20_PIN_SET_OUT() GPIO_Init(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN, GPIO_Mode_Out_PP_High_Fast)
#define DS18B20_PIN_SET_IN() GPIO_Init(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN, GPIO_Mode_In_PU_No_IT)
#define DS18B20_WR1() GPIO_SetBits(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)
#define DS18B20_WR0() GPIO_ResetBits(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)
#define R_DS18B20() GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)
/* Private macro -------------------------------------------------------------*/
#define READ_ROM 0x33 //Read serial number
#define SKIP_ROM 0xCC //Skip ROM
#define MATCH_ROM 0x55 //Match ROM
#define CONVERT_TEM 0x44 //Conversion temperature
#define READ_RAM 0xBE //Read temporary register
#define WRITE_RAM 0x4E //Write register
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void _delay_us(uint16_t nCount)
{
nCount *= 3;
while(--nCount);
}
/**
* @brief _delay_ms
* @param nCount
* @retval None
*/
void _delay_ms(uint16_t nCount)
{
while(nCount--)
{
_delay_us(1000);
}
}
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
void Alarm_for_No_DS18B20(void)
{
//If DS18B20 is not found on the single bus, an alarm will be triggered. This action will be handled according to the specific application.
}
//---------------------------------------------------------------------
unsigned char DS18B20_Start(void) //Reset ds18b20 chip
{
unsigned char i,succ=0xff;
DS18B20_PIN_SET_OUT(); //Set to output port
DS18B20_WR0(); //The bus generates a falling edge, initialization starts
for(i=0; i<30; i++)_delay_us(25); //The bus keeps low level between 480 - 960 microseconds; i<20; (25us)
DS18B20_WR1(); //Pull the bus high
DS18B20_PIN_SET_IN(); //Set to input, the host releases the bus and prepares to receive the response pulse of DS18B20
i=0;
while(R_DS18B20()) //Wait for DS18B20 to send a response pulse
{
_delay_us(5); //5
if(++i>12) //DS18B20 waits for 15-60us after detecting the rising edge of the bus
{
succ=0x00; //If the waiting time is greater than about 60us, report reset failure
break;
}
}
i=0;
while(!R_DS18B20()) //DS18B20 sends out existence pulse, lasting 60-240us
{
_delay_us(5); //5
if(++i>48) //If the waiting time is greater than about 240us, report reset failure
{
succ=0x00;
break;
}
}
_delay_us(20);
return succ;
}
//---------------------------------------------------------------------
void DS18B20_SendU8(unsigned char d8) // write one byte to DS18B20
{
unsigned char i;
DS18B20_PIN_SET_OUT(); //Set to output port
for(i=0; i<8; i++)
{
DS18B20_WR0(); //Pull the bus low to start the "write time slice"
_delay_us(2); // greater than 1 microsecond
if(d8&0x01)DS18B20_WR1();
_delay_us (32); //Delay at least 60 microseconds to make the write effective
_delay_us (30);
DS18B20_WR1(); //Pull the bus high, release the bus, and prepare to start the next "write time slice"
d8>>=1;
_delay_us (1);
}
DS18B20_PIN_SET_IN(); //Host releases the bus
}
//---------------------------------------------------------------------
unsigned char DS18B20_ReadU8(void) // read 1 byte from DS18B20
{
unsigned char i,d8;
for(i=0; i<8; i++)
{
d8>>=1;
DS18B20_PIN_SET_OUT(); //Set as output port
DS18B20_WR0(); //Pull the bus low to start reading the "time slice"
_delay_us(2); // greater than 1 microsecond
DS18B20_WR1(); //Host releases the bus, and the read is valid within (2~15)us
DS18B20_PIN_SET_IN(); //Set the pin as input port, ready to read
_delay_us(2); //Delay 2 us before reading
if(R_DS18B20())d8|=0x80; //Read bus data within about 15 microseconds from the time the bus is pulled low
_delay_us(32); //Reading completed after 60us
_delay_us(30);
DS18B20_WR1(); //Pull the bus high, the host releases the bus, and prepares to start the next "write time slice"
}
DS18B20_PIN_SET_IN(); //Host releases the bus
return(d8);
}
//------------------------------------------------------------------------------------
void Init_DS18B20(void) //Initialize DS18B20
{
unsigned char i;
i=DS18B20_Start(); //Reset
if(!i) //no DS18B20 is found on the single bus, then an alarm is raised
{
Alarm_for_No_DS18B20();
return;
}
DS18B20_SendU8 (SKIP_ROM); //Skip ROM matching
DS18B20_SendU8 (WRITE_RAM); //Set write mode
DS18B20_SendU8 (0x64); //Set the upper temperature limit to 100 degrees Celsius
DS18B20_SendU8 (0x8a); //Set the temperature to -10 degrees Celsius
DS18B20_SendU8 (0x7f); //12bit (default)
}
//--------------------------------------------------------------------------------------
unsigned int Read_DS18B20(void) //Read and calculate the temperature to be output
{
unsigned char i;
unsigned char tl;
unsigned int th;
i=DS18B20_Start(); //Reset
if(!i) //no DS18B20 is found on the single bus, then an alarm is raised
{
Alarm_for_No_DS18B20();
return 0;
}
_delay_ms(1);
DS18B20_SendU8(SKIP_ROM); //Send the command to skip the serial number detection
DS18B20_SendU8(CONVERT_TEM); // Command Ds18b20 to start converting temperature
i=0;
_delay_ms(1);
while(!R_DS18B20()) //When temperature conversion is in progress, the host will receive 0 when reading the bus, and 1 when the conversion is completed
{
_delay_ms(3);
if(++i>250) break; //The maximum conversion time is 750ms
}
DS18B20_Start(); //Initialization
_delay_ms(1);
DS18B20_SendU8(SKIP_ROM); //Send the command to skip the serial number detection
DS18B20_SendU8(READ_RAM); //Send command to read temperature data
tl=DS18B20_ReadU8(); //Read the lower 8 bits of temperature data first
th=DS18B20_ReadU8()<<8; //Read the high 8-bit temperature data again
return (th|tl)*10>>4; //The temperature is magnified 10 times,*0.0625=1/16=>>4
}
Previous article:How does STVD/COSMIC assign variables to specified addresses?
Next article:Summary of STM8 usage problems 2----STVD (COSMIC) defines variables to specify their types and bit definitions
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Block diagram of a generic superheterodyne receiver.
- 【Contactless facial recognition access control system】+ 7-maix_bit facial recognition
- [Synopsys IP Resources] Using ARC VPX DSP IP to achieve high-performance sensor fusion on an embedded budget
- RISC-V MCU Development (VIII): Project Management
- Infrared remote control
- [ART-Pi Review] 6: OTA connection to onenet cloud platform
- Job Opportunity Recommendation - Product Development Engineer (Relay)
- Differences between SMBus and I2C
- DSP Image Data Flow Design
- [AT-START-F425 Review] GPIO control LCD12864 display + USART data transmission and reception