本帖最后由 小S咯 于 2017-11-30 17:35 编辑
我用的是STM32F030K6T6芯片,我配置TIM1的3通道和TIM3的4通道PWM输出,为什么示波器看一直是高电平,没有PWM输出。另外有这个芯片的数据手册吗???网上找不到,下面程序。还有一个问题:因为定时器PWM不行,所以我就想IO口模拟PWM,于是开TIM3的中断,结果flash写会卡死,单独使用就没问题。
程序:
void fmc_write(uint32_t addr, uint16_t Data)
{
FLASH_Unlock();
FLASH_ErasePage(FLASH_START_ADDR+(addr*2));
FLASH_ProgramHalfWord(FLASH_START_ADDR+(addr*2),Data);
FLASH_Lock();
}
//GPIO初始化的
void gpio_init()
{
GPIO_InitTypeDef GPIO_InitStructure1; //PA
GPIO_InitTypeDef GPIO_InitStructure2; //PB
/* GPIOA Periph clock enable */
//PA9=P,PA10=Y,PA11=R PWM
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_2);
/* GPIOB Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
//PB0=G,PB1=B,PB4=A,PB5=W PWM
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_1);
}
//定时器初始化
void PWM_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure1;
TIM_OCInitTypeDef TIM_OCInitStructure1;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure3;
TIM_OCInitTypeDef TIM_OCInitStructure3;
//TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure1.TIM_Prescaler = 0;
TIM_TimeBaseStructure1.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure1.TIM_Period = 255;
TIM_TimeBaseStructure1.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure1);
TIM_OCInitStructure1.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure1.TIM_OutputState = TIM_OutputNState_Enable;
TIM_OCInitStructure1.TIM_OCPolarity = TIM_OCPolarity_High;
//CH1~CH4
// TIM_SetCompare1(TIM1,64);
// TIM_OC1Init(TIM1,&TIM_OCInitStructure1);
// TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_SetCompare2(TIM1,128);
TIM_OC2Init(TIM1,&TIM_OCInitStructure1);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_SetCompare3(TIM1,200);
TIM_OC3Init(TIM1,&TIM_OCInitStructure1);
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_SetCompare1(TIM1,255);
TIM_OC4Init(TIM1,&TIM_OCInitStructure1);
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
//TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure3.TIM_Prescaler = 0;
TIM_TimeBaseStructure3.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure3.TIM_Period = 16;
TIM_TimeBaseStructure3.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure3);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_OCInitStructure3.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure3.TIM_OutputState = TIM_OutputNState_Enable;
TIM_OCInitStructure3.TIM_OCPolarity = TIM_OCPolarity_High;
//CH1~CH4
TIM_SetCompare1(TIM3,6550);
TIM_OC1Init(TIM3,&TIM_OCInitStructure3);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_SetCompare1(TIM3,13000);
TIM_OC2Init(TIM3,&TIM_OCInitStructure3);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_SetCompare1(TIM3,33000);
TIM_OC3Init(TIM3,&TIM_OCInitStructure3);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_SetCompare1(TIM3,45000);
TIM_OC4Init(TIM3,&TIM_OCInitStructure3);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE);
TIM_CtrlPWMOutputs(TIM3,ENABLE);
} |
|