1、按钮通过外部中断控制LED
#include "stm32f10x.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIOInitStruct;
GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIOInitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB,&GPIOInitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
GPIOInitStruct.GPIO_Pin = GPIO_Pin_0;
GPIOInitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA,&GPIOInitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
EXTI_InitTypeDef EXTIInitStruct;
EXTIInitStruct.EXTI_Line = EXTI_Line0;
EXTIInitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTIInitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTIInitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTIInitStruct);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitTypeDef NVICInitStruct;
NVICInitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVICInitStruct.NVIC_IRQChannelSubPriority = 0;
NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVICInitStruct);
while(1)
{
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line0) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0) == RESET)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
}
else
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
}
}
}
2、旋转编码器计次
#include "stm32f10x.h"
#include "OLED.h"
uint32_t count = 0;
int main(void)
{
OLED_Init();
OLED_Clear();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIOInitStruct;
GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIOInitStruct.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
GPIO_Init(GPIOB,&GPIOInitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitTypeDef EXTIInitStruct;
EXTIInitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line1;
EXTIInitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTIInitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTIInitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTIInitStruct);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitTypeDef NVICInitStruct;
NVICInitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVICInitStruct.NVIC_IRQChannelSubPriority = 0;
NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVICInitStruct);
NVICInitStruct.NVIC_IRQChannel = EXTI1_IRQn;
NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVICInitStruct.NVIC_IRQChannelSubPriority = 0;
NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVICInitStruct);
while(1)
{
OLED_ShowSignedNum(1,1,count,5);
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line0) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
count++;
}
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line1) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
count--;
}
}