1602 LCD perpetual calendar source program - with temperature, backlight, grayscale digital

Publisher:CuriousObserverLatest update time:2015-06-25 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//1602 LCD screen perpetual calendar clock source program --- add temperature, backlight, grayscale digital?
/****************************************************** *******************************************
Program name: 1602 LCD clock program      
Date of writing: June 8, 2012
Hardware support: LCD1602 LCD screen STC12C2052 external 12MHZ crystal oscillator
Note: Use 2402 LCD screen program, fully compatible with 1602 LCD screen program
           
2011-11-18 23:36 Implement NPN13001 transistor to control backlight, and connect P3^7 to 10K current limiting resistor.
2011-11-19 10:10 Added function keys for adjusting year, month, day and time
2011-11-20 08:25 Added alarm function
2011-11-20 18:00 Added PWM dimming function
2011-11-21 22:00 Implementing 4-bit bus_2-line display
2011-11-23 20:33 Add temperature display ds18b02 without pull-up resistor
2011-11-26 13:40 Experiment: Use one button to realize long and short switches instead of two buttons
2011-12-11 14:50 Add positive and negative signs to judge the decimal point to display temperature, which is more accurate
2011-12-18 23:36 Implementing NPN13001 transistor to control grayscale, P3^5 connected to 10K current limiting resistor
Prepare to add light-controlled backlight LED light measurement principle to apply when there is light but no backlight, and when there is dark external light but there is backlight.
/****************************************************** *******************************************/
#include //header file
#include 
/****************************************************** *******************************************/
typedef unsigned char uint8; // unsigned 8-bit integer variable //
#define uchar unsigned char
#define uint unsigned int
/****************************************************** *******************************************
// Pin definition // (users can change according to actual situation)
/****************************************************** *******************************************/
sbit key1=P3^0; //Menu
sbit key2=P3^1; //Increase
unsigned char data MENU = 0; //Default menu = 0 Normal display clock
/****************************************************** ******************************************/

