CEPARK 51 MCU DS18B20 test program

Publisher:PositiveVibesLatest update time:2015-11-13 Source: eefocusKeywords:CEPARK  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/**********************************************************************
* 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

Previous article:51 MCU button double click
Next article:MP28GA stepper motor C language test program

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号