Hi,

Trying to write some C code for an assignment using a PIC16F84 for a digital watch/alarm.

RA0 to increment time
RA1 to decrement time
RA2 set-move to next digit
RB7 set time/alarm
RB6 enable/disable alarm

1. press RB7 once to set time.
2. press RA0 or RA1 repeatedly until hour is set.
3. press RA2 once to move cursor to minutes.
4. set minutes and seconds as above.
5. press RB7 to set alarm.
6. set alarm as time.
7. alarm is activated by pressing RB6 once and is deactivated by pressing it a second time.

This is what I have so far for the watch but how do I now fit the alarm function in ?

#include<io16f84.h>
#include"K:\New Folder\LCDdrive.h"

void del(void);			/*Function prototype*/
void write(unsigned char a, unsigned char b,unsigned int d);

#pragma vector=0x04		/*Set interupt vector*/

__interrupt void my_ISR(void);

unsigned char x, hr, min, sec, d;

void main(void)
{


unsigned char y=0,z=0;      
  /*Timer 0, Interrupt, PORTA and PORTB are initialised here*/
  
  RP0=1;			/*Select Bank 1*/
  TRISB=0xC0;			/*Make PORTB input*/
  TRISA=0x07;			/*Make PORTA input*/
  OPTION=0x07;			/*Configure Timer*/
  RP0=0;			/*Select Bank 0*/
  INTCON=0xa0;			/*Configure Timer0*/
  TMR0=0x00;			/*Start Timer*/
 
  LCD_initialise();
  while(1)			/*Endless loop*/
  {
                                       
    LCD_cursor(3,0);
    LCD_puts("HR");             /*Displays hour*/
    
    write(3,1,hr);
    /*LCD_cursor(3,1);
      LCD_display_value(hr);*/
    

 
    LCD_cursor(6,0);
    LCD_puts("MIN");            /*Displays minutes*/
    
    write(7,1,min);
    /*LCD_cursor(7,1);
      LCD_display_value(min);*/
    

    
     
    LCD_cursor(10,0);
    LCD_puts("SEC");            /*Displays seconds*/
   
    write(11,1,sec);
    /*LCD_cursor(11,1);
      LCD_display_value(sec);*/

    

    if (sec>=59)                /*Counts seconds*/
    {
     min=min+1;
     sec=0;                     /*Resets second counter*/
    }
    
    if (min==60)                /*Counts minutes*/
    {
     hr=hr+1;
     min=0;                     /*Resets minute counter*/
    }
    
    if (hr==13)                 /*Counts hours*/
    {
     
     hr=0;
     d=d+1;
    }
    
    if (d==0)                   
    {
     LCD_cursor(13,1);
     LCD_puts("am");            /*Displays am*/
    }
    else                        /*Changes am to pm*/
    {
     LCD_cursor(13,1);
     LCD_putch('p');            /*Displays pm*/     
    }
  ////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////  
    del();
    if ( RB7==1)                /*If RB7 pressed, set time*/
    { 
      del();
      del();
      
       
        sec=0;                  /*Resets seconds*/
        write(11,1,sec);        /*Writes the value of seconds*/
            do
              {
               if (y%2!=0)      /*if count is even*/
               {
                del();
                LCD_cursor(3,1);
                LCD_puts("  ");
                del();
                write(3,1,hr);  /*Writes the value of hours*/
                 /*
                  LCD_cursor(3,1);
                  LCD_display_value(hr);
                  */
                                
                
                    if (RA0==1)       /*Increase hour time value*/     
                    {
                      hr=hr+1;
                    }
                    
                    if (RA1==1)       /*Decrease hour time value*/
                    {
                      hr=hr-1;
                    }
                    /*==============================*/
                    if (hr>=13)       /*Resets to 0*/
                    { 
                    hr=0;
                    }
                    
                    
                }
                
                else 
                {
                LCD_cursor(7,1);
                LCD_puts("  ");
                del();
                write(7,1,min);   /*Writes the value of minutes*/
                 /*
                  LCD_cursor(7,1);
                  LCD_display_value(min);
                  */
                del();
                
                    if (RA0==1)   /*Increase minute time value*/   
                    {
                      min=min+1;
                    }
                    
                    if (RA1==1)   /*decrease minute time value*/
                    {
                      min=min-1;
                    }
                    
                    /*==============================*/
                    if (min>=60)  /*Resets to 0*/
                    {
                    min=0;
                    }
                }
                
                if (RA2==1)     /*setting time value moves from hours to minutes*/
                {
                  y=y+1;
                }
                
              } while (RB7==0); /*Exits when RB7 is pressed again and time is set*/
              
              y=0;
       
         
    }
  
 

   
   
 }
}
#include"K:\New Folder\LCDdrive.c"

void write(unsigned char a, unsigned char b,unsigned int d)

{
    
    
    LCD_cursor(a,b);
    LCD_display_value(d);
    
    /*
    LCD_cursor(3,1);
    LCD_display_value(hr);
    */
    
}


void del(void)			/*Delay Function*/
  {
   unsigned int i;		/*Clear TMR0*/
   for(i=0; i<32000;i++);	/*Wait for a while*/
  }
  
  /*Interrupt Service Routine. This Program will only run when Timer 0 overflows*/
  __interrupt void my_ISR(void)
  { 
    
    x=x+1;
    T0IF=0;			/*Clear Timer 0 flag*/
     
    if (x==13)                  /*1 second delay*/
    {   
     sec=sec+1;                 /*Counts seconds*/
     x=0;	
    }

    TMR0=0x00;		        /*Start the timer again*/
      
    }

Thanks for any replies.

Recommended Answers

All 3 Replies

Good project, good job. We need to know about the libraries used in the program to create an idea about alarm().

This must be an enjoyable project :)

I'm sure this was for an assignment in University of Huddersfield.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.