//DS18b20 C language driver, three-digit digital tube display, read the current ambient temperature, accuracy of 0.1 degrees, temperature range 0-99 degrees
//DS18B20 detailed pin function description 1. GND ground signal; 2. DQ data input and output pin. Open drain single bus interface pin. When used under parasitic power supply
//It can also provide power to the device; 3. VDD optional VDD pin. When working under parasitic power supply, this pin must be grounded.
//How to use DS18B20. DS18B20 uses 1-Wire bus protocol
: //That is, to realize bidirectional transmission of data on one data line
//Compilation environment AVR Studio 4.13/AVR GCC
//System clock 7.3728MHz, set the fuse bit to external high-frequency quartz crystal oscillator, startup time 4.1ms // *********************************************************************** //
Include
files
//***********************************************************************
#include
#include
#define load0 PORTD &= ~(1 << PD4) //Serial command loading, rising delay activation
#define load1 PORTD |= (1 << PD4)
#define DQ_IN DDRC &= ~(1 << PC6) //Set input
#define DQ_OUT DDRC |= (1 << PC6) //Set output
#define DQ_CLR PORTC &= ~(1 << PC6) //Set low level
#define DQ_SET PORTC |= (1 << PC6) //Set high level
#define DQ_R PIN C & (1 << PC6) //Read level
#define delay_us(x) _delay_us(x) //AVR GCC delay function x(us)
#define delay_ms(x) _delay_ms(x) //AVR GCC delay function x(ms)
#define uchar unsigned char
#define uint unsigned int
uint value;
uint temp,A1,A2,A3; //Defined variables, display data processing
uchar flag1;
uchar table[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,
0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,
0x0e,0x0f}; //BCD code of digital tube
//****************************************************************************
// Initialization subroutine
//****************************************************************************
void system_init()
{
SFIOR=(1<<2)|SFIOR; //PUD=1; turn off the pull-up resistor
PORTD=0x70;
DDRD=0x70;
}
void ch451_init()
{
din0; //first low then high, select 4-wire input
din1;
}
//****************************************************************************
// Output command subroutine
// Define an unsigned integer variable to store 12 bytes of command word
//********************************************************************
void ch451_write(unsigned int command)
{
unsigned char i;
load0; // Command start
for(i=0;i<12;i++)
{ // Input 12 bits of data, low bit firstif
(command&1)
{
din1;
}
else
din0;
dclk0;
dclk1; // Rising edge valid
command>>=1;
}
load1; // Load data
}
//*************************************************************************
// Display function
//*********************************************************************
void display(uint b_d,uint s_d,uint g_d)
{
ch451_init();
ch451_write(CH451_SYSOFF); //Turn off display, keyboard, and watchdog
ch451_write(CH451_SYSON1); //Turn on display
ch451_write(CH451_BCD); //Set BCD decoding mode
ch451_write(CH451_TWINKLE); //Set twinkle control
ch451_write(CH451_DIG0|table[b_d]); //Display hundreds digit
datach451_write(CH451_DIG1|table[s_d]|0x80); //Display tens digit
datach451_write(CH451_DIG2|table[g_d]); //Display units digit
datach451_write(CH451_DIG3|led3); //Other digital tubes are not displayed and are offch451_write
(CH451_DIG4|led4);
ch451_write(CH451_DIG5|led5);
ch451_write(CH451_DIG6|led6);
ch451_write(CH451_DIG7|led7);
}
//*************************************************************************
// DS18B20 initialization
//************************************************************************
unsigned char ds1820_reset(void) //initialization and reset
{
unsigned char i;
DQ_OUT;
DQ_CLR;
delay_us(500); //delay 500uS(480-960)
DQ_SET;
DQ_IN;
delay_us(80); //delay 80uS
i = DQ_R;
delay_us(500); //delay 500uS(keep>480uS)
if (i)
{
return 0x00;
}
else
{
return 0x01;
}
}
//*****************************************************************************
// DS18B20 read a byte function
//*************************************************************************
unsigned char ds1820_read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i = 8; i != 0; i--)
{
value >>= 1;
DQ_OUT;
DQ_CLR;
delay_us(4); //*Delay 4uS
DQ_SET;
DQ_IN;
delay_us(10); //*Delay 10uS
if (DQ_R)
{
value|=0x80;
}
delay_us(60); //*Delay 60uS
}
return(value);
}
//*****************************************************************************
// Write a byte function to 18B20
//*************************************************************************
/*DS18B20 byte write function*/
void ds1820_write_byte(unsigned char value)
{
unsigned char i;
for (i = 8; i != 0; i--)
{
DQ_OUT;
DQ_CLR;
delay_us(4); //delay 4uS
if (value & 0x01)
{
DQ_SET;
}
delay_us(80); //delay 80uS
DQ_SET; //bit end
value >>= 1;
}
}
//************************************************************************
// Send temperature conversion command
//****************************************************************************
/*Start ds1820 conversion*/
void ds1820_start(void) {
ds1820_reset();
ds1820_write_byte(0xCC); //Do not omit address
ds1820_write_byte(0x44); //Start conversion
}
//*****************************************************************************
// DS8B20 reads temperature information
//*****************************************************************************
unsigned int ds1820_read_temp(void)
{
unsigned int i;
unsigned char buf[9];
ds1820_reset();
ds1820_write_byte(0xCC); //Do not omit address
ds1820_write_byte(0xBE); //Read temperaturefor
(i = 0; i < 9; i++)
{
buf[i] = ds1820_read_byte();
}
i = buf[1];
i <<= 8;
i |= buf[0];
value=i;
value=value*0.625; //The reason for not multiplying by 0.0625 is to convert the data after the decimal point into data that can be displayed
. //For example, the temperature itself is 27.5 degrees. In order to get the BCD code in the subsequent data processing program, we first enlarge it to 275.
//Then determine the position of the decimal point when displaying, and 27.5 degrees can be displayed.
//value=value*10;
return i;
}
//********************************************************************************
// Temperature data processing function
//****************************************************************************
void data_do(uint d)
{
uint A2t;
A1=d/100; //Separate hundreds, tens, and onesA2t
=d%100;
A2=A2t/10;
A3=A2t%10;
}
//****************************************************************************
// Main program
//****************************************************************************
void main(void)
{
DDRC = 0x00;
PORTC = 0xFF;
system_init(); //System initialization
delay_ms(250);
ds1820_reset(); //Reset D18B20
while (1)
{
ds1820_start(); //Start a conversion
delay_ms(300); //Wait for the conversion to end
ds1820_read_temp(); //Read the temperature value
data_do(value); //Process the data and get the value to be displayed
display(A1,A2,A3); //Display the temperature value
delay_ms(1000); //Delay 1S
}
}
Previous article:ATmega16 read and write EEPROM AT25256 (ICC compilation) program
Next article:M16 GCC LED Flash Example Program
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Maxwell's Equations 3D Animation
- Some issues with ob6572 chip
- [RVB2601 creative application development] hello world
- MicroPython further improves the file system
- Do you use cmake a lot in practice? Please share your thoughts on using it, having used it, or thinking of using it.
- EEWORLD University Hall----Industrial Robot Seminar
- TMS320C62x Boot Mode
- GPIO block diagram of C6000 series DSP
- [New version of Zhongke Bluexun AB32VG1 RISC-V development board] - 0: Unboxing post - At this point it surpasses "UNO"
- ADCPro and DXP FAQ