stm32f407的TIM1的中斷和PWM
/************************************************************
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燈翻轉(zhuǎn)標志位
void GPIO_Config(void);
void TIM_Config(void);
void NVIC_Config(void);
void Delay(__IO uint32_t nTime);
main ()
{
/*在主函數(shù)main之前通過調(diào)用啟動代碼運行了SystemInit函數(shù),而
這個函數(shù)位于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)
{
/*產(chǎn)生一個軟件中斷
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配置函數(shù)
Input: 無
Output:無
Return:無
*************************************************/
void GPIO_Config(void)
{
/*定義了一個GPIO_InitStructure的結(jié)構(gòu)體,方便一下使用 */
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOG時鐘(時鐘結(jié)構(gòu)參見“stm32圖解.pdf”)*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
/*僅設置結(jié)構(gòu)體中的部分成員:這種情況下,用戶應當首先調(diào)用函數(shù)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); //調(diào)用IO初始化函數(shù)
/*配置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產(chǎn)生無影響
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: 定時器配置函數(shù)
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; //查數(shù)據(jù)手冊可知,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;//向上計數(shù)
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/*配置輸出比較,產(chǎn)生占空比為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(占空比數(shù)值)
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: 嵌套中斷斷配置函數(shù)
Input: 無
Output: 無
*************************************************/
void NVIC_Config(void)
{
/*配置定時器TIM1中斷嵌套*/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中斷優(yōu)先級分組為1
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; //嵌套中斷通道為TIM1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級為1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優(yōu)先級為0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中斷
NVIC_Init(&NVIC_InitStructure);
}
/*************************************************
Function: void Delay(__IO uint32_t nTime)
Description: 精確延時函數(shù),時基根據(jù)前面設定,當前
為1ms
Input: 需要延時的時間,單位ms
Output:無
*************************************************/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}
/*************************************************
Function: void TimingDelay_Decrement(void)
Description: SysTick中斷服務函數(shù),加在_it.h中的
void SysTick_Handler(void)函數(shù)內(nèi)
Input: 無
Output:無
*************************************************/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
評論