blinky.c

来自「8051试验程序 基础教材」· C语言 代码 · 共 119 行

C
119
字号
//------------------------------------------------------------------------------------
// Blinky.c
//------------------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 10 OCT 01
//
// This program flashes the green LED on the C8051F2xx target board about five times
// a second using the interrupt handler for Timer2.
//
// Target: C8051F2xx
//
//

//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------

//TARGET MCU  : C8051F206, 'F220, 'F221, 'F226, 'F230, 'F231 and F236.
// Uncomment to include SFR declarations for desired derivative

#include <ioC8051F206.h>                    // SFR declarations
//#include <ioC8051F220.h>                    // SFR declarations
//#include <ioC8051F221.h>                    // SFR declarations
//#include <ioC8051F226.h>                    // SFR declarations
//#include <ioC8051F230.h>                    // SFR declarations
//#include <ioC8051F231.h>                    // SFR declarations
//#include <ioC8051F236.h>                    // SFR declarations

//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F2xx
//------------------------------------------------------------------------------------

__sfr __no_init volatile unsigned short DP       @ 0x82;                    // data pointer
__sfr __no_init volatile unsigned short ADC0     @ 0xbe;                    // ADC0 data
__sfr __no_init volatile unsigned short ADC0GT   @ 0xc4;                    // ADC0 greater than window
__sfr __no_init volatile unsigned short ADC0LT   @ 0xc6;                    // ADC0 less than window
__sfr __no_init volatile unsigned short RCAP2    @ 0xca;                    // Timer2 capture/reload
__sfr __no_init volatile unsigned short T2       @ 0xcc;                    // Timer2

//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------

#define SYSCLK 2000000                    // approximate SYSCLK frequency in Hz

#define LED P2_bit.P24                         // green LED: '1' = ON; '0' = OFF

//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_Init (void);
void Timer2_Init (int counts);
__interrupt void Timer2_ISR (void);

//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {

   // disable watchdog timer
   WDTCN = 0xde;
   WDTCN = 0xad;
	
   PORT_Init ();
   Timer2_Init (SYSCLK / 12 / 10);        // Init Timer2 to generate interrupts
                                          // at a 10Hz rate.

   IE_bit.EA = 1;											// enable global interrupts

   while (1) {                            // spin forever
   }
}

//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure port mux and GPIO ports
//
void PORT_Init (void)
{
   PRT2CF |= 0x10;                        // enable P2.4 (LED) as push-pull output
}

//------------------------------------------------------------------------------------
// Timer2_Init
//------------------------------------------------------------------------------------
//
// Configure Timer2 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
void Timer2_Init (int counts)
{
   T2CON  = 0x00;                         // Stop Timer2; configure for auto-reload
   CKCON &= ~0x20;                        // T2M=0 (use SYSCLK/12 as timebase)
   RCAP2  = -counts;                      // Init reload value
   T2     = 0xffff;                       // set to reload immediately
   IE_bit.ET2    = 1;                            // enable Timer2 interrupts
   T2CON_bit.TR2    = 1;                            // start Timer2
}

//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
// Timer2_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer2 overflows.
//
#pragma vector=0x2B //interrupt 5
__interrupt void Timer2_ISR (void)
{
   T2CON_bit.TF2 = 0;                               // clear Timer2 overflow interrupt flag
   LED = ~LED;                            // change state of LED
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?