今天遇到了一个奇怪的问题,在这里分享一下。
首先是完整的程序:
//基于最小系统板
//库文件V3.50
//功能:PWM实现四路呼吸灯
//20210513
#include<stm32f10x.h>
//
#define K1_DOWN GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0
#define K2_DOWN GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)==0
//
//#define Premap//重定位定义控制,若没有则不重定义引脚
#define Fremap
//根据宏定义控制PWM的引脚
#ifdef Premap//部分重定位
#define Ch1 GPIO_Pin_4//GPIOB
#define Ch2 GPIO_Pin_5//GPIOB
#define Ch3 GPIO_Pin_0//GPIOB
#define Ch4 GPIO_Pin_1//GPIOB
#else
#ifdef Fremap//完全重定位
#define Ch1 GPIO_Pin_6//GPIOC
#define Ch2 GPIO_Pin_7//GPIOC
#define Ch3 GPIO_Pin_8//GPIOC
#define Ch4 GPIO_Pin_9//GPIOC
#else
#define Ch1 GPIO_Pin_6//GPIOA
#define Ch2 GPIO_Pin_7//GPIOA
#define Ch3 GPIO_Pin_0//GPIOB
#define Ch4 GPIO_Pin_1//GPIOB
#endif
#endif
//STEP1
//PWM对应的输出通道配置
//TIM3的Ch3(PC8/TIM3_CH3),即对应的D2
//但是用完全复用功能
//开启TIM3时钟以及复用功能时钟,配置PC8为复用输出
//参数:chx为要选择的通道号:1,2,3,4
void GPIO_Tim3PWM(u8 chx)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);//GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用时钟使能
switch(chx)
{
case 1:
GPIO_InitStructure.GPIO_Pin = Ch1;//TIM_CH1
break;
case 2:
GPIO_InitStructure.GPIO_Pin = Ch2;//TIM_CH2
break;
case 3:
GPIO_InitStructure.GPIO_Pin = Ch3;//TIM_CH3
break;
case 4:
GPIO_InitStructure.GPIO_Pin = Ch4;//TIM_CH4
break;
}
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//选择对应的引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入(复位为高电平)
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化PC端口
//GPIO_SetBits(GPIOC,GPIO_Pin_9);//K1引脚初始化为高电平,本行可省略
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//选择对应的引脚
GPIO_InitStructure