/******************************************18b20**** ***************************************/
sbit DQ=P1^0; //temperature input port                        
//uchar temp; //8-bit temperature value variable
uint temp; //temperature value variable 16 bits
uchar s; //negative number flag s=1
//**************Delay b*******************
void delay(uchar b)
{
     while (b--)
     {
         _nop_(); _nop_();
     }
}
//**************initialization************************
void init_ds18b20(void)
{
uchar n;
DQ=1;
delay(60); // 15-60us
DQ=0;
delay(240);delay(240); //480-960us
DQ=1;
delay(60);
n=DQ; //Collect data n=1 Whether there is a device
delay(60);

}
//**************Write 1 byte of data*lowest bit first*************************************
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
delay(1);
DQ=dat&0x01;
delay(60);
DQ=1;
dat>>=1;
}
delay(60);
}
//***************Read 1 byte of data*************************************
uchar read_byte(void)
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
delay(1);
if(DQ)
value|=0x80;
delay(60);

}
return value;
}
//******************Read temperature value*******************************
uchar readtemperature(void)
{
        uchar a,b;
        float tt; //Floating point variables have decimal calculations
        
        init_ds18b20(); //initialization
        write_byte(0xcc); //Write instruction skips ROM
        write_byte(0x44); //Write command to start temperature measurement
        delay(300); //delay
        
        init_ds18b20(); //initialization
        write_byte(0xcc); //Write instruction skips ROM
        write_byte(0xbe); //Write instruction to read temperature data
        
        a=read_byte(); //Read the lower 8 bits
        b=read_byte(); //Read the upper 8 bits
//Judge positive and negative temperature: If it is negative, take the complement code
        if(b>127) // When the 8th bit of the high 8 bits is 1, it is a negative value 1000 0000 0x80 128
        {
                a=~a+1; // When it is a negative value, the operation of the lower 8 bits
                if(~a>=0xff) // When the lower 8 bits are <-127 degrees, the upper 8 bits carry        
                        {
                                b=~b+1; // high 8-bit carry        
                        }
                        else // When it is not less than -127 degrees, the upper 8 bits are inverted
                        {
                                b=~b;
                        }        
                 s=1; //Negative value sign, you just need to display a “-” on the LCD.
        
        }        
        temp = b; //First assign the high eight bits of valid data to temp
        temp <<= 8; //Move the above 8-bit data from the lower 8 bits of temp to the higher 8 bits
        temp = temp|a; //Two bytes combined into an integer variable
        tt = temp*0.0625; //Get the true decimal temperature value because DS18B20 can be accurate to 0.0625 degrees //so the lowest bit of the read data represents 0.0625 degrees
        temp = tt*10+0.5; //Magnify ten times. The purpose of this is to convert the first decimal place into a displayable number and perform a rounding operation at the same time.
        return temp; //Return the temperature value. Each digit represents the decimal because it is magnified 10 times.

}
/******************************************18b20**** ***************************************/
/****************************************************** *******************************************/
#define LCM2402_DB0_DB7 P1 // Define the data bus of LCM2402
sbit LCM2402_RS = P3 ^ 2; // Define the RS control line of LCM2402
sbit LCM2402_RW = P3 ^ 3; // Define the RW control line of LCM2402
sbit LCM2402_E = P3 ^ 4; // Define the E control line of LCM2402
sbit LCM2402_Busy = P1 ^ 7; // Define the busy line of LCM2402 (associated with LCM2402_DB0_DB7)
data unsigned char TIME_DD,TIME_MO,TIME_YY,TIME_WW,TIME_HH,TIME_MM,TIME_SS; //Set the storage area for day, month, year, week, hour, minute, second and temperature
data unsigned char Clock_HH,Clock_MM,PWM; // alarm clock dimming
data bit DAY_BIT = 0; //Day increase flag (used to start date carry)
data unsigned char DIS_BIT = 0; //Switch display of various information
data unsigned char cou = 0; // Soft counter, accumulates 10ms time base signal to 1s
/****************************************************** *******************************************
// Define LCM2402 instruction set // (See the technical manual for details)
/****************************************************** *******************************************/
#define CMD_clear 0x01 // Clear the screen
#define CMD_back 0x02 // DDRAM back to zero
#define CMD_dec1 0x04 // After reading, AC (pointer) decreases by 1 and writes to the left
#define CMD_add1 0x06 // After reading, AC (pointer) adds 1 and writes to the right
#define CMD_dis_gb1 0x0f // Turn on display, turn on cursor, turn on cursor blinking
#define CMD_dis_gb2 0x0e // Turn on display, turn on cursor, turn off cursor blinking
#define CMD_dis_gb3 0x0c // Turn on display, turn off cursor, turn off cursor blinking
#define CMD_OFF_dis 0x08 // Turn off display, turn off cursor, turn off cursor blinking
#define CMD_set82 0x38 // 8-bit bus_2 lines display
#define CMD_set81 0x30 // 8-bit bus_1 line display (upper line)
#define CMD_set42 0x28 // 4-bit bus_2 lines display
#define CMD_set41 0x20 // 4-bit bus_1 line display (upper line)
#define lin_1 0x80 // 4-bit bus_1 line display (top line)
#define lin_2 0xc0 // 4-bit bus_1 line display (upper line)

