Programming Example of 51 Single Chip Microcomputer Digital Clock Thermometer

Publisher:心灵飞翔Latest update time:2015-08-06 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "reg51.h"

char disp[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
char disp_dot[11]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xc0};

sbit DQ = P1^3; //define communication port

//晶振12MHz
void delay_18B20(unsigned int i)
{
while(i--);
}

//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay_18B20(8); //Slight delay
DQ = 0; //MCU pulls DQ low
delay_18B20(80); //Precise delay greater than 480us
DQ = 1; //Pull the bus high
delay_18B20(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay_18B20(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_18B20(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_18B20(5);
DQ = 1;
dat>>=1;
}
}


//Read temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;

Init_DS18B20();
WriteOneChar(0xCC); // Skip reading serial number and column
numberWriteOneChar(0x44); // Start temperature conversion
delay_18B20(100);
Init_DS18B20();
WriteOneChar(0xCC); // Skip reading serial number and column numberWriteOneChar
(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturea
=ReadOneChar();
b=ReadOneChar();

//Sensor return value divided by 16 to get the actual temperature value
//In order to get 2 decimal places, multiply by 100 first, then divide by 16, considering the length of integer data,
//After processing, multiply by 25 first, then divide by 4, and divide by 4 is implemented by right shift

t = (b*256+a)*25;
return( t >> 2 );
}

main()
{
unsigned int tmp;
unsigned char counter;
while(1)
{
//The temperature measurement frequency does not need to be too high. Too high a frequency will affect the digital display.
//So use a counter to control it
if(counter-- == 0)
{
tmp = ReadTemperature();
counter = 20;
}

P2 = 0xff;
P0 = disp[tmp%10];
P2 = 0xfb;
delay_18B20(1000);
P2 = 0xff;
P0 = disp[tmp/10%10];
P2 = 0xf7;
delay_18B20(1000);
P2 = 0xff;
P0 = disp_dot[tmp/100%10];
P2 = 0xef;
delay_18B20(1000);
P2 = 0xff;
P0 = disp[tmp/1000%10];
P2 = 0xdf;
delay_18B20(1000);
}
}

Reference address:Programming Example of 51 Single Chip Microcomputer Digital Clock Thermometer

Previous article:Application of AT89C2051 in the external display screen of money counting machine
Next article:Typical case development of single chip microcomputer (V)

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

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号