1602 alarm program with customizable hours and minutes

Publisher:trendsetter10Latest update time:2012-10-11 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The program has been slightly modified on the original basis. The day of the week display has been removed, and the original hourly alarm has been changed to an alarm that can freely set hours + minutes. Other functions remain unchanged.

Function: time, hour and minute alarm setting, upper and lower temperature limit setting, temperature display.

The alarm can be set to the minute, and the ringing time is still 10 seconds.

Expandable functions: music ringing, alarm setting for any day of the week, just need to add or modify the program. The program is just simply organized, and many places are messy. Friends who need the program had better have good programming skills, otherwise it is difficult to understand the writing thinking of the program.


Simulation diagram Complete source code download: http://www.51hei.com/ziliao/file/1602_naoz.rar
Click to browse the next page

#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int


   sbit BEEP=P3^7; //// Define the buzzer port

   unsigned char L,H,Z,Y; //// Define high and low temperature variables


////////////////////////////////////////////////////// ///

 /**********Alarm sound**************/

 bi(uint t)
   {
    int c,n;
    for(c=0;c0;i--)
     {
       DQ = 0; //The read cycle starts when the high level is pulled to the low level
       dat>>=1;
       DQ = 1; // give pulse signal
       if(DQ)
       dat|=0x80;  
       delay_18B20(4);
     }
     return(dat);
   }

////////////////////////////////////////////////////

/*************ds18b20 writes a byte****************/

void WriteOneChar(unsigned char dat)
 {
   unsigned char i=0;
   for (i=8; i>0; i--)
   {
     DQ = 0; //When pulled from high level to low level, the write cycle starts
     DQ = dat&0x01; //The lowest bit of data is written first
     delay_18B20(5); //60us to 120us delay
     DQ = 1;        
     dat>>=1; //Input from the lowest bit to the highest bit
   }
 }


////////////////////////////////////////////////////// /////////

/**************Read the current temperature of ds18b20************/

void ReadTemperature(void)
 {
    unsigned char a=0,b=0,t=0;

    Init_DS18B20 ( );
    WriteOneChar ( 0xCC ); // Skip reading the serial number and column number
    WriteOneChar ( 0x44 ); // Start temperature conversion
    delay_18B20 ( 100 ); // this message is very important
    Init_DS18B20 ( );
    WriteOneChar ( 0xCC ); // Skip the operation of reading the serial number and column number
    WriteOneChar ( 0xBE ); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperature
    delay_18B20 ( 100 );
    a=ReadOneChar() ; //Read the low bit of the temperature value
    b=ReadOneChar() ; //Read the high bit of the temperature value

    temp1 = b<<4; //value of the last three digits in the upper 8 bits
    temp1+= ( a&0xf0 )>>4; //The high 4 bits in the low 8 bits plus the last three digits in the high 8 bits temp1 room temperature integer value
    temp2 = a&0x0f; //decimal value
    temp = ( ( b*256+a ) >>4 ); // Current collected temperature value divided by 16 to get the actual temperature value zhenshu    
    xs=temp2*0.0625*10; //decimal place, if it is 0.5, it will be counted as 5 to display the decimal of xs xiaoshu      
 }


////////////////////////////////////////////////////// ///////////////////

 


/*************************1602 LCD program************************/
 

/******Define LCD control port*******/
 
    sbit lcdrs=P2^5; //Define three control ports of LCD
    sbit lcdrw=P2^6;
    sbit lcden=P2^7;

    sbit K1=P3^0; //define three switches
 sbit K2=P3^1;
 sbit K3=P3^2;
 ////////////////////////////////////////////////////// /////////

/******Define variables*******/
  
uchar count,shi,fen,miao,xq,K1num; //Define the variables needed in the time program


////////////////////////////////////////////////////// /////////////////

/******Define array*******/

 // uchar code table [ ] = "H:38 L:25 00 Q:7"; //Contents displayed on the LCD interface
 //uchar code table1[ ]= "23:59:55 . C";

  uchar code table [ ] = "01:00:55 01-01"; //Contents displayed on the LCD interface
  uchar code table1[ ]= "H:38 L:25 . C";
////////////////////////////////////////////////////// ///////////

/******Delay program*******/
 
  void delay(uint m) //LCD delay program
     {
        uint i,j;
      for(i=m;i>0;i--)
     for(j=110;j>0;j--);
     }
