void init_tim2_cam(u16 psc, u16 arr, u8 way, u8 dir)
{
RCC->APB1ENR |= 1 << 0; //使能定时器2时钟
RCC->APB2ENR |= 1 << 2; //使能PortA
switch (way)
{
case 1:
GPIOA->CRL &= 0xfffffff0;
GPIOA->CRL |= 0x00000008;
break;
case 2:
GPIOA->CRL &= 0xffffff00;
GPIOA->CRL |= 0x00000088;
break;
case 3:
GPIOA->CRL &= 0xfffff000;
GPIOA->CRL |= 0x00000888;
break;
case 4:
GPIOA->CRL &= 0xffff0000;
GPIOA->CRL |= 0x00008888;
break;
}
TIMER->PSC = psc;
TIMER->ARR = arr;
switch (way)
{
case 4:
TIMER->CCMR2 |= 1 << 8;
if (dir == 0)
TIMER->CCER |= 1 << 13; //下降沿捕获
else
TIMER->CCER &= ~(1 << 13); //上升沿捕获
TIMER->CCER |= 1 << 12;
TIMER->DIER |= 1 << 4;
case 3: //CCR3 PA2
TIMER->CCMR2 |= 1 << 0;
if (dir == 0)
TIMER->CCER |= 1 << 9; //下降沿捕获
else
TIMER->CCER &= ~(1 << 9); //上升沿捕获
TIMER->CCER |= 1 << 8;
TIMER->DIER |= 1 << 3;
case 2: //CCR2 PA1
TIMER->CCMR1 |= 1 << 8; //CCR2配置通道方向:输入
if (dir == 0)
TIMER->CCER |= 1 << 5; //下降沿捕获
else
TIMER->CCER &= ~(1 << 5); //上升沿捕获
TIMER->CCER |= 1 << 4; //CCR2通道捕获使能
TIMER->DIER |= 1 << 2; //CCR2通道允许捕获中断
case 1: //>CCR1 PA0
TIMER->CCMR1 |= 1 << 0; //CCR1配置通道方向:输入
if (dir == 0)
TIMER->CCER |= 1 << 1; //下降沿捕获
else
TIMER->CCER &= ~(1 << 1); //上升沿捕获
TIMER->CCER |= 1 << 0; //CCR1捕获使能
TIMER->DIER |= 1 << 1; //CCR1通道允许捕获中断
break;
}
TIMER->DIER |= 1 << 0; //允许更新中断
MY_NVIC_Init(1, 2, TIM2_IRQChannel, 2); //中断
TIMER->CR1 = 0x01; //使能定时器
TIMER->SR &= ~(1 << 0);
}
|