mmuuss586 发表于 2014-4-25 12:11
PWM程序发上来看看
这是RCC:
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
/* Enable All GPIO and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG
| RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC3 | RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 , ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2 | RCC_AHBPeriph_SRAM
| RCC_AHBPeriph_FSMC | RCC_AHBPeriph_SDIO, ENABLE);
这是GPIO
void GPIO_PortInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ------------------- Button ------------------------------------ */
/* Configure PE.02, PE.03, PE.04, PE.05, PE.06 as input floating, for buttun used */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* ------------------- Buzzer and LED --------------------------- */
GPIO_InitStructure.GPIO_Pin = BUZZER_PIN | LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(BUZ_LED_PORT, &GPIO_InitStructure);
/* ---------------------- ADC Port ------------------------------ */
// Panel
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Actuator and BackLight (PC4)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ---------------------- PWM Port ------------------------------ */
// VIBRATE, Heat,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7
| GPIO_Pin_8 | GPIO_Pin_9 | BL_PWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
////////////////////////////////////////////////////////////////////////
AfioMapr2 = AFIO->MAPR2;
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_TIM4, DISABLE);
AFIO->MAPR2 |= 0x0400; // The NADV signal is not connected. The I/O pin can be used by another peripheral
AfioMapr2 = AFIO->MAPR2;
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);
// BackLight
/* GPIO_InitStructure.GPIO_Pin = BL_PWM_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BL_PWM_PORT, &GPIO_InitStructure);
*/
/* --------------------- Keyboard Port -------------------------- */
GPIO_InitStructure.GPIO_Pin = KEY_UP | KEY_DOWN | KEY_LIFT | KEY_RIGHT | KEY_ENTER;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEYBOARD_PORT, &GPIO_InitStructure);
AfioMapr2 = AFIO->MAPR2;
#if 0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource1);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
#endif
}
TIM:
void TIMER_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//---------- Back light ----------- TIM1 C2N
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //72M
TIM_TimeBaseStructure.TIM_Prescaler = 0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 360; // 200kHz
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; // RepetitionCounter+1 times overflow
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/* Channel 2N Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // TIM_OCMode_Timing
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50; // write to CCR
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_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);// TIM1 Update events as a trigger TRGO
TIM_ClearFlag(TIM1,TIM_FLAG_Update); // prevent interrupt at initial state
TIM_Cmd(TIM1,ENABLE); //enable TIM1
//----------------------------------------- vibrate TIM3 CH1, 2
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; //18M
TIM_TimeBaseStructure.TIM_Prescaler = 180; //100KHz, 1Tick=1ms
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000 ;// period = 0.01s
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Channel 1, 2 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; // write to CCR
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_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_ClearFlag(TIM3,TIM_FLAG_Update); // prevent interrupt at initial state
TIM_Cmd(TIM3,ENABLE); //enable TIM1
// ------------------------------------- PWM , heat , TIM4 CH1, 2, 3, 4
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; //18M
TIM_TimeBaseStructure.TIM_Prescaler = 1800; //100KHz, 1Tick=1ms
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000 ;// period = 0.1s
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; // write to CCR
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;
//AFIO->MAPR =
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);////////////////////////////////////
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_SelectOutputTrigger(TIM4,TIM_EventSource_CC4);// TIM4 OC events as a trigger TRGO
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_Cmd(TIM4,ENABLE); // enable TIM3 --- Counter enable
//TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
|