//////////////////////////////////////////////////
//////////////////////////////////////////////////

/**********Write instruction*************/
  void write_com(uchar com)
      {
         lcdrs=0;
         lcdrw=0;
         lcden=0;
         P0=com;
         delay(5);
         lcden=1;
         delay(5);
         lcden=0;
      }

 ////////////////////////////////////////////////////// /////

/************Write data*************/

  void write_data(uchar date)
      {
         lcdrs=1;
         lcdrw=0;
         lcden=0;
         P0=date;
         delay(5);
         lcden=1;
         delay(5);
         lcden=0;
      }


////////////////////////////////////////////////////// ///////
////////////////////////////////////////////////////// //////

 /***************LCD display program***************/  
  
   /*********Separation of hours, minutes and seconds**********/
    
  void write_sfm(uchar add,uchar date)
      {
        uchar shi,ge; //Separate numbers, such as 35, into 3 and 5  
     shi=date/10;     
      ge=date%10;     
        write_com ( 0x80+add );    
        write_data ( 0x30+shi );       
     write_data ( 0x30+ge );       
      }

 ////////////////////////////////////////////////////// /////////////////

/**********Week***********/

   void write_xq(uchar add1,uchar date1)
       {
          uchar ge; //Only one number is needed for the week   
       ge=date1%10;    
          write_com ( 0x80+add1 );    
        write_data ( 0x30+ge );      
       }
  

  ////////////////////////////////////////////////////// ///////////////
 
 /************Separation of high and low temperature numbers************/

  void write_HL(uchar add2,uchar date2)
     {
         uchar shi,ge;   
      shi=date2/10;     
       ge=date2%10;      
         write_com(0x80+0x40+add2);     
      write_data( 0x30+shi );  
      write_data( 0x30+ge );   
     }
 
  ////////////////////////////////////////////////////// //////////////
  ////////////////////////////////////////////////////// ////////////////
 
 /***************Key timing program***************/
   
   void keycan()
    {
         /**********Function selection**********/
      if( K1==0 ) { K1num++; delay(1); if( K1==0 ) { while(!K1);bi(200);
 
   if( K1num==1 ) { TR0=0; write_com ( 0x80+1 ); write_com(0x0f); } //
   if( K1num==2 ) { write_com ( 0x80+4 ); } //points
   if( K1num==3 ) { write_com ( 0x80+7 ); } //Seconds
   //if( K1num==4 ) { write_com ( 0x80+11 ); } //Weekday
   if( K1num==4 ) { write_com ( 0x80+12 ); } //Alarm
   if( K1num==5 ) { write_com ( 0x80+15 ); }
   if( K1num==6 ) { write_com ( 0x80+0x40+3 ); } //Upper limit
   if( K1num==7 ) { write_com ( 0x80+0x40+8 ); } //Lower limit
   if( K1num==8 ) { K1num=0; write_com(0x0c); TR0=1; } //Exit settings
       }
      }

////////////////////////////////////////////////////// //////////////////////

         /************Function plus*********/
     
       if( K1num != 0 ) {
         if( K2==0 ) { delay(1);
         if( K2==0 ) { while(!K2);bi(200);

   if( K1num==1 ) { shi++; if( shi==24 ) shi=0; write_sfm ( 0,shi ); write_com ( 0x80+1 ); }      
         if( K1num==2 ) { fen++; if( fen==60 ) fen=0; write_sfm ( 3,fen ); write_com ( 0x80+4 ); }
      if( K1num==3 ) { miao++; if( miao==60 ) miao=0; write_sfm ( 6,miao); write_com ( 0x80+7 ); }
      //if( K1num==4 ) { xq++; if( xq==8 ) xq=1; write_xq (11,xq ); write_com ( 0x80+11); }
   if( K1num==4 ) { Z++; if( Z==24 ) Z=0; write_sfm (11,Z ); write_com ( 0x80+12); }
   if( K1num==5 ) { Y++; if( Y==60 ) Y=0; write_sfm (14,Y ); write_com ( 0x80+15); }
   if( K1num==6 ) { H++; if( H==100) H=0; write_HL ( 2,H ); write_com ( 0x80+0x40+3);}
   if( K1num==7 ) { L++; if( L==100) L=0; write_HL ( 7,L ); write_com ( 0x80+0x40+8);}
   
    }
      }
     }
  //////////////////////////////////////////////////
         /**********Function reduction************/
      if(K1num!=0) {
      if(K3==0 ) { delay(1);
      if(K3==0 ) { while(!K3); bi(200);

   if( K1num==1 ) { shi--; if( shi==-1 ) shi=23; write_sfm ( 0,shi ); write_com ( 0x80+1 ); }      
         if( K1num==2 ) { fen--; if( fen==-1 ) fen=59; write_sfm ( 3,fen ); write_com ( 0x80+4 ); }
      if( K1num==3 ) {miao--; if( miao==-1 ) miao=59; write_sfm ( 6,miao); write_com ( 0x80+7 ); }
     // if( K1num==4 ) { xq--; if( xq==0 ) xq=7; write_xq (11,xq ); write_com ( 0x80+11); }
         if( K1num==4 ) { Z--; if( Z==-1 ) Z=23; write_sfm (11,Z ); write_com ( 0x80+12); }
   if( K1num==5 ) { Y--; if( Y==-1 ) Y=59; write_sfm (14,Y ); write_com ( 0x80+15); }
   if( K1num==6 ) { H--; if( H==-1 ) H=99; write_HL ( 2,H ); write_com ( 0x80+0x40+3);}
   if( K1num==7 ) { L--; if( L==-1 ) L=99; write_HL ( 7,L ); write_com ( 0x80+0x40+8);}
   
    }
      }
     }
    }