/****************************************************** *******************************************
// Read LCM busy program [underlying protocol] // (all underlying protocols do not need to be paid attention to)
// LCM2402 is busy. If LCM2402 is busy, this function will wait until it is not busy. //
/****************************************************** *******************************************/
void LCM2402_TestBusy(void){
           LCM2402_DB0_DB7 = 0xff; //Device read status
           LCM2402_RS = 0;
           LCM2402_RW = 1;
           LCM2402_E = 1;
           while(LCM2402_Busy); //Wait for LCM to be unbusy
           LCM2402_E = 0; //
}
/****************************************************** *******************************************
//Write instruction program//
// Write command to LCM2402. This function requires an entry parameter of the instruction set. //
/****************************************************** *******************************************/
void LCM2402_WriteCMD(uint8 LCM2402_command) {
          LCM2402_TestBusy();
          LCM2402_RS = 0;
          LCM2402_RW = 0;
// LCM2402_DB0_DB7 = LCM2402_command; // 8-bit bus
// LCM2402_E = 1;
// LCM2402_E = 0;
          LCM2402_DB0_DB7 = LCM2402_command/16 << 4; // 4-bit bus high four bits
          LCM2402_E = 1;
          LCM2402_E = 0;
        LCM2402_DB0_DB7 = LCM2402_command%16 << 4; // 4-bit bus lower four bits
          LCM2402_E = 1;
          LCM2402_E = 0;

}
/****************************************************** *******************************************
//Write data program//
// Write data to LCM2402 //
/****************************************************** *******************************************/
void LCM2402_WriteData(uint8 LCM2402_data){
    LCM2402_TestBusy();
          LCM2402_RS = 1;
          LCM2402_RW = 0;
// LCM2402_DB0_DB7 = LCM2402_command; // 8-bit bus
// LCM2402_E = 1;
// LCM2402_E = 0;
        LCM2402_DB0_DB7 = LCM2402_data/16 << 4; // 4-bit bus high four bits
        LCM2402_E = 1;
          LCM2402_E = 0;
        LCM2402_DB0_DB7 = LCM2402_data%16 << 4; // 4-bit bus lower four bits
        LCM2402_E = 1;
          LCM2402_E = 0;

}
/****************************************************** *******************************************
// Print string program // (This function calls the pointer function)
// Send a string to LCM, the length is within 48 characters
// The first row position is 0x00~0x17, the second row position is 0x40~0x57
// Application example: print(0x80,"doyoung.net"); //Print the string doyoung.net from left to right at the first position in the first line
/****************************************************** *******************************************/
void print(uint8 a,uint8 *str){
        LCM2402_WriteCMD(a | 0x80);
        while(*str != ''){
                LCM2402_WriteData(*str++);
        }
        *str = 0;
}
/****************************************************** *******************************************
//Print single character program//
// The first row position is 0x00~0x17, the second row position is 0x40~0x57
// Send a character to LCM, expressed in hexadecimal (0x00)
// Application example: print(0xc0,0x30); //Print the character "0" at the first position of the second line
/****************************************************** *******************************************/
void print2(uint8 a,uint8 t){
                LCM2402_WriteCMD(a | 0x80);
                LCM2402_WriteData(t);
}
/****************************************************** *******************************************
//Define small Chinese characters//
// 8 self-defined characters can be written, and after writing, their CGRAM codes can be directly extracted and displayed.
// Please refer to the technical manual for character definition method
/****************************************************** *******************************************/
uint8 code Xword[]={
    0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, //℃, code 0x00
    0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00, //1, code 0x01
    0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00, //2, code 0x02
    0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00, //Three, code 0x03
    0x00,0x00,0xff,0xf5,0xfb,0xf1,0xff,0x00, //Four, code 0x04
    0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00, //Five, code 0x05
    0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00, //Six, code 0x06
    0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00, //Day, code 0x07
};
void CgramWrite(void) { // Load into CGRAM //
    uint8 i;
        LCM2402_WriteCMD(0x06); // CGRAM address automatically increases by 1
        LCM2402_WriteCMD(0x40); // Set CGRAM address to 00
    for(i=0;i<64;i++) {
            LCM2402_WriteData(Xword[i]); // Write data by array
    }
}
/****************************************************** *******************************************
// LCM2402 initialization // (user-defined, program lines with * must be retained but can be modified)
/****************************************************** *******************************************/
void LCM2402_Init(void){
// LCM2402_WriteCMD(CMD_set82); //* Display mode setting: display 2 lines, each character is 5*7 pixels 8-bit bus communication
          LCM2402_WriteCMD(CMD_set42); //* Display mode setting: display 2 lines, each character is 5*7 pixels, 4-bit bus communication
          LCM2402_WriteCMD(CMD_set42); //* Display mode setting: display 2 lines, each character is 5*7 pixels, 4-bit bus communication
  
          LCM2402_WriteCMD(CMD_clear); // Clear the screen
          LCM2402_WriteCMD(CMD_back); //* The data pointer points to the first character position of the first line
          LCM2402_WriteCMD(CMD_add1); // Display cursor movement settings: text does not move, cursor moves right
          LCM2402_WriteCMD(CMD_dis_gb3); //Display on and cursor settings: display on, cursor on, blink on
        CgramWrite(); // Write custom characters to CGRAM
}[page]
/****************************************************** *******************************************/
// The above is the LCM2402 driver //
/****************************************************** *********************************************/

