51 single chip 8X8 dot matrix scrolling display temperature--C51 source code

Publisher:EuphoricMelodyLatest update time:2019-05-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//LED8*8 scrolling display

//Column scan, low level is valid

/*--------------------------------------------------------------*/

//Include header file

#include

#include "74HC595.H"

#include //Header file containing _nop_() function definition

 

/*--------------------------------------------------------------*/

//Global variable definition

unsigned char  i,flag,t,w,t;

unsigned int  m,n;

unsigned char  t1,t2,t3,t4;

 

/*--------------------------------------------------------------*/

//Code library

#define num sizeof(buff) //code length

unsigned char code aa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

 

unsigned char code table2[]={

    0x60,0x60,0x00,0x3E,0x7F,0x41,0x7F,0x3E,/*"0",0*/

    0x60,0x60,0x00,0x42,0x7F,0x7F,0x40,0x00,/*"1",1*/

    0x60,0x60,0x00,0x62,0x73,0x59,0x4F,0x46,/*"2",2*/

    0x60,0x60,0x00,0x22,0x6B,0x49,0x7F,0x36,/*"3",3*/

    0x60,0x60,0x00,0x38,0x3E,0x7F,0x7F,0x20,/*"4",4*/

    0x60,0x60,0x00,0x4F,0x4F,0x49,0x79,0x31,/*"5",5*/

    0x60,0x60,0x00,0x3E,0x7F,0x49,0x7B,0x32,/*"6",6*/

    0x60,0x60,0x00,0x03,0x73,0x79,0x0F,0x07,/*"7",7*/

    0x60,0x60,0x00,0x36,0x7F,0x49,0x7F,0x36,/*"8",8*/

    0x60,0x60,0x00,0x26,0x6F,0x49,0x7F,0x3E,/*"9",9*/

};

 

unsigned char code table1[]= {

//Modulus mode negative code column scan reverse

    0x00,0x3E,0x7F,0x41,0x7F,0x3E,0x00,0x00,/*"0",0*/

    0x00,0x42,0x7F,0x7F,0x40,0x00,0x00,0x00,/*"1",1*/

    0x00,0x62,0x73,0x59,0x4F,0x46,0x00,0x00,/*"2",2*/

    0x00,0x22,0x6B,0x49,0x7F,0x36,0x00,0x00,/*"3",3*/

    0x00,0x38,0x3E,0x7F,0x7F,0x20,0x00,0x00,/*"4",4*/

    0x00,0x4F,0x4F,0x49,0x79,0x31,0x00,0x00,/*"5",5*/

    0x00,0x3E,0x7F,0x49,0x7B,0x32,0x00,0x00,/*"6",6*/

    0x00,0x03,0x73,0x79,0x0F,0x07,0x00,0x00,/*"7",7*/

    0x00,0x36,0x7F,0x49,0x7F,0x36,0x00,0x00,/*"8",8*/

    0x00,0x26,0x6F,0x49,0x7F,0x3E,0x00,0x00,/*"9",9*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

};

unsigned char buff[]={

 

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

    0x00,0x03,0x03,0x3E,0x7F,0x41,0x63,0x22,/*"C",0*/

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",10*/

};

/************************************************************************

The following is the operating procedure of DS18B20

************************************************************************/

sbit DQ=P3^3;

unsigned char time; //Set global variables specifically for strict delay

/*****************************************************

Function: Initialize the DS18B20 sensor and read the response signal

Export parameter: flag

***************************************************/

bit Init_DS18B20(void)

{

    bit flag; //Store the flag of whether DS18B20 exists, flag=0 means it exists; flag=1 means it does not exist

    DQ = 1; //Pull the data line high first

    for(time=0;time<2;time++) //Slight delay of about 6 microseconds

        ;

    DQ = 0; //Pull the data line from high to low again, and keep it for 480~960us

    for(time=0;time<200;time++) //Slight delay of about 600 microseconds

        ; //Send a low-level reset pulse lasting 480~960us to DS18B20

    DQ = 1; //Release the data line (pull the data line high)

    for(time=0;time<10;time++)

        ; //Delay about 30us (after releasing the bus, you need to wait 15~60us for DS18B20 to output a pulse)

    flag=DQ; //Let the MCU detect whether the output pulse exists (DQ=0 means existence)

    for(time=0;time<200;time++) //Delay long enough to wait for the pulse output to complete

        ;

    return (flag); //Return the detection success flag

}

/*****************************************************

Function: Read one byte of data from DS18B20

Export parameter: dat

***************************************************/

unsigned char ReadOneChar(void)

{

    unsigned char i=0;

    unsigned char dat; //Store one byte of data read out

    for (i=0;i<8;i++)

    {

        DQ =1; // Pull the data line high first

        _nop_();     //Wait for a machine cycle

        DQ = 0; //When the MCU reads data from DS18B20, pull the data line from high to low to start the read sequence

        that>>=1;

        _nop_(); //Wait for a machine cycle

        DQ = 1; //Pull the data line "artificially" high to prepare for the microcontroller to detect the output level of DS18B20

        for(time=0;time<2;time++)

        ; //Delay about 6us, so that the host can sample within 15us

        if(DQ==1)

            dat|=0x80; //If the data read is 1, store 1 in dat

        else

            dat|=0x00; //If the data read is 0, store 0 in dat

        //Store the level signal DQ detected by the microcontroller into r[i]

        for(time=0;time<8;time++)

        ; //Delay 3us, there must be a recovery period greater than 1us between two read sequences

    }

    return(dat); //Return the decimal data read

}

