Electronic clock based on 51 single chip microcomputer

Publisher:RainbowJoyLatest update time:2021-07-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Using LCD1602 to display hours and minutes based on 51 single chip microcomputer

2. Button control hour-minute adjustment

3. Can realize the function of full-time timing (buzzer sounds)

4. Alarm mode

5. Press the button to switch modes (Mode 1: hour-minute display, Mode 2: 60-second countdown)


1. Design ideas:

main body:

The mod value is controlled by external interrupt 0; mod=0, 1.2, 3 correspond to clock mode, adjustment mode, alarm setting mode, and one-minute countdown mode respectively.

detail:

mod0

Through the timing counter, the variable second (s) is increased every second. Every 60 seconds, 1 minute (min) is increased and s is set to 0. Every 60 minutes, 1 hour h is increased. When h>23, h=0; a one-day cycle is performed.

mod1

Key control adds min, h and s to 0

mod2

In addition, set the variables min1 and h1. When min=min1, h=h1, the buzzer will sound.

mod3

Set the variable daojishi=60, through the timing counter, daojishi decreases by 1 every second, when daojishi<0, the buzzer sounds

in addition:

1. Set external interrupt 2 and turn off the buzzer

2. Time can only send one bit at a time


program:


#include

//After K1, K3 adds minutes, K4 adds time, K2 adds seconds, K1 enters alarm setting, K2 exits

//K2 turns off the alarm


typedef unsigned int u16; //declare and define the data type

typedef unsigned char u8;


#define data8b P1


sbit K1=P3^2; //External interrupt 0

sbit K2=P3^3; //External interrupt 1

sbit K3=P3^0;

sbit K4=P3^1;


sbit BUZ=P2^4; //Buzzer, 0 beeps

sbit RW=P2^5; //Pin 4, data (1) or command (0)

sbit RS=P2^6; //Pin 5, read (1) write (0)

sbit E=P2^7; //pin 6, enable signal


u8 code dat1[]={0X30,0X31,0X32,0X33,

                               0X34,0X35,0X36,0X37,

                               0X38,0X39};


void delay(u16 i) //delay function

{

while(i--);

}


void open012() //Open interrupt 0,1, timer interrupt 0

{

TMOD|=0X01; //Select timer 0 mode, working mode 1

  

ET0=1; //Enable timer 0 interrupt

EA=1; //Open the general interrupt

TR0=1; //Turn on the timer

EX0=1; //Open external interrupt 0

IT0=1; //Edge trigger mode

EX1=1; //Open external interrupt 1

IT1=1; //Edge trigger

  

}



void wrm(u8 dat) //write command

{

delay(1000);

RS=0;

RW=0;

E=0;

data8b=dat;

E=1;

delay(1000);

E=0;

}



void wrd(u8 dat) //write data

{

delay(1000);

RS=1;

RW=0;

E=0;

data8b=dat;

E=1;

delay(1000);

E=0;

}


void zero()

{

wrm(0X38); //8-bit data, two lines display, 5*7

wrm(0X0c); //No cursor, turn on display

wrm(0X06); //Move the cursor right, but the screen does not move

wrm(0X01); //Clear screen

wrm(0X80); //Set the data pointer starting point

}


u8 fg=0,sg=0,bfg=0,bsg=0;

u16 i=0;

u8 s=0;

u8 mod=0;

char dingshi;

bit bell=0;

bit zanting=1;


void fangsong()

{

  wrd(dat1[sg/10]); //tens digit of the hour

wrd(dat1[sg%10]); //Hour digit

wrd(0x3A); //:

wrd(dat1[fg/10]); //ten digits

wrd(dat1[fg%10]); //dividing into units

wrd(0x3A); //:

wrd(dat1[(s/10)]); //10 seconds

wrd(dat1[(s%10)]); //seconds

}



void fangsong1()

{

wrm(0X80);   

  wrd(dat1[sg/10]); //tens digit of the hour

wrd(dat1[sg%10]); //Hour digit

wrd(0x3A); //:

wrd(dat1[fg/10]); //ten digits

wrd(dat1[fg%10]); //dividing into units

wrd(0x3A); //:

wrd(dat1[(s/10)]); //10 seconds

wrd(dat1[(s%10)]); //seconds

}

void chuli()

{

if(fg==60)

{

sg++;

fg=0;

}


if(sg==24)

{

sg=0;

}

}