/****************************************************** *********************************************/
bit IsLeapYear(void){ //Calculate whether this year is a leap year
        unsigned int a;
        a = 2000+TIME_YY; // add 2000 to represent a complete year
        if((a%4==0 && a%100!=0)||(a%400==0)){ //Calculation formula for leap year
                        return 1; // Return 1 if it is a leap year
                }
        else{
                        return 0; //If it is not a leap year, return 0
                }
}
/****************************************************** *********************************************/        
void month_day(void){  
        unsigned char mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
        TIME_DD++; //Day plus 1
        TIME_WW++; // Week value plus 1
        if(TIME_WW > 7){
                TIME_WW = 1; //Time limit
        }
        if(TIME_DD > mon_day[TIME_MO-1]){//Check if the day is greater than the maximum value of the month
                if(IsLeapYear()&&TIME_MO==2){ //Calculate whether this month is February of a leap year
                        TIME_DD = 29; //If it is a leap year and February, the day is 29
                        }
                else{
                        TIME_DD = 1; //Otherwise it is equal to 1
                        TIME_MO++; //month plus 1
                        if(TIME_MO > 12){
                                TIME_MO = 1; //If the month is greater than 12, the month is equal to 1
                                TIME_YY++; //year plus 1 (unlimited accumulation of Gregorian calendar years)
                        }
                }
        }
}
/****************************************************** *********************************************/        
void init (void){ //Power-on initialization
        TMOD = 0x11; // Timer/Counter 0, 1 works in mode 1   
    TH0 = 0x3c; // Preset to generate 50ms time base signal   
    TL0 = 0xb0;   
    EA = 1; // Enable general interrupt   
    ET0 = 1; // Timer/Counter 0 enables interrupt   
    TR0 = 1; // Open and close timer/counter 0   
////
        TIME_DD = 17; //The time is the value of the first use, and the value of the previous day will be automatically recorded in EEPROM
        TIME_MO = 11; // Initial time: Monday, November 17, 2011, 23:08:00
        TIME_YY = 11;
        TIME_WW = 4;
        TIME_HH = 1;
        TIME_MM = 0;
        TIME_SS = 0;
}
/****************************************************** *******************************************
//Display the project time part and display the time in the first line
*************************************************** *******************************************/    
void RealTime_Display(void)
{
            print(0x80,"20");
            print2(0x82,TIME_YY/10+0x30);
            print2(0x83,TIME_YY%10+0x30);
                print(0x84,"/"); // Display year
                //
            print2(0x85,TIME_MO/10+0x30);
            print2(0x86,TIME_MO%10+0x30);
                print(0x87,"/"); // Display month
                //
            print2(0x88,TIME_DD/10+0x30);
            print2(0x89,TIME_DD%10+0x30); // Display date

                print(0x8b,"["); // Display [
                print2(0x8c,TIME_WW%10); //week
                print(0x8d,"]"); // display]

            print2(0x40,TIME_HH/10+0x30);//hours
            print2(0x41,TIME_HH%10+0x30);
                print(0x42,":"); // Display the first font of cgram:"
                //
            print2(0x43,TIME_MM/10+0x30);//minutes
            print2(0x44,TIME_MM%10+0x30);
                print(0x45,"."); // Display the first font of cgram "."
                //
            print2(0x46,TIME_SS/10+0x30);//seconds
            print2(0x47,TIME_SS%10+0x30);
                //
}
/****************************************************** *********************************************/        
void delay_ms(unsigned int a){//- delay function 1MS/time        
        unsigned char i;
        while( --a != 0){                
                for(i = 0; i < 125; i++); //one; represents an empty statement, the CPU is idling.
        } // i increases from 0 to 125, the CPU takes about 1 millisecond
}


/****************************************************** *********************************************
/****************************************************** *********************************************
Function name: PWM initialization function
Call: PWM_init();
Parameters: None
Return value: None
Result: PCA is initialized to PWM mode with an initial duty cycle of 0
Note: If you need more PWM outputs, just plug them into CCAPnH and CCAPnL
/****************************************************** *********************************************/
void PWM_init (void){
CMOD=0x02; //Set PCA timer
        CL=0x00;
        CH=0x00;
        CCAPM0=0x42; //PWM0 sets PCA working mode to PWM mode (0100 0010)
        CCAP0L=0x00; //Set the initial value of PWM0 to be the same as CCAP0H
        CCAP0H=0x00; // PWM0 is initially 0

        CCAPM1=0x42; //PWM1 sets PCA working mode to PWM mode (delete when using //)
        CCAP1L=0x00; //Set the initial value of PWM1 to be the same as CCAP0H
        CCAP1H=0x00; // PWM1 is initially 0

        //CCAPM2=0x42; //PWM2 sets PCA working mode to PWM mode
        //CCAP2L=0x00; //Set the initial value of PWM2 to be the same as CCAP0H
        //CCAP2H=0x00; // PWM2 is initially 0

        //CCAPM3=0x42; //PWM3 sets PCA working mode to PWM mode
        //CCAP3L=0x00; //Set the initial value of PWM3 to be the same as CCAP0H
        //CCAP3H=0x00; // PWM3 is initially 0

        CR=1; //Start PCA timer
}
/****************************************************** *********************************************/

