#include
#include
#define Disdata P0 //segment code output port
#define discan P2 //scan port
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7; //temperature input port
sbit DIN=P0^0; //LED decimal point control
uint h;
uint temp;
//
//
//**************Temperature decimal part uses table lookup method***********//
uchar code ditab[16]=
{0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//
uchar code dis_7[12]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xff,0x02};
//Common anode LED segment code table "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "off" "-"
uchar code scan_con[4]={0xf7,0xfB,0xfD,0xff}; //Column scan control word
uchar data temp_data[2]={0x00,0x00}; //Read temperature temporarily
uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //Display unit data, a total of 4 data and one operation temporarily
//
//
//
/*********************11us delay function****************************/
//
void delay(uint t)
{
for (;t>0;t--);
}
//
/****************Display scan function****************************/
scan()
{
char k;
for(k=0;k<4;k++) //4-bit LED scan control
{
Disdata=dis_7[display[k]]; //Data displayif
(k==1){DIN=1;} //Decimal point displaydiscan
=scan_con[k]; //Bit selectiondelay
(300);
}
}
//
//
/****************DS18B20 reset function****************************/
ow_reset(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();//Pull from high to low
DQ=0;
delay(50); //550 us
DQ=1;
delay(6); //66 us
presence=DQ; //presence=0 Reset successful, continue to the next step
}
delay(45); //Delay 500 us
presence=~DQ;
}
DQ=1; //Pull high
}
//
//
/****************DS18B20 write command function****************************/
//Write 1 byte to the 1-WIRE bus
void write_byte(uchar val)
{
uchar i;
for(i=8;i>0;i--)
{
DQ=1;_nop_();_nop_(); //Pull from high to low
DQ=0;_nop_();_nop_();_nop_();_nop_(); //5 us
DQ=val&0x01; //The lowest bit is moved out
delay(6); //66 us
val=val/2; //Shift right 1 bit
}
DQ=1;
delay(1);
}
//
/****************DS18B20 read 1 byte function****************************/
//Get 1 byte from the bus
uchar read_byte(void)
{
uchar i;
uchar value=0;
for(i=8;i>0;i--)
{
ow_reset(); delay(200); write_byte(0xcc); //send command write_byte(0x44); //send conversion command ow_reset(); delay(1); write_byte
(
0xcc
) ;
// send command write_byte( 0xbe) ; temp_data [ 0]= read_byte ( ) ; // read the byte of temperature value temp_data [1 ] = read_byte ( ) ; //Read the high byte of temperature value temp=temp_data[1];
temp<<=8;
temp=temp|temp_data[0]; // Two bytes are combined into an integer variable.
return temp; //Return temperature value
}
//
/****************Temperature data processing function****************************/
//The lower half byte of the binary high byte and the higher half byte of the low byte form a byte.
After the binary of this //byte is converted to decimal, it is the hundreds, tens, and units of the temperature value, and
the lower half byte of the remaining //low byte is converted to decimal to be the decimal part of the temperature value.
/************************************************************/
work_temp(uint tem)
{
uchar n=0;
if(tem>6348) // Temperature value positive or negative judgment
{tem=65536-tem;n=1;} // Negative temperature two's complement, flag position 1
display[4]=tem&0x0f; // Get the value of the decimal part
display[0]=ditab[display[4]]; // Store the decimal part display
[4]=tem>>4; // Get the middle eight digits, that is, the value of the integer part
display[3]=display[4]/100; // Get the hundreds digit data and store it
display[1]=display[4]%100; // Get the last two digits and store it
display[2]=display[1]/10; // Get the tens digit data and store
it display[1]=display[1]%10;
/******************Sign bit display judgment*****************************/
if(!display[3])
{
display[3]=0xF5; //No display when the highest bit is 0if
(!display[2])
{
display[2]=0xF5; //No display when the second highest bit is 0
}
}
if(n){display[3]=0xF4;} //The highest bit displays "-" when the temperature is negative
}
//
//
/********************Main function****************************/
main()
{
Disdata=0x00; //Initialize portdiscan
=0x00;
for(h=0;h<4;h++) //Display "8888" when powered on
{display[h]=8;}
ow_reset(); //Convert once before powering
onwrite_byte(0xcc); //Skip ROM
write_byte(0x44); //Send conversion commandfor
(h=0;h<100;h++) //Display "8888" when powered on
{scan();}
while(1)
{
work_temp(read_temp()); //Process temperature data
scan(); //Display temperature value
}
}
Previous article:Example of reading temperature sensor DS18B20
Next article:Example of STC15F2K60S2 reading DHT11 temperature and humidity serial port display
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Vicor high-performance power modules enable the development of low-altitude avionics and EVTOL
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- "Cross-chip" quantum entanglement helps build more powerful quantum computing capabilities
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Europe's three largest chip giants re-examine their supply chains
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- MSP430F149 reads rc522 RFID card number reference routine
- 11 TCP Protocol (State Machine)
- DC-DC Conversion Technology Brochure
- Solution to the failure of IAR programming MSP430
- MicroPython sets up a separate factory reset code file
- Use ustruct to reorganize byte values, Pandora development board gyroscope
- Display community logo on three-color E-Ink Gizmo
- 1.5V Mini Wireless FM Microphone
- 【GD32450I-EVAL】UART
- Simple VGA driver for MicroPython