Electronic clock program with adjustable time

Publisher:WhisperingGlowLatest update time:2021-05-25 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


return (TH1 * 256 + TL1); //Return T1 count value

}


/*External interrupt 1 service, determine infrared and receive infrared NEC decoding*/

void EXINT1_ISP() interrupt 2 //EXINT1 interrupt number is 2

{

unsigned char byte = 0; //Receive decoded data

unsigned int time = 0; //Receive the pilot code and idle and carrier time

unsigned char i, j;


time = GetLowTimers(); //Carrier pilot code (9ms carrier + 4.5ms idle): range (8.5ms-9.5ms)

if ((time < 7833) || (time > 8755)) //Counting cycle = 12/11059200 Counting time = (TH1*256+TL1)*12/11059200

{

//Error code, clear interrupt flag and exit interrupt

IE1 = 0;

return;

}


time = GetHighTimers(); //4.5ms idle time: range: 4ms-5ms

if ((time < 3686) || (time > 4608)) // () must be added when judging

{

//incompatible

IE1 = 0; //External interrupt flag

return; //Exit interrupt function

}

//The boot code is correct, start looping to receive user code and key code

for (i = 0; i < 4; i++) //Receive 4 bytes of data

{

for (j = 0; j < 8; j++) //Receive 8 bits

{

time = GetLowTimers(); //Carrier: 560us carrier + 560us/1.68ms idle: 340us 760us

if ((time < 313) || (time > 700))

{

//Error code

IE1 = 0; // Clear external interrupt flag

return;

}

time = GetHighTimers(); //Idle

if ((time > 313) && (time < 700)) //560us idle, bit value '0' time greater than 340us and time < 780us

{

byte >>= 1; //Low bit first, receive bit by bit, shift the data bit right to the low bit

}

else if ((time > 1271) && (time < 1658)) //1.68ms idle: range: 1.38ms-1.8ms, bit value is '1'

{

byte >>= 1; // First shift right one bit and move this bit to the highest bit of byte

byte |= 0x80; //Set this position to 1

}

else

{

//The received code is an error

IE1 = 0; // Clear external interrupt 1 flag

return; //Exit the interrupt function

}

}

irdCode[i] = byte; //Receive code value

}

flagIrd = 1; //Receive completion flag set to 1

IE1 = 0; // Clear external interrupt flag

}


/**

************************************************************************

* @file : Keyboard.c

* @author : xr

* @date : April 21, 2014 22:23:12

* @version : V1.2.3

* @brief: Button bottom driver microcontroller STC89C52RC MCU crystal oscillator 11.0592MHZ

************************************************************************

*/

#include


//KEY

sbit KEY_IN_1 = P2^4;

sbit KEY_IN_2 = P2^5;

sbit KEY_IN_3 = P2^6;

sbit KEY_IN_4 = P2^7;

sbit KEY_OUT_1 = P2^3;

sbit KEY_OUT_2 = P2^2;

sbit KEY_OUT_3 = P2^1;

sbit KEY_OUT_4 = P2^0;


static unsigned char volatile keySta[4][4] = {{1, 1, 1, 1}, {1, 1, 1, 1},

{1, 1, 1, 1}, {1, 1, 1, 1}}; //Current status of 16 buttons

unsigned char pdata keyCodeMap[4][4] = { //Key code is encoded according to ASCII code

{'1', '2', '3', 0x26}, //Number keys 1, 2, 3 and up key

{'4', '5', '6', 0x25}, //Number keys 4, 5, 6 and left key

{'7', '8', '9', 0x28}, //Number keys 7, 8, 9 and down key

{'0', 0x1B, 0x0D, 0x27} //Number key 0 and ESC key, Enter key, Right key

};


/*You need to use the structure variable timeBuf of type struct Time in the main.c file and need to redeclare it*/

struct Time {

unsigned char year;

unsigned char month;

unsigned char day;

unsigned char hour;

unsigned char min;

unsigned char sec;

unsigned char week;

};


extern struct Time timeBuf;

extern unsigned char setTimeIndex; //external variable declaration

void LcdCoursorRight();

void LcdCoursorLeft();

void SetTimeBcdByte(unsigned char keyNum);

void KeyAction(unsigned char keycode);

extern void LcdRefreshSetCoursor();

extern void LcdCloseCoursor();

extern void IncTimeBysetTimeIndex();