/****************************************************** *********************************************
Function name: PWM0 duty cycle setting function
Call: PWM0_set();
Parameter: 0x00~0xFF (0~255 can also be used)
Return value: None
Result: Set the PWM mode duty cycle, all high level when it is 0, all low level when it is 1
Note: If you need the setting function of PWM1, just change the 0 in CCAP0L and CCAP0H to 1.
/****************************************************** *********************************************/
void PWM0_set (unsigned char a){
        CCAP0L= a; //Set value to be written directly into CCAP0L
        CCAP0H= a; //Set value to be written directly into CCAP0H
}
/****************************************************** *********************************************
Function name: PWM1 duty cycle setting function
Call: PWM1_set();
Parameter: 0x00~0xFF (0~255 can also be used)
Return value: None
Result: Set the PWM mode duty cycle, all high level when it is 0, all low level when it is 1
Note: If you need the setting function of PWM1, just change the 0 in CCAP0L and CCAP0H to 1.
/****************************************************** *********************************************/
void PWM1_set (unsigned char a){
        CCAP1L= a; //Set the value to be written directly into CCAP1L
        CCAP1H= a; //Set value to be written directly into CCAP1H
}

/**********************Temperature display function************************************************************/
void WD_XS(void)
{
                                delay_ms(1000); //The temperature conversion time needs to be more than 750ms
                                temp=readtemperature();                                
                                if(s==1){print(0x48," -");}
                                print(0x48," ");
                                print2(0x4b,temp/100+0x30); // Hundreds digit and tens digit of actual temperature
                            print2(0x4c,temp%100/10+0x30); //Ten digits of actual temperature
                                print(0x4d,"."); //decimal point
                            print2(0x4e,temp%10+0x30); //actual temperature decimal place
                                print2(0x4f,0); // ℃           

}
/*****************************************Main function****************************************************/
void main (void)
{          
        delay_ms(1000); //delay 1S
        PWM_init(); //PWM initialization
        PWM=120;
        PWM0_set(PWM); //Set PWM0 duty cycle [0-255] backlight default value
        PWM1_set(PWM); //Set PWM1 duty cycle [0-255] Grayscale default value
    
        init(); // Initialize year, month, day, hour, minute, and second                          
        LCM2402_Init(); //LCM2402 initialization        
        
        MENU = 0 ;
        temp=readtemperature(); //Read temperature value
        
        while(1)
        { //Main thread//

                                
                RealTime_Display();            
                   if(DAY_BIT == 1)
                        { // Check if the day is updated, if so, calculate the Gregorian calendar
                                month_day(); //Calculate the Gregorian calendar date        
                                DAY_BIT = 0; //After calculation, set the date change flag to 0
                        }
                
            
                
        /*Adjust time and date************************************************************************************/
                if(MENU == 0)
                        {
                                
                                if(TIME_SS%10==1){ WD_XS(); } //Run the temperature display function once every 10 seconds [when the seconds digit = 1]
                                
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 1;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"menuY"); } // Enter menu 1
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                         PWM=PWM-10; //Backlight switch
                                                         if(PWM<0){PWM=255;} //                                                         
                                                         PWM0_set(PWM); //Set PWM duty cycle [0-255] NPN 13001 8050 255 is all off, 0 is all on.                                                         
                                                         print2(0x49,PWM/100+0x30); //
                                                         print2(0x4a,(PWM-PWM/100*100)/10+0x30);//
                                                         print2(0x4b,(PWM-PWM/100*100)%10+0x30);
                                                         print(0x4c,"*");
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }                 

                if(MENU == 1)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 2;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"menuM"); } // Enter menu 2
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_YY ++ ;
                                                                if(TIME_YY > 99){
                                                                TIME_YY = 0;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }                 
        
                if(MENU == 2)
                        {
                            print(0x4a,"menuM");
                                if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 3;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"menuD"); } // Enter menu 3
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_MO++;
                                                                if(TIME_MO > 12){
                                                                TIME_MO = 1;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }                 
        
                if(MENU == 3)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 4;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," "); print(0x4a,"menuW"); } // Enter menu 4
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_DD ++;
                                                                if(TIME_DD > 31){
                                                                TIME_DD = 1;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                                
                        }                 
        
                if(MENU == 4)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 5;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"menuH"); } // Enter menu 5
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_WW++;
                                                                if(TIME_WW > 7){
                                                                TIME_WW = 1;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }                 
        
                if(MENU == 5)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 6;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," "); print(0x4a,"menum"); } // Enter menu 6
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_HH++;
                                                                if(TIME_HH > 23){
                                                                TIME_HH = 0;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }                 
        
                if(MENU == 6)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 7;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"HuiDu");}// Enter menu 7
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        TIME_MM++; TIME_SS=0;
                                                                if(TIME_MM > 59){
                                                                TIME_MM = 0;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }
                if(MENU == 7) //Adjust grayscale contrast
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 8;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," "); print(0x4a,"--H--"); } // Entering menu 8 when the alarm sounds
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        PWM=PWM-10; //Grayscale switching
                                                         if(PWM<0){PWM=255;} //                                                         
                                                         PWM1_set(PWM); //Set PWM duty cycle [0-255] NPN 13001 8050 255 is all off, 0 is all on.                                                         
                                                         print2(0x49,PWM/100+0x30); //
                                                         print2(0x4a,(PWM-PWM/100*100)/10+0x30);//
                                                         print2(0x4b,(PWM-PWM/100*100)%10+0x30);
                                                         print(0x4c,"*");
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }
                if(MENU == 8)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 9;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," ");print(0x4a,"--m--"); } // Enter the menu 9 alarm minutes
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        Clock_HH++;
                                                                 print2(0x49,Clock_HH/10+0x30);//hour
                                                            print2(0x4a,Clock_HH%10+0x30);
                                                                print(0x4b,":"); // Display the first font of cgram:"
                                                                //
                                                            print2(0x4c,Clock_MM/10+0x30);//minutes
                                                            print2(0x4d,Clock_MM%10+0x30);
                                                                print(0x4e," ");
                                                                if(Clock_HH > 23){
                                                                Clock_HH = 0;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }
                if(MENU == 9)
                        {
                            if(key1 == 0 && key2 == 1)
                                        {                        
                                                delay_ms(200);//debounce
                                                if(key1 == 0 && key2 == 1)
                                                        {
                                                                MENU = 0;
                                                                while(key1 == 0 || key2 == 0){ print(0x48," "); } // Enter menu 0 and display normally
                                                        }
                                        }
                                if(key2 == 0 && key1 == 1){        
                                        delay_ms(200);//debounce
                                                if(key2 == 0 && key1 == 1){
                                                        Clock_MM++;
                                                                print2(0x49,Clock_HH/10+0x30);//hour
                                                            print2(0x4a,Clock_HH%10+0x30);
                                                                print(0x4b,":"); // Display the first font of cgram:"
                                                                //
                                                            print2(0x4c,Clock_MM/10+0x30);//minutes
                                                            print2(0x4d,Clock_MM%10+0x30);
                                                                print(0x4e," ");
                                                                if(Clock_MM > 59){
                                                                Clock_MM = 0;
                                                                }
                                                        
                                                        }
                                        }
                                        while(key1 == 0 || key2 == 0){ }
                        }
                                                                              
