Implementation of perpetual calendar (including alarm clock and stopwatch) based on 51 single chip microcomputer

Publisher:qinghongLatest update time:2020-06-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Let me start with a little chat:


Taking advantage of the winter vacation, and also taking advantage of the course design that happens to be making a perpetual calendar, I plan to go through it from beginning to end. To increase my knowledge. First of all, I am also a newbie, and I would be grateful if the masters can help correct my mistakes. I write blogs just to summarize my experience, and it would be even better if it can help some people. I think I should introduce everything from hardware to software in detail, and I also want to talk about some problems I encountered, which may take a longer time to write. I will upload the code later. Okay, no more small talk. Let's get to the point.


Overview:


First, let me talk about the things I used. In terms of hardware (the circuits were all soldered by myself using a universal board): a 51 single-chip microcomputer, a 12864 LCD, a ds1302 clock chip, and four buttons. There are also some capacitors, resistors, crystal oscillators, etc., which will be discussed later. That's all the main ones. Let me briefly talk about the functions of the buttons. Assume that the buttons are k1, k2, k3, and k4. First, the main interface of the LCD displays the current date and time and the time of the four alarms. Attached picture. When k1, k2, k3, and k4 are pressed at the beginning, the corresponding functions are k1: enter the time setting mode; k2: enter the date setting mode; k3: enter the alarm setting mode; k4: enter the stopwatch counting mode. After entering different modes, the four buttons have new functions. First, k4 is always exit, which means exiting to the first four modes. k1, k2, k3 have the same functions for date and time setting modes. k1: add 1 to the value, k2: subtract 1 from the value, k3: change whether to adjust the hour, minute, year or month. For alarm mode, k1: add 1 to the value, k2: change whether to adjust the hour, minute, k3: change which alarm is adjusted. For stopwatch mode, k1: the first press starts counting, and then the next press records the current number of seconds, up to 9 times. k2: pause/start, k3: restart counting. It's a bit confusing, to put it simply, there are two loops. If you still don't understand, you can look at the following code.


1. Hardware circuit


How to say this part, it is quite simple. But there is a stalk that I have not gotten over yet. At first, I planned to solder a download circuit on it myself, but I couldn't download the program. This part is off topic, but I still want to talk about it briefly. At first, I planned to use the CH340 chip to directly convert USB to UART, but after I bought the chip, I found that there seemed to be no direct plug-in. It was too troublesome to rot the board myself. Finally, I planned to use USB to convert the nine-pin serial port to RS232 level first, and then use Max232 to convert to UART level. I soldered it according to the circuit diagram, and the result was as I expected. It was impossible to successfully download the program at once. I looked for the problem, and looked for it. It seems that I found one. Isn't there a process of powering off and then powering on when downloading the program on 51? I did it this way, but there seems to be a problem. The power that was cut off should only be the power of the microcontroller, not the power of Max232. So I changed the circuit again, but it still didn't work. Forget it, I will talk about it after I figure it out.


The rest should not be that difficult. Just find a 51 minimum system schematic and solder it according to it. There shouldn't be any big problems.


By the way, there are a few small points to mention. The P0 port of 51 is equivalent to an open collector gate circuit, so remember to connect a pull-up resistor. When the LCD screen is booted, if nothing is displayed at first, remember to adjust the backlight by adjusting the potentiometer connected to pin 3.


2. Software Design


1.Key detection


This part seemed to me like nothing big to write about at first, and there was nothing worth writing about. People with some basics could write the program in a few minutes. But when I really wrote it, I realized that I didn't know much and had too much to learn. The two most common functions of the IO port of the microcontroller are input and output. I remember when I learned stm32, the input and output of the IO port had to be defined at the beginning of the initialization. That is, the IO port can only have one function at the same time, and it can't have both input and output. But what about 51? It made me confused. There was no definition of whether the IO port was input or output anywhere, including in the startup file. This made me very depressed. It's impossible that after I let an IO port output a high level, I can also read the input from the IO port. In that case, shouldn't I always read the high level that I output? It wasn't until I studied the internal circuit of the 51 IO port that I understood the mystery.

Here is the simplest internal structure diagram of P1 port. People who have some basic knowledge of digital electronics can probably understand it. I won’t go into details. You can refer to it here https://www.eeworld.com.cn/mcu/article_2017120236473_2.html


