/*********************************************************
** FileName: 中断实验
** Description:
实验中断触发,用串口通信进行检测。
** Author: 老猫
** Date: 2019/10/7
** Others:
*********************************************************/
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "stdio.h"
#include "delay.h"
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void EXIT_Configuration(void);
int main (void){//主程序
RCC_Configuration(); //时钟设置
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
EXIT_Configuration();
while(1);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//设置PA0脚为外部中断0输入通道
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);//设置PA1脚为外部中断0输入通道
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);//设置PA2脚为外部中断0输入通道
//TX端设置为推挽输出
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);
//RX端设置为浮空输入脚
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void NVIC_Configuration(void){ //嵌套中断向量控制器 的设置
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//0通道,2级先占优先级,0级次占优先级
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//1通道,1级先占,0级次占
NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//2通道,0级先占,0级次占
NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXIT_Configuration(void)
{
//设置中断通道沿下降沿触发
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line2;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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);
}
//将printf函数定位到usatr1
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
return ch;
}
#include "stm32f10x_it.h"
#include "stdio.h"
//外部中断0中断服务函数
void EXTI0_IRQHandler(void)
{
printf("rnEXIT0 enter.rn");
EXTI_GenerateSWInterrupt(EXTI_Line1);
printf("rnEXIT0 return.rn");
EXTI_ClearFlag(EXTI_Line0);
}
//外部中断1中断服务函数
void EXTI1_IRQHandler(void)
{
printf("rnEXIT1 enter.rn");
EXTI_GenerateSWInterrupt(EXTI_Line2);
printf("rnEXIT1 return.rn");
EXTI_ClearFlag(EXTI_Line1);
}
//外部中断2中断服务函数
void EXTI2_IRQHandler(void)
{
printf("rnEXIT2 enter.rn");
printf("rnEXIT2 return.rn");
EXTI_ClearFlag(EXTI_Line2);
}
/*********************************************************
** FileName: 中断实验
** Description:
实验中断触发,用串口通信进行检测。
** Author: 老猫
** Date: 2019/10/7
** Others:
*********************************************************/
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "stdio.h"
#include "delay.h"
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void EXIT_Configuration(void);
int main (void){//主程序
RCC_Configuration(); //时钟设置
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
EXIT_Configuration();
while(1);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//设置PA0脚为外部中断0输入通道
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);//设置PA1脚为外部中断0输入通道
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);//设置PA2脚为外部中断0输入通道
//TX端设置为推挽输出
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);
//RX端设置为浮空输入脚
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void NVIC_Configuration(void){ //嵌套中断向量控制器 的设置
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//0通道,2级先占优先级,0级次占优先级
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//1通道,1级先占,0级次占
NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//2通道,0级先占,0级次占
NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXIT_Configuration(void)
{
//设置中断通道沿下降沿触发
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line2;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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);
}
//将printf函数定位到usatr1
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
return ch;
}
#include "stm32f10x_it.h"
#include "stdio.h"
//外部中断0中断服务函数
void EXTI0_IRQHandler(void)
{
printf("rnEXIT0 enter.rn");
EXTI_GenerateSWInterrupt(EXTI_Line1);
printf("rnEXIT0 return.rn");
EXTI_ClearFlag(EXTI_Line0);
}
//外部中断1中断服务函数
void EXTI1_IRQHandler(void)
{
printf("rnEXIT1 enter.rn");
EXTI_GenerateSWInterrupt(EXTI_Line2);
printf("rnEXIT1 return.rn");
EXTI_ClearFlag(EXTI_Line1);
}
//外部中断2中断服务函数
void EXTI2_IRQHandler(void)
{
printf("rnEXIT2 enter.rn");
printf("rnEXIT2 return.rn");
EXTI_ClearFlag(EXTI_Line2);
}
举报