📄 f52x_blinky.c
字号:
//-----------------------------------------------------------------------------
// F52x_Blinky.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// File Description:
//
// This file blinks the LED at a constant rate.
//
//
// How To Test:
//
// 1) Download code to either Side A or Side B of a C8051F530 target board
// 2) Ensure that the LED jumper is shorted; The jumper is located on
// HDR3 for the Side A LED and HDR4 for the Side B LED.
// 3) Run the code and if the LED blinks, the code works
//
//
// FID: 52X000005
// Target: C8051F52x/53x
// Command Line: None
//
// Release 1.0
// -Initial Revision (CG)
// -07 SEP 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <ioc8051f520.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR and SBIT definitions
//-----------------------------------------------------------------------------
__sfr __no_init volatile unsigned short TMR2R @ 0xCA; // Timer2 16-bit reload reg. definition
__sfr __no_init volatile unsigned short TMR2 @ 0xCC; // Timer2 16-bit register
#define LED P1_bit.BIT3 // LED definition
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
long COUNTER; // Counter used to measure time between
// toggles of the LED
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // System Clock in MHz
//-----------------------------------------------------------------------------
// Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void Timer2_Init (void);
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
void main ( void )
{
PCA0MD &= ~0x40; // Disable the watchdog timer
COUNTER = 0; // Initialize timer internal counter
OSCILLATOR_Init (); // Initialize the oscillator
PORT_Init (); // Initialize the crossbar and ports
Timer2_Init (); // Initialize Timer2
IE_bit.EA = 1; // Enable global interrupts
while (1) // Loop forever
{
if ( COUNTER == 0 ) // If the counter is zero reload it
{ // and toggle the LED
COUNTER = 500; // Initialize the counter to be
// decremented by timer2; time
// in miliseconds
LED = ~LED; // Toggle LED
}
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to (24.5 Mhz)
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
OSCICN = 0x87; // Configure internal oscillator for
// its highest frequency
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P1.3 digital push-pull LED1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P1MDOUT = 0x08; // Force LED pin to be push-pull
XBR1 = 0x40; // Enable crossbar, enable weak pull-ups
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
// Description : Function that initialized the timer2 to create a 1msec time
// base.
// Note: Uses the value defined in the SYSCLK. Enables Interrupt.
//-----------------------------------------------------------------------------
void Timer2_Init (void)
{
CKCON |= 0x10; // Timer2 uses the SYSCLK
TMR2R = -((SYSCLK+500L)/1000); // Time for 1msec @SYSCLK in both reload
TMR2 = TMR2R; // and timer register
TMR2CN = 0x04; // Timer2 in 16-bit reload timer mode
IE_bit.ET2 = 1; // Enable interrupt in timer2
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// Description : Timer2 Interrupt. Asserted every 1msec and decrements the
// the counter used to trigger an LED toggle.
//-----------------------------------------------------------------------------
#pragma vector = 0x2B
__interrupt void Timer2_ISR (void)
{
TMR2CN_bit.TF2H = 0; // Reset timer2 interrupt flag
if ( COUNTER ) { // Decrement counter if necessary
COUNTER--; }
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -