51 MCU OLED clock display

Publisher:清晨微风Latest update time:2020-02-12 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The source program of the oled clock microcontroller is as follows:


#include "REG52.h"

#include "oled.h"


#define uchar unsigned char

#define uint  unsigned int

#include

sbit DQ = P3^6; //data port define interface

/***********************DS1302 port definition***********************************************/

sbit clock_rst=P3^5; //1302 reset pin definition

sbit clock_dat=P1^3; //1302I/O pin definition

sbit clock_clk=P1^2; //1302 clock pin definition  

/***************Register definition (used to record the input and output of 1302IO)****************************/

sbit a0=ACC^0;

sbit a1=ACC^1;

sbit a2=ACC^2;

sbit a3=ACC^3;

sbit a4=ACC^4;

sbit a5=ACC^5;

sbit a6=ACC^6;

sbit a7=ACC^7;


/**********************1302 Global variable definition******************************************/

uchar sec,min,hour,day,month,year1,year2=20; //seconds, minutes, hours, days, months, weeks, years

/****************************Buzzer port definition******************************************/

sbit beep=P3^7;

/**************************Button definition*****************************************************/

sbit key1=P1^0;

sbit key2=P1^1;

sbit key3=P3^4;

uchar status=0; //key function selection definition (year 0, month 1, day 2, hour 3, minute 4)




uint tmp; //variable of temperature

uint Temp_Buffer = 0;

uint t1;

uint  t2;

uint t3;

float tt;

void delay2(unsigned char i) //delay function

{

        while(--i); 

}

/**********************Delay function*****************************************************/

void delay(uint t)  

{

uint b;

for(;t;t--)

  for(b=0;b<121;b++); 

}



void Init_Ds18b20(void)     //DS18B20初始化send reset and initialization command

{

        DQ = 1; //DQ reset, it is also possible not to do so.

        delay2(1); //slight delay

        DQ = 0; //MCU pulls down the bus

        delay2(250); //Precise delay, maintain at least 480us

        DQ = 1; //Release the bus, that is, pull the bus high

        delay2(100); //The delay here is sufficient to ensure that the DS18B20 can send a presence pulse.

}

uchar Read_One_Byte() //Read a byte of data read a byte date

                            //When reading data, the data is shifted out of the bus with the least significant bit of the byte first

{

        fly i = 0;

        float dat = 0;

        for(i=8;i>0;i--)

        {

           DQ = 0; //Pull the bus low and release the bus after 1us

                                   //The MCU must read data within 15us after this falling edge to make it effective.

           _nop_(); //At least 1us is maintained, indicating that the read sequence starts

           dat >>= 1; //Let the bit data read from the bus move from high to low in sequence.

           DQ = 1; //Release the bus, then DS18B20 will control the bus and transfer data to the bus

           delay2(1); //Delay 7us, refer to the recommended read timing diagram here, try to put the controller sampling time in the last part of 15us after the read timing

           if(DQ) //controller performs sampling

           {

            dat |= 0x80; //bus is 1, DQ is 1, then set the highest bit of dat to 1; if it is 0, do not process, keep it 0

           }        

           delay2(10); //This delay cannot be less to ensure the read sequence length is 60us.

        }

        return (that);

}

void Write_One_Byte(uchar dat)

{

        fly i = 0;

        for(i=8;i>0;i--)

        {

           DQ = 0; //Pull the bus low

           _nop_(); //At least 1us is maintained, indicating that the write timing (including write 0 timing or write 1 timing) starts

           DQ = dat&0x01; //Transmit from the lowest bit of the byte

                                         //The lowest bit of the instruction dat is assigned to the bus, which must be within 15us after the bus is pulled low.

                                         //Because DS18B20 will sample the bus after 15us.

           delay2(10); //The write sequence must last at least 60us

           DQ = 1; //After writing, the bus must be released.

           that >>= 1;

           delay2(1);

        }

}

uint Get_Tmp() //get the temperature

{

        

    fly a=0,b=0;

        

        Init_Ds18b20(); //Initialization

        Write_One_Byte(0xcc); //Ignore ROM instructions

        Write_One_Byte(0x44); //Temperature conversion instruction

        Init_Ds18b20(); //Initialization

        Write_One_Byte(0xcc); //Ignore ROM instructions

        Write_One_Byte(0xbe); //Read register instruction

        a = Read_One_Byte(); //The first byte read is the temperature LSB

        b = Read_One_Byte(); //The first byte read is the temperature MSB

        tmp = b; //First assign the high eight bits of valid data to temp

        tmp <<= 8; //Move the above 8-bit data from the lower 8 bits of temp to the higher 8 bits

        tmp = tmp|a; //Two bytes are combined into an integer variable

        tt = tmp*0.0625; //Get the true decimal temperature value, DS18B20 can be accurate to 0.0625 degrees

    tmp =(uint)( tt*10+0.5); //Magnify ten times, the purpose is to convert the first decimal point into a displayable number                               

        return (tmp);

}

void Display_Temp()

{

        

        Temp_Buffer = Get_Tmp() ; //Read the value of DS18B20

        t1=(Temp_Buffer%1000/100); //Display temperature in tens place

        t2=(Temp_Buffer%100/10); //Display the temperature value

                                                   

        t3=(Temp_Buffer%10); //Display temperature tenths value

                                                   

                

}


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