////////////////////////////////////////////////////// /////////

/*************initialization**************/
 
  void init()
      {
        uchar num; //Define a variable num for initializing the following array//  
        lcden=0;
        write_com ( 0x38 );
        write_com ( 0x0c ); //Do not display the cursor  
        write_com ( 0x06 ); //The cursor automatically moves to the right  
        write_com ( 0x01 ); // clear screen  

        shi=1; fen=00; miao=55; //xq=7;
  H=38; L=25; Z=1; Y=1;
        write_com ( 0x80 );     
               for( num=0; num<16; num++ )
                  { write_data( table[num] ); delay(5); }

        write_com ( 0x80+0x40 );       
               for( num=0; num<16; num++ )
                  { write_data( table1[num] ); delay(5); }


 ////////////////////////////////////////////////////// /////////
/********Timer initialization*************/

          TMOD=0x01; //Define timer 0, working mode 1  
          TH0=(65536-50000)/256; //high 8 bits  
          TL0=(65536-50000)%256; //low 8 bits  
          EA=1; //Open global interrupt  
          ET0=1;    
          TR0=1; //Turn on the timer  
        }

 

////////////////////////////////////////////////////// ///////////////////

/***********Timer interrupt function************/
 
  void timer0() interrupt 1
   {
     TH0=(65536-50000)/256;
     TL0=(65536-50000)%256;
     count++;
   if( count==20 ) { count=0; miao++;
   if(miao==60) { miao=0; fen++; }  
   if( fen==60 ) { fen=0; shi++; }  
   if( shi==24 ) { shi=0; //xq++;
   //if( xq==8 ) { xq=1; }  
        }      
   }
  }

 


////////////////////////////////////////////////
////////////////////////////////////////////////////// ////
////////////////////////////////////////////////////// /

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


uchar code mytab[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};
/* Set the display position           
/******************************************/
void lcd_pos(uchar pos)
{                          
  write_com(pos | 0x80); //data pointer = 80 + address variable
}

  ////////////////////////////////////////////////
/*Write custom characters into CGRAM   
/************************************/
void writetab()  
{  
    unsigned char i ;
    write_com(0x40); //Write CGRAM
    for (i = 0 ; i < 8 ; i++)       
    write_data(mytab[ i ]) ;        
}

 

////////////////////////////////////////////////////// ////////////////////
////////////////////////////////////////////////////// //////////////////////
////////////////////////////////////////////////////// //////////////////////

void wendu() /////Temperature display program/////   
 {
     
        writetab(); //Write custom characters into CGRAM
        delay ( 5 ) ;
        lcd_pos ( 0x4e ) ; //display circle
        write_data( 0x00 ) ; //Display custom characters
        write_com ( 0x0c );
        ReadTemperature();    
         
        write_com ( 0x80+0x40+10 );
        write_data ( 0x30+temp/10 );

        write_com ( 0x80+0x40+11 );
        write_data ( 0x30+temp%10 );
  
        write_com ( 0x80+0x40+13 );
        write_data ( 0x30+xs%10 );


         /////Time display address/////  
        
        
     
        write_sfm( 0,shi );
  write_sfm( 3,fen );
  write_sfm( 6,miao );
 // write_xq (11,xq );
  }


