STM32 定時(shí)器產(chǎn)生PWM徹底應(yīng)用
看到這個(gè)題,我先看STM32的數(shù)據(jù)手冊,把STM32的定時(shí)器手冊看完就花了一天,但是看了一遍任然不知道所云,就看庫函數(shù),略有點(diǎn)理解,就想一哈把這個(gè)程序調(diào)出來,于是就花了一天多時(shí)間仿照網(wǎng)上別人的程序來寫,花了一天多寫出來調(diào)試,結(jié)果行不通,做了無用功,于是靜下心來想想,還是一步一步的來。
本文引用地址:http://m.butianyuan.cn/article/201612/325053.htm我先用STM32的通用定時(shí)器用PWM模式產(chǎn)生四路相同占空比,不同頻率的PWM波,配置如下:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能TIM2時(shí)鐘
TIM_InternalClockConfig(TIM2);//使用內(nèi)部時(shí)鐘
TIM_BaseInitStructure.TIM_Prescaler=3; //設(shè)置TIM時(shí)鐘頻率除數(shù)的預(yù)分頻值
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//選擇計(jì)數(shù)器模式
TIM_BaseInitStructure.TIM_Period=1799;//設(shè)置下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值
TIM_BaseInitStructure.TIM_ClockDivision=0;//設(shè)置時(shí)鐘分割
TIM_TimeBaseInit(TIM2,&TIM_BaseInitStructure);
//通道1
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//選擇定時(shí)器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//選擇輸出比較狀態(tài)
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//選擇互補(bǔ)輸出比較狀態(tài)
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;//設(shè)置了待裝入捕獲比較器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//設(shè)置輸出極性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//設(shè)置互補(bǔ)輸出極性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//選擇空閑狀態(tài)下得非工作狀態(tài)
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//選擇互補(bǔ)空閑狀態(tài)下得非工作狀態(tài)
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
//通道2
TIM_OCInitStructure.TIM_Pulse=CCR2_Val;//設(shè)置了待裝入捕獲比較器的脈沖值
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
//通道3
TIM_OCInitStructure.TIM_Pulse=CCR3_Val;//設(shè)置了待裝入捕獲比較器的脈沖值
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
//通道4
TIM_OCInitStructure.TIM_Pulse=CCR4_Val;//設(shè)置了待裝入捕獲比較器的脈沖值
TIM_OC4Init(TIM2,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2,ENABLE);
用pwm模式輸出的頻率和占空比是固定的,不可調(diào),要想輸出頻率可調(diào),占空比可調(diào),必須得使用比較輸出模式。這點(diǎn)資料是在STM32全國巡回研討會上看到的,如圖:
所以,接下來我就寫了一個(gè)程序通過輸出比較模式產(chǎn)生一路PWM波,這個(gè)波的頻率和占空比都由自己確定,函數(shù)配置如下:
TIM_BaseInitStructure.TIM_Prescaler=3; //設(shè)置TIM時(shí)鐘頻率除數(shù)的預(yù)分頻值(18M)
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//選擇計(jì)數(shù)器模式
TIM_BaseInitStructure.TIM_Period=1800;//設(shè)置下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值
TIM_BaseInitStructure.TIM_ClockDivision=0;//設(shè)置時(shí)鐘分割
TIM_TimeBaseInit(TIM2,&TIM_BaseInitStructure);
//通道1
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//選擇定時(shí)器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//選擇輸出比較狀態(tài)
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//選擇互補(bǔ)輸出比較狀態(tài)
TIM_OCInitStructure.TIM_Pulse=CCR1_Val1;//設(shè)置了待裝入捕獲比較器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//設(shè)置輸出極性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//設(shè)置互補(bǔ)輸出極性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//選擇空閑狀態(tài)下得非工作狀態(tài)
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//選擇互補(bǔ)空閑狀態(tài)下得非工作狀態(tài)
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);
if(n==1)
{
n=0;
TIM_SetCompare1(TIM2,CCR1_Val2);
}
else
{
n=1;
TIM_SetCompare1(TIM2,CCR1_Val1);
}
}
通過改變比較寄存器(CCR1)中的值,改變PWM的占空比,在每次匹配中斷中改變CCR1的值。上面程序?qū)崿F(xiàn)的是產(chǎn)生一路頻率為10K占空比為40%的PWM波。
有了上面的思想我就想產(chǎn)生四路不同頻率不同占空比的PWM波,經(jīng)過反復(fù)思考光配函數(shù)似乎不能實(shí)現(xiàn),在網(wǎng)上去查了的,很多網(wǎng)友也說不能實(shí)現(xiàn),有一個(gè)網(wǎng)友給了一個(gè)提示:軟件模擬。剛開始沒明白什么意思,于是還是自己繼續(xù)配置庫函數(shù),在這個(gè)過程中一直有兩個(gè)疑問:
每次中斷中,CCR寄存器的值都在循環(huán)的增加,CCR的寄存器不可能是無限大吧?就算是無限大,計(jì)數(shù)器也不是無限大呀,他只能記到65535。初步確定使用匹配中斷不行,我有想過同時(shí)使用溢出中斷和匹配中斷,但這樣四路PWM波只能是固定的,頻率和占空比不能調(diào)。大概說一下怎樣用溢出中斷和匹配中斷實(shí)現(xiàn)四路固定的PWM波,把計(jì)數(shù)器寄存器(CNT)的值裝最大周期的那個(gè)PWM波,當(dāng)一次計(jì)數(shù)完成算一下三路小點(diǎn)周期數(shù),在匹配中斷中對應(yīng)的設(shè)個(gè)變量,CCR就改變幾次,溢出中斷來了就再次給計(jì)數(shù)器裝初值,同時(shí)四個(gè)比較寄存器從裝初值,這樣很麻煩,理論上可以實(shí)現(xiàn),但我考慮到最終不能實(shí)現(xiàn)我的要求,就沒有去驗(yàn)證。所以產(chǎn)生四路頻率可調(diào)占空比可調(diào),用一個(gè)定時(shí)器似乎不能實(shí)現(xiàn),就一直卡到這里,我又在想飛哥說能實(shí)現(xiàn),就肯定能實(shí)現(xiàn),我又在網(wǎng)上找資料,還是沒找到,只是有人題四路,軟模擬,于是我就思考用軟模擬實(shí)現(xiàn),最后在一個(gè)師兄的指點(diǎn)下,確實(shí)用軟件模擬一個(gè)中間比較寄存器能實(shí)現(xiàn),思路大概是這樣子的,首先讓比較寄存器裝滿,也就是最大值(65535),然后通過改變模擬比較寄存器的值,每次匹配中斷只需把模擬比較寄存器的值去比較就行,具體方案看程序。
unsigned charCnt[4]; //一個(gè)數(shù)組,這個(gè)數(shù)組的每個(gè)元素對應(yīng)一個(gè)通道,用來判斷裝PWM得高電平還是低電平數(shù)
unsigned intT[4];//周期數(shù)組
unsigned intR[4];//模擬的比較寄存器數(shù)組,一樣的每個(gè)通道對應(yīng)一個(gè)數(shù)組元素
unsigned intRh[4];//模擬的PWM高電平比較寄存器
unsigned intRl[4]; //模擬的PWM低電平比較寄存器
unsigned char F[4];//占空比數(shù)組
unsigned int CCR1,CCR2,CCR3,CCR4;
void Init(void)
{
unsigned char i = 0;
for(i = 0; i < 4; i++)
{
Cnt[i]= 0;
T[i]= 0;
R[i]= 0;
Rh[i] = 0;
Rl[i] = 0;
F[i]= 0;
}
//t的范圍為(0~65536)
T[0] = 450;//F=40K
T[1] = 600;//F=30K
T[2] = 900;//F=20K
T[3] = 1800;//F=10K
//F(占空比)的范圍為(0~100)
F[0] = 40;
F[1] = 30;
F[2] = 20;
F[3] = 10;
for(i = 0; i < 4; i++)
{
Rh[i] = (T[i] * F[i]) / 100;
Rl[i] = T[i] - Rh[i];
}
R[0] = Rl[0];
R[1] = Rl[1];
R[2] = Rl[2];
R[3] = Rl[3];
CCR1 = R[0];
CCR2 = R[1];
CCR3 = R[2];
CCR4 = R[3];
}
對應(yīng)的數(shù)組初始化
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
}
時(shí)鐘配置
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//Key1 PA0 Key3 PA8
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//Key2 PC13
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//Key PD3
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOD,&GPIO_InitStructure);
//TIM3 CH1 CH2
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//TIM3 CH3 CH4
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
管腳配置
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中斷配置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_InternalClockConfig(TIM3);
TIM_BaseInitStructure.TIM_Prescaler=3;//4分頻,18M
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_Period=65535;
TIM_BaseInitStructure.TIM_ClockDivision=0;
TIM_BaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_Pulse=CCR2;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_Pulse=CCR3;
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_Pulse=CCR4;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
Cnt[0]=(~Cnt[0])&0x01;
if(Cnt[0]==0x01)
R[0]+=Rl[0];
else
R[0] += Rh[0];
if(R[0]>65535)
R[0]=R[0]-65535;
CCR1=R[0];
TIM_SetCompare1(TIM3,CCR1);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
Cnt[1]=(~Cnt[1])&0x01;
if(Cnt[1]==0x01)
R[1]+=Rl[1];
else
R[1] += Rh[1];
if(R[1]>65535)
R[1]=R[1]-65535;
CCR2=R[1];
TIM_SetCompare2(TIM3,CCR2);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC3)!=RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);
Cnt[2]=(~Cnt[2])&0x01;
if(Cnt[2]==0x01)
R[2]+=Rl[2];
else
R[2] += Rh[2];
if(R[2]>65535)
R[2]=R[2]-65535;
CCR3=R[2];
TIM_SetCompare3(TIM3,CCR3);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC4)!=RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);
Cnt[3] = (~Cnt[3])&0x01;
if(Cnt[3]==0x01)
R[3]+=Rl[3];
else
R[3] += Rh[3];
if(R[3]>65535)
R[3]=R[3]-65535;
CCR4=R[3];
TIM_SetCompare4(TIM3,CCR4);
}
}
中斷函數(shù)
其余就是按鍵掃描函數(shù),通過改變周期數(shù)組中的值和占空比寄存器中的值就能改變PWM波的頻率和占空比,當(dāng)然按鍵可以設(shè)置為4個(gè)(一個(gè)按鍵對應(yīng)一個(gè)通道),如果IO夠用也可以設(shè)置8個(gè),沒兩個(gè)按鍵對應(yīng)一個(gè)通道分別改變頻率和占空比。
評論