PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 25, 2005 2:57 pm |
|
|
Here is sample code to generate a RTCC interrupt every 10 ms.
Code: | #include <16F628A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, ERRORS)
int8 interrupt_flag;
// The rtcc interrupt occurs when the rtcc rolls over
// from FF to 00.
//
// RTCC interrupt rate =
// Fosc / (4 * rtcc pre-scaler * rtcc pre-load)
//
// = 4 MHz / (4 * 256 * 39)
//
// = 100.16 Hz
//
// This gives us a timer tick approx. every 10 ms
// (9.98 ms actually).
#define RTCC_PRELOAD (256 - 39)
#int_rtcc
void rtcc_isr(void)
{
set_rtcc(RTCC_PRELOAD);
interrupt_flag = 1;
}
//===============================
void main()
{
// Setup the RTCC to increment at a rate of 3906.25 Hz.
// Then set the preload to 39 so that it will roll over
// after 39 clocks, which gives a 100 Hz interrupt rate.
setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
set_rtcc(RTCC_PRELOAD);
interrupt_flag = 0;
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(1)
{
// Create a 1 ms pulse each time the interrupt flag is set
// by the int_rtcc routine.
if(interrupt_flag == 1)
{
output_high(PIN_B1);
delay_ms(1);
output_low(PIN_B1);
interrupt_flag = 0; // Then clear the flag
}
}
} |
|
|