📄 f32x_timer2_two_8bittimers.c
字号:
//-----------------------------------------------------------------------------
// F32x_Timer2_two_8bitReloadTimers.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program presents an example of use of the Timer2 of the C8051F32x's in
// dual 8bit reload timer mode. It uses the 'F320DK as HW platform.
//
// It sets Timer2 to be two 8-bit timers that interrupt every millisecond.
//
// When the Timer2 low interrupts a counter (low_counter) increments and upon
// reaching the value defined by LED1_TOGGLE_RATE toggles LED1
// When the Timer2 high interrupts a counter (high_counter) increments and upon
// reaching the value defined by LED2_TOGGLE_RATE toggles LED2
//
// Pinout:
//
// P2.2 -> LED1 (toggled by TMR2L)
// P2.3 -> LED2 (toggled by TMR2H)
//
// all other port pins unused
//
// How To Test:
//
// 1) Open the F32x_Timer2_two_8bitTimers.c file in the Silicon Labs IDE
// 2) If necessary change the number of interrupts 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 'F32x TB.
// 5) Run the code
// 6) Verify that both LEDs are blinking at the proper frequencies.
//
// FID: 32X000092
// Target: C8051F32x
// Tool chain: KEIL C51 7.50 / KEIL EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (CG)
// -8 Mar 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F320.h> // SFR declarations
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 12000000/8 // SYSCLK in Hz (12 MHz internal
// oscillator / 8)
// the internal oscillator has a
// tolerance of +/- 2%
#define TIMER_PRESCALER 12 // Based on Timer2 CKCON and TMR2CN
// 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, the LED 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, the LED will
// be on for 1 millisecond and off for
// 1 millisecond
#define TIMER2_RELOAD_HIGH AUX2 // Reload value for Timer2 high byte
#define TIMER2_RELOAD_LOW AUX2 // Reload value for Timer2 low byte
sbit LED1 = P2^2; // LED1='1' means ON
sbit LED2 = P2^3; // LED2 to be used also by Timer2
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void Port_Init (void); // Port initialization routine
void Timer2_Init (void); // Timer2 initialization routine
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // Clear watchdog timer enable
Timer2_Init (); // Initialize the Timer2
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.
//
// P2.2 -> LED1 (toggled by TMR2L)
// P2.3 -> LED2 (toggled by TMR2H)
//
// all other port pins unused
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
XBR1 = 0x40; // Enable crossbar
P2MDOUT = 0x0C; // Set LEDs to push-pull
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures Timer2 as two 8-bit reload timers with interrupt.
// Using the internal osc. at 12MHz/8 with a timer prescaler of 1:12
// reloading the TMR2H register with TIMER2_RELOAD_HIGH and TMR2L with
// TIMER2_RELOAD_HIGH.
//
// Note: The Timer2 uses a 1:12 prescaler. If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer2_Init(void)
{
CKCON &= ~0x60; // Timer2 uses SYSCLK/12
TMR2CN &= ~0x01;
TMR2RLH = TIMER2_RELOAD_HIGH; // Init Timer2 Reload High register
TMR2RLL = TIMER2_RELOAD_LOW; // Init Timer2 Reload Low register
TMR2H = TIMER2_RELOAD_HIGH; // Init Timer2 High register
TMR2L = TIMER2_RELOAD_LOW; // Init Timer2 Low register
TMR2CN = 0x2C; // Enable Timer2 in dual 8bit reload
ET2 = 1; // Timer2 interrupt enabled
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// This function processes the Timer2 interrupt. It checks both counters for
// matches and, upon a match, toggles LED1 and LED2.
//
//-----------------------------------------------------------------------------
void Timer2_ISR (void) interrupt 5
{
static unsigned int low_counter=0; // Define counter variables
static unsigned int high_counter=0;
if(TF2L == 1)
{
if((low_counter++) == LED1_TOGGLE_RATE)
{
low_counter = 0; // Reset interrupt counter
LED1 = ~LED1; // Toggle LED1
}
TF2L = 0;
}
if(TF2H == 1)
{
if((high_counter++) == LED2_TOGGLE_RATE)
{
high_counter = 0; // Reset interrupt counter
LED2 = ~LED2; // Toggle LED2
}
TF2H = 0;
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -