本帖最后由 sqqdfny 于 2017-12-13 20:02 编辑
下面这个代码是在STM32上量产过的,在GD的芯片上,不能计数,咨询代理商,说是GD的不支持外部计数。有大神能指点一下么。
QQ:792739069 email:sqqdfny@163.com
static u32 _count[4] = {0,0,0,0};
//===================================================================
u16 GetExtCounter(u32 ch)
{
if(ch > 3) return (0);
return ((u16)_count[ch]);
}
//===================================================================
//在RTC 1Hz中断中调用,每秒调用一次
void ExtCounterHandler(void)
{
_count[0] = TIM2->CNT; TIM2->CNT = 0;
_count[1] = TIM3->CNT; TIM3->CNT = 0;
_count[2] = TIM4->CNT; TIM4->CNT = 0;
_count[3] = TIM5->CNT; TIM5->CNT = 0;
}
//===================================================================
void ExtCounterInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
//PA0 TIM2_CH1
//PA1 TIM5_CH2
//PA6 TIM3_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB7 TIM4_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM2->CR1 = (1 << 8); //外部采样时钟 36M
TIM2->CR2 = 0;
TIM2->SMCR = (7) | (5 << 4);
TIM2->CCMR1 = (1) | (7 << 4); //上升沿计数,1分频, 1/4采样,8次采样确认一个脉冲
TIM2->CCER = 0;
TIM2->DIER = 0; //关中断
TIM2->CNT = 0;
TIM2->CR1 |= BIT0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM3->CR1 = (1 << 8); //外部采样时钟 36M
TIM3->CR2 = 0;
TIM3->SMCR = (7) | (5 << 4);
TIM3->CCMR1 = (1) | (7 << 4); //上升沿计数,1分频, 1/4采样,8次采样确认一个脉冲
TIM3->CCER = 0;
TIM3->DIER = 0; //关中断
TIM3->CNT = 0;
TIM3->CR1 |= BIT0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM4->CR1 = (1 << 8); //外部采样时钟 36M
TIM4->CR2 = 0;
TIM4->SMCR = (7) | (6 << 4);
TIM4->CCMR1 = (1 << 8) | (7 << 12); //上升沿计数,1分频, 1/4采样,8次采样确认一个脉冲
TIM4->CCER = 0;
TIM4->DIER = 0; //关中断
TIM4->CNT = 0;
TIM4->CR1 |= BIT0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM5->CR1 = (1 << 8); //外部采样时钟 36M
TIM5->CR2 = 0;
TIM5->SMCR = (7) | (6 << 4);
TIM5->CCMR1 = (1 << 8) | (7 << 12); //上升沿计数,1分频, 1/4采样,8次采样确认一个脉冲
TIM5->CCER = 0;
TIM5->DIER = 0; //关中断
TIM5->CNT = 0;
TIM5->CR1 |= BIT0;
} |