对于定时器,勾引我要研究的只是一句话,而这句话老让我范合计。
有的看官觉得说的没有争议。但实践引来就不一样了。
下边的代码是初使化的通用代码:
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
//???????
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//TIM_ClearFlag(TIM2,TIM_FLAG_Update);//??????
//????????0-0xffff,72M/36000=2000
TIM_TimeBaseStructure.TIM_Period = arr;
//????????0-oxffff,???36000??
TIM_TimeBaseStructure.TIM_Prescaler = psc;
////???????????
//????,???0,??????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_CKD_DIV1 是预分频吗?????
我查了下书:
显然,它不是预分频。那么只有prescaler是了。有的看官说了,这不费话么,写着预分频么????
本人对英语不是十分精通。
疑问又来了,谁没有事会把预分频设为1呢????
我想这块之所以到现在没有范病,是因为大家都抄来抄去,哪家例程也不会设为1。那么,设为1果真是36MHZ么????
我用下边的证明:
#include "stm32f10x.h"
#include "stdio.h"
#include "main.h"
#include "stdlib.h"
#define B_ON GPIO_ResetBits(GPIOA,GPIO_Pin_0)
#define B_OFF GPIO_SetBits(GPIOA,GPIO_Pin_0)
void GPIO_Config(void);
void TIM2_Config(u16 arr,u16 psc);
void NVIC_Config(void);
uint32_t count;
//???
int main(void)
{
GPIO_Config();
//((1+arr )/72M)*(1+psc )=((1+1999)/72M)*(1+35999)=1?
//TIM2_Config(1999,35999);
TIM2_Config(35999,1);
NVIC_Config();
while(1);
}
//???GPIO
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//PC13?
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //??LED
}
void TIM2_Config(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//TIM_ClearFlag(TIM2,TIM_FLAG_Update);//??????
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//--------------------stm32f10x_it.c------------------------
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
count++;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
if(count >= 1000)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)(!GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0)));
count = 0;
}
}
}
它和不用1时的时间是一样的。这就证明了,只要我们在预分频不用1,定时器的时钟就是APB时钟的两倍即72MHZ。
话又说回来了,谁平时有病才把第二个参数设为1。
此内容由EEWORLD论坛网友ddllxxrr原创,如需转载或用于商业用途需征得作者同意并注明出处