TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
void TIM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef TIM_OCInitSrtucture;
GPIO_InitTypeDef GPIO_InitStructure;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
/* ÅäÖÃGPIO¹Ü½Å¸´ÓÃ*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;// | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Ó³ÉäI/OΪÍâÉèËùÓÃ
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_0);
// GPIO_InitTypeDef GPIO_InitStructure;
/* ʹÄÜGPIOʱÖÓ */
// RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
// /* ÅäÖÃGPIO¹Ü½Å¸´ÓÃ*/
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;// | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_2);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
}
void TIM_PWM_Config(void)
{
/* TIM14 µÄÅäÖà ---------------------------------------------------PB1Êä³ö36Mhz clock
TIM1 ÊäÈëʱÖÓ(TIM1CLK) ÉèÖÃΪ APB2 ʱÖÓ (PCLK2)
=> TIM1CLK = PCLK2 = SystemCoreClock
TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
SystemCoreClock Ϊ48 MHz
ÎÒÃǵÄÄ¿±ê²úÉú 4 ·PWM ÐźÅÔÚ17.57 KHz:
- TIM1_Period = (SystemCoreClock / 17570) - 1
ÐŵÀ1ÉèÖõÄÕ¼¿Õ±ÈΪ 50%
ÐŵÀ2ÉèÖõÄÕ¼¿Õ±ÈΪ 37.5%
ÐŵÀ3ÉèÖõÄÕ¼¿Õ±ÈΪ 25%
ÐŵÀ4ÉèÖõÄÕ¼¿Õ±ÈΪ 12.5%
¶¨Ê±Æ÷Âö³åµÄ¼ÆË㷽ʽÈçÏÂ:
- ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
*/
/*¼ÆËãÔ¤¶¨±íµÄÖµ£¬Ò²¾ÍÊǶàÉÙ¸öʱÖÓ¼ÆÊýΪһ¸öÖÜÆÚ*/
//TimerPeriod = (SystemCoreClock / 17570 ) - 1;
//TimerPeriod = (SystemCoreClock/1000000) -1 ;
TimerPeriod = (SystemCoreClock/1000000) -1 ;
/*¼ÆËãCCR1 Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ50%ʱ*/
Channel1Pulse = (uint16_t) (((uint32_t) 5 * ( TimerPeriod - 1)) / 10);
/*¼ÆËãCCR2 Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ37.5%ʱ*/
// Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
// /*¼ÆËãCCR3 Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ25%ʱ*/
// Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
// /*¼ÆËãCCR4Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ12.5%ʱ*/
// Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
/* TIM14 ʱÖÓʹÄÜ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14 , ENABLE);
/* Time ¶¨Ê±»ù´¡ÉèÖÃ*/
// u16 CCR1= 0;
// u16 CCR2= 0;
TIM_TimeBaseStructure.TIM_Prescaler =0 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* Time ¶¨Ê±ÉèÖÃΪÉÏÉýÑؼÆËãģʽ*/
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; /* Time ¶¨Ê±ÉèÖÃΪÏÂÉýÑؼÆËãģʽ*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
/* ƵµÀ1£¬2£¬3£¬4µÄPWM ģʽÉèÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
//TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//ʹÄÜƵµÀ1ÅäÖÃ
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//ʹÄÜƵµÀ1ÅäÖÃ
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//ʹÄÜƵµÀ2ÅäÖÃ
// TIM_OC2Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;//ʹÄÜƵµÀ3ÅäÖÃ
// TIM_OC3Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;//ʹÄÜƵµÀ4ÅäÖÃ
// TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 ¼ÆËãÆ÷ʹÄÜ*/
TIM_Cmd(TIM14, ENABLE);
// /* TIM1 Ö÷Êä³öʹÄÜ */
TIM_CtrlPWMOutputs(TIM14, ENABLE);
// TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
//// .NVIC_IRQChannel=TIM3_IRQN;
//// NVIC_Init
} |