/*****************************DS1302 sub-function**********************************************/

/****************************1302 Write driver function********************************************/

void write_clock(uchar dat)

{

ACC=that;

clock_dat=a0; clock_clk=1; clock_clk=0; //Data changes on the rising edge

clock_dat=a1; clock_clk=1; clock_clk=0; //Write 1302 on the falling edge

clock_dat=a2; clock_clk=1; clock_clk=0;

clock_dat=a3; clock_clk=1; clock_clk=0;

clock_dat=a4; clock_clk=1; clock_clk=0;

clock_dat=a5; clock_clk=1; clock_clk=0;

clock_dat=a6; clock_clk=1; clock_clk=0;

clock_dat=a7; clock_clk=1; clock_clk=0;

}

/****************************1302 read driver function********************************************/

uchar read_clock()

{

clock_dat=1; //output is assigned 1

a0=clock_dat;clock_clk=1; clock_clk=0; //Bit 0 is 1, read 1302

a1=clock_dat;clock_clk=1; clock_clk=0; //Falling edge readout is valid

a2=clock_dat;clock_clk=1; clock_clk=0; 

a3=clock_dat;clock_clk=1; clock_clk=0;

a4=clock_dat;clock_clk=1; clock_clk=0; 

a5=clock_dat;clock_clk=1; clock_clk=0; 

a6=clock_dat;clock_clk=1; clock_clk=0; 

a7=clock_dat;clock_clk=1; clock_clk=0;

return(ACC);

}

/***************************Write data into 1302********************************************/

void write_dat_clock(uchar add,uchar dat)

{

clock_clk=0; 

clock_rst=0; //When the clock pulse is low

clock_rst=1; //The reset terminal can be set high

write_clock(add); //Write address (command byte)

write_clock(dat); //Write data

clock_rst=0; //Terminate data transmission

clock_clk=1; 

}

/***************************Read data from 1302**************************************/

uchar read_dat_clock(uchar add)

{

uchar dat=0; //define data storage variable

clock_clk=0;

clock_rst=0;

clock_rst=1;

write_clock(add); //Write the address of the data to be read

dat=read_clock(); //Read data

clock_rst=0; //Terminate data transmission

clock_clk=1;

return dat; //return data

}

/**************************Set the initial time function*****************************************/

void set_time()

{

read_dat_clock(0x81); //Read seconds

if(sec==0x80) //sec is the value read in seconds. If the highest bit is 1, initialize

write_dat_clock(0x8e,0x00); //Allow write operation

write_dat_clock(0x8c,0x13);  //年

write_dat_clock(0x8a,0x03); //week

write_dat_clock(0x88,0x10);  //月

write_dat_clock(0x86,0x14);  //日

write_dat_clock(0x84,0x21); //hours

write_dat_clock(0x82,0x56);  //分

write_dat_clock(0x80,0x00); //seconds, and the oscillator is working

write_dat_clock(0x90,0xa5); //Charge

write_dat_clock(0x8e,0x80); //Disable write operation

}


void get_time()

{

sec=read_dat_clock(0x81); //Read seconds

sec=(sec>>4)*10+(sec&0x0f); //second binary conversion

delay(1);

min=read_dat_clock(0x83); //Read minutes

min=(min>>4)*10+(min&0x0f); // binary conversion

delay(1);

hour=read_dat_clock(0x85); //Read the hour

hour=(hour>>4)*10+(hour&0x0f); //hour binary conversion

delay(1);

day=read_dat_clock(0x87); //Read date

day=(day>>4)*10+(day&0x0f); //date binary conversion

delay(1);

month=read_dat_clock(0x89); //Read the month

month=(month>>4)*10+(month&0x0f); //month binary conversion

delay(1);

//week=read_dat_clock(0x8b); //Read the week

//week=(week>>4)*10+(week&0x0f);//week binary conversion

delay(1);

year1=read_dat_clock(0x8d); //Read the year

year1=(year1>>4)*10+(year1&0x0f); //year binary conversion

}          


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

/*********************************Key function*****************************************/

void key_1() //Select variable function

{

key1=1;

if(key1==0)

  delay(10);                                                                                                                                                                                                                                

{

  if(key1==0)

  {

   if(++status>5)status=0; //1 controls the change of year, 2 controls the change of month, 3 controls the change of day

  } //4 controls the change in time, 5 controls the change in minutes

  while(key1==0);

}

}

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

void key_2() //Add function 

{

key2=1;

if(key2==0)

{

  delay(10);

  if(key2==0)

  {

   switch(status)

   {

    case 1:if(++year1>99)year1=0;break;

    case 2:if(++month>12)month=1;break;

    case 3:if(++day>31)day=1;break;

    case 4:if(++hour>=24)hour=0;break;

    case 5:if(++min>=60)min=0;break;

   }

  }

while(key2==0);

}

}

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

[1] [2]
Reference address:51 MCU OLED clock display

Previous article:Single chip microcomputer LM75 temperature alarm LCD1602 display experiment
Next article:51 MCU drives YL-69 soil moisture sensor

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号