/**********************************************************************
* File name: CEPARK 51 V2.0 development board
* Program version: V1.0
* Technical support: http://bbs.cepark.com
*CEPARK
http://bbs.cepark.com/viewthread.php?tid=34&fromuid=6136
* Cepark summer 51 free online training program---activity registration address:
http://bbs.cepark.com/viewthread.php?tid=14228&fromuid=6136
**********************************************************************/
#include "STC89C51RC_RD_PLUS.H"
#define uchar unsigned char
#define uint unsigned int
uchar code C51BOX2[3] _at_ 0x43; //The emulator uses three bytes of space
uchar code DispTab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e}; //Segment code 0-9,af
uchar DispBuf[4];
uchar TempH,TempL;
uint Temp;
bit Presence;
sfr LedPort=0x80; //P0 port segment code
sbit Led0 = P1^0; //LED control pin
sbit Led1 = P1^1;
sbit Led2 = P1^2;
sbit Led3 = P1^3;
sbit DataPort=P1^6; //18B20 data port
void uDelay(uchar DelayTime) //Delay subroutine, each increase of 1 in the entry parameter adds about 2us
{ while(--DelayTime);
}
void Hex2Bcd() //HEX to BCD program
{
if(Presence==1)
{ //If no existence is detected, display "no"
DispBuf[0]=0xab;
DispBuf[1]=0xa3;
DispBuf[2]=0xff;
DispBuf[3]=0xff;
}
else
{ //After decomposition, look up the table to get the segment code and put it into the display buffer
Temp=TempH*256+TempL; //Merge the data into a double-byte numberif
((Temp>>12)==0xf) //Shift right 12 bits to determine whether it is a negative temperature
{Temp=0x10000-Temp; //Calculate the negative temperature valueDispBuf
[0]=0xbf;} //And display the "-" sign on the leftmost digital tubeelse
DispBuf[0]=0xff; //Otherwise, do not display
TempH=Temp>>4; //After removing the 4-digit decimal point, get the integer part and put it in TempH
Temp=(Temp&0x0f)*625; //AND out the 4-digit decimal point, and get the decimal point value by multiplication
DispBuf[3]=DispTab[Temp/1000]; //Only the highest digit of the decimal point is taken, and the last three digits are not displayed
DispBuf[2]=DispTab[TempH%10]&0x7f; //Process the ones digit and light up the decimal point
TempH=TempH/10; //Get the hundreds and tens digits
if(!TempH) DispBuf[1]=0xff;//If the hundreds and tens digits are both 0, the tens digit will not be displayed.
else DispBuf[1]=DispTab[TempH%10];
if(DispBuf[0]==0xff)//Positive temperature condition
{
if(TempH/10) DispBuf[0]=DispTab[TempH/10];//Hundreds place has a value displayedelse
DispBuf[0]=0xff;//No display if it is 0
}
}
}
void Display(uchar j) //Display subroutine
{
for(;j>0;j--)
{
LedPort=DispBuf[0];
Led0=0;
uDelay(20);
Led0=1;
LedPort=DispBuf[1];
Led1=0;
uDelay(20);
Led1=1;
LedPort=DispBuf[2];
Led2=0;
uDelay(20);
Led2=1;
LedPort=DispBuf[3];
Led3=0;
uDelay(20);
Led3=1;
}
}
void Inti()
{
DataPort=0; //Pull low
uDelay(180); //Pull high after delay
DataPort=1;
uDelay(24); //Read status after delay, if exists, 18B20 will pull port low
Presence=DataPort; //Save status for later display judgment
uDelay(230); //Delay completes one cycle time
}
void WriteByte(uchar val) //Byte write
{
uchar i;
for (i=8; i>0; i--)
{
DataPort = 0;
uDelay(4);
DataPort = val&0x01;
uDelay(20);
DataPort = 1;
val=val/2;
}
uDelay(50);
}
uchar ReadByte() //Byte read
{
uchar i;
uchar value=0;
for (i=0;i<8;i++)//Read 8 bytes
{
DataPort=0; //Pull low and then high to generate a read gapDataPort
=1;
uDelay(4); //Read after delayif
(DataPort) value|=0x01< uDelay(20); //Delay completes one cycle time
}
return (value);
}
void SetStep(uchar Dat) //Entry parameters, 1F-0.5 degree step, 3F-0.25 degree, 5F-0.125 degree, 7F-0.0625 degree
{
Inti();
WriteByte(0xcc);
WriteByte(0x4e);
WriteByte(0x15); //Write address 2—TH
WriteByte(0x00); //Write address 3—TL
WriteByte(Dat); //Address 4, reserved area, used for decimal resolution, 1F, 3F, 5F, 7F values, the larger the value, the higher the resolution
Inti();
WriteByte(0xcc);
WriteByte(0xbe);
ReadByte();
Inti();
WriteByte(0xcc);
WriteByte(0x48); //Copy instruction, wait 6ms after sending it, wait for 18B20 to write data into EEPROM, reset after writing is completed.
uDelay(100);
Inti();
DataPort=1;
}
void GetTemp() //Temperature acquisition subroutine, return value is in TempH, TempL
{
Inti(); Display(8); //Add display each time an instruction is executed to eliminate flicker
WriteByte(0xcc); Display(8);
WriteByte(0x44); Display(8);
Inti(); Display(8);
WriteByte(0xcc); Display(8);
WriteByte(0xbe); Display(8);
TempL=ReadByte(); Display(8);
TempH=ReadByte(); Display(8);
}
void main(void)
{
uDelay(100);
Presence=1;
SetStep(0x7f);
GetTemp();Display(100); //After sending the first temperature acquisition instruction, it takes a while to wait for the internal conversion. (Solve the problem of 85 degrees display when booting.)
while(1){GetTemp();Hex2Bcd();Display(50);}
}
Keywords:CEPARK DS18B20
Reference address:CEPARK 51 MCU DS18B20 test program
* File name: CEPARK 51 V2.0 development board
* Program version: V1.0
* Technical support: http://bbs.cepark.com
*CEPARK
http://bbs.cepark.com/viewthread.php?tid=34&fromuid=6136
* Cepark summer 51 free online training program---activity registration address:
http://bbs.cepark.com/viewthread.php?tid=14228&fromuid=6136
**********************************************************************/
#include "STC89C51RC_RD_PLUS.H"
#define uchar unsigned char
#define uint unsigned int
uchar code C51BOX2[3] _at_ 0x43; //The emulator uses three bytes of space
uchar code DispTab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e}; //Segment code 0-9,af
uchar DispBuf[4];
uchar TempH,TempL;
uint Temp;
bit Presence;
sfr LedPort=0x80; //P0 port segment code
sbit Led0 = P1^0; //LED control pin
sbit Led1 = P1^1;
sbit Led2 = P1^2;
sbit Led3 = P1^3;
sbit DataPort=P1^6; //18B20 data port
void uDelay(uchar DelayTime) //Delay subroutine, each increase of 1 in the entry parameter adds about 2us
{ while(--DelayTime);
}
void Hex2Bcd() //HEX to BCD program
{
if(Presence==1)
{ //If no existence is detected, display "no"
DispBuf[0]=0xab;
DispBuf[1]=0xa3;
DispBuf[2]=0xff;
DispBuf[3]=0xff;
}
else
{ //After decomposition, look up the table to get the segment code and put it into the display buffer
Temp=TempH*256+TempL; //Merge the data into a double-byte numberif
((Temp>>12)==0xf) //Shift right 12 bits to determine whether it is a negative temperature
{Temp=0x10000-Temp; //Calculate the negative temperature valueDispBuf
[0]=0xbf;} //And display the "-" sign on the leftmost digital tubeelse
DispBuf[0]=0xff; //Otherwise, do not display
TempH=Temp>>4; //After removing the 4-digit decimal point, get the integer part and put it in TempH
Temp=(Temp&0x0f)*625; //AND out the 4-digit decimal point, and get the decimal point value by multiplication
DispBuf[3]=DispTab[Temp/1000]; //Only the highest digit of the decimal point is taken, and the last three digits are not displayed
DispBuf[2]=DispTab[TempH%10]&0x7f; //Process the ones digit and light up the decimal point
TempH=TempH/10; //Get the hundreds and tens digits
if(!TempH) DispBuf[1]=0xff;//If the hundreds and tens digits are both 0, the tens digit will not be displayed.
else DispBuf[1]=DispTab[TempH%10];
if(DispBuf[0]==0xff)//Positive temperature condition
{
if(TempH/10) DispBuf[0]=DispTab[TempH/10];//Hundreds place has a value displayedelse
DispBuf[0]=0xff;//No display if it is 0
}
}
}
void Display(uchar j) //Display subroutine
{
for(;j>0;j--)
{
LedPort=DispBuf[0];
Led0=0;
uDelay(20);
Led0=1;
LedPort=DispBuf[1];
Led1=0;
uDelay(20);
Led1=1;
LedPort=DispBuf[2];
Led2=0;
uDelay(20);
Led2=1;
LedPort=DispBuf[3];
Led3=0;
uDelay(20);
Led3=1;
}
}
void Inti()
{
DataPort=0; //Pull low
uDelay(180); //Pull high after delay
DataPort=1;
uDelay(24); //Read status after delay, if exists, 18B20 will pull port low
Presence=DataPort; //Save status for later display judgment
uDelay(230); //Delay completes one cycle time
}
void WriteByte(uchar val) //Byte write
{
uchar i;
for (i=8; i>0; i--)
{
DataPort = 0;
uDelay(4);
DataPort = val&0x01;
uDelay(20);
DataPort = 1;
val=val/2;
}
uDelay(50);
}
uchar ReadByte() //Byte read
{
uchar i;
uchar value=0;
for (i=0;i<8;i++)//Read 8 bytes
{
DataPort=0; //Pull low and then high to generate a read gapDataPort
=1;
uDelay(4); //Read after delayif
(DataPort) value|=0x01< uDelay(20); //Delay completes one cycle time
}
return (value);
}
void SetStep(uchar Dat) //Entry parameters, 1F-0.5 degree step, 3F-0.25 degree, 5F-0.125 degree, 7F-0.0625 degree
{
Inti();
WriteByte(0xcc);
WriteByte(0x4e);
WriteByte(0x15); //Write address 2—TH
WriteByte(0x00); //Write address 3—TL
WriteByte(Dat); //Address 4, reserved area, used for decimal resolution, 1F, 3F, 5F, 7F values, the larger the value, the higher the resolution
Inti();
WriteByte(0xcc);
WriteByte(0xbe);
ReadByte();
Inti();
WriteByte(0xcc);
WriteByte(0x48); //Copy instruction, wait 6ms after sending it, wait for 18B20 to write data into EEPROM, reset after writing is completed.
uDelay(100);
Inti();
DataPort=1;
}
void GetTemp() //Temperature acquisition subroutine, return value is in TempH, TempL
{
Inti(); Display(8); //Add display each time an instruction is executed to eliminate flicker
WriteByte(0xcc); Display(8);
WriteByte(0x44); Display(8);
Inti(); Display(8);
WriteByte(0xcc); Display(8);
WriteByte(0xbe); Display(8);
TempL=ReadByte(); Display(8);
TempH=ReadByte(); Display(8);
}
void main(void)
{
uDelay(100);
Presence=1;
SetStep(0x7f);
GetTemp();Display(100); //After sending the first temperature acquisition instruction, it takes a while to wait for the internal conversion. (Solve the problem of 85 degrees display when booting.)
while(1){GetTemp();Hex2Bcd();Display(50);}
}
Previous article:51 MCU button double click
Next article:MP28GA stepper motor C language test program
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- [RVB2601 creative application development] + AHT10 temperature and humidity sensor
- A nice frame diagram
- Measured CC2530 OSAL sleep wake-up time, current, and power consumption
- [Sipeed LicheeRV 86 Panel Review] - 5 waft-ui component tests (2)
- Does anyone have a detailed tutorial on Cadence 16.6 Allegro PCB package drawing?
- Problem with the start address of SDRAM W9825G6KH
- How to analyze this circuit
- Proteus simulation of motor control PID algorithm based on 51 single chip microcomputer
- AC Filter
- Bluetooth wireless communication technology