新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2017-7-16
- 在线时间
- 2 小时
|
1金钱
程序这里 我把TIM3初始化给屏蔽了void TIM3_Int_Init(u16 arr,u16 psc),还是是能跟不屏蔽TIM3初始化一样的效果,故这个TIM3初始化有什么用?
#include "pwm.h"
#include "led.h"
#include "usart.h"
//void TIM3_Int_Init(u16 arr,u16 psc)
//{
//
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
//
// //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Äü
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//
// TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ ¼Æêyμ½5000Îa500ms
// TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 10KhzμļÆêyÆμÂê
// TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
// TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//
// TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM3ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
// NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3ÖD¶Ï
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶0¼¶
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
// NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
// TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIMxíaéè
//
//}
////¶¨ê±Æ÷3ÖD¶Ï·tÎñ3ìDò
//void TIM3_IRQHandler(void) //TIM3ÖD¶Ï
//{
// if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
// {
// TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
// LED1=!LED1;
// }
//}
//TIM3 pwm初始化
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä TIM3_CH2->B5
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH2μÄWMÂö3å2¨DÎ GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO
//3õê¼»ˉTIM3
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM3 Channel2 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIM3
}
还有这个主程序,//NVIC_Configuration(); //uart_init(9600); 这两句串口跟向量中断设置跟pwm输出有什么联系,我屏蔽了也不影响结果啊。
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
//NVIC_Configuration(); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
//uart_init(9600); //′®¿ú3õê¼»ˉÎa9600
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
TIM3_PWM_Init(899,0); //2»·ÖÆμ¡£PWMÆμÂê=72000/900=8Khz
while(1)
{}
|
|