Rewrote the latest DS18B20 driver, slightly and share with you, the program to write is not good, forgive me,
In the main program, the interrupt call time-sharing method, interval about 1 s call setup conversion and read two functions, the following program is on the AVR mega8 development, the compiler is ICC AVR 7.2
Need special attention, DS18B20 is strict with time slot, according to the manual, in order to get 12 precision temperature data, conversion time to 750 ms, all start after conversion, interval of 1 s to read again, if only a single task, you can use the method of cyclic delay, many tasks, then suggest using interrupt time polling method calls, program tested available,
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: DS18B20_Reset
Function: Reset DS18B20. Verify that the DS18B20 is online.
The Description
Parameter: None
The Return Value: online=0; Offline=1;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void DS18B20A_Reset (void)
{
DS18B20_PinA_OUT ();//Set DS18B20 Pin output
DS18B20_DQA_H ();
Asm (" nop ");
DS18B20_DQA_L ();//Set DS18B20 Pin Low
Delay_us (750);
DS18B20_DQA_H ();//Set DS18B20 Pin High
Delay_us (30);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: u8 DS18B20_Check (void)
Function: Verify that the DS18B20 is online.
The Description online=1
Offline=0
Parameter: None
The Return Value: online Return 0; Offline return 1;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char DS18B20A_Check (void)
{
Unsigned char retry=0;//init DS18B20 statu
DS18B20_PinA_IN ();//Set DS18B20 Pin Input
DS18B20_DQA_H ();
While (DS18B20_DQA_Read () & amp; & (retry & lt; 200))
{
Retry++;
Delay_us (1);
}
If (retry & gt;
=200){
return 1;
}
The else
{
return 0;
}
while(! DS18B20_DQA_Read () & amp; & (retry & lt; 240))
{
Retry++;
Delay_us (1);
}
If (retry & gt;
=240){
return 1;
}
The else
{
return 0;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: u8 DS18B20_Init (void)
Function: Init DS18B20. Master Tx Reset pulse.
The Description Verify that the DS18B20 is online.
The online=0
Offline=1
Parameter: None
The Return Value: online Return 0; Offline return 1;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char DS18B20A_Init (void)
{
Unsigned char error=0;
DS18B20A_Reset ();
Error=DS18B20A_Check ();
Delay_us (500);
Return the error;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: u8 DS18B20_Read_Bit (void)
Function: read DS18B20 1 bit data
The Description
Parameter: None
The Return Value: Return data 1/0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char DS18B20A_Read_Bit (void)
{
Unsigned char test_bit=0;
Unsigned char bit_data=https://bbs.csdn.net/topics/0;//init return data
DS18B20_PinA_OUT ();//Set DS18B20 Pin Output
DS18B20_DQA_L ();//Set DS18B20 Pin Low
Delay_us (1);
Asm (" nop ");
DS18B20_PinA_IN ();
DS18B20_DQA_H ();
Delay_us (1);
Test_bit=TESTBIT (PINC, PC1);
If (test_bit)
{
Bit_data=https://bbs.csdn.net/topics/1;
}
The else
{
Bit_data=https://bbs.csdn.net/topics/0;
}
Delay_us (50);
Return bit_data;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: u8 DS18B20_Read_Byte (void)
Function: read DS18B20 1 byte data
The Description
Parameter: None
The Return Value: Return data 1/0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char DS18B20A_Read_Byte (void)
{
Unsigned char I=0;
Unsigned char j=0;
Unsigned char byte_data=https://bbs.csdn.net/topics/0;
for(i=1; i<=8; I++)
{
Byte_data & gt;>=1;
J=DS18B20A_Read_Bit ();
If (j)
{
Byte_data |=0 x80;
}
}
Return byte_data;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: DS18B20_Write_Byte (unsigned char data)
Function: the Write 1 byte data to DS18B20
The Description
The Parameter: 1 byte data
The Return Value: none
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void DS18B20A_Write_Byte (unsigned char data)
{
Unsigned char I=0;
for(i=0; i<8;
i++)//Bit counter{
DS18B20_PinA_OUT ();//DS18B20 Pin set output
DS18B20_DQA_L ();//DS18B20 Pin set low, start the write data
Delay_us (1);//waite 1 us
If (data & amp; 0 x01)//test data bit=1?
{
DS18B20_DQA_H ();//data bit=1, DS18B20 Pin=High
}
The else
{
DS18B20_DQA_L ();//data bit=0, DS18B20 Pin=Low,
}
Delay_us (50);//delay 50 us
DS18B20_DQA_H ();
The data & gt;>=1;//moves to the right, for writing new data to prepare
}
Delay_us (50);//wait 50 microseconds
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Function Name: DS18B20_Start (void)
Function: DS18B20 start temperature conversion
The Description
Parameter: none
The Return Value: none
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull