本帖最后由 vention123456 于 2016-12-12 12:00 编辑 STM32F103RC的芯片,有TIM8的。 随便写了一个串口的初始化函数,和一个TIM8的pwm输出,发现,屏蔽掉串口初始化,TIM8正常PWM输出,开启了usart1初始化,TIM8的PWM就没有输出了。铺上代码: 附件附上CubeMX生成的HAL库工程,做对比 #include "stm32f10x.h" void uart_init(u32 bound); void time_init(void); int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); time_init(); while(1) { } } void uart_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_InitStructure.USART_BaudRate = bound; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); } void time_init() { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC| RCC_APB2Periph_TIM8, ENABLE); /* TIM8 channels as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 720-1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 98-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 5; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); /* Channels Configuration in Toggle mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM8, &TIM_OCInitStructure); TIM_OC3Init(TIM8, &TIM_OCInitStructure); TIM_OC2Init(TIM8, &TIM_OCInitStructure); TIM_OC4Init(TIM8, &TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM8, ENABLE); TIM_SelectOnePulseMode(TIM8, TIM_OPMode_Repetitive); TIM_Cmd(TIM8, ENABLE); } void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { u8 Res =USART_ReceiveData(USART1); } } |
pwm.zip
下载9.25 MB, 下载次数: 224, 下载积分: ST金币 -1
我找不到原因。上面的程序也十分的简短,就两个功能函数,一个usart1 初始化, 一个TIM8的PWM输出。貌似之前也看到过有帖子别人也遇到类似问题。
评分
查看全部评分
有怀疑没有uart1的中断入口, 我把uart1的中断补上,中断里面几乎不做什么。那我如何调TIM8的中断级别?TIM8默认的中断级别是什么?
评分
查看全部评分
那样是可以的。我之前试过了。 先初始化TIM8 再初始化Uart1,就没有问题了,我就是想不明白为什么。而我用CubeMX 生成的HAL库函数,是没有问题的,无论放TIM8初始化在前面,还是放Uart1初始化在前面,都不会有这个问题。
尝试去看,看不太懂,还对比着。基础水平太差了
已经附上HAL库工程。
调换
MX_USART1_UART_Init();
MX_TIM8_Init();
两者前后顺序,可以正常输出PWM
PWM是硬件输出。很怀疑你管脚配置是否有冲突。
评分
查看全部评分
STM32F103RC
TIM8和Uart1 完全是不一样的引脚啊,一个是PC6,7,8,9 一个是PA9,10
oh,那再看看代码方面。