/* ***************************************************************************** * * File Name - intrupt.c * Function - Demonstrates a 1 mS interrupt by generating a 1Hz squarewave on * port P0 * * You will need: * (1) uFlash & CCS PIC C Compiler (PCM/PCW) * * Instructions: * (1) Compile & downloading to the uFlash * (2) Observe a 1 Hz square wave on pin P0 (if you have a uS-RL4 board you * can have a relay turning on and off with a very satisfying click!) * ***************************************************************************** */ #include void init_hardware(void); // function prototypes /* ******************************** main *************************************** */ void main(void) { init_hardware(); // start up the ISR while(true) // main can now implement other {} // functions, the square wave // on P0 will continue in the // background without further // intervention. } /* ************************** init_hardware ************************************ * * Setup the timer and enable the interrupt */ void init_hardware(void) { set_rtcc(0); setup_counters(RTCC_INTERNAL,RTCC_DIV_32); enable_interrupts(RTCC_ZERO); enable_interrupts(GLOBAL); } /* **************************** timer_isr *************************************** * * Once set up this function is run automatically every 1mS * As a demonstration we will use the 1mS interrupt to generate a 1Hz * squarewave on P0 */ #INT_RTCC void timer_isr() { static WORD tick_count=1000; // a 1 mS tick counter (init to 1000) set_rtcc(0-156); // timer reload value for 1ms switch(tick_count) // depending on the tick count { case 0: // 1mS count has reached 0 (1 second) tick_count=1000; // reset the tick counter output_low(P0); // set P0 low break; case 500: // 1 mS count has reached 500 (1/2 second) output_high(P0); // set P0 high break; } // do anything else you want to happen every 1 mS here tick_count--; // decrement the tick count }