/*****************************************************

Function: Write a byte of data to DS18B20

Entry parameter: dat

***************************************************/

WriteOneChar(unsigned char dat)

{

    unsigned char i=0;

    for (i=0; i<8; i++)

    {

        DQ =1; // Pull the data line high first

        _nop_();      //Wait for a machine cycle

        DQ=0; // Pull the data line from high to low to start the write sequence

        DQ=dat&0x01; //Use AND operation to get a binary data to be written,

        // and send it to the data line to wait for DS18B20 sampling

        for(time=0;time<10;time++)

        ;//Delay of about 30us, DS18B20 samples from the data line during about 15~60us after being pulled low

        DQ=1; //Release data line

        for(time=0;time<1;time++)

        ;//Delay 3us, at least 1us recovery period is required between two write sequences

        dat>>=1; //Shift each binary bit in dat right by 1 bit

    }

    for(time=0;time<4;time++)

    ; // Delay a bit to give the hardware some time to react

}

 

void ReadyReadTemp(void)

{

    Init_DS18B20(); //Initialize DS18B20

    WriteOneChar(0xCC); // Skip reading the serial number and column number

    WriteOneChar(0x44); // Start temperature conversion

    for(time=0;time<100;time++)

    ; //Temperature conversion takes a while

    Init_DS18B20(); //Initialize DS18B20

    WriteOneChar(0xCC); //Skip the operation of reading the serial number and column number

    WriteOneChar(0xBE); //Read the temperature register, the first two are the low and high bits of the temperature respectively

}

 

/*--------------------------------------------------------------*/

//Display function

void Display(void)

{

    if(flag==2){

        Ser_IN[[aa[i]]]; //Column scan data

        Ser_IN(buff[i + n]); //Look up the table to get the row scan data

        Par_OUT(); //Output display

        i++; 

        if(i == 8) i = 0; //Loop scan

        m++; 

        if(m == 100) {m = 0; n++;} //Scrolling speed control

        if(n == num-7)

        {

            n = 0; // loop display

            flag=1;

        }

    }

}

/*--------------------------------------------------------------*/

//Timer initialization

void T_init(void)

{

    TMOD = 0x11;

    TH0  = 0xfc; //1MS

    TL0  = 0x66;

    EA = 1; // Enable CPU interrupts

    ET0 = 1; //Timer 0 interrupt is enabled

    TR0  = 1;

}

/*--------------------------------------------------------------*/

//Timer interrupt service

void T0_intservice(void) interrupt 1 using 0

{

    TH0 = 0xfc;

    TL0 = 0x66;

    Display();

}

/*--------------------------------------------------------------*/

//Main function

void main (void)

{

    unsigned char TL; //Store the low temperature bit of the register

    unsigned char TH; //Store the high temperature bit of the register

    unsigned char TN; //Store the integer part of the temperature

    unsigned char TD; //Store the decimal part of the temperature

 

    flag=1;

 

    T_init();

    while(1){

        if(flag==1){

            ReadyReadTemp(); //Read temperature preparation

            TL=ReadOneChar(); //Read the low bit of the temperature value first

            TH=ReadOneChar(); //Then read the high bit of the temperature value

            TN=TH*16+TL/16; //Actual temperature value = (TH*256+TL)/16, i.e.: TH*16+TL/16

            //This is the integer part of the temperature, the decimal part is discarded

            TD=(TL%16)*10/16; //Calculate the decimal part of the temperature, multiply the remainder by 10 and divide by 16 to get the integer.

            t1=TN/100; // take the hundredth place

            t2=(TN%100)/10; //Take the tens digit

            t3=TN%10; //get the unit digit

            t4=TD;

            flag=0;

        }

 

        if(flag==0){

            for(w=1;w<6;w++){

                if(w==5){

                    flag=2;

                }

                for(t=0;t<8;t++){

                    switch(w){

                        case 1:

                            buff[t+(8*w)]=table1[(8*t1)+t];

                            break;

                        case 2:

                            buff[t+(8*w)]=table1[(8*t2)+t];

                            break;

                        case 3:

                            buff[t+(8*w)]=table1[(8*t3)+t];

                            break;

                        case 4:

                            buff[t+(8*w)]=table2[(8*t4)+t];

                            break;

                    }

                }

            }

        }

    };

}

 

//Note: 74HC595 driver

//      '''                   '''

//Note: MR master reset connects to the positive pole of the power supply, OE enable terminal, output valid connects to the negative pole of the power supply

/*--------------------------------------------*/

#ifndef '''74HC595_H'''

#define '''74HC595_H'''

/*--------------------------------------------*/

sbit SD    = P1^4; //Serial data input

sbit ST_CK = P1^5; //Storage register clock input

sbit SH_CK = P1^6; //Shift register clock input

/*--------------------------------------------*/

//Definition of digital tube break code and bit code

//unsigned char code seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00,0xff};

[1] [2]
Reference address:51 single chip 8X8 dot matrix scrolling display temperature--C51 source code

Previous article:Digital tube dynamic and static display principle
Next article:51 single-chip remote control decoding is displayed through the computer serial port--C51 source code

Latest Microcontroller Articles
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号