<> Blue Bridge Cup 8 Provincial Games _ Electronic clock
Modified version
Basic knowledge learning comes from little bee teacher yyds
The following code functions are not fully implemented If there is any mistake, I hope the boss will correct it
Summary and analysis :
Personally, the difficulty of the eighth session is obviously much harder than that of the seventh session , Of course, it may also be that I haven't mastered the relevant programming in this aspect From my personal problems in programming .
First of all, when you start programming, you must read the topic clearly and understand it Let your brain have a basic framework , Then it is divided into modular programming Make corresponding logical combination , Remember not to come back to the mode after you have written it all
Write some functions and debug them In this way, even if there is an error, it can quickly locate the error and solve it , Secondly, when practicing by yourself Be patient with the debugging of the program , This is also a crucial factor in success
, Solve it myself Bug It took a long time, even a small mistake Sometimes they have been debugged many times Fortunately, I didn't give up and finally solved it successfully .
Some problems were encountered at the beginning of the eighth topic practice , Then I want to find the code written by other big guys on the Internet , From the heart, what they write is really good
But maybe I'm too shallow to understand , Then we can only rely on ourselves to realize it slowly bit by bit It took nearly as long as the game 2 In the final analysis, I didn't practice enough Um It can only be said that we should continue to work hard .
The function of the following program is tested on my development board There is no problem with the display of various modes At that time, I didn't know why to modify the clock and set the alarm clock There is something wrong with the number displayed
then Led The function of the operation has not been written yet , It will continue to be improved in the future .
What a long long road! come on.
#include "reg52.h" #include "onewire.h" #include "absacc.h"
/*=================================================================
Date:2022-1-14 Author: Xiao Yin Version:1.1 IO mode
==================================================================*/ sbit S7 =
P3^0; sbit S6 = P3^1; sbit S5 = P3^2; sbit S4 = P3^3; sbit Led1 = P0^0; unsigned
char T_Hour = 23,T_Min= 59,T_Sec = 50; unsigned char C_Hour = 0,C_Min = 0,C_Sec
= 0; unsigned char Temperature = 0;// Temperature storage unsigned char time_set = 0,clock_set =
0,mode_set = 0;// Time setting and alarm clock flag unsigned char t_count0 = 0,t_flag = 0,t_count1,
c_count= 0;// Timing and flashing signs unsigned char t_led = 0,led_flag = 0;// Mark separately 0.2s and 5s
unsigned char code smg_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
,0xbf,0xff}; //------------------------------ Simple delay ----------------------------
void Delay(unsigned int t) { while(t--); }
//--------------------------74CHC138 initialization ------------------------- void
Init_74HC138(unsigned char n) { switch(n) { case 0:P2 = (P2 & 0x1f) | 0x00;break
; case 4:P2 = (P2 & 0x1f) | 0x80;break; case 5:P2 = (P2 & 0x1f) | 0xa0;break;
case 6:P2 = (P2 & 0x1f) | 0xc0;break; case 7:P2 = (P2 & 0x1f) | 0xe0;break; } }
//------------------------- System initialization ----------------------------- void InitSyetem(
void) { Init_74HC138(4); P0 = 0xff; // close LED Init_74HC138(5); P0 = 0x00;
// Turn off the relay and buzzer } //------------------------- Nixie tube position display ------------------------- void
SMG_DisplayBit(unsigned char pos,unsigned char dat) { Init_74HC138(6); P0 = (
0x01 << pos); Init_74HC138(7); P0 = dat; } void SMG_Close(void) { Init_74HC138(6
); P0 = 0xff; Init_74HC138(7); P0 = 0xff; }
//----------------------- Nixie tube display ------------------------------ void SMG_Display(
unsigned char hour,unsigned char min,unsigned char sec) { SMG_DisplayBit(0,
smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(200);
SMG_DisplayBit(2,smg_duan[10]); Delay(200); SMG_DisplayBit(3,smg_duan[min/10]);
Delay(200); SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); SMG_DisplayBit(5,
smg_duan[10]); Delay(200); SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200);
SMG_DisplayBit(7,smg_duan[sec%10]); Delay(200); SMG_Close(); } void
SMG_Hour_Flash(unsigned char hour,unsigned char min,unsigned char sec) {
SMG_DisplayBit(2,smg_duan[10]); Delay(200); SMG_DisplayBit(3,smg_duan[min/10]);
Delay(200); SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); SMG_DisplayBit(5,
smg_duan[10]); Delay(200); SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200);
SMG_DisplayBit(7,smg_duan[sec%10]); Delay(200); if(t_flag == 0) { SMG_DisplayBit
(0,smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(
200); } else { SMG_DisplayBit(0,smg_duan[11]); Delay(200); SMG_DisplayBit(1,
smg_duan[11]); Delay(200); } SMG_Close(); } void SMG_Min_Flash(unsigned char
hour,unsigned char min,unsigned char sec) { SMG_DisplayBit(0,smg_duan[hour/10]);
Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(200); SMG_DisplayBit(2,
smg_duan[10]); Delay(200); SMG_DisplayBit(5,smg_duan[10]); Delay(200);
SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200); SMG_DisplayBit(7,smg_duan[sec%10
]); Delay(200); if(t_flag == 0) { SMG_DisplayBit(3,smg_duan[min/10]); Delay(200)
; SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); } else { SMG_DisplayBit(3,
smg_duan[11]); Delay(200); SMG_DisplayBit(4,smg_duan[11]); Delay(200); }
SMG_Close(); } void SMG_Sec_Flash(unsigned char hour,unsigned char min,unsigned
char sec) { SMG_DisplayBit(0,smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,
smg_duan[hour%10]); Delay(200); SMG_DisplayBit(2,smg_duan[10]); Delay(200);
SMG_DisplayBit(3,smg_duan[min/10]); Delay(200); SMG_DisplayBit(4,smg_duan[min%10
]); Delay(200); SMG_DisplayBit(5,smg_duan[10]); Delay(200); if(t_flag == 0) {
SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200); SMG_DisplayBit(7,smg_duan[sec%10
]); Delay(200); } else { SMG_DisplayBit(6,smg_duan[11]); Delay(200);
SMG_DisplayBit(7,smg_duan[11]); Delay(200); } SMG_Close(); }
//--------------------------------- Temperature reading ------------------------ void
Read_Temperature(void) { unsigned char LSB = 0,MSB = 0; init_ds18b20();
Write_DS18B20(0xcc);// skip Rom Write_DS18B20(0x44);// Start conversion Delay(500); init_ds18b20(
); Write_DS18B20(0xcc);// skip Rom Write_DS18B20(0xbe);// Read temperature to staging area LSB = Read_DS18B20
();// Read page 0 byte MSB = Read_DS18B20();// Read page 1 byte Temperature = (MSB << 8)| LSB;
Temperature= Temperature >> 4;// Reserved integer /* Decimal processing if((Temperature & 0x1f) != 0x0000)
// The top five digits are symbols All zeros + { Temperature = Temperature >> 4; Temperature = Temperature * 10;
Temperature = Temperature + (LSB & 0x0f) *0.625; } */ }
//------------------------------- Temperature display --------------------------- void
Temperature_Display(void) { SMG_DisplayBit(0,smg_duan[11]); Delay(200);
SMG_DisplayBit(1,smg_duan[11]); Delay(200); SMG_DisplayBit(2,smg_duan[11]);
Delay(200); SMG_DisplayBit(3,smg_duan[11]); Delay(200); SMG_DisplayBit(4,
smg_duan[11]); Delay(200); SMG_DisplayBit(5,smg_duan[Temperature/10]); Delay(200
); SMG_DisplayBit(6,smg_duan[Temperature%10]); Delay(200); SMG_DisplayBit(7,0xC6
); // C Delay(200); SMG_Close(); }
//---------------------------------- Key processing ------------------------ void KeyScans(
void) { if(S7 == 0) { Delay(20); if(S7 == 0) { mode_set = 1; while(S7 == 0);
time_set++; ET0 = 0,TR0 = 0; if(time_set == 1) { // Time setting SMG_Hour_Flash(T_Hour,
T_Min,T_Sec); } else if(time_set == 2) { // Sub setting SMG_Min_Flash(T_Hour,T_Min,T_Sec)
; } else if(time_set > 3) { // second SMG_Sec_Flash(T_Hour,T_Min,T_Sec); time_set = 0;
ET0= 1,TR0 = 1; } } } // s6 Alarm clock hour, minute and second selection if(S6 == 0) { Delay(20); if(S6 == 0) {
clock_set++; mode_set = 2; ET0 = 0,TR0 = 0; while(S6 == 0); // Wait for the key to release if(
clock_set== 1) // When selecting { SMG_Hour_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set
== 2) // Selective score { SMG_Min_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set == 3) {
SMG_Sec_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set == 4) { SMG_Display(
T_Hour,T_Min,T_Sec); // Real time display of time clock_set = 0; ET0 = 1,TR0 = 1; } } } if(S5 == 0)
{ Delay(20); if(S5 == 0) { while(S5 == 0); if(time_set == 1) // Add hours { T_Hour++;
if(T_Hour >= 24) // Boundary treatment { T_Hour = 0; } } else if(time_set == 2) // Minutes to add { T_Min
++; if(T_Min >= 60) // Boundary treatment { T_Min = 0; } } else if(time_set == 3)// Seconds to add { T_Sec
++; if(T_Sec >= 60) // Boundary treatment { T_Sec = 0; } } else if(clock_set == 1) { C_Hour++;
if(C_Hour == 24) // Boundary treatment { C_Hour = 0; } } else if(clock_set == 2) { C_Min++; if(
C_Min== 60) { C_Min = 0; } } else if(clock_set == 3) { C_Sec++; if(C_Sec == 60)
{ C_Sec = 0; } } } } if(S4 == 0) { Delay(20); if(S4 == 0) { while(S4 == 0); if(
time_set== 1)// Subtract when { T_Hour--; if(T_Hour == 0)// Boundary treatment { T_Hour = 24; } } else if
(time_set == 2) // Minute minus { T_Min--; if(T_Min == 0) // Boundary treatment { T_Min = 60; } } else if
(time_set == 3) // Seconds to subtract { T_Sec--; if(T_Sec == 0)// Boundary treatment { T_Sec = 60; } } else if
(clock_set == 1) { C_Hour--; if(C_Hour == 0) // Boundary treatment { C_Hour = 24; } } else if(
clock_set== 2) { C_Min--; if(C_Min == 0) { C_Min = 60; } } else if(clock_set ==
3) { C_Sec--; if(C_Sec == 0) { C_Sec = 60; } } } } }
//------------------------------------- timer initiated ------------------- void
Init_Timer0(void) { TMOD = 0x11; // timer 0 timer 1 Mode 1 16 TH0 = (65535-50000)/256;
//50ms TL0 = (65535-50000)%256; TH1 = (65535-50000)/256;//50ms TL1 = (65535-
50000)%256; ET1 = 1; TR1 = 1; ET0 = 1; TR0 = 1; EA = 1; }
//----------------------------- Main function ------------------------------- void main(void
) { InitSyetem(); Init_Timer0(); while(1) { // Read temperature Read_Temperature(); if((
time_set== 0) && (clock_set == 0)) { if(S4 == 0) { Delay(20); if(S4 == 0) {
while(S4 == 0) { Delay(200); // Display temperature Temperature_Display(); } } } SMG_Display(
T_Hour,T_Min,T_Sec); // Real time display time // SMG_Display(C_Hour,C_Min,C_Sec); // Real time display of alarm clock }
KeyScans(); if(mode_set == 1) { switch(time_set) { case 1:SMG_Hour_Flash(T_Hour,
T_Min,T_Sec);break; case 2:SMG_Min_Flash(T_Hour,T_Min,T_Sec);break; case 3:
SMG_Sec_Flash(T_Hour,T_Min,T_Sec);break; } } else if(mode_set == 2) { switch(
clock_set) { case 1:SMG_Hour_Flash(C_Hour,C_Min,C_Sec);break; case 2:
SMG_Min_Flash(C_Hour,C_Min,C_Sec);break; case 3:SMG_Sec_Flash(C_Hour,C_Min,C_Sec
);break; } } // The alarm clock responds when it arrives led1 0.2s twinkle 5s else if(((T_Hour == 0) && (T_Min == 0) && (
T_Sec== 0))|| ((T_Hour == C_Hour) && (T_Min == C_Min) && (T_Sec == C_Sec))) {
Init_74HC138(4); if(led_flag == 0) { P0 = 0xfe; } else if(led_flag == 1) { P0 =
0xff; } Delay(50000); Delay(50000); P0 = 0xff; } } }
//----------------------------- Timer interrupt service ------------------------- void
Server_Timero() interrupt 1 { TH0 = (65535-50000)/256; TL0 = (65535-50000)%256;
t_count0++; c_count++; if(t_count0 == 4) // 0.2s { led_flag = ~led_flag; } else
if(t_count0 == 20) // 1s Time count flag { t_count0 = 0; if(T_Sec++ == 60) { T_Sec = 0;
T_Min++; if(T_Min == 60) { T_Min = 0; T_Hour++; if(T_Hour == 24) { T_Hour = 0; }
} } } if(c_count == 20) // Alarm clock timing 1s { c_count = 0; C_Sec++; if(C_Sec == 60) { C_Sec
= 0; C_Min++; if(C_Min == 60) { C_Min = 0; C_Hour++; if(C_Hour == 24) { C_Hour =
0; } } } } } void Server_Timer1() interrupt 3 { TH1 = (65535-50000)/256;//50ms
TL1= (65535-50000)%256; t_count1++; if(t_count1 == 20) //1s { t_count1 = 0;
t_flag= ~t_flag; } }
Technology