For mid-range PIC, there's only one interrupt vector locates at 0x04 of the program memory. Using the interrupt service routine (ISR), it is very fast and no need to do it in the main program loop.
In this example, I use the ISR to increase the value of PortD at every 325 milli seconds.
C source code:
#include "xc.h"
// PIC16F887 Configuration Bit Settings
// CONFIG1
#pragma config FOSC = XT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config IESO = ON
#pragma config FCMEN = ON
#pragma config LVP = OFF
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
unsigned char cnt_0=0;
unsigned char cnt_1=0;
void main(){
PORTD=0x00;
TRISD=0x00;
T0CS=0;
PSA=0;
T0IE=1;
GIE=1;
T0IF=0;
TMR0=0;
while(1){
PORTD=cnt_1;
}
}
void interrupt _ISR(void){
if(T0IF){
cnt_0+=1;
T0IF=0;
}
/*325 milli seconds*/
if(cnt_0>=5){
cnt_0=0;
cnt_1+=1;
}
}
Schematic diagram.
Schematic diagram while in simulating. PortD output the counting increment ticks by the timer 0 overflow interrupt. |
No comments:
Post a Comment