void main()

{

u8 shijian;

open012();

zero();

chuli();

fangsong();

shijian=100;

while(1)

{

while(mod==0)

{

EX1=1; //Open external interrupt 1

if(s==60)

{

fg++; //Convert 60 seconds to 1 minute

s=0;

}

chuli();

if((fg==0)&&(shijian!=sg))

{

  BUZ=0;

shijian=sg;

}

fangsong1();

if((BUZ==0)&&(bell==0))

  {

delay(1000);

BUZ=1;

  }

if((fg==bfg)&&(sg==bsg)&&(bell==1))

BUZ=0;

else BUZ=1;

}

while(mod==1)

{

EX1=0; //Disable external interrupt 1

zero();

    fangsong();

if(K3==0)

  {

  delay(1000);

  if(K3==0)

  fg++;

   }

if(K4==0)

   {

  delay(1000);

  if(K4==0)

sg++;

   }

if(K2==0)

   {

  delay(1000);

  if(K2==0)

s=0;

   }

if(fg>59)

{

fg=0;

}

  if(sg>23)

{

sg=0;

}

if(s>=59)

{

s=0;

}

     }

 

 

while(mod==2) //Set the alarm

{

    if(bfg==60)

    {

    bsg++;

    bsg=0;

    }

    if(bsg==24)

    {

     bsg=0;

     }

    zero();

wrd(0x20);

wrd(0x20);

wrd(0x20);

  wrd(dat1[(bsg/10)]); //tens digit of the hour

    wrd(dat1[(bsg%10)]); //Hour digit

    wrd(0x3A); //:

    wrd(dat1[(bfg/10)]); //ten digits

    wrd(dat1[(bfg%10)]); //divide into units

if(K3==0)

{

delay(1000);

if(K3==0)

bfg++;

}

if(K4==0)

{

delay(1000);

if(K4==0)

bsg++;

}

bell=1;

zero();

}

while(mod==3)

{

while(zanting)

{

dingshi=60;

EX1=1; //Open external interrupt 1

wrm(0X80);   

wrd(dat1[(dingshi/10)]); //Tens digit of the hour

wrd(dat1[(dingshi%10)]); //Hour digit

   }

   wrm(0X80);   

wrd(dat1[(dingshi/10)]); //Tens digit of the hour

wrd(dat1[(dingshi%10)]); //Hour digit

while(dingshi<0)

{

wrm(0X80);   

wrd(dat1[0]); //tens digit of the hour

   wrd(dat1[0]); //Hour digit

BUZ=0;

}

}

}

}


void time0() interrupt 1

{

   TH0=0XFC; //Assign initial value to the timer, set the timer to 1ms

TL0=0X18;

i++;

if(i==1000) //ms converted to s

{

i=0;

s++;

dingshi--;

}

}


void key1() interrupt 0 //External interrupt 0, adjust time

{

delay(1000);

if(K1==0)

  { 

  mod++;

while(!K1);

  }


if(mod>3)

{

mod=0;

}

zero();

}


void naozhong() interrupt 2 //Switch the alarm

{

if(K2==0)

{

delay(1000); //debounce

if(K2==0)

{

bell=0;

BUZ=1;

zanting=~zanting;

} //Turn off the buzzer

while(!K2); //Confirm button is released

     }

}

insert image description here

2. Harvest

1. Become more proficient in the use of timer interrupts and external interrupts

insert image description here

insert image description here

The same button can be used to achieve different functions by turning on and off external interrupts.


2. Learn a little bit about register operations

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

3. Summary of Interrupt Configuration

External Interrupt

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

Timer interrupt

insert image description here

Serial communication

insert image description here

4. Be more proficient in using Proteus

Made a small software development version

insert image description here

5. Learned how to learn a new component (LCD1602)

1. Read the instructions, focusing on the timing diagram, truth table, etc.

2. Write the program according to the timing diagram

3. When neither of the above two conditions is true, look for relevant information

6. Download Principle

The principle of microcontroller programming:

Microcontroller burning, also known as microcontroller program downloading and burning, is essentially the microcontroller and PC transferring the compiled program to the microcontroller through the interface specified by the chip manufacturer according to the programming protocol specified by the chip manufacturer, and the microcontroller stores the data in its own memory.

To understand this principle, you need to know several knowledge points:

