原子大哥你好,最近想做一个定时器测频率的试验,具体想法是这样的:timer3产生一定的频率通过PA1输出,timer2通道2工作在捕获模式,用于测量频率,通过液晶显示出来,现在的问题是好像timer2的定时器没有被启动,液晶上显示的始终是0,通过定时器观察窗口看到定时器没有记数,具体程序如下,麻烦帮忙看一下!
timer2.c:
#include"timer2.h"
//timer4 工作在输入捕获模式
//用来测量脉冲宽度
void Timer2_Init(void)
{
RCC->APB2ENR |= 1<<2;
GPIOA->CRL &= 0XFFFFFF0F;
GPIOA->CRL |= 0X00000040;
RCC->APB1ENR |= 1<<0; //
TIM2->CCMR1 |= 1<<8; //输入捕获模式TI2映射到IC2
TIM2->CCMR1 |= 3<<12; //滤波宽度8
TIM2->CCER |= 0<<5; //不反向
TIM2->CCMR1 |= 0<<10; //不分频
TIM2->CCER |= 1<<4; //允许捕获计数器的值到捕获寄存器中
// TIM2->CCER |= 1<<0;
TIM2->EGR |= 1<<0;
TIM2->EGR |= 1<<2;
TIM2->EGR |= 1<<6;
TIM2->SMCR |= 1<<2;
TIM2->SMCR |= 3<<4;
TIM2->SMCR |= 1<<14;
TIM2->SR |= 1<<0;
TIM2->SR |= 1<<2;
TIM2->ARR = 1000;
TIM2->SC = 0;
TIM2->CR1 |= 1<<7;
TIM2->CR1 |= ~(1<<1);
TIM2->CR1 |= 1<<0;
}
int Get_Capture(void)
{
int i;
if(TIM2->SR&0X0001)//溢出中断
TIM2->SR&=~(1<<0);//清除中断标志位
TIM2->SR &=~(1<<6);
i = TIM2->CCR2;
// i = 10;
return i;
}
主函数程序如下:
#include <stm32f10x_lib.h>
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "exti.h"
#include "wdg.h"
#include "timer.h"
#include "timer2.h"
#include "lcd.h"
//Mini STM32开发板范例代码7
//定时器中断 实验
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
int main(void)
{ u32 i;
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
uart_init(72,9600); //串口初始化
LED_Init(); //初始化与LED连接的硬件接口
LCD_Init();
Timerx_Init(5000,7199);//10Khz的计数频率,计数到5000为500ms
POINT_COLOR=CYAN;
BACK_COLOR = BLUE;
LCD_Clear(RED);
LCD_ShowString(30,100,"PNNPLV: ");
Timer2_Init();
while(1)
{
i = Get_Capture();
LCD_ShowNum(90,100,i,6,16);
}
}
|