⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f31x_timer2_16bitreloadtimer.c

📁 C8051F31系列单片机的例子
💻 C
字号:
//-----------------------------------------------------------------------------
// F31x_Timer2_16bitReloadTimer.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program presents an example of use of the Timer2 of the C8051F31x's in
// 16-bit reload counter/timer mode. It uses the 'F31xDK as HW platform.
//
// The timer reload registers are initialized with a certain value so that
// the timer counts from that value up to FFFFh and when it overflows an
// interrupt is generated and the timer is automatically reloaded.
// In this interrupt the ISR toggles the LED.
//
// Pinout:
//
//    P3.3 -> LED
//
//    all other port pins unused
//
// How To Test:
//
// 1) Open the F31x_Timer2_16bitReloadTimer.c file in the Silicon Labs IDE.
// 2) If a change in the interrupt period is required, change the value of
//    LED_TOGGLE_RATE.
// 3) Compile and download the code.
// 4) Verify the LED pins of J3 are populated on the 'F31x TB.
// 5) Run the code.
// 6) Check that the LED is blinking
//
//
// FID:            31X000023
// Target:         C8051F31x
// Tool chain:     KEIL C51 7.20 / KEIL EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (CG)
//    -09 NOV 2005
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include "C8051F310.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            1   // Based on Timer2 CKCON and TMR2CN
                                       // settings

#define LED_TOGGLE_RATE            10 // LED toggle rate in milliseconds
                                       // if LED_TOGGLE_RATE = 1, the LED will
                                       // be on for 1 millisecond and off for
                                       // 1 millisecond

// 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)
#define TIMER_TICKS_PER			TIMER_TICKS_PER_MS/1000

// Note: LED_TOGGLE_RATE*TIMER_TICKS_PER_MS should not exceed 65535 (0xFFFF)
// for the 16-bit timer

#define AUX1     TIMER_TICKS_PER*LED_TOGGLE_RATE
#define AUX2     -AUX1

#define TIMER2_RELOAD            AUX2  // Reload value for Timer2

sbit LED = P3^3;                       // LED='1' means ON


//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void Port_Init (void);                 // Port initialization routine
void Timer2_Init (void);               // Timer2 initialization routine

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

sfr16 TMR2RL = 0xCA;                   // Timer2 Reload Register
sfr16 TMR2 = 0xCC;                     // Timer2 Register

//-----------------------------------------------------------------------------
// 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.
//
// Pinout:
//
//    P3.3 -> LED
//
//    all other port pins unused
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
   XBR1 = 0x40;                        // Enable crossbar
   P3MDOUT = 0x08;                     // Set LED to push-pull
}

//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures Timer2 as a 16-bit reload timer, interrupt enabled.
// Using the SYSCLK at 16MHz/8 with a 1:12 prescaler.
//
// Note: The Timer2 uses a 1:12 prescaler.  If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer2_Init(void)
{
   int s = AUX1;
   unsigned int t = TIMER_TICKS_PER;
 /* 
   CKCON &= ~0x60;                     // Timer2 uses SYSCLK/12
   TMR2CN = 0x00;

   TMR2RL = TIMER2_RELOAD;             // Reload value to be used in Timer2
   TMR2 = TMR2RL;                      // Init the Timer2 register

   TMR2CN = 0x04;                      // Enable Timer2 in auto-reload mode
   ET2 = 1;                            // Timer2 interrupt enabled
*/   
   TMR3CN = 0x00;                      // STOP Timer3; Clear TF3H and TF3L;
   									   // Timer3 operates in 16 bits auto reload mode
                                       // disable low-byte interrupt; disable
                                     // split mode; select internal timebase
   CKCON |= 0xc0;                      // Timer3 uses SYSCLK as its timebase

   //TMR3RLL  = -counts>>8;                  // Init Timer3 (Hi&Lo) reload register values
   //TMR3L  = TMR3RLL;                    // Init Timer3 (Hi&Lo) load register values

	TMR3RLH = TIMER2_RELOAD>>8;
	TMR3RLL = TIMER2_RELOAD;   
	TMR3L =  TMR3RLL;

   EIE1 |= 0x80;                        // Enable Timer3 interrupts
   TMR3CN |= 0x04;                      // start Timer3, and set low bytes overflow flags

}


//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer2 interrupt and toggle the LED
//
//-----------------------------------------------------------------------------
void Timer2_ISR (void) interrupt 14
{
   static unsigned int n = 0;
   static int s = 0;
   if (n++ >= (1000*2))
   {
        n = 0;
   	    if (s++>=100)
		{
            s = 0;
   	        LED = ~LED;                         // Toggle the LED
		}
   }
  // TF2H = 0;                           // Reset Interrupt
  TMR3CN &= 0x7F;
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -