?? f50x_blinky.c
字號:
//-----------------------------------------------------------------------------
// F500_Blinky.c
//-----------------------------------------------------------------------------
// Copyright (C) 2008 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the green LED on the C8051F500 target board about
// five times a second using the interrupt handler for Timer2.
//
//
// How To Test:
//
// 1) Download code to the 'F500 target board
// 2) Ensure that the P1.3 pins are shorted together on the J19 header
// 3) Run the program. If the LED flashes, the program is working
//
//
// Target: C8051F500 (Side A of a C8051F500-TB)
// Tool chain: Keil C51 8.0 / Keil EVAL C51
// Command Line: None
//
// Release 1.1 / 09 JUN 2008 (ADT)
// -Edited formatting
//
// Release 1.0 / 18 MAR 2008 (GP)
// -Initial Revision
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h> // Compiler declarations
#include <C8051F500_defs.h> // SFR declarations
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24000000/8 // SYSCLK frequency in Hz
SBIT (LED, SFR_P1, 3); // LED='1' means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void TIMER2_Init (U16 counts);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
SFRPAGE = ACTIVE_PAGE; // Configure for PCA0MD
PCA0MD &= ~0x40; // Disable the watchdog timer
OSCILLATOR_Init (); // Configure system clock
PORT_Init (); // Initialize crossbar
TIMER2_Init (SYSCLK / 12 / 10); // Init Timer2 to generate interrupts
// at a 10 Hz rate.
EA = 1; // Enable global interrupts
while (1); // Spin forever
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure the internal oscillator to maximum internal frequency / 8
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x84; // Set oscillator divide to /8
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure the Crossbar and GPIO ports.
//
// P1.3 - digital push-pull LED
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P1MDOUT |= 0x08; // Enable LED as a push-pull output
P1SKIP |= 0x08; // Skip the LED pin on the crossbar
XBR2 = 0x40; // Enable crossbar and weak pull-ups
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: U16 counts - number of system clock cycles between interrupts
// range is 0 to 65535
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/12 as its time base.
//
//-----------------------------------------------------------------------------
void TIMER2_Init (U16 counts)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK/12 as timebase
CKCON &= ~0x60; // Timer2 clocked based on T2XCLK;
TMR2RL = -counts; // Init reload values
TMR2 = 0xFFFF; // Set to reload immediately
ET2 = 1; // Enable Timer2 interrupts
TR2 = 1; // Start Timer2
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// This routine changes the state of the LED whenever Timer2 overflows.
//
//-----------------------------------------------------------------------------
INTERRUPT(Timer2_ISR, INTERRUPT_TIMER2)
{
TF2H = 0; // Clear Timer2 interrupt flag
LED = !LED; // Change state of LED
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -