我们用定时器做个40ms的定时。
定时器的计数周期为SMCLK/8,其中SMCLK=12M.
所以定时40ms需要计数60000次
定时器初始化:
void timer_init(void)
{
TIMER_A0->CCTL[0] = TIMER_A_CCTLN_CCIE; // TACCR0 interrupt enabled
TIMER_A0->CCR[0] = 60000;//40ms
TIMER_A0->CTL = TIMER_A_CTL_SSEL__SMCLK | // SMCLK, continuous mode
TIMER_A_CTL_MC__CONTINUOUS|
TIMER_A_CTL_ID_3;//SMCLK/8 =1.5M
NVIC->ISER[0] |= 1 << ((TA0_0_IRQn) & 31);//使能中断
}
中断函数:
// Timer A0 interrupt service routine
void TA0_0_IRQHandler(void) {
TIMER_A0->CCTL[0] &= ~TIMER_A_CCTLN_CCIFG;
P1->OUT ^= BIT0;//翻转LED灯
TIMER_A0->CCR[0] = 60000; // Add Offset to TACCR0
}
主程序:
int main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | // Stop watchdog timer
WDT_A_CTL_HOLD;
clk_init();//clk init
uart_init();//uart init,9600
timer_init();
GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0);//设置P1.0为输出
//sprintf((char *)ptr,"%.2f\r\n",a);
//printf("test...\r\n");
while(1)
{
jansson_pack_test();
//GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
for(int i=0;i<100000;i++)
{;}
}
}
实际的现象也是led灯进行周期性翻转。
|