调试了多半响TIM1的中止和PWM,总算算是调通了。遇到了不少问题,也深化了解了TIM1的装备进程。代码如下:
/************************************************************
Copyright (C), 2012-2022, yin.
FileName: main.c
Author: ycw Version : 1.0 Date: 2012.04.25
Description: TIM1 PWM NVIC
Version: V1.0
Function List: TIM1 PWM NVIC
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)
{
/*发生一个软件中止
TIM_GenerateEvent(TIM1,TIM1_UP_TIM10_IRQn);
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 | RCC_AHB1Periph_GPIOB, 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_8,作为TIM1_Channel2 PWM输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11; //指定复用引脚
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_InitStructure.GPIO_Pin = GPIO_Pin_13; //GPIOB_Pin13作为TIM1_CH1N输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1); //复用GPIOA_Pin7为TIM1_Ch1N,作用不抱负高电平不到0.5V
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); //复用GPIOA_Pin8为TIM1_Ch1,
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);//复用GPIOA_Pin10为TIM1_Ch3,
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);//复用GPIOA_Pin11为TIM1_Ch4,留意没有CH4N
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);//复用GPIOB_Pin13为TIM1_Ch1N,作用很好,能够和GPIOA_Pin7一起复用
}
/*************************************************
Function: void TIM_Config(void)
Description: 守时器装备函数
Input: 无
Output: 无
*************************************************/
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_DeInit(TIM1);//初始化TIM1寄存器
/*分频和周期计算公式:
Prescaler = (TIMxCLK / TIMx counter clock) – 1;
Period = (TIMx counter clock / TIM3 output clock) – 1
TIMx counter clock为你所需求的TXM的守时器时钟
*/
TIM_TimeBaseStructure.TIM_Period = 10000-1; //查数据手册可知,TIM1与TIM8为16位主动装载
/*在system_stm32f4xx.c中设置的APB2 Prescaler = 2 ,可知
*APB1时钟为168M/2*2,由于假如APB1分频不为1,则守时时钟x2
*/
TIM_TimeBaseStructure.TIM_Prescaler = 16800-1; //分频为10K
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/*装备输出比较,发生占空比为20%的PWM方波*/
TIM_OCStructInit(&TIM_OCInitStructure); //填入缺省值,其间后几项只对TIM1和TIM8有用
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1为正常占空比形式,PWM2为反极性形式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出答应
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出答应
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //High为占空比高极性,此刻占空比为20%;Low则为反极性,占空比为80%
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//互补输出,与以上相反
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_Pulse = 2000; //输入通道1 CCR1(占空比数值)
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //Ch1初始化
TIM_OCInitStructure.TIM_Pulse = 4000;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 7000;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//CCR主动装载默许也是翻开的
TIM_ARRPreloadConfig(TIM1, ENABLE); //ARR主动装载默许是翻开的,能够不设置
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1守时器
TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM1的PWM输出,TIM1与TIM8有用
}
/*************************************************
Function: void NVIC_Config(void)
Description: 嵌套中止断装备函数
Input: 无
Output: 无
*************************************************/
void NVIC_Config(void)
{
/*装备守时器TIM1中止嵌套*/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中止优先级分组为1
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; //嵌套中止通道为TIM1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //呼应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中止
NVIC_Init(&NVIC_InitStructure);
}
/*************************************************
Function: void Delay(__IO uint32_t nTime)
Description: 准确延时函数,时基依据前面设定,当时
为1ms
Input: 需求延时的时刻,单位ms
Output:无
*************************************************/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}
/*************************************************
Function: void TimingDelay_Decrement(void)
Description: SysTick中止服务函数,加在_it.h中的
void SysTick_Handler(void)函数内
Input: 无
Output:无
*************************************************/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay–;
}
}