stm32
编程
1.GPIO
初始化
GPIO_InitTypeDef GPIO_InitStrcture;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStrcture);
GPIO_SetBits(GPIOB,GPIO_Pin_All);
##########################################
只需要定义三个
GPIO_Mode
GPIO_Speed
GPIO_Pin
############################################
对GPIO_Speed:
GPIO_Speed_50MHz
GPIO_Speed_2MHz
GPIO_Speed_10MHz
2.GPIO
操作
GPIO_SetBits(GPIOB,GPIO_Pin_All); //置1
GPIO_ResetBits(GPIOB,GPIO_Pin_All); //置0
GPIO_Write(GPIOB,0xfffe);
##################
1.哪个GPIO端口:GPIOA、GPIOB、GPIOC、GPIOD
2.哪个管脚:GPIO_PIN_15或者赋值
3.GPIO
输入输出方式
模拟输入 | GPIO_Mode_AIN | 用于模数转换器模拟输入 |
---|---|---|
浮空输入 | GPIO_Mode_IN_FLOATING | 端口默认状态下什么都不接,呈现高阻态 |
上拉输入 | GPIO_Mode_IPU | 端口在默认状态下输入为高电平 |
下拉输入 | GPIO_Mode_IPD | 端口在默认状态下输入为低电平 |
开漏输出 | GPIO_Mode_Out_OD | 本身不输出电压 |
推挽输出 | GPIO_Mode_Out_PP | 直接输出高低电平电压 |
复用开漏输出 | GPIO_Mode_AF_OD | 片内外设 |
复用推挽输出 | GPIO_Mode_AF_PP | 片内外设 |
4.对外设使能时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_pppx,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_pppx,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_pppx,ENABLE);
#################################################
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//这个是必须的,仿真软件必须的
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
5.定时器初始化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XmljJuHh-1651046229866)(file:///C:\Users\16438\Documents\Tencent Files\1014185858\Image\C2C\28_SLG]V3]U9G9WF]5B@$CM.png)]
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//这个是必须的,仿真软件必须的
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割1=1,1=2,1=4
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //(抢占)先占优先级0级,长的为抢占
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx
}
6.定时器中断函数
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
LEDA7=!LEDA7;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
}
}
7.NVIC
初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
8.外中断初始化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rHaTMvhP-1651046229867)(file:///C:\Users\16438\Documents\Tencent Files\1014185858\Image\C2C\8]3$NR2[N@PED6JH_1L_C.png)]
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
//GPIOE.2 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line=EXTI_Line2; //KEY2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键WK_UP所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
9.外部中断中断函数
void EXTI0_IRQHandler(void)
{
delay_ms(10);//消抖
if(WK_UP==1) //WK_UP按键
{
BEEP=!BEEP;
}
EXTI_ClearITPendingBit(EXTI_Line0); //清除LINE0上的中断标志位
}