Advertisement
Guest User

Untitled

a guest
Oct 21st, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1.  
  2. /**
  3.  * Simple Real time clock example by Garth Klee.
  4.  * If run on PICDEM2+, time will be displayed
  5.  * on LCD and will incorporate LED pendulum.
  6.  * Has been written to run on PIC16F87x/A
  7.  */
  8.  
  9. #include <htc.h>
  10. #include <stdio.h>
  11. #include "lcd.h"
  12. #include "rtc.h"
  13.  
  14. __CONFIG(XT & WDTDIS & LVPDIS & DEBUGEN);
  15.  
  16. volatile near unsigned char tickCounter;
  17. volatile near unsigned char newSecond;
  18.  
  19. near unsigned char hours;
  20. near unsigned char minutes;
  21. near unsigned char seconds;
  22. near bit ampm;
  23.  
  24. // Pendulum pattern to display on LEDs (RB0:3)
  25. const unsigned char pattern[] = {1,2,4,8,4,2};
  26.  
  27. void init(void){
  28.     /***** Common Code ****
  29.      *  Timer 2 interrupt enabled.
  30.      */
  31.     PIE1    = 0b00000010;
  32.     /*
  33.      *  Peripheral interrupts enabled
  34.      *  Global interrupt disabled during initialization
  35.      */
  36.     INTCON  = 0b01000000;
  37.    
  38.     /***** Timer 2 Code ****
  39.      *  Prescale ratio set at 1:16
  40.      *  Timer2 module activated
  41.      *  Postscale ratio set at 1:10
  42.      */
  43.     T2CON   = 0b01001110;
  44.     /*
  45.      *  Period register set to 0xF9
  46.      */
  47.     PR2 = 0b11111001;
  48.  
  49. #if PENDULUM == ON
  50.     TRISB = 0;
  51. #endif
  52. #if ((TICKING == ON)||(ALARM == ON))
  53.     TRISC2=0;
  54. #endif
  55.    
  56.     ei();   // Global interrupts enabled
  57. }
  58.  
  59. void main(void){
  60.     init();
  61. #if SCREEN == ON
  62.     lcd_init();
  63. #endif
  64.  
  65.     newSecond = tickCounter = 0;
  66.  
  67.     // Initialise the current time
  68.     hours = START_H;
  69.     minutes = START_M;
  70.     seconds = START_S;
  71.     ampm = START_AP;
  72.  
  73.     // Measure time
  74.     while(1){
  75.         if(newSecond){
  76.             // A second has accumulated, count it
  77.             newSecond--;
  78.             if(++seconds > 59){
  79.                 seconds=0;
  80.                 if(++minutes > 59){
  81.                     minutes = 0;
  82.                     hours++;
  83.                     if(hours == 12)
  84.                         ampm^=1;
  85.                     if(hours>12)
  86.                         hours=1;
  87.                 }
  88.             }
  89.  
  90. #if SCREEN == ON
  91.             // print time on LCD screen
  92.             lcd_goto(0);
  93.             lcd_puts("Time=");
  94.             // Print hours
  95.             if(hours/10)
  96.                 lcd_putch('1');
  97.             else
  98.                 lcd_putch(' ');
  99.             lcd_putch((hours%10)+'0');
  100.             lcd_putch(':');
  101.             // print minutes
  102.             lcd_putch((minutes/10)+'0');
  103.             lcd_putch((minutes%10)+'0');
  104.             lcd_putch(':');
  105.             // print seconds
  106.             lcd_putch((seconds/10)+'0');
  107.             lcd_putch((seconds%10)+'0');
  108.             if(ampm)
  109.                 lcd_putch('P');
  110.             else
  111.                 lcd_putch('A');
  112.             lcd_putch('M');
  113. #endif
  114.  
  115. #if PENDULUM == ON
  116.             // Rotate LED pattern every second
  117.             PORTB=(pattern[seconds%sizeof(pattern)]);
  118. #endif
  119. #if TICKING == ON
  120.             RC2^=1; // tick effect
  121. #endif
  122.  
  123.         }
  124. #if ALARM == ON
  125.         // If time matches alarm setting, toggle RC2
  126.         if((hours == ALARM_H) &&
  127.                 (minutes == ALARM_M) &&
  128.                 (ampm == ALARM_AP)&&
  129.                 (seconds < ALARM_LENGTH)){
  130.             unsigned int tone;
  131.             RC2 ^= 1;   // generate buzz
  132.             tone=TONE2;
  133.             if(seconds&1)
  134.                 tone=TONE1;
  135.             while(tone--)continue;  // tone generation
  136.             RC2 ^= 1;   // generate buzz
  137.         }
  138. #endif
  139.     }
  140. }
  141.  
  142. void interrupt isr(void){
  143.         /***** Timer 2 Code *****/
  144.     if((TMR2IE)&&(TMR2IF)){
  145.         // Interrupt period is 40 mSec, 25 interrupts = 1 Sec
  146.         if(++tickCounter == 25){
  147.             tickCounter=0;
  148.             newSecond++;    // Notify a second has accumulated
  149.         }
  150.         TMR2IF=0;   // clear event flag
  151.     }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement