sp3ctrm5tr

PIC16F877A Timer 2 and Interrupt

Apr 6th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. /*---------------------------------------------/
  2. Name    : Mohd Syahri Fikri Bin Samusi
  3.  
  4. Pulse Generated using TIMER2
  5. (interrupt) and switch RB4 interrupt
  6.    
  7. Date modified : 02 April 2012
  8. /---------------------------------------------*/
  9. #include <htc.h>
  10. #include <stdio.h>
  11. #include <pic16f877a.h>
  12. #include <usart.c>
  13. #define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
  14.  
  15.  
  16. void init_rs232()
  17. {  
  18.     BRGH = 1;    
  19.     SPBRG = 129;    // set baud rate: Fosc=20Mhz, BR=9600
  20.     TX9  = 0;   // ninebits?1:0,,,8- or 9-bit transmission
  21.     SYNC = 0;   // asynchronous
  22.     SPEN = 1;   // enable serial port pins
  23.     TXEN = 1;   // enable the transmitter & (automatically)TXIF=1
  24.     CREN = 1;   // enable reception
  25.     SREN = 0;   // no effect
  26.     TXIE = 0;   // disable tx interrupts
  27.     RCIE = 0;   // disable rx interrupts
  28. }
  29.  
  30.  
  31. //--Interrupt Service function--
  32. interrupt ISR(void) // Interrupt function definition
  33. {
  34.     //Interrupt TIMER2
  35.     int a, b=1000;
  36.     if (TMR2IF) // If the TMR2 Overflow flag is 1 (ie. TMR2 to PR2 match occurred), do .....
  37.     {  
  38.         TMR2IE=0; // TMR2 Overflow Interrupt Disable
  39.             RB2=!RB2; // Blinking the LED RB2
  40.  
  41.         for (a=0; a<b; a++)  
  42.         {
  43.             PORTD=0;
  44.         }
  45.        
  46.         for (a=b; a>0; a--)  
  47.         {
  48.             PORTD=0x20;
  49.         }
  50.        
  51.         TMR2IF=0; // Reset the TMR2 Overflow interrupt flag
  52.         TMR2IE=1; // Re-enable TMR2 Overflow Interrupt
  53.     }
  54.    
  55.     //Interrupt RB4
  56.     int c;
  57.     if(RBIF) // if RB port change interrupt flag
  58.     {
  59.         RBIE = 0; // Disable RB port change interrupt
  60.         if(RB4)
  61.         {
  62.             c++;
  63.             init_rs232();
  64.             printf("Count up = %d \r\n", c);
  65.         }
  66.  
  67.         RBIF = 0; // Reset RB port change interrupt flag
  68.         RBIE = 1; // Re-enable RB port change interrupt    
  69.     }
  70.     GIE=1;
  71. }
  72.  
  73. //--Main Funtion--
  74. void main(void)
  75. {  
  76.     TRISC = 0x00; // Port C as outputs
  77.     TRISB = 0xF0; // Port B as outputs/inputs
  78.     TRISD = 0x00; // Port D as outputs
  79.     //init_rs232();
  80.     T2CON = 0b00001001;
  81.     /*  Timer 2 control register
  82.         TOUTPS3:TOUTPS0: Timer2 Output Postscale Select to 1:2
  83.         T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select to 4 */
  84.        
  85.     TMR2IE = 1; // Enable the TMR2 to PR2 match interrupt
  86.     RBIE = 1;   // Enable the RB port change interrupt
  87.     PR2 = 0xA0; // Timer0 Counter
  88.     GIE = 1;    // Global interrupt enable
  89.     TMR2IF = 0; // Clear TMR2 interrupt flag (no TMR2 to PR2 match occurred)
  90.     RBIF = 0;   // Clear RB port change interrupt flag
  91.     TMR2ON = 1; // Turn on Timer 2
  92.     PEIE = 1;   // Peripheral Interrupt Enable
  93.  
  94.     while (1); //Run Run Run!  
  95. }
  96. //End of Program
Advertisement
Add Comment
Please, Sign In to add comment