这次程序是上个笔记的延续
这个是TIME四个定时器一起启动,每个控制一个LED,看上去木有规率,其实是有规率地
*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*---------------------------------------------------------------------------------------------------------*/
/* Include hear file */
/*---------------------------------------------------------------------------------------------------------*/
#include "NUC1xx.h"
/*----------------------------------------------------------------------------
Define variable
----------------------------------------------------------------------------*/
static uint32_t TimerCOUN=0;
static uint32_t TimerCOUN1=0;
static uint32_t TimerCOUN2=0;
static uint32_t TimerCOUN3=0;
/*----------------------------------------------------------------------------
Function subroutine
----------------------------------------------------------------------------*/
void Delay(uint32_t delayCnt)
{
while(delayCnt--)
{
__NOP();
__NOP();
}
}
uint32_t GetTDR(void)
{
return TIMER0->TDR;
}
uint32_t GetTDR1(void)
{
return TIMER1->TDR;
}
uint32_t GetTDR2(void)
{
return TIMER2->TDR;
}
uint32_t GetTDR3(void)
{
return TIMER3->TDR;
}
/*----------------------------------------------------------------------------
Interrupt subroutine
----------------------------------------------------------------------------*/
void TMR0_IRQHandler(void) // Timer0 interrupt subroutine
{
TIMER0->TISR.TIF =1;
TimerCOUN++;
}
void TMR1_IRQHandler(void) // Timer1 interrupt subroutine
{
TIMER1->TISR.TIF =1;
TimerCOUN1++;
}
void TMR2_IRQHandler(void) // Timer2 interrupt subroutine
{
TIMER2->TISR.TIF =1;
TimerCOUN2++;
}
void TMR3_IRQHandler(void) // Timer3 interrupt subroutine
{
TIMER3->TISR.TIF =1;
TimerCOUN3++;
}
/*----------------------------------------------------------------------------
MAIN function
----------------------------------------------------------------------------*/
int32_t main (void)
{
//unsigned int TEMP;
//uint32_t TEMP;
GPIOA->PMD.PMD2=1; //分别设连接LED管脚为,IO模式
GPIOA->PMD.PMD3=1;
GPIOA->PMD.PMD4=1;
GPIOA->PMD.PMD5=1;
NVIC_DisableIRQ(TMR0_IRQn); //Disable Timer0 interrupt
outpw(&TIMER0->TCSR ,0 ); //Disable Timer0//清Timer0的中断标志
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR0_S = 4; //Select 22Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR0_EN =1; //Enable Timer0 clock source
/* Step 2. Select Operation mode */
TIMER0->TCSR.MODE=1; //定时器工作在周期模式.当中断使能时,
//一旦定时器计数器的值(24位递增计数器)等于比较寄存器(TCMPR)的值,
//就会触发定时器中断。
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER0->TCSR.PRESCALE=0; // Set Prescale [0~255]
TIMER0->TCMPR= 2765;//TICR = 2765; // Set TICR(TCMP) [0~16777215]
// (1/22118400)*(0+1)*(2765)= 125.01usec or 7999.42Hz
/* Step 4. Enable interrupt */
TIMER0->TCSR.IE = 1;
TIMER0->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR0_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER0->TCSR.CRST = 1; //Reset up counter
TIMER0->TCSR.CEN = 1; //Enable Timer0
TIMER0->TCSR.TDR_EN=1; // Enable TDR function
//以下是TIMER1的设置
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR1_S = 4; //Select 22Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR1_EN =1; //Enable Timer0 clock source
/* Step 2. Select Operation mode */
TIMER1->TCSR.MODE=1; //定时器工作在周期模式.当中断使能时,
//一旦定时器计数器的值(24位递增计数器)等于比较寄存器(TCMPR)的值,
//就会触发定时器中断。
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER1->TCSR.PRESCALE=20; // Set Prescale [0~255]
TIMER1->TCMPR= 2765;//TICR = 2765; // Set TICR(TCMP) [0~16777215]
// (1/22118400)*(0+1)*(2765)= 125.01usec or 7999.42Hz
/* Step 4. Enable interrupt */
TIMER1->TCSR.IE = 1;
TIMER1->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR1_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER1->TCSR.CRST = 1; //Reset up counter
TIMER1->TCSR.CEN = 1; //Enable Timer0
TIMER1->TCSR.TDR_EN=1; // Enable TDR function
//以下设TIMER2的设置
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR2_S = 4; //Select 22Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR2_EN =1; //Enable Timer0 clock source
/* Step 2. Select Operation mode */
TIMER2->TCSR.MODE=1; //定时器工作在周期模式.当中断使能时,
//一旦定时器计数器的值(24位递增计数器)等于比较寄存器(TCMPR)的值,
//就会触发定时器中断。
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER2->TCSR.PRESCALE=10; // Set Prescale [0~255]
TIMER2->TCMPR= 2765;//TICR = 2765; // Set TICR(TCMP) [0~16777215]
// (1/22118400)*(0+1)*(2765)= 125.01usec or 7999.42Hz
/* Step 4. Enable interrupt */
TIMER2->TCSR.IE = 1;
TIMER2->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR2_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER2->TCSR.CRST = 1; //Reset up counter
TIMER2->TCSR.CEN = 1; //Enable Timer0
TIMER2->TCSR.TDR_EN=1; // Enable TDR function
//以下设TIMER3的设置
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR3_S = 4; //Select 22Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR3_EN =1; //Enable Timer0 clock source
/* Step 2. Select Operation mode */
TIMER3->TCSR.MODE=1; //定时器工作在周期模式.当中断使能时,
//一旦定时器计数器的值(24位递增计数器)等于比较寄存器(TCMPR)的值,
//就会触发定时器中断。
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER3->TCSR.PRESCALE=2; // Set Prescale [0~255]
TIMER3->TCMPR= 2765;//TICR = 2765; // Set TICR(TCMP) [0~16777215]
// (1/22118400)*(0+1)*(2765)= 125.01usec or 7999.42Hz
/* Step 4. Enable interrupt */
TIMER3->TCSR.IE = 1;
TIMER3->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR3_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER3->TCSR.CRST = 1; //Reset up counter
TIMER3->TCSR.CEN = 1; //Enable Timer0
TIMER3->TCSR.TDR_EN=1; // Enable TDR function
while(1)
{
Delay(10);
//TEMP = GetTDR(); //Get value of up counter
if (TimerCOUN>1000)
{
GPIOA->DOUT ^= BIT2;
TimerCOUN=0;
}
//TEMP1 = GetTDR1(); //Get value of up counter
if (TimerCOUN1>1000)
{
GPIOA->DOUT ^= BIT3;
TimerCOUN1=0;
}
//TEMP2 = GetTDR2(); //Get value of up counter
if (TimerCOUN2>1000)
{
GPIOA->DOUT ^= BIT4;
TimerCOUN2=0;
}
//TEMP3 = GetTDR3(); //Get value of up counter
if (TimerCOUN3>1000)
{
GPIOA->DOUT ^= BIT5;
TimerCOUN3=0;
}
}
}
程序打包:
NUC testtimermult.rar
(2.21 MB)
|