我用STM32F103C8T6的串口1发送数据,同时配置TIM1_CH1让PA8产生PWM波形,在main.c中进行初始化发现:
1、同时初始化uart_init();和pwm_init();后发现没有PWM波产生
2、只初始化pwm_init();后PA8有PWM波形产生
请问各位大神这是什么原因?
main.c中只初始化pwm_init();可以产生PWM波
单片机源程序如下:
- #include <stm32f10x.h>
- #include "uart.h"
- #include "pwm.h"
- int main(void)
- {
- //uart_init(); //115200
- pwm_init();
- //printf("Hello STM32!\r\n");
- while(1)
- {
- TIM_SetCompare1(TIM1,50);
- }
- }
复制代码 uart.c 串口1的配置
- #include "uart.h"
- void uart_init(void)
- {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //使能端口时钟 TXD RXD GPIO PA10 PA9
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
- //使能串口1时钟 APB2
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
-
- /* 配置RXD引脚 PA10 浮空输入模式 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* 配置TXD PA9 复用推挽输出模式 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200; //配置波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //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;//全双工 既可以发送也可以接收
- /* 初始化USART1 */
- USART_Init(USART1, &USART_InitStructure);
- /* 使能串口工作 */
- USART_Cmd(USART1, ENABLE);
- }
- void send_byte(uint8_t byte)
- {
- USART_SendData(USART1, byte);
- /* 等待发送完成 */
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
- {
- }
- }
- void send_string(uint8_t *str)
- {
- while(*str != '\0')
- {
- send_byte(*str++);
- }
- }
- void send_buf(uint8_t *buf,uint16_t len)
- {
- uint16_t i;
- for(i = 0;i < len;i++)
- {
- send_byte(buf[i]);
- }
- }
- int fputc(int ch,FILE *f)
- {
- send_byte(ch);
- return(ch);
- }
复制代码 pwm.c PWM中TIM1_CH1的配置
- #include "pwm.h"
- void pwm_init(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //使能TIM1
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- /* GPIOA 端口时钟 */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
- //PA8 配置复用推挽输出模式
- 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);
-
- /* 定时器配置 产生频率 50HZ-60HZ 100HZ */
- TIM_TimeBaseStructure.TIM_Period = 10000/100 - 1; //计数值 范围0 - 99
- TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; //72MHZ/7200 = 10 000
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
-
- /* PWM1 模式配置:TIM1_CH1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM输出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //设置比较值 比较值决定了占空比 值得范围由计数值决定
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- //使能预装载寄存器
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
-
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- //使能自动重装载寄存器
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- /* 使能TIM1计数 */
- TIM_Cmd(TIM1, ENABLE);
- }
复制代码
|