extern void DecTimeBySetTimeIndex();

extern GetTimeFromDS1302(struct Time * time);

extern void SetTimeToDS1302(struct Time * time);

extern void RefreshLcdShowTime();


/*Key driver, judge the key status, and pass the key code of the corresponding key to the key action function*/

void KeyDriver()

{

static unsigned char pdata keyBackup[4][4] = {{1, 1, 1, 1}, {1, 1, 1, 1},

{1, 1, 1, 1}, {1, 1, 1, 1}}; //The backup of the key is the status value of the last key press

unsigned char i, j;


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

{

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

{

if (keySta[i][j] != keyBackup[i][j])

{

//Button has action

if (keyBackup[i][j] != 0) //Determine the current key value based on the last backup value, that is, the key action

{

// Press the button to execute the action

KeyAction(keyCodeMap[i][j]); // pass the corresponding key code

}

//Backup the value of this button

keyBackup[i][j] = keySta[i][j];

}

}

}

}


/*Dynamic scanning of keys to debounce keys*/

void KeyScan()

{

static unsigned char keyout = 0; //Key row index, the last changed keyout value is used each time, so it is defined as a static variable

static unsigned char keybuff[4][4] = {{0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF},

{0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}}; //Key scan buffer

unsigned char i = 0;


/*Scan the key value and store the key status value in the keybuff buffer*/

keybuff[keyout][0] = (keybuff[keyout][0] << 1) | KEY_IN_1; //Store the scan value of the first key in the keyout row into the keybuff[keyout][0] buffer

keybuff[keyout][1] = (keybuff[keyout][1] << 1) | KEY_IN_2;

keybuff[keyout][2] = (keybuff[keyout][2] << 1) | KEY_IN_3;

keybuff[keyout][3] = (keybuff[keyout][3] << 1) | KEY_IN_4;


/*Update the status of the button after scanning and debounce*/

for (i = 0; i < 4; i++) // Each row has four buttons that need to be judged

{

if ((keybuff[keyout][i] & 0x1F) == 0x00) //The values ​​of the 5 key scans are all 0, indicating that the key is pressed steadily

{

keySta[keyout][i] = 0;

}

else if ((keybuff[keyout][i] & 0x1F) == 0x1F) //The scan values ​​of the 5 keys are all 1, indicating that the key is stable.

{

keySta[keyout][i] = 1;

}

}

//row++

keyout++;

keyout &= 0x03; //Return to zero at 4


/*Determine which row of keys to scan based on the value of keyout*/

switch (keyout)

{

case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break; // Scan line 1

case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break; // Scan line 2

case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break; // Scan line 3

case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break; // Scan line 4

default: break;

}

}


/*Key action, perform corresponding operations according to the key code*/

void KeyAction(unsigned char keycode)

{

if (keycode >= '0' && keycode <= '9')

{

if (setTimeIndex != 0) // only respond when in the setting state

SetTimeBcdByte(keycode - '0'); //Number keys to set time

}

else if (keycode == 0x26) //Use the up key to adjust the value at the cursor flashing position++

{

IncTimeBysetTimeIndex(); //Increment the high or low digits of the BCD code

}

else if (keycode == 0x27) // Use the right key to move the cursor to the right

{

if (setTimeIndex != 0) // only respond when in the setting state

LcdCoursorRight(); //Decrement the high or low digit of the BCD code

}

else if (keycode == 0x25) // Use the left key to move the cursor to the left

{

if (setTimeIndex != 0)

LcdCoursorLeft();

}

else if (keycode == 0x28) //Decrement the value at the flashing position of the down key cursor

{

DecTimeBySetTimeIndex();

}

else if (keycode == 0x1B) //ESC key, cancel the current setting

{

setTimeIndex = 0; //Clock normal operation index = 0

LcdCloseCoursor(); //Close cursor flashing

}

else if (keycode == 0x0D) //Enter key, switch between setting time and restoring clock operation

{

if (setTimeIndex == 0) //Normal operation state, start switching to setting state

{

setTimeIndex = 1;

LcdRefreshSetCoursor(); //Refresh the cursor flashing position and make the cursor flash

}

else

{

//Write the set time value and switch to normal operation

SetTimeToDS1302(&timeBuf);

GetTimeFromDS1302(&timeBuf);

setTimeIndex = 0;

LcdCloseCoursor(); //Close cursor flashing

}

}

}


