Thread: C Programming a board w/ LCD alarm clock

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    1

    Post C Programming a board w/ LCD alarm clock

    Hi, im a newbie at c programming, currently im trying to program a board using atmel 89c5131 microchip. Currently i managed to to do the clock but i cant seem to figure out how to make the alarm function. i have 5 buttons to use, button 1-5, them being P33, P10, P12, P13 and P14 respectively. Below is my current state of the program. Would appreciate some help and directions. I would also like the clock to be running at the back when i am setting the alarm. Thanks in advance!

    this is my code currently, sorry i do not how to post the code in the right format:
    Code:
    #include "reg52.h"
    #include <string.h>
     
    //--- LCD Control Register Declaration -------
    #define RS			P16	
    #define RW			P17		
    #define E			P11
    
    unsigned int calHr, calMin, calSec;
    unsigned int hr, min, sec;
    unsigned char msec = 0;
    
    //----- Delay Subroutine -------
    void delay( unsigned long duration)
    {
    	while ( ( duration -- )!= 0);
    }
    //--------- Strobe Signal for LCD -----
    void strobe()
    {
    	E = 1;
    	delay( 50 );
    	E = 0;
    	delay( 50 );
    }
    
    //----- LCD initialisation -----
    void LCD_init()
    {
    	unsigned char x;
    			
    	for ( x = 0 ; x < 3 ; x ++){
    		P0 = 0x30;
    		RS = 0;
    		RW = 0;
    		strobe();
    		delay( 750 );
    	}
    	
    	P0 = 0x38;
    	strobe();
    	P0 = 0x0c;
    	strobe();
    	P0 = 0x01;
    	strobe();
    	P0 = 0x02;
    	strobe();
    }
    
    //----- LCD Character Print routine --------
    void LCD_Print( unsigned char x)
    {
    	P0 = x;
    	RS = 1;
    	strobe();
    }
    
    //----- LCD Command ----------
    void LCD_Command( unsigned char x)
    {
    	P0 = x;
    	RS = 0;
    	strobe();
    }
    
    //------ LCD Line Print ---------
    void LCD_Print_Line( printData )
    unsigned char *printData;
    {
    	unsigned char x;
    
    	for( x = 0 ; x < strlen( printData ); x++)
    		LCD_Print( printData[ x ] );	
    }
    
    //-------CLOCK--------
    void displayClock(hr, min, sec)
    {	
    	unsigned int calHr;
    	unsigned int calMin;
    	unsigned int calSec;
    	
    	RS=1;
    	calHr = hr;
    	calMin = min;
    	calSec = sec;
    	
    	//display hour
    	P0=(calHr/10)+0x30;
    	strobe();
    	
    	P0=(calHr%10)+0x30;
    	strobe();
    				
    	P0=0x3A; //display
    	strobe();
    				
    	//display minutes
    	P0=(calMin/10)+0x30;
    	strobe();
    	
    	P0=(calMin%10)+0x30;
    	strobe();
    				
    	P0=0x3A; //display
    	strobe();
    				
    	//display seconds
    	P0=(calSec/10)+0x30;
    	strobe();
    
    	P0=(calSec%10)+0x30;
    	strobe();
    }
    
    
    void main()
    {		  
    	P26=0;						//off buzzer
    	E = 0;  
    	LCD_init();				//initialize the LCD 
    	LCD_Command( 0x01 ); // clear screen
    	
       
    	for(;;){
    		
    		//CLOCK
    		TR1=1;					//start time
    		while(TF1==0); 
    		if(TF1==1)
    		{
    			TF1=0;					//reset the timer flag
    			msec++;					//increment millisec by 1
    			TH1=0xD8;					//this its to set the 10ms time
    			TL1=0xF0;
    			if(msec==22)
    			{
    				sec++;				//increment sec by 1
    				msec=0;				//reset millisec back to 0
    	
    				if(sec==60)
    				{
    					min++;			//increment min by 1
    					sec=0;			//reset sec back to 0
    				
    					if(min==60)
    					{
    						hr++;			//increment hr by 1
    						min=0;		//reset min back to 0
    						
    						if(hr>12)
    						{
    							hr=1;		//reset back hour to 1
    						}
    					}
    				}
    			}
    		}
    		LCD_Command(0x85);
    		LCD_Print_Line("CLOCK:");
    		LCD_Command(0xC4);
    		displayClock(hr, min, sec);
    		
    		/*LCD_Command( 0x80 );
    		LCD_Print_Line("Hello Mr James.");
    		LCD_Command( 0xc0 );
    		LCD_Print_Line("Can I help you?");*/
    	
    	} 
    }
    Attached Files Attached Files
    Last edited by Salem; 07-25-2016 at 10:40 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sorry i do not how to post the code in the right format:
    Did you try to find out?
    http://cboard.cprogramming.com/c-pro...uncements.html

    > i have 5 buttons to use, button 1-5, them being P33, P10, P12, P13 and P14 respectively.
    You should move more code out of main and into functions.

    Your main should be
    Code:
    for ( ; ; ) {
      if(TF1==1) {
        updateHMS();
        resetTimer();
        if ( showClock ) {
          // flag is true so long as no other buttons are pressed
        }
      }
      if ( P33 == 1 ) {
        // do something with this button
      }
      // etc
    }
    The point is, you shouldn't prevent other code from running by locking up the processing by doing things like
    while(TF1==0);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I write an alarm clock program?
    By xeon321 in forum C Programming
    Replies: 3
    Last Post: 06-25-2012, 09:42 AM
  2. codes in making alarm clock in c++
    By elrafa712 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2011, 01:02 AM
  3. not able to work for alarm clock
    By ameyjah in forum C Programming
    Replies: 1
    Last Post: 10-08-2009, 08:51 PM
  4. IDEA: Alarm Clock
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-02-2002, 05:25 PM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 12:24 AM

Tags for this Thread