#include //Includes model header file #include //Includes "bit" operation header file #include //Standard input and output header file #include //Includes custom constant header file #include "SMG.C" //Includes digital tube display function /********************************************
#include//Include model header file
#include //Include "bit" operation header file
#include //Standard input and output header files
#include《AVR_PQ1A.h》//Includes custom constant header file
#include "SMG.C" //Includes digital tube display function
/*******************************************
Function name: B20_init
Function: Reset DS18B20
Parameters: None
Return value: None
/************************************************/
void B20_init(void)
{
DDRA|=BIT(DS18B20); //Configure as output
PORTA|=BIT(DS18B20);
Delayus (10);
PORTA&=~BIT(DS18B20); //pull low
Delayus(750); //Wait for 600 microseconds
PORTA|=BIT(DS18B20); //Release the bus
Delayus(60); //Wait for 60 microseconds
DDRA&=~BIT(DS18B20); //Configure as input
while(( PI NA & (BIT(DS18B20)))); //Wait for DS18B20 to pull low
while(!(PINA&(BIT(DS18B20)))); //Wait for DS18B20 to release the bus
}
/*******************************************
Function name: B20_readB
Function: Read one byte of data
Parameters: None
Return value: retd - one byte of data returned
/************************************************/
ucharB20_readB(void)
{
uchari,retd=0;
for (i = 0; i < 8; i ++) // bit count value
{
retd》》=1; //Shift right, ready to receive new data bits
DDRA|=BIT(DS18B20); //Configure as output
PORTA&=~BIT(DS18B20); //Pull low to start reading data bit
PORTA|=BIT(DS18B20); //Release the bus
Delayus(5); //Wait for 5 microseconds
DDRA&=~BIT(DS18B20); //Configure as input, start reading data bits
if (PINA & BIT (DS18B20)) // Is the bit high?
{
retd|=0x80; //Yes, set this position high
}
Delayus(50); //Wait for 50 microseconds
}
returnretd; //Return the read byte
}
/*******************************************
Function name: B20_write B
Function: Write one byte of data
Parameter: wrd--data to be written
Return value: None
/************************************************/
void B20_writeB(ucharwrd)
{
uchari;
for (i = 0; i < 8; i ++) // bit count value
{
DDRA|=BIT(DS18B20); //Configure as output
PORTA&=~BIT(DS18B20); //Pull low to start writing data bit
Delayus(1); //Wait for 1 microsecond
if (wrd & 0x01) // Is this bit data high?
{
PORTA|=BIT(DS18B20); //If it is high, pull the single bus high
}
else
{
PORTA&=~BIT(DS18B20); //If it is low, pull the single bus low
}
Delayus(50); //Wait for 50 microseconds
PORTA|=BIT(DS18B20); //Release the bus
wrd》》=1; //Shift right to prepare for writing new data bits
}
Delayus(50); //Wait for 50 microseconds
}
/*******************************************
Function Name: Read_temp
Function: Read temperature value
Parameters: None
Return value: rettemp--returned temperature value
/************************************************/
uintRead_temp(void)
{
uchartempl,temph;
uinttemp;
B20_init(); // Initialization, each write command starts from initialization
B20_writeB(0xCC); //skip ROM
B20_writeB(0x44); //Start temperature conversion
B20_init(); // Initialization, each write command starts from initialization
B20_writeB(0xcc); //skip ROM
B20_writeB(0xbe); //read register
templ=B20_readB(); //read temperature low byte
temph=B20_readB(); //read temperature high byte
temp=templ+temph*256; //Organize the temperature into a 16-bit variable
returntemp; //Return 16-bit variable
}
/*******************************************
Function name: Num_BCD
Function: Convert a one-byte integer into a three-digit BCD code
Parameter: num - the integer to be converted
Return value: chr - three-digit BCD code array pointer
/************************************************/
uchar* Num_BCD(uint num)
{
uchari,chr[3];
uchar*rept;
rept=&(chr[0]); //Return pointer to BCD code array
for (i=0;i<3;i++)
{
chr[2-i]=num%10; //Remainder of 10 (actually modulus, but for positive numbers, remainder and modulus are equal)
num/=10; // Divide by 10 to prepare for taking out the next digit
}
returnrept; //Return pointer
}
/*******************************************
Function name : main
Function: Complete temperature reading and display
Parameters: None
Return value: None
/************************************************/
voi dma in (void)
{
uintt;
uchar*temppt;
uchari, temp[3];
Board_init(); //Initialize the development board
while(1)
{
t=Read_temp(); //Read temperature value
t*=0.625; //Convert to 10 times the actual temperature
while(t==850)
{
t=Read_temp(); //Read temperature value
t*=0.625;
}
temppt=Num_BCD(t); //Convert 10 times the actual temperature into BCD code
for (i=0;i<3;i++) //Store the converted BCD code in the display array
{
temp[i]=*(temppt+i);
}
for (i = 0; i < 3; i++) // display temperature
{
if (i == 1)
{
temp[1]+=16; //If it is the same as the digits, add a decimal point (the decoding array of the digital tube has changed, the reader can see the SMG.C file in the 10_DS18B20 folder)
}
One_smg_display(temp[i],i+1);
Delayms(5);
}
}
}
Previous article:AVR high voltage parallel programmer manufacturing method
Next article:How to Design a Solar Cell Controller Using an AVR Microcontroller
Recommended ReadingLatest update time:2024-11-16 11:47
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Buck-boost supercapacitor charging solution
- [GD32L233C-START Review] 19. Low-power serial port (deep sleep wake-up, idle interrupt indefinite length data reception)
- Are DDS frequency synthesizer and signal generator the same concept?
- Temperature detection [What is the device sensor you are most comfortable using]?
- ReSpeaker Core V2.0 builds Bluetooth speakers
- Raspberry Pico Review Summary
- How to modify the default value of protel99sePCB toolbar?
- XMC4800 Relax ECAT Kit Review 3 - Testing the EtherCAT Slave
- JZ2440 Development Board
- EEPROM lifespan issue?