Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*---------------------------------------------/
- Name : Mohd Syahri Fikri Bin Samusi
- Pulse Generated using TIMER2
- (interrupt) and switch RB4 interrupt
- Date modified : 02 April 2012
- /---------------------------------------------*/
- #include <htc.h>
- #include <stdio.h>
- #include <pic16f877a.h>
- #include <usart.c>
- #define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
- void init_rs232()
- {
- BRGH = 1;
- SPBRG = 129; // set baud rate: Fosc=20Mhz, BR=9600
- TX9 = 0; // ninebits?1:0,,,8- or 9-bit transmission
- SYNC = 0; // asynchronous
- SPEN = 1; // enable serial port pins
- TXEN = 1; // enable the transmitter & (automatically)TXIF=1
- CREN = 1; // enable reception
- SREN = 0; // no effect
- TXIE = 0; // disable tx interrupts
- RCIE = 0; // disable rx interrupts
- }
- //--Interrupt Service function--
- interrupt ISR(void) // Interrupt function definition
- {
- //Interrupt TIMER2
- int a, b=1000;
- if (TMR2IF) // If the TMR2 Overflow flag is 1 (ie. TMR2 to PR2 match occurred), do .....
- {
- TMR2IE=0; // TMR2 Overflow Interrupt Disable
- RB2=!RB2; // Blinking the LED RB2
- for (a=0; a<b; a++)
- {
- PORTD=0;
- }
- for (a=b; a>0; a--)
- {
- PORTD=0x20;
- }
- TMR2IF=0; // Reset the TMR2 Overflow interrupt flag
- TMR2IE=1; // Re-enable TMR2 Overflow Interrupt
- }
- //Interrupt RB4
- int c;
- if(RBIF) // if RB port change interrupt flag
- {
- RBIE = 0; // Disable RB port change interrupt
- if(RB4)
- {
- c++;
- init_rs232();
- printf("Count up = %d \r\n", c);
- }
- RBIF = 0; // Reset RB port change interrupt flag
- RBIE = 1; // Re-enable RB port change interrupt
- }
- GIE=1;
- }
- //--Main Funtion--
- void main(void)
- {
- TRISC = 0x00; // Port C as outputs
- TRISB = 0xF0; // Port B as outputs/inputs
- TRISD = 0x00; // Port D as outputs
- //init_rs232();
- T2CON = 0b00001001;
- /* Timer 2 control register
- TOUTPS3:TOUTPS0: Timer2 Output Postscale Select to 1:2
- T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select to 4 */
- TMR2IE = 1; // Enable the TMR2 to PR2 match interrupt
- RBIE = 1; // Enable the RB port change interrupt
- PR2 = 0xA0; // Timer0 Counter
- GIE = 1; // Global interrupt enable
- TMR2IF = 0; // Clear TMR2 interrupt flag (no TMR2 to PR2 match occurred)
- RBIF = 0; // Clear RB port change interrupt flag
- TMR2ON = 1; // Turn on Timer 2
- PEIE = 1; // Peripheral Interrupt Enable
- while (1); //Run Run Run!
- }
- //End of Program
Advertisement
Add Comment
Please, Sign In to add comment