本帖最后由 oayzw 于 2015-4-1 15:25 编辑 STM32F103C8T6 TIM3 CH1可以输出到PA6和PB4 #include "stm32f10x.h" void pwm_init(){ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef timInitStruct; TIM_OCInitTypeDef TIM_OCInitStructure; //设置IO口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //设置定时器3 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //重新将Timer设置为缺省值 TIM_DeInit(TIM3); //采用内部时钟给TIM3提供时钟源 TIM_InternalClockConfig(TIM3); timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; timInitStruct.TIM_Prescaler = 0; timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数 timInitStruct.TIM_RepetitionCounter = 0; timInitStruct.TIM_Period = 100-1; //这个值实际上就是TIMX->ARR,延时开始时重新设定即可 TIM_TimeBaseInit(TIM3, &timInitStruct); //设置PWM输出 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 30; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //ARR预装载缓冲器使能 TIM_ARRPreloadConfig(TIM3, ENABLE); //开启定时器 TIM_Cmd(TIM3, ENABLE); } GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOA, &GPIO_InitStructure); 用上面这段,在PA6上有PWM输出 //============================================================================= // PWM-> PB4 //============================================================================= GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); 但这段在PB4上没有输出PWM 谢谢! |
【MCU实战经验】基于STM32F103C8T6的hart总线调试器设计
求教STM32F103进入STOP模式后用外部中断唤醒的问题
基于STM32F103RCT6的无源蜂鸣器音乐播放(生日快乐歌)
STM32F103c8t6有没有DAC 功能?
STM32F103x中文数据手册
新手求教,为何在我电脑上找不到STM32F1Xx.h文件
金龙107例程汇总(STM32F107)
万利STM32F107VC 原理图
STM32F103 ADC多通道检测必须要DMA吗?
【官方例程】STM32F107以太网官方例程
改成下面这样还是没有
/=============================================================================
// PWM-> PB4
//=============================================================================
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE); //PB4
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(RGB_RED_PORT, &GPIO_InitStructure);
本人亲测这样设置是可以的注意此选项:GPIO_Mode_AF_PP