主函数如下:(问题在主函数加黑红 {MOD}部分)
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
u8 T_Clock;
delay_init(); //延时函数初始化
LED_GPIO_Config(); //LED I/O初始化
uart_init(115200); //初始化串口1的波特率为115200 初始化串口1(PA9 PA10)放TIM1_PWM_Init()前PA8(不正常)无电压变化-0V 串口发送正常
delay_ms(5);
TIM1_PWM_Init(900-1,1-1); //3分频,则计数频率为72M/3=24 Hz,PWM频率=24M/24000=1Khz (PA8)
// uart_init(115200); //初始化串口1的波特率为115200 初始化串口1(PA9 PA10)放TIM1_PWM_Init()后PA8正常 串口发送也正常
while(1)
{
if(T_Clock==200)
{
LED0 = !LED0;
printf("程序正在运行
");
}
T_Clock++;
delay_ms(5);
}
}
串口初始化函数:
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//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);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
定时器初始化函数:
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO结构体变量
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定时器结构体变量
TIM_OCInitTypeDef TIM_OCInitStructure; //定时器输出通道结构体变量
NVIC_InitTypeDef NVIC_InitStructure; //中断初始化结构体变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);/* 使能定时器时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* 使能定时器通道引脚GPIO时钟 */
/* 配置定时器通道1--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);
/* 配置定时器参数:自动重装载值、预分频、时钟分割、计数模式、重复计数器 参数*/
TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
// TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; /* 设置时钟分频系数:不分频(这里用不到) */
TIM_TimeBaseStructure.TIM_ClockDivision = 0 ; /* 设置时钟分频系数:不分频(这里用不到) */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* 向上计数模式 */
TIM_TimeBaseStructure.TIM_RepetitionCounter = 99; /* 重复计算器 */
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/* 配置定时器通道1的输出参数配置 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; /* 模式配置:PWM模式2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* 输出状态设置:使能输出 */
TIM_OCInitStructure.TIM_Pulse = arr >> 1; /* 设置待装入捕获比较寄存器的值,当计数器计数到这个值时,电平发生跳变 */
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
/* 配置定时器中断参数配置 */
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //允许定时器1更新中断
NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_IRQn; //定时器1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
求解!!!
一周热门 更多>