As can be seen from the figure above, to correctly read external information from the pin, the field effect tube must be turned off first so that the state of the pin can be determined by the external input information. For this reason, before reading the pin, l must be written to the port first. The input/output port with this operation characteristic is called a quasi-bidirectional I/O port. P1, P2, and P3 of the 8051 microcontroller are all quasi-bidirectional ports. Since the output of the P0 port has a three-state function, the port line is already in a high-impedance state before input, and there is no need to write l before performing a read operation. After understanding the internal structure of the IO port. I will directly go to the program and study it slowly. Don't worry about the commented and unused parts.


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

Program description: Key detection program (based on 51 single-chip microcomputer), now only has independent key detection function

Author: xingcheng

IO Description: Button connected

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

 

#include "key.h"

sbit KeyPort2=P1^5;

sbit KeyPort0=P1^7;

sbit KeyPort1=P1^6;

sbit KeyPort3=P1^4; //Connect the soldered key to the MCU pin

//sbit KeyPort2=P1^2;

//sbit KeyPort0=P1^0;

//sbit KeyPort1=P1^1;

//sbit KeyPort3=P1^3;

 

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

Function name: key_scan()

Function: 4 independent key detection

Input parameters: None

Return value: KeyV determines which key is pressed by the difference in return values.

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

uchar key_scan()

{

uchar KeyV;

KEYPORT=0xff; //Read data from 51IO port, generally write 1 to the latch first,

//For details, please refer to the internal structure of 51IO port

if(KeyPort0==0||KeyPort1==0||KeyPort2==0||KeyPort3==0) //Determine if a key is pressed

//Change it to if((P3&0xf0)!=0xf0) here. It is always wrong. The reason may be that P3 reads data not from the pin.

//But it is read from the latch, which is always 0xff

{

delay_ms(10); //Prevent jitter (when experimenting with the board, it was found that the delay here had no effect)

if(KeyPort0==0) //Determine which key is pressed//

{

KeyV=K1;

}

else if(KeyPort1==0)

{

KeyV= K2;

}

else if(KeyPort2==0)

{

KeyV=K3;

}

else if(KeyPort3==0)

{

KeyV=K4;

}

else 

{

KeyV= 0;

} //Determine which button is pressed//

if(KeyV != 0) //When a key is pressed, release detection is performed

              while(KeyPort0==0||KeyPort1==0||KeyPort2==0||KeyPort3==0);

delay_ms(10); //Delay to eliminate jitter (use the board to experiment, delay is very necessary here)

   }

 

    return KeyV; //Return KeyV to identify which key is pressed

}

 

/*********************************I will improve the functions of continuous pressing and long pressing when I have time****************************/

 

/* while((KEYPORT&0Xf0)!=NO_KEY)

{

delay_ms(15);

PressCnt--;

if(PressCnt==0)

{

PressCnt=SHORTCNT;

return KeyV;

}

}

}

delay_ms(15);

if((KEYPORT&0Xf0)==NO_KEY)

{

ReleaseCon=0;

return KeyV;

}

  */

#ifndef __KEY_H #define _KEY_H

 

#include#include"delay.h"

 

#ifndef uchar #define uchar unsigned char #endif

 

#define KEYPORT P3 // The four buttons are connected to the four pins of port P3 #define NO_KEY 0xf0 #define K1 0X01 #define K2 0X02 #define K3 0X03 #define K4 0X04 #define KEYSUB 0X02 #define KEYADD 0X01 #define KEYSET 0X04 #define KEYNEXT 0X03 // K1, 2, 3, 4 are the same as these, but when writing .c files #define LONGCNT 150 #define SHORTCNT 12

 

 

 

      uchar key_scan();

 

#endif



 

  2. lcd12864


     There is really nothing much to say about this. Just remember to adjust the potentiometer to adjust the backlight. By the way, there is another tricky part. I wonder if you have a solution. The cursor (the one that flashes) moves two words at a time. Upload the program.


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

Program function: 12864 LCD driver

Others: Only basic string display functions are included

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

 

 

#include

#define uchar unsigned char

#define uint unsigned int

#define LCD_data P0 //Data port

 

 

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

Function name: delay(int ms)

Function: Delay

Input parameter: ms The number of ms to delay                                                                                                                                                                      

Return value: None

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

[1] [2] [3] [4] [5]
Reference address:Implementation of perpetual calendar (including alarm clock and stopwatch) based on 51 single chip microcomputer

Previous article:51 MCU (Part 9). 51 MCU simple project - perpetual calendar and temperature acquisition
Next article:51 single chip perpetual calendar

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号