奇怪的我把这个代码烧到板子里面去,我只点亮了一个灯,为什么四个灯都亮了,还有的就是我改变串口的值,但是占空比还是没有变,想不通,编译没有错,也许是我对配置还不完全理解,但是我再看了手册,是这么配置的, 希望那位大哥可以帮我看看, 上次也谢谢原子哥,他说得很对,我根据他说的,解决了那个问题
串口中断代码void USART1_IRQHandler(void)
{
uint16_t USART1_RxBuffer;
uint16_t CCR3_Val;
TIM_OCInitTypeDef TIM_OCInitStructure;
//配置Channel 1,2,3为PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //设置PWM方式 分为1,2 脉宽调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // PWM功能使能
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出比较 极性
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //同向输出
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //
//接收中断
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART1_RxBuffer = USART_ReceiveData(USART1);
CCR3_Val = USART1_RxBuffer;
// 设置待装入捕获比较寄存器 占空比
if(CCR3_Val != CCR3_Val)
{
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
GPIO_SetBits(GPIOD , GPIO_Pin_1);
}
TIM_Cmd(TIM4, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM4, ENABLE);//使能TIM1的主输出
}
}
主函数
#include "stm32f10x.h"
#include <stdio.h>
#include "usart.h"
void PWM_Configuration(void);
void GPIO_Configuration(void);
/*Private variables---------------------------------------------------------------------*/
uint16_t CCR3_Val ;
/****************************************************************************************
函数名: main
输 入: void
输 出: int
功 能: 程序入口
*****************************************************************************************/
int main(void)
{
SystemInit();
USART_Configuration();
PWM_Configuration();
GPIO_Configuration();
USART1_SendString("USART1");
USART1_SendString("ping");
while(1)
{
}
}
/*****************************************************************************************
函数名: PWM_Configuration
输 入: void
输 出: void
功 能: 配置PWM,
说 明: TIM4_CH3 PB8
*****************************************************************************************/
void PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
//配置PWM波输出端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*------------------------------------------------------------------------------------
配置TIM1,产生6路三种不同占空比的PWM;
TIM1CLK = 72 MHz, Prescaler = 0, TIM1 counter clock = 72 MHz
TIM1 frequency = TIM1CLK/(TIM1_Period + 1) = 17.57 KHz
- TIM1 Channel1 & Channel1N duty cycle = TIM1->CCR1 / (TIM1_Period + 1) = 50%
- TIM1 Channel2 & Channel2N duty cycle = TIM1->CCR2 / (TIM1_Period + 1) = 37.5%
- TIM1 Channel3 & Channel3N duty cycle = TIM1->CCR3 / (TIM1_Period + 1) = 25%
--------------------------------------------------------------------------------------*/
//配置Time Base
TIM_TimeBaseStructure.TIM_Prescaler = 0; // 预分频因子
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 定时器记数模式 向上记数模式
TIM_TimeBaseStructure.TIM_Period = 4095; // 定时周期值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 设置时钟分割
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //指定重新计时的值 t=(TIM_Prescaler+1)*(TIM_Period+1)/(72*106)
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // 根据 TIM_TimeBaseInitStruct 中指定的参数初始化 TIMx 的时间基数单位
/*
//配置Channel 1,2,3为PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //设置PWM方式 分为1,2 脉宽调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // PWM功能使能
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出比较 极性
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //同向输出
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //
// 设置待装入捕获比较寄存器 占空比
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM4, ENABLE);//使能TIM1的主输出
*/
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO结构体 打开时钟总线,D端口
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD , ENABLE);
/**
* LED1 -> PD8 , LED2 -> PD9 , LED3 -> PD10 , LED4 -> PD11
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_0; //选手端口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //标志速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; ; //配置模式 推免输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //让数据生效,把那些初始化数据写到寄存器里面去,
}
|