产生PWM
void My_PWM_Init(void){
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_Prescaler = 900-1;
TIM_TimeBaseInitStructure.TIM_Period = 100-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_InternalClockConfig(TIM3);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 99;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3,ENABLE);
}
void PWM_SetCompare1(uint16_t plause){
TIM_SetCompare1(TIM3,plause);
}
void PWM_SetPrescaler(uint16_t prescaler){
TIM_PrescalerConfig(TIM3, prescaler, TIM_PSCReloadMode_Immediate);
}
输入捕获测量频率和占空比
#include "stm32f4xx.h" // Device header
uint8_t first = 1, down = 0, finish = 0;
uint32_t first_cnt = 0, all_cnt = 0, high_cnt = 0;
uint8_t overflow = 0;
void Input_Capture_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM1);
TIM_TimeBaseInitTypeDef TIM_TimBaseInitStructure;
TIM_TimBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimBaseInitStructure.TIM_Prescaler = 180-1;
TIM_TimBaseInitStructure.TIM_Period = 65536-1;
TIM_TimBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimBaseInitStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_ITConfig(TIM1, TIM_IT_CC1 | TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
if(down == 1){
high_cnt = TIM_GetCapture1(TIM1) - first_cnt + overflow * 65536;
down = 0;
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising); // 继续完成上升降沿捕获,测频率
}
else{
if(first == 1){
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling); // 配置为下降沿捕获,测占空比
down = 1;
first_cnt = TIM_GetCapture1(TIM1);
first = 0;
}else{
all_cnt = TIM_GetCapture1(TIM1) - first_cnt + overflow * 65536;
first = 1;
finish = 1;
}
}
overflow = 0;
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1); //清除中断标志
}
void TIM1_UP_TIM10_IRQHandler(void){
overflow ++;
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
uint8_t isFinished(void){
return finish;
}
uint32_t Get_ALLN(void){
return all_cnt;
}
uint32_t Get_HighN(void){
return high_cnt;
}
main()中while循环语句:
注意,stm32f429i-Disc用于生成基准频率的TIM1定时器频率为180MHZ,在经过TIM_Prescaler = 180-1 的分频后基准频率变为1MHZ,此为Frequency中1000000的来源。
while(1){
uint32_t Frequency = 0;
uint32_t Duty = 0;
if(isFinished()){
uint32_t ALLN = Get_ALLN();
uint32_t HIGHN = Get_HighN();
Frequency = (1000000)/(uint32_t)ALLN;
Duty = Get_HighN()*100 / (uint16_t)Get_ALLN();
}
}
标签:TIM1,void,NVIC,TIM,Disc,InitStructure,占空比,GPIO,PWM
From: https://blog.csdn.net/qq_39683145/article/details/140349157