微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32F407的TIM定时器输出PWM波

stm32F407的TIM定时器输出PWM波

时间:11-10 来源:互联网 点击:
今天测试了stm32F407TIM定时器输出PWM波,了解了其配置过程。要点如下:

1.使能GPIO的复用功能,指的是1)GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);和2)GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF。这两条缺一不可,在F4里没有了开始复用时钟(RCC)功能。

2.分频和周期计算公式:
Prescaler = (TIMxCLK / TIMx counter clock) - 1;
Period = (TIMx counter clock / TIM3 output clock) - 1
TIMx counter clock为你所需要的TXM的定时器时钟
3.PWM的幅值为3.3V

/************************************************************
Copyright (C), 2012-2022, yin.
FileName: main.c
Author: ycw Version : 1.0 Date: 2012.04.24
Description: TIM2 PWM
Version: V1.0
Function List: TIM2 PWM
History:

#include

static __IO uint32_t TimingDelay;//__IO为volatile的宏定义
int8_t LED_Flag = 1;//LED灯翻转标志位
void GPIO_Config(void);
void TIM_Config(void);
void NVIC_Config(void);
void Delay(__IO uint32_t nTime);

main ()
{
/*在主函数main之前通过调用启动代码运行了SystemInit函数,而
这个函数位于system_stm32f4xx.c”。程序运行起始于启动文件的第
175行(LDR R0, =SystemInit)。sys时钟为HSE频率/PLL_M*PLL_N/PLL_P,
定义HSE为25M,则sys时钟频率为168M */

GPIO_Config();
TIM_Config();
NVIC_Config();
/*SystemCoreClock / 1000时基为1ms*/
if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}
while (1)
{/*产生一个软件中断
EXTI_GenerateSWInterrupt(EXTI_Line0);
Delay(1000); */
if (LED_Flag != 1)
{
GPIO_SetBits(GPIOG, GPIO_Pin_6); //setbits使能IO,当前下指输出(此时为灭)
}
else
{
GPIO_ResetBits(GPIOG, GPIO_Pin_6); //Resetbits屏蔽IO,当前下指不输出(此时为亮)
}
}
}

/*************************************************
Function: void GPIO_Config(void)
Description: GPIO配置函数
Input: 无
Output:无
Return:无
*************************************************/
void GPIO_Config(void)
{
/*定义了一个GPIO_InitStructure的结构体,方便一下使用 */
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOG时钟(时钟结构参见“stm32图解.pdf”)*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA, ENABLE);
/*仅设置结构体中的部分成员:这种情况下,用户应当首先调用函数PPP_SturcInit(..)
来初始化变量PPP_InitStructure,然后再修改其中需要修改的成员。这样可以保证其他
成员的值(多为缺省值)被正确填入。*/
GPIO_StructInit(&GPIO_InitStructure);
/* 初始化GPIOG的Pin_6为推挽输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //指定第六引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式为输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //频率为快速
GPIO_Init(GPIOG, &GPIO_InitStructure); //调用IO初始化函数

/*配置GPIOA_Pin_1,作为TIM_Channel2 PWM输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //指定第六引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //模式必须为复用!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //频率为快速
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉与否对PWM产生无影响
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);//复用GPIOA_Pin1为TIM2_Ch2
}

/*************************************************
Function: void TIM_Config(void)
Description: 定时器配置函数
Input: 无
Output: 无
*************************************************/
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);//初始化TIM2寄存器
/*分频和周期计算公式:
Prescaler = (TIMxCLK / TIMx counter clock) - 1;
Period = (TIMx counter clock / TIM3 output clock) - 1
TIMx counter clock为你所需要的TXM的定时器时钟
*/
TIM_TimeBaseStructure.TIM_Period = 10000-1; //查数据手册可知,TIM2与TIM5为32位自动装载
/*在system_stm32f4xx.c中设置的APB1 Prescaler = 4 ,可知
APB1时钟为168M/4*2,因为如果APB1分频不为1,则定时时钟*2
*/
TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top