There are programs inside the MCU, which are fixed in the hardware when it leaves the factory and cannot be modified by the user (this will also be considered as no programs inside it). These programs can call various communication interfaces, internal memory, etc.

Downloadable communication interfaces: JTAG, SPI, UART, USB, etc. (There are also many that can be expanded to 485, Ethernet, etc.)

Programming protocol: Generally, it will be made public by major manufacturers and can be found in the chip's dedicated technical manual;

Memory: There are many types, such as mask, EPROM, EEROM, flash, etc., with different lifespans. Mask can only be used once and must be made by the factory. Flash can be erased and written 10,000 times or more.

It can be understood metaphorically like this: the microcontroller is the motherboard of the computer, the program we write is the operating system, the basic program for booting the operating system is loaded into the motherboard, and downloading the program is like installing the system on the computer!


7. Others

1. Understand the operating principles of current electronic watches

2. Learn to use ready-made products (spreadsheets) as references to write programs

3. Understand the importance of communicating with others (inspired by Wang, improved the original program)


3. Later plans

1. Continue to study 32

2. Practice welding skills

3. Continue to learn other peripherals of 51

4. Learn circuits, analog electronics, digital electronics, DXP ​​and other related knowledge according to your interests

Reference address:Electronic clock based on 51 single chip microcomputer

Previous article:51 single chip DHT11 temperature and humidity ESP8266WiFi mobile phone APP display design
Next article:LCD display 51 single chip simple calculator

Recommended ReadingLatest update time:2024-11-17 00:03

LCD1602 Display Summary
The simulation schematic is as follows: 1. LCD1602 normally displays two lines of data /*------------------------ Function: LCD1602 displays two lines of data Author: Zhang Kaizhou Date: 2019-6-3 12:42:22 ------------------------*/ #include reg52.h #define uchar unsigned char #define uint unsigned int uchar co
[Microcontroller]
LCD1602 Display Summary
MCU write LCD1602 liquid crystal program source code
Download the schematic diagram of this program in pdf format:  http://www.51hei.com/f/ks51.pdf   /***********Write 1602 LCD**************/ /** *Function: Write 1602 LCD *Time: March 15, 2014 08:59:26 *Author: Han Zhuzi **/ #include reg52.h #include intrins.h typedef unsigned int uint; typedef unsigned cha
[Microcontroller]
Based on 51 single chip microcomputer driving LCD1602 liquid crystal display thb6064ah subdivision 64
#include #include intrins.h #define uchar unsigned char #define uint unsigned int #define LCD_DB P0 sbit LCD_RS = P2^4; sbit LCD_RW = P2^3; sbit LCD_E = P2^2; sbit K1 = P1^0; sbit K2 = P1^1; sbit K3 = P1^2; sbit K4 = P1^3; sbit K5 = P1^4; sbit M1 = P1^5; sbit M2 = P1^6; sbit M3 = P1^7; sbit DCY1 = P3^0; sbit
[Microcontroller]
The Path of Learning for a Newbie in Single-Chip Microcomputers (Part 41) --- Understanding LCD1602 Liquid Crystal
Objective: Understand the working principle of LCD1602 liquid crystal 1. LCD1602 Introduction 1602 LCD is also called 1602 character LCD. It can display 2 lines of character information, and each line can display 16 characters, so it is referred to as 1602. It is a dot matrix LCD module specially used to display let
[Microcontroller]
The Path of Learning for a Newbie in Single-Chip Microcomputers (Part 41) --- Understanding LCD1602 Liquid Crystal
89C52 and LCD1602
I have read C51 data in the past two days and familiarized myself with the statements. I haven't read any resources for 51 yet, but I wrote this program in a hurry. It's very clumsy. Finally, LCD1602 works and displays. Difficulty 1: Timing, Difficulty 2: 11 instructions of LCD1602; If the timing is correct, LCD1602
[Microcontroller]
PCF8574 STC15 MCU driver LCD1602 display
I found a lot of 1602 programs on the Internet, but they were all ARDUINO and STM32, or none of them were reliable, so I decided to write it myself. After testing it for a long time, I finally wrote it. It looks good hahahaha! (MCU is STC15F2K60S2) The actual picture of the product is as follows: The microcontroller
[Microcontroller]
PCF8574 STC15 MCU driver LCD1602 display
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号