//M051 Timer Interrupt test
#define CLOCK_SETUP 1
#define CLOCK_EN 0x1F
#define PLL_Engine_Enable 0
#define PLL_SEL 0x00080000
#define CLOCK_SEL 0x0
#include <stdio.h>
#include <stdint.h>
#include "M051.h"
#include "Register_Bit.h"
#include "Common.h"
static uint32_t TMR_Tick = 0;
void TMR0_Init(void);
void TMR0_IRQHandler(void);
void TMR0_Init(void)
{
/* Enable Timer0 clock source */
APBCLK |= TMR0_CLKEN;
/* Select Timer0 clock source as 內部 22.1184MHz */
CLKSEL1 = (CLKSEL1 & (~TM0_CLK)) | TM0_CLK ; // TM0_12M
/* Reset 外設IP TMR0 */
IPRSTC2 |= TMR0_RST;
IPRSTC2 &= ~TMR0_RST;
/* Select timer0 Operation mode as period mode */
TCSR0 &= ~TMR_MODE;
TCSR0 |= MODE_PERIOD;
/* Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TCSR0 = TCSR0 & 0xFFFFFF00; //沒預除 Set Prescale [0~255]
TCMPR0 = 120000; // Set TCMPR [0~16777215]<< 這是TCMPR的範圍
/* Enable timer0 interrupt */
TCSR0 |= TMR_IE;
NVIC_ISER = TMR0_INT;
/* Reset timer0 counter */
TCSR0 |= CRST; //設置CRST位 將復位(清除)定時器 計數器,且使CEN=0
/* Enable Timer0 */
TCSR0 |= CEN;
}
//-----------------------------------------------------------------------------
// Function: TMR0_IRQHandler
void TMR0_IRQHandler(void)
{
/* Clear timer0 interrupt flag */
TISR0 |= TMR_TIF;
TMR_Tick++;
}
main(void)
{
Un_Lock_Reg();
PWRCON |= OSC22M_EN;
while((CLKSTATUS & OSC22M_STB) == 0); //Wait until 22M clock is stable.
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_22M ;//Set internal 22.1148M as the system clock
P0_PMD = 0X5555; // P0 ALL OUTPUT
P1_PMD = 0X5555; // P1 ALL OUTPUT
P2_PMD = 0X5555; // P2 ALL OUTPUT
P3_PMD = P3_PMD|Px0_QB|Px1_QB|Px2_QB|Px3_OUT|Px4_OUT|Px5_OUT|Px6_OUT|Px7_OUT;
// P30 P31 P32 雙向 P33 P34 P35 P36 P37 輸出
P4_PMD = 0X5555; // P4 ALL OUTPUT
TMR0_Init();
while (1)
{
if (TMR_Tick >= 100)
{
P41_DOUT=!P41_DOUT; //P41 Not
TMR_Tick=0;
}
}
}
留言列表