|
int main(void)
{
RCC_Configuration(); //系统时钟初始化
GPIO_Configuration();
TIM4_CH1PWM_Configuration(); //打开ctrl PWM 30HZ
TIM1_CH1PWM_Configuration(); //打开boost升压PWM 33.3kHZ
}
void RCC_Configuration(void)
{
SystemInit(); //系统时钟初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //oled端口使能 PA端口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ctrl PWM定时器挂接 30Hz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //GPIOB挂接在APB2上 ctrl PWM PB6
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //BOOST PWM定时器挂接 33KHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //打开APB2复用功能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //USART3挂接在APB1上
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3时钟挂载在APB1上
}
void GPIO_Configuration(void) //PWM端口配置写在PWM发生函数中
{
GPIO_InitTypeDef GPIO_InitStructure;
/*oled端口配置*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB11用作蓝牙的rx口 浮空输入模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB10用作蓝牙的Tx口 推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB1用作蓝牙的模式选择口 推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB8配置为开漏输出 蓝牙信息发送开关
}
void TIM4_CH1PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM4);
TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Period = 33333-1; //脉冲宽度
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //72:0x48 1MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //1分频:72MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;////////////////////////////
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 33331;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //30hz,导通2us
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
/*boost PWM端口函数 33.3khz*/
void TIM1_CH1PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM1);
TIM_InternalClockConfig(TIM1);
TIM_TimeBaseStructure.TIM_Period = 270-1; //脉冲宽度
TIM_TimeBaseStructure.TIM_Prescaler = 8-1; //72:0x48 9MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //1分频:72MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;////////////////////////////
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 108;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //33khz
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
} |
|