////////////////////////////////////////////////////// /////////////
////////////////////////////////////////////////////// /////////////
////////////////////////////////////////////////////// /////////////

   //////Temperature alarm program//////
  void baojing()
   {  
     if(H==temp)
    {
      bi(500);
       }
      if(L==temp)
    {
      bi(500);
       }
    if((Z==shi)&(Y==fen)&(miao<10)) //Set the alarm to sound for 10 seconds.
    {   
      
     {  
    bi(500);
    }
    }
   }


 ////////////////////////////////////////////////////// /////////////
////////////////////////////////////////////////////// /////////////
////////////////////////////////////////////////////// /////////////
 /***************Main function***************/
   void main()
     {
        init();
        while(1)  
        {
  keycan();
        wendu();
  baojing();
     }
    }

Reference address:1602 alarm program with customizable hours and minutes

Previous article:ds18b20 temperature + digital tube display
Next article:51 MCU Series: Flashing Light

Recommended ReadingLatest update time:2024-11-16 23:35

Single chip microcomputer learning - LCD1602 display experiment one
Title: Displayed on the LCD1602 display    "I LIKE MCU!        XIAO WO ”    And the display method is to dynamically move from the right screen to the left in sequence. Solution: (C language programming) #include reg52.h #define uchar unsigned char #define uint unsigned int uchar code table ="I LIKE MCU!";
[Microcontroller]
18B20 temperature 1602 display
// Crystal oscillator 12mhz #include reg51.h sbit RS=P2^0; // RS data and command sbit RW=P2^1; // read and write sbit EN=P2^2; // read enabled sbit busy=P0^7;  sbit DQ=P3^4; #define uchar unsigned char #define uint unsigned int uchar temp_value;  unsigned char show1 ="18B20"; unsigned char show2 ="t
[Microcontroller]
18B20 temperature 1602 display
Realization of LCD1602 Liquid Crystal Driver Based on 51 Single Chip Microcomputer
The main contents of this article are as follows: 1. Basic introduction of LCD1602 2. Implementation of LCD1602 liquid crystal driver     According to the analysis of the existing LCD1602 liquid crystal driver, I always feel that it is a bit messy and troublesome. To put it bluntly, it is not easy to use. Here I pla
[Microcontroller]
Detailed explanation of 51 microcontroller driver LCD1602 program
  Detailed explanation of 51 single chip LCD1602 program   LCD1602   Industrial character LCD. 1602 means that the content displayed by the LCD is 16X2, that is, it can display two lines of 16 characters each.      Special interface description   RS: register selection input   RS=1: Point to data register   RS=0: p
[Microcontroller]
Detailed explanation of 51 microcontroller driver LCD1602 program
1602 Character LCD Display
        In daily life, we are familiar with LCDs. LCD modules have been used as devices in many electronic products, such as calculators, multimeters, electronic watches and many household electronic products. They mainly display numbers, special symbols and graphics. In the human-computer interaction interface of the
[Microcontroller]
1602 Character LCD Display
51 single chip GPS + sim800c GSM positioning SMS LCD1602 LCD display program
sim800c+GPS chip is a small design for practice, a little ugly. Attached is the program and the principle PCB diagram. It is   my first post, please forgive me if it is not good. Button function: one button alarms, buzzer sounds, another button sends a text message to the specified mobile phone, the mobile phone num
[Microcontroller]
51 single chip GPS + sim800c GSM positioning SMS LCD1602 LCD display program
[C51 code] Menu function [for LCD1602]
#ifndef __menu_h__ #define __menu_h__ #include "head.h" #include "LCD1602_2.h" #include "DS18B20.h" #define Null 0 /*********************** * Function declaration * ***********************/ void ShowMenu(void); void Menu_Change(volatile KeyNum); /*********************** * Key function key macro defi
[Microcontroller]
LCD1602 dynamic display
The schematic diagram of the simulation element is as follows: /*------------------------------------- Function: LCD1602 local dynamic display Description: Display count at a fixed position on the second line of the screen: Then the number of key presses will be displayed dynamically (00 ~ 99) Author: Zhang Kaizhou D
[Microcontroller]
LCD1602 dynamic 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号