/****************************************************************************** humanoid project RC servo control program interrupt_1.c move LED light using timer0 interrupt Author : Takashi Tomiyama ( yij01260@nifty.com ) Target : PIC16F876A Clock : 20MHz Compiler : CSS-PCM ******************************************************************************/ /* peripheral setting RB0-7 --- LED */ // history // 2003-03-28 ver.0 #include <16f876a.h> #fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP // constant definition ------------------------------------------------------- // how derive count setting 1ms interval of timer0 // 0xff - (interval time) / (cpu clock * 4) / (priscaler) // = 0xff - 1ms / (50ns * 4) / 64 // = 0xff - 1000000ns / 200ns / 64 // = 0xff - 5000 / 64 // = 0xff - 0x4e (78=0x4e) // = 0xb1 #define TIMER0_CNT 0xb1 #define LED_CONTROL_PERIOD_MS 100 // LED control period 100ms // global variables ----------------------------------------------------------- unsigned long waitCountMs; unsigned int8 led = 0x01; // prototype declaration ------------------------------------------------------ void LED_Update(void); // ============================================================================ // interval timer (timer0 interrupt hander) // ============================================================================ #int_timer0 void IntervalTimer(void) { set_timer0( TIMER0_CNT ); // 1ms interval if( 0 < waitCountMs ) waitCountMs--; } // ============================================================================ // main function // ============================================================================ void main(void) { waitCountMs = LED_CONTROL_PERIOD_MS; // setup interrupt timer0 ----------------------------- setup_timer_0( RTCC_INTERNAL | RTCC_DIV_64 ); set_timer0( TIMER0_CNT ); // 1ms interval // enable interrupt enable_interrupts( INT_TIMER0 ); enable_interrupts( GLOBAL ); // endless loop --------------------------------------- while( 1 ){ if( 0 == waitCountMs ){ waitCountMs = LED_CONTROL_PERIOD_MS; LED_Update(); } } } // ---------------------------------------------------------------------------- // LED related function // ---------------------------------------------------------------------------- void LED_Update(void) { if( 0x80 <= led ) led = 0x01; else led = led << 1; output_b(led); }