/*End of adjustment time and date*******************************************************************************/

                        if(Clock_MM==TIME_MM && Clock_HH==TIME_HH && Clock_MM > TIME_MM-1)
                                {   
                                        print(0x49,"clock!!"); //Alarm for 1 minute                                                
                                }
/*Alarm is judged to be set to display the clock!!********************************************************************/        
        
        } //while(1) loop ends
} //End of main loop
/****************************************************** *******************************************/    
void tiem0(void) interrupt 1{ // T/C0 interrupt service routine (generates 50ms time base signal)   
    cou++; // soft counter plus 1   
    if(cou > 19){ // Count value reaches 100(1s)   
        cou = 0; //soft counter clear   
        TIME_SS++; // Second counter plus 1 (carry 10ms*100=1s)   
        if(TIME_SS > 59){ // The second count reaches 60   
           
            TIME_SS = 0; // Clear the second counter   
            TIME_MM++; // minute counter plus 1 (carry 60s=1m)  
            if(TIME_MM > 59){ // count to 60   
                TIME_MM = 0; // Clear the minute counter   
                TIME_HH++; //Time counter plus 1 (carry 60m=1h)   
                if(TIME_HH > 23){ // Count to 23   
                    TIME_HH = 0; // Time counter cleared
                                        DAY_BIT = 1; //Day increase flag
                                }  
            }   
        }   
    }   
    TH0 = 0x3c; // reset the timing constant   
    TL0 = 0xb0;   
}
/****************************************************** *********************************************/
Reference address:1602 LCD perpetual calendar source program - with temperature, backlight, grayscale digital

Previous article:A non-traditional matrix keyboard scanning function
Next article:I2C communication module C language library file

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号