📄 f35x_timer0_two_8bittimers.c
字号:
//-----------------------------------------------------------------------------
// F35x_Timer0_two_8bitTimers.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program presents an example of use of the Timer0 of the C8051F35x's in
// two 8-bit reload counter/timer mode. It uses the 'F350DK as HW platform.
//
// It sets the two timers to interrupt every millisecond.
//
// When the timer low interrupts a counter (low_counter) increments and upon
// reaching the value defined by LED1_TOGGLE_RATE toggles LED1
// When the timer high interrupts a counter (high_counter) increments and upon
// reaching the value defined by LED2_TOGGLE_RATE toggles LED2
//
// Pinout:
//
// P0.6 -> LED1 (toggled by TL0)
// P0.7 -> LED2 (toggled by TH0)
//
// all other port pins unused
//
// How To Test:
//
// 1) Open the F35x_Timer0_two_8bitTimers.c file in the Silicon Labs IDE.
// 2) If desired, change the number of interrupts (ms) to toggle an output:
// LED1_TOGGLE_RATE
// LED2_TOGGLE_RATE
// 3) Compile and download the code.
// 4) Verify the LED pins of J3 are populated on the 'F35x TB.
// 5) Run the code.
// 6) Verify that the LEDs blink.
//
//
// FID: 35X000023
// Target: C8051F35x
// Tool chain: KEIL C51 7.20 / KEIL EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (CG)
// -14 DEC 2005
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F350.h> // SFR declarations
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 24500000/8 // SYSCLK in Hz (24.5 MHz internal
// oscillator / 8)
// the internal oscillator has a
// tolerance of +/- 2%
#define TIMER_PRESCALER 48 // Based on Timer CKCON settings
// There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
// SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.
#define TIMER_TICKS_PER_MS SYSCLK/TIMER_PRESCALER/1000
// Note: TIMER_TICKS_PER_MS should not exceed 255 (0xFF) for the 8-bit timers
#define AUX1 TIMER_TICKS_PER_MS
#define AUX2 -AUX1
#define LED1_TOGGLE_RATE 100 // LED1 toggle rate in milliseconds
// if LED1_TOGGLE_RATE = 1, LED1 will
// be on for 1 millisecond and off for
// 1 millisecond
#define LED2_TOGGLE_RATE 30 // LED2 toggle rate in milliseconds
// if LED2_TOGGLE_RATE = 1, LED2 will
// be on for 1 millisecond and off for
// 1 millisecond
#define TIMER0_RELOAD_HIGH AUX2 // Reload value for Timer0 high byte
#define TIMER0_RELOAD_LOW AUX2 // Reload value for Timer0 low byte
sbit LED1 = P0^6; // LED1 ='1' means ON
sbit LED2 = P0^7; // LED2 to be used also by Timer0
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void Port_Init (void); // Port initialization routine
void Timer0_Init (void); // Timer0 initialization routine
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // Clear watchdog timer enable
Timer0_Init (); // Initialize the Timer0
Port_Init (); // Init Ports
EA = 1; // Enable global interrupts
while (1); // Loop forever
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// Pinout:
//
// P0.6 -> LED1 (toggled by TL0)
// P0.7 -> LED2 (toggled by TH0)
//
// all other port pins unused
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
XBR1 = 0x40; // Enable crossbar
P0MDOUT = 0xC0; // Set LED1 and LED2 to
// be used as push-pull
}
//-----------------------------------------------------------------------------
// Timer0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the Timer0 as two 8-bit timers, interrupt enabled.
// Using the internal osc. at 24.5MHz with a prescaler of 1:8 and reloading the
// TH0 register with TIMER0_RELOAD_HIGH and TL0 with TIMER0_RELOAD_HIGH.
//
// Note: The Timer0 uses a 1:48 prescaler. If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer0_Init(void)
{
TH0 = TIMER0_RELOAD_HIGH; // Init Timer0 High register
TL0 = TIMER0_RELOAD_LOW; // Init Timer0 Low register
TMOD = 0x03; // Timer0 in two 8-bit mode
CKCON = 0x02; // Timer0 uses a 1:48 prescaler
ET0 = 1; // Timer0 interrupt enabled
ET1 = 1; // Timer1 interrupt enabled
TCON = 0x50; // Timer0 and timer1 ON
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer0_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer0 interrupt and toggle LED1.
//
//-----------------------------------------------------------------------------
void Timer0_ISR (void) interrupt 1
{
static unsigned int low_counter=0; // Define counter variable
TL0 = TIMER0_RELOAD_LOW; // Reinit Timer0 Low register
if((low_counter++) == LED1_TOGGLE_RATE)
{
low_counter = 0; // Reset interrupt counter
LED1 = ~LED1; // Toggle LED1
}
}
//-----------------------------------------------------------------------------
// Timer1_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer1 interrupt and toggle LED2.
// Note: In this mode the Timer0 is effectively using the Timer1 interrupt
// vector. For more info check the Timers chapter of the datasheet.
//-----------------------------------------------------------------------------
void Timer1_ISR (void) interrupt 3
{
static unsigned int high_counter=0;
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 High register
if((high_counter++) == LED2_TOGGLE_RATE)
{
high_counter = 0; // Reset interrupt counter
LED2 = ~LED2; // Toggle LED2
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -