使用的是
STM32f107vc
芯片,想要达到的效果是LED1亮,按下Key1,LED1灭,再按下Key1,LED1灯亮,如此循环:
#include "stm32f10x.h"
#define BitBand(addr,n) *(vola
tile unsigned long*)((addr & 0xf0000000)+0x20000000+(addr & 0xfffff)*32+n*4) //位绑定公式
#define GPIOC_ODR_A (GPIOC_BASE+0x0c)
#define GPIOC_IDR_A (GPIOC_BASE+0x08)
#define GPIOD_ODR_A (GPIOD_BASE+0x0c)
#define GPIOD_IDR_A (GPIOD_BASE+0x08)
#define PCout(n) BitBand(GPIOC_ODR_A,n)
#define PCin(n) BitBand(GPIOC_IDR_A,n)
#define PDout(n) BitBand(GPIOD_ODR_A,n)
#define PDin(n) BitBand(GPIOD_IDR_A,n)
#define Key1 PCin(4)
#define LED1 PDout(2)
void RCC_Configuration(void);
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
LED1=0; //开始点亮LED灯
while(1);
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
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);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource4);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(Key1==0) //判断是否有中断
{
LED1=!LED1; //LEd灯翻转
}
EXTI_ClearITPendingBit(EXTI_Line4); //清空中断标志位
}
试过了,上下拉没用
一周热门 更多>