ToolStick Led Blink 프로그램은 다음과 같다.
프로그램코드는 제공되는 CD안에 들어있으며, 역시 인터넷으로 Silabs 홈페이지에서 검색하여 사용이 가능하다.
컴파일은 Keil C51을 사용하였다.
--- 프로그램코드 ---
컴파일과 링크 후 작동되는 모습의 동영상을 함께 올려본다.
프로그램코드는 제공되는 CD안에 들어있으며, 역시 인터넷으로 Silabs 홈페이지에서 검색하여 사용이 가능하다.
컴파일은 Keil C51을 사용하였다.
--- 프로그램코드 ---
//-----------------------------------------------------------------------------
// BLINK_LED.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: PB
// DATE: 20 JUN 05
//
// This program flashes the red and green LEDs (P0.2 & P0.3) on the tools demo
// board about 2.5 times a second using the interrupt handler for Timer2.
//
// Target: C8051F300 (Tools Demo Board)
//
// Tool chain: KEIL Eval 'c'
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f300.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
sbit REDLED = P0^2; // REDLED='1' means ON
sbit GRNLED = P0^3; // GRNLED='1' means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void Timer2_Init (void);
void Timer2_ISR (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
PCA0MD &= ~0x40; // WDTE = 0 (disable Watchdog Timer)
SYSCLK_Init (); // Initialize System Clock
PORT_Init (); // Initialize Crossbar and GPIO
REDLED = 1; // Turn REDLED ON
GRNLED = 0; // Turn GRNLED OFF
Timer2_Init (); // Init Timer2 to generate
// interrupts at a 5Hz rate.
EA = 1; // Enable global interrupts
while (1) { // Spin forever
}
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// 24.5MHz / 8 as its clock source. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
OSCICN = 0x04; // Set SYSCLK as Int Osc (24.5 MHz)/8
RSTSRC = 0x04; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.0 -
// P0.1 -
// P0.2 - <skipped> REDLED (push-pull)
// P0.3 - <skipped> GRNLED (push-pull)
// P0.4 -
// P0.5 -
// P0.6 -
// P0.7 - C2D
//
void PORT_Init (void)
{
XBR0 = 0x0C; // Skip P0.2 (REDLED) and P0.3 (GRNLED)
// in Crossbar pin assignments
XBR1 = 0x00; // No digital peripherals selected
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x0C; // Enable REDLED and GRNLED as
// push-pull outputs
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at some
// interval (reloads to TMR2RL after overflow) using SYSCLK/12 as its timebase.
//
// TMR2RL | Interrupt Frequency
// ----------------------------------
// 0x389E | 5Hz
// 0x9C4F | 10Hz
// 0xCE27 | 20Hz
// 0xEC0F | 50Hz
// 0xF607 | 100Hz
//
void Timer2_Init (void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// select SYSCLK/12 in T2XCLK
CKCON &= ~0x60; // Timer2 clocked based on T2XCLK;
TMR2RL = 0xCE27; // Reload to 0x389E after overflow
// 이 값을 고쳐서 점멸시간 간격 조절
TMR2 = TMR2RL; // Initialize Timer2 to reload value
ET2 = 1; // Enable Timer2 interrupts
TR2 = 1; // Start Timer2
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// This routine inverts the state of the LED pins whenever Timer2 overflows.
//
void Timer2_ISR (void) interrupt 5
{
static int T2_Overflow_Count = 0; // Counter for Timer2 interrupts
TF2H = 0; // Clear Timer2 interrupt flag
P0 ^= 0x0C; // Invert the state of the LEDs
T2_Overflow_Count++; // Increment counter
}
// BLINK_LED.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: PB
// DATE: 20 JUN 05
//
// This program flashes the red and green LEDs (P0.2 & P0.3) on the tools demo
// board about 2.5 times a second using the interrupt handler for Timer2.
//
// Target: C8051F300 (Tools Demo Board)
//
// Tool chain: KEIL Eval 'c'
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f300.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
sbit REDLED = P0^2; // REDLED='1' means ON
sbit GRNLED = P0^3; // GRNLED='1' means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void Timer2_Init (void);
void Timer2_ISR (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
PCA0MD &= ~0x40; // WDTE = 0 (disable Watchdog Timer)
SYSCLK_Init (); // Initialize System Clock
PORT_Init (); // Initialize Crossbar and GPIO
REDLED = 1; // Turn REDLED ON
GRNLED = 0; // Turn GRNLED OFF
Timer2_Init (); // Init Timer2 to generate
// interrupts at a 5Hz rate.
EA = 1; // Enable global interrupts
while (1) { // Spin forever
}
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// 24.5MHz / 8 as its clock source. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
OSCICN = 0x04; // Set SYSCLK as Int Osc (24.5 MHz)/8
RSTSRC = 0x04; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.0 -
// P0.1 -
// P0.2 - <skipped> REDLED (push-pull)
// P0.3 - <skipped> GRNLED (push-pull)
// P0.4 -
// P0.5 -
// P0.6 -
// P0.7 - C2D
//
void PORT_Init (void)
{
XBR0 = 0x0C; // Skip P0.2 (REDLED) and P0.3 (GRNLED)
// in Crossbar pin assignments
XBR1 = 0x00; // No digital peripherals selected
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x0C; // Enable REDLED and GRNLED as
// push-pull outputs
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at some
// interval (reloads to TMR2RL after overflow) using SYSCLK/12 as its timebase.
//
// TMR2RL | Interrupt Frequency
// ----------------------------------
// 0x389E | 5Hz
// 0x9C4F | 10Hz
// 0xCE27 | 20Hz
// 0xEC0F | 50Hz
// 0xF607 | 100Hz
//
void Timer2_Init (void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// select SYSCLK/12 in T2XCLK
CKCON &= ~0x60; // Timer2 clocked based on T2XCLK;
TMR2RL = 0xCE27; // Reload to 0x389E after overflow
// 이 값을 고쳐서 점멸시간 간격 조절
TMR2 = TMR2RL; // Initialize Timer2 to reload value
ET2 = 1; // Enable Timer2 interrupts
TR2 = 1; // Start Timer2
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// This routine inverts the state of the LED pins whenever Timer2 overflows.
//
void Timer2_ISR (void) interrupt 5
{
static int T2_Overflow_Count = 0; // Counter for Timer2 interrupts
TF2H = 0; // Clear Timer2 interrupt flag
P0 ^= 0x0C; // Invert the state of the LEDs
T2_Overflow_Count++; // Increment counter
}
컴파일과 링크 후 작동되는 모습의 동영상을 함께 올려본다.