基于stm32f103zet6按键计数的程序
时间: 2023-10-11 07:13:00 浏览: 107
基于stm32F103ZET6的超声波测距程序
5星 · 资源好评率100%
以下是基于STM32F103ZET6的按键计数程序,使用了STM32标准外设库(StdPeriph):
```c
#include "stm32f10x.h"
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
uint8_t count = 0;
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
while(1);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line13);
count++;
}
}
```
该程序通过GPIO配置将PC13引脚配置为上拉输入模式,通过EXTI配置将该引脚连接到外部中断线13上,并设置下降沿触发。在NVIC配置中,将EXTI中断优先级设置为1,启用该中断。在EXTI的中断服务函数中,当检测到下降沿触发时,计数器count加一。
阅读全文