C language program using DS18B20 temperature sensor in single chip microcomputer (reference 3)

Publisher:快乐旅途Latest update time:2016-09-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include"reg51.h"

sbit DQ =P3^7; //define communication port

//Delay function
void delay(unsigned int i)
{
while(i--);
}

//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ down
delay(80); //Precise delay is greater than 480us
DQ = 1; //Pull the bus high
delay(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay(20);
}

//Read a
byteReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //Give pulse signaldat
>>=1;
DQ = 1; //Give pulse signalif
(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}

//Write one byte
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}

//Read temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0x44); //Start temperature conversionInit_DS18B20
();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturesa
=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //Merge the high and low bits of the temperaturet
= tt*10+0.5; //Round the result
return(t);
}

//Digital codes from 0 to 9 respectively. Note that this code is only suitable for the QQ microcontroller experiment board. For codes on other circuit boards, please refer to the above algorithm to calculate them yourself.
unsigned char code Num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

//QQ MCU private function, may not work well on other circuit boards
//Display any characters on 8 digital tubes, the character content is determined by 8 parameters LED1--LED8 If the displayed character is empty, this digital tube will not be scanned to shorten the scanning time
ShowAny(unsigned char LED1,unsigned char LED2,unsigned char LED3,unsigned char LED4,unsigned char LED5,unsigned char LED6,unsigned char LED7,unsigned char LED8)
{
if(LED1) { P2=0xEF; P0=LED1; delay(100); }
if(LED2) { P2=0xDF; P0=LED2; delay(100); }
if(LED3) { P2=0xBF; P0=LED3; delay(100); }
if(LED4) { P2=0x7F; P0=LED4; delay(100); }
if(LED5) { P2=0xFE; P0=LED5; delay(100); }
if(LED6) { P2=0xFD; P0=LED6; delay(500); }
if(LED7) { P2=0xFB; P0=LED7; delay(500); } if
(LED8) { P2=0xF7;

main()
{
unsigned int i=0;
while(1)
{
i=ReadTemperature(); //Read the current temperature
ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //Show the current temperature
}
}

Keywords:MCU Reference address:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 3)

Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 4)
Next article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 2)

Recommended ReadingLatest update time:2024-11-16 16:17

Dynamic display of digital tube based on 51 single chip microcomputer
#include reg52.h #include intrins.h #define uchar unsigned char #define uint unsigned int sbit P2_6=P2^6; //digital tube bit select 74HC573 latch end sbit P2_7=P2^7; //Digital segment selects 74HC573 latch terminal oid delay(uchar xms); //declare the prototype of the delay() function, delay function void display(cha
[Microcontroller]
AVR MCU reset detection how to identify reset conditions
When the AVR is reset, all I/O registers are set to their initial values ​​and the program starts executing from the reset vector. The instruction at the reset vector must be an absolute jump JMP instruction so that the program jumps to the reset processing routine. There are five reset signal sources for AVR: Power
[Microcontroller]
AVR MCU reset detection how to identify reset conditions
Serial Communication between PC and MCU under Windows CE.NET 4.2
In industrial control, it is often necessary to transmit the data collected by the microcontroller to the PC for processing and display, and send control commands to the microcontroller based on the processing results. Although serial communication has a relatively slow transmission speed, it has a low transmission
[Microcontroller]
Single chip school bell system
Chapter 1 Design Brief 1.1 Purpose and Objectives In school life, every day and the next class are inseparable from the use of bells. The bell can provide reminders for students and teachers in school, and it can also be used as a reminder for students to arrange their schedules, so that we have a sense of time and fo
[Microcontroller]
Single chip school bell system
Perpetual calendar program based on 51 single chip microcomputer and digital tube
Available functions: Start the program and the digital tube displays the hours, minutes, seconds and running time in the format of ××-××-×× After pressing C1 button, the year, month and day will be displayed in the format of ××-××-××. Press the C3 time adjustment mode button. The hours, minutes, seconds/year, month, a
[Microcontroller]
Stepper motor subdivision control method based on single chip microcomputer and TA8435
1 Stepper Motor       The stepper motor is a pure digital control motor, which converts the electrical pulse signal into angular displacement, that is, when a pulse is given, the stepper motor rotates an angle, so it is very suitable for single-chip microcomputer control. In the case of non-overload, the motor sp
[Microcontroller]
Stepper motor subdivision control method based on single chip microcomputer and TA8435
Problems and Techniques in 51 Single-Chip Microcomputer Operating System Development
Introduction The 51 series single-chip microcomputer is a high-performance 8-bit single-chip microcomputer launched by Intel Corporation in the United States in 1980. It is widely used in China. At present, software engineers need to start from the bottom in software design, and a lot of repetitive work needs
[Microcontroller]
Problems and Techniques in 51 Single-Chip Microcomputer Operating System Development
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号