📄 f93x_blinky.c
字号:
//-----------------------------------------------------------------------------
// F93x_Blinky.c
//-----------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the yellow LED on the C8051F930 target board about
// five times a second using the interrupt handler for Timer2.
//
//
// How To Test:
//
// 1) Download code to the 'F93x target board
// 2) Ensure that pins 2 and 3 are shorted together on the J8 header
// 3) Run the program. If the Yellow LED flashes, the program is working
//
//
// Target: C8051F93x-C8051F92x
// Tool chain: Generic
// Command Line: None
//
// Release 1.0
// -Initial Revision (FB)
// -4 OCT 2007
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h> // compiler declarations
#include <C8051F930_defs.h> // SFR declarations
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
SBIT (RED_LED, SFR_P1, 5); // '0' means ON, '1' means OFF
SBIT (YELLOW_LED, SFR_P1, 6); // '0' means ON, '1' means OFF
SBIT (SW2, SFR_P0, 2); // SW2 == 0 means switch pressed
SBIT (SW3, SFR_P0, 3); // SW3 == 0 means switch pressed
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 20000000/8 // SYSCLK frequency in Hz
#define LED_ON 0
#define LED_OFF 1
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void);
void Timer2_Init (int counts);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init ();
Timer2_Init (SYSCLK / 12 / 10); // Init Timer2 to generate interrupts
// at a 10 Hz rate.
EA = 1; // Enable global interrupts
while (1) {} // Spin forever
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and ports pins.
//
// P0.2 digital open-drain Switch 2
// P0.3 digital open-drain Switch 3
// P1.5 digital push-pull Red LED
// P1.6 digital push-pull Yellow LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P0MDIN |= 0x0C; // P0.2, P0.3 are digital
P1MDIN |= 0x60; // P1.5, P1.6 are digital
P0MDOUT &= ~0x0C; // P0.2, P0.3 are open-drain
P1MDOUT |= 0x60; // P1.5, P1.6 are push-pull
P0 |= 0x0C; // Set P0.2, P0.3 latches to '1'
XBR2 = 0x40; // Enable crossbar and enable
// weak pull-ups
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/48 as its time base.
//
void Timer2_Init (int counts)
{
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
}
//-----------------------------------------------------------------------------
// 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
YELLOW_LED = !YELLOW_LED; // change state of LEDs
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -