//------------------------------------------------------------------------------------ // Blinky.c //------------------------------------------------------------------------------------ // Copyright (C) 2004 Silicon Laboratories, Inc. // // AUTH: BW // DATE: 4 SEP 01 // // This program flashes the green LED on the C8051F020 target board about five times // a second using the interrupt handler for Timer3. // Target: C8051F02x // // Tool chain: KEIL Eval 'c' // //------------------------------------------------------------------------------------ // Includes //------------------------------------------------------------------------------------ #include // SFR declarations //------------------------------------------------------------------------------------ // 16-bit SFR Definitions for 'F02x //------------------------------------------------------------------------------------ sfr16 DP = 0x82; // data pointer sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 T2 = 0xcc; // Timer2 sfr16 RCAP4 = 0xe4; // Timer4 capture/reload sfr16 T4 = 0xf4; // Timer4 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd5; // DAC1 data //------------------------------------------------------------------------------------ // Global CONSTANTS //------------------------------------------------------------------------------------ #define SYSCLK 2000000 // approximate SYSCLK frequency in Hz sbit LED = P1^6; // green LED: '1' = ON; '0' = OFF //------------------------------------------------------------------------------------ // Function PROTOTYPES //------------------------------------------------------------------------------------ void PORT_Init (void); void Timer3_Init (int counts); void Timer3_ISR (void); //------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void main (void) { // disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; PORT_Init (); Timer3_Init (SYSCLK / 12 / 10); // Init Timer3 to generate interrupts // at a 10Hz rate. EA = 1; // enable global interrupts while (1) { // spin forever } } //------------------------------------------------------------------------------------ // PORT_Init //------------------------------------------------------------------------------------ // // Configure the Crossbar and GPIO ports // void PORT_Init (void) { XBR2 = 0x40; // Enable crossbar and weak pull-ups P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output } //------------------------------------------------------------------------------------ // Timer3_Init //------------------------------------------------------------------------------------ // // Configure Timer3 to auto-reload and generate an interrupt at interval // specified by using SYSCLK/12 as its time base. // void Timer3_Init (int counts) { TMR3CN = 0x00; // Stop Timer3; Clear TF3; // use SYSCLK/12 as timebase TMR3RL = -counts; // Init reload values TMR3 = 0xffff; // set to reload immediately EIE2 |= 0x01; // enable Timer3 interrupts TMR3CN |= 0x04; // start Timer3 } //------------------------------------------------------------------------------------ // Interrupt Service Routines //------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------ // Timer3_ISR //------------------------------------------------------------------------------------ // This routine changes the state of the LED whenever Timer3 overflows. // void Timer3_ISR (void) interrupt 14 { TMR3CN &= ~(0x80); // clear TF3 LED = ~LED; // change state of LED }