發新話題
打印

Timer3使用

Timer3使用

看完data之後覺得用法似乎和timer2雷同
直接由sample code 中的timer2修改
timer3的中斷卻無法執行
是因為設定上timer3有什麼特別的地方嗎

附上我完整的程式碼

----------------------------------

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

#include <C8051F410.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            12  // Based on Timer2 CKCON and TMR2CN
                                       // settings

#define LED_TOGGLE_RATE            50  // 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

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

#define AUX1     TIMER_TICKS_PER_MS*LED_TOGGLE_RATE
#define AUX2     -AUX1

#define TIMER3_RELOAD            AUX2  // Reload value for Timer2

sbit LED = P2^1;                       // LED='1' means ON


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

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

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

sfr16 TMR3RL = 0x92;                   // Timer2 Reload Register
sfr16 TMR3 = 0x94;                     // Timer2 Register

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------

void main (void)
{
   PCA0MD &= ~0x40;                    // Clear watchdog timer enable

   OSCICN |= 0x04;                     // Force Internal Osc. 1:8 prescaler
   while ((OSCICN & 0x40 ) == 0);      // Wait until stable

   Timer3_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:
//
//    P2.1 -> LED
//
//    all other port pins unused
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
   XBR1 = 0x40;                        // Enable crossbar
   P2MDOUT = 0x02;                     // 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 Timer3_Init(void)
{
   CKCON &= ~0x60;                     // Timer2 uses SYSCLK/12
   TMR3CN &= ~0x01;

   TMR3RL = TIMER3_RELOAD;             // Reload value to be used in Timer2
   TMR3 = TMR3RL;                      // Init the Timer2 register

   TMR3CN = 0x04;                      // Enable Timer2 in auto-reload mode
   EIE1 = 0x80;                            // Timer2 interrupt enabled
}


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

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer2 interrupt and toggle the LED
//
//-----------------------------------------------------------------------------
void Timer3_ISR (void) interrupt 5
{
   LED = ~LED;                         // Toggle the LED
   TMR3CN = 0x00;                           // Reset Interrupt
}

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

TOP

你可以用 Config Wizard 2 這套免費軟體來設定產生 Timer3 Initial code,會比較快解決你的問題,這套軟體可到以下下載:
https://www.silabs.com/products/mcu/Pages/SoftwareDownloads.aspx

TOP

目前已初步可以執行timer3的中斷
想請問timer3的中斷在使用上與timer2相同嗎?
還是有其他特殊的地方?

TOP

Hi!
Timer 0/1/2 為標準8051/52的Timer. Time3/4/5..就不一定了,這Silabs MCU都有不同用途,
你用WZ2會比較容易懂及設置,但你還是須要看Datasheet中的方塊圖,會讓你更容易使用.



J.K

TOP

感謝兩位版主的解答
目前我遇到的問題是timer3的中斷程式
可以進行簡單的指令
如 LED = 1
若是給予if的判斷式
則不會執行判斷式
直接執行判斷式以下的程式
所以
我才會懷疑是不是timer3在使用上是不是有特殊地方需要注意

TOP

發新話題