/*setTimeIndex++, to set the cursor to move right when blinking*/

void LcdCoursorRight()

{

if (setTimeIndex < 12)

setTimeIndex++; //Cursor setting index++

else

setTimeIndex = 1;

LcdRefreshSetCoursor();

}


/*setTimeIndex--, set the cursor to blink left*/

void LcdCoursorLeft()

{

if (setTimeIndex > 1)

setTimeIndex--; //Cursor setting index--

else

setTimeIndex = 12;

LcdRefreshSetCoursor();

}


/*Set the high digit of the BCD code*/

unsigned char SetBCDHighByte(unsigned char bcd, unsigned char keyNum)

{

bcd &= 0x0F; // clear high bit

bcd |= (keyNum << 4); //keyNum moves to a higher position


return (bcd);

}


/*Set the low digit of the BCD code*/

unsigned char SetBCDLowByte(unsigned char bcd, unsigned char keyNum)

{

bcd &= 0xF0; //low bit clear

bcd |= keyNum; //Put the keyNum number in the lower position


return (bcd); //Return the changed bcd byte data

}


/*Set the time value by key value (set the high and low digits of the time value)*/

void SetTimeBcdByte(unsigned char keyNum) //Transfer the value of the key

{

switch (setTimeIndex)

{

case 1: timeBuf.year = SetBCDHighByte(timeBuf.year, keyNum); break; //Set the high BCD byte of the year

case 2: timeBuf.year = SetBCDLowByte(timeBuf.year, keyNum); break; //Set the low BCD byte of the year

case 3: timeBuf.month = SetBCDHighByte(timeBuf.month, keyNum); break;

case 4: timeBuf.month = SetBCDLowByte(timeBuf.month, keyNum); break;

case 5: timeBuf.day = SetBCDHighByte(timeBuf.day, keyNum); break;

case 6: timeBuf.day = SetBCDLowByte(timeBuf.day, keyNum); break;

case 7: timeBuf.hour = SetBCDHighByte(timeBuf.hour, keyNum); break;

case 8: timeBuf.hour = SetBCDLowByte(timeBuf.hour, keyNum); break;

case 9: timeBuf.min = SetBCDHighByte(timeBuf.min, keyNum); break;

case 10: timeBuf.min = SetBCDLowByte(timeBuf.min, keyNum); break

case 11: timeBuf.sec = SetBCDHighByte(timeBuf.sec, keyNum); break;

case 12: timeBuf.sec = SetBCDLowByte(timeBuf.sec, keyNum); break

}

RefreshLcdShowTime(); //Refresh time value display

LcdCoursorRight(); //Cursor flashes and moves right

LcdRefreshSetCoursor(); //Refresh cursor flashing

}


[1] [2] [3]
Reference address:Electronic clock program with adjustable time

Previous article:Design of emergency power inverter circuit using single chip microcomputer SPWM
Next article:Circuit structure of P0 port of 51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 19:52

A matrix keyboard program under the stc89c52 microcontroller
    The following is a matrix keyboard program for the stc89c52 microcontroller. The keyboard is connected to port P0 and displayed on port P2.      #include reg52.h  #define uchar unsigned char  #define uint unsigned int  sbit key1=P3^2;  sbit key2=P3^3; uchar code tab ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
[Microcontroller]
Design of LCD display system based on STC89C52 and HT1621D
Liquid crystal display (LCD) has many advantages that many other displays cannot match, such as low power consumption, small size, light weight, ultra-thin, large visible area, good picture effect, high resolution, strong anti-interference ability, etc. In recent years, it has been widely used in intelligent instrum
[Power Management]
Design of LCD display system based on STC89C52 and HT1621D
STC89C52MCU -- DS18B20 temperature sensor
DS18B20 temperature sensor uses "single bus" serial transmission mode At present, the serial buses for microcontroller data transmission are mainly Inter IC Bus, SPI and SCI. Among them, the IIC bus communicates in a synchronous serial two-wire mode (one data line and one clock line), the SPI bus communicates in a syn
[Microcontroller]
Introduction to 51 MCU timer settings (STC89C52RC)
STC MCU timer setting The use of STC microcontroller timer can be said to be very simple, as long as you understand the principle and have a basic understanding of C language. The key points are as follows: 1. You must know the original form of the English abbreviation, so you don't have to remember the name of th
[Microcontroller]
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号