我的程序:
#include "stm32f10x.h"
#define button_gpio GPIOD
#define button_port_source GPIO_PortSourceGPIOD
#define button_irqn EXTI9_5_IRQn
#define button2 GPIO_Pin_14 //按键2在GPIOD_14
#define button2_source GPIO_PinSource14
#define button2_exti_line EXTI_Line8
void USART_Configuration(void);
void EXTI_Configuration(void);
void KEY_Configuration(void);
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
KEY_Configuration();
EXTI_Configuration();
USART_Configuration();
while (1)
{
}
}
void KEY_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=button2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(button_gpio,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_EXTILineConfig(button_port_source,button2_source);
EXTI_InitStructure.EXTI_Line=button2_exti_line ;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt ;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling ;
EXTI_InitStructure.EXTI_LineCmd=ENABLE ; //使能外部中断
EXTI_Init(&EXTI_InitStructure) ;
NVIC_InitStructure.NVIC_IRQChannel=button_irqn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure) ;
}
void USART_Configuration(void) //串口参数设置
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口1TXD为复用推挽
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口1RXT为输入复用浮空
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC); //清除接收中断
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure) ;
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断使能
}
#include "stm32f10x.h"
#define button_gpio GPIOD
#define button_port_source GPIO_PortSourceGPIOD
#define button_irqn EXTI9_5_IRQn
#define button2 GPIO_Pin_14 //按键2在GPIOD_14
#define button2_source GPIO_PinSource14
#define button2_exti_line EXTI_Line8
void USART_Configuration(void);
void EXTI_Configuration(void);
void KEY_Configuration(void);
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
KEY_Configuration();
EXTI_Configuration();
USART_Configuration();
while (1)
{
}
}
void KEY_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=button2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(button_gpio,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_EXTILineConfig(button_port_source,button2_source);
EXTI_InitStructure.EXTI_Line=button2_exti_line ;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt ;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling ;
EXTI_InitStructure.EXTI_LineCmd=ENABLE ; //使能外部中断
EXTI_Init(&EXTI_InitStructure) ;
NVIC_InitStructure.NVIC_IRQChannel=button_irqn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure) ;
}
void USART_Configuration(void) //串口参数设置
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口1TXD为复用推挽
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口1RXT为输入复用浮空
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC); //清除接收中断
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure) ;
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断使能
}
