//------------------------------------------------------------------------------------ // Blinky.c //------------------------------------------------------------------------------------ // // This program flashes the green LED on the C8051F330 target board about // 5x a sec using the interrupt handler for Timer3. // // Target: C8051F330 // // (based on AN198) // // Date: 2009-02-04 // // Tool chain: SDCC 'c' // // Release 1.0 //------------------------------------------------------------------------------------ // Includes //------------------------------------------------------------------------------------ #include // SFR declarations //------------------------------------------------------------------------------------ // Global CONSTANTS //------------------------------------------------------------------------------------ #define SYSCLK 2000000 // approximate SYSCLK frequency in Hz sbit at 0x93 LED; // p1.3 F330 green LED: '1' = OFF; '0' = ON #define BLINK_RATE 5 // Min blink rate in Hz for Timer mode //------------------------------------------------------------------------------------ // Function PROTOTYPES //------------------------------------------------------------------------------------ char _sdcc_external_startup(void); void PORT_Init(void); void Timer3_Init(void); void Timer3_ISR(void) interrupt 14; //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- long Num_LED_Flashes = 0; // Simply counts the number of times // the LED blinks //------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void main (void) { PORT_Init(); Timer3_Init(); // Init Timer3 to generate interrupts EA = 1; // enable global interrupts while (1) { // spin forever } } //----------------------------------------------------------------------------- // SDCC special - stop the Watchdog timer prior to initialization // // !!! to work properly this requires prototype define (see above) //----------------------------------------------------------------------------- char _sdcc_external_startup(void) { // disable watchdog on F330 PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable) return 0; // do not skip variable initialization } //------------------------------------------------------------------------------------ // PORT_Init //------------------------------------------------------------------------------------ // // Configure the Crossbar and GPIO ports // void PORT_Init (void) { P1MDOUT = 0x08; // P1.3 is push-pull XBR1 = 0x40; // Enable Crossbar; pull-ups enabled } //----------------------------------------------------------------------------- // TIMER3_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure Timer3 to 16-bit auto-reload and generate an interrupt at // interval specified by using SYSCLK/12 as its time base. // // Initially sets Timer3 to overflow at the maximum blink rate. // The Timer3 ISR is used to toggle the LED. // //----------------------------------------------------------------------------- void Timer3_Init(void) { TMR3CN = 0x00; // Stop Timer3; Clear flags; // use SYSCLK/12 as timebase CKCON &= ~0xC0; // Timer3 clocked based on T3XCLK; TMR3RL = -(SYSCLK / 12 / BLINK_RATE); // Init reload values TMR3 = 0xffff; // Set to reload immediately EIE1 |= 0x80; // Enable Timer3 interrupts TMR3CN |= 0x04; // Start Timer3 } //------------------------------------------------------------------------------------ // Interrupt Service Routines //------------------------------------------------------------------------------------ //----------------------------------------------------------------------------- // TIMER3_ISR //----------------------------------------------------------------------------- // // This ISR is triggered upon a Timer3 overflow. The ISR simply toggles // the state of the LED and keeps track of the number of times the LED blinks. // //----------------------------------------------------------------------------- void Timer3_ISR(void) interrupt 14 { TMR3CN &= ~0x80; // Clear Timer3 Flags LED = !LED; Num_LED_Flashes++; }