|
简单记录LPC1788定时器匹配中断设置--通过寄存器直接操作
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "timer_lpc1788.h"</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "uart_lpc1788.h"</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void TIMER0_IRQHandler(void) //TIMER0中断函数</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> if(((LPC_TIM0->IR) &(1<<0))==(1<<0)) //匹配中断发生</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> {</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> UARTSendStr("CLOS\r\n");</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> }</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->IR |=(1<<0);//清除匹配中断标志位</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void TIMER0_Init(uint32_t clk, uint32_t howtime)</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_SC->PCONP|=(1<<1);//打开TIMER0外设时钟</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->CTCR &=(~0x03);//计数控制寄存器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->CTCR |=0x00;//定时器模式</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//----PC=PR-----TC++</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->TC =0;//定时计数器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->PR =0;//预分频寄存器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->PC =0;//预分频计数器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->TCR |=(1<<1); //定时器控制寄存器--复位定时器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->TCR &=~(1<<1); //清除复位</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->PR =(clk/1000000-1);//预分频寄存器--系统外设时钟/1000000=1us需要的PC值</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->IR=0xFFFFFFFF; //清除中断</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//--------------------</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->MR0=howtime;//匹配寄存器---多少个1us将匹配</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->MCR &=~(0x07); //匹配控制寄存器--禁止匹配-复位-停止产生的中断</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->MCR |=(1<<0); //允许匹配中断</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->MCR |=(1<<1); //允许复位中断--匹配时TC复位-重新计数</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); //设置定时器中断优先级</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> NVIC_EnableIRQ(TIMER0_IRQn); //定时器中断使能</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
- </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LPC_TIM0->TCR |=(1<<0);; //启动定时器</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</font>
复制代码
|
|