stm32标准库中断实验

1、stm32中断实验

题目:用stm32F103核心板的GPIOA端一管脚接一个LED,GPIOB端口一引脚接一个开关(用杜邦线模拟代替)。采用中断模式编程,当开关接高电平时,LED亮灯;接低电平时,LED灭灯。如果完成后,尝试在main函数while循环中加入一个串口每隔1s 发送一次字符的代码片段,观察按键中断对串口发送是否会带来干扰或延迟。

代码如下:

main.c

#include "stm32f10x.h"    // Device header
#include "Delay.h"
#include "Serial.h"

void CountSensor_Init(void){//配置中断
	
/*开启APB2的时钟*/
	
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//开启APB2的外设,GPIOB是APB2的外设
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//开启AFIO的外设,AFIO是APB2的外设

//GPIOB
GPIO_InitTypeDef GIPO_InitStruct;//结构体配置GPIO
GIPO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入模式input、out
GIPO_InitStruct.GPIO_Pin = GPIO_Pin_14;//开启14号引脚
GIPO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GIPO_InitStruct);
	
//AFIO外外部引脚选择
EXTI_InitTypeDef EXTI_Int_Struct;//结构体Exti
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);//PB14连接
EXTI_Int_Struct.EXTI_Line = EXTI_Line14;
EXTI_Int_Struct.EXTI_LineCmd = ENABLE;//开启
EXTI_Int_Struct.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式
EXTI_Int_Struct.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
EXTI_Init(&EXTI_Int_Struct);

NVIC_InitTypeDef NVIC_IniyStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//响应等级
NVIC_IniyStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_IniyStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_IniyStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_IniyStruct.NVIC_IRQChannelSubPriority = 1;


NVIC_Init(&NVIC_IniyStruct);
}

//GPIOA初始化
void GPIOA_INIT(void){
    GPIO_InitTypeDef GPIO_InitStructure;//定义变量GPIOA结构体
	GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//
	GPIO_SetBits(GPIOA, GPIO_Pin_0);
}




int count = 0;

void LED1_Turn(void){
	
	if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0)		
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_0);					
	}
	else												
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_0);		
	}
}

int a=1;
void EXTI15_10_IRQHandler(void){
	
     if(EXTI_GetITStatus(EXTI_Line14) == SET){
			 
			  EXTI_ClearITPendingBit(EXTI_Line14);
				
			if(a==1)
			 {
			 a=0;	 
			 }
		 }
}


int main(void){
  
	CountSensor_Init();//初始化中断
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启PA端口时钟
	GPIOA_INIT();//初始化GPIOA代码
	Serial_Init();
	Serial_SendString("Init success!\t");
	while(1){
		Serial_SendString("a\t");
		if(a==0)
			 {LED1_Turn();
				 a=1;
			 }
		Delay_ms(1000);
	}
	
}

Serail.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//单片机输出口初始化
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//单片机输入口初始化
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

实验现象:

        单片机不断向计算机发送a的符号,同时如果我们按下按钮led实现亮灭。由于中断时间太短,我们无法在这里得到中断是否干扰主程序,需要我们精确计时或者修改方案。

 2、串口中断实验

题目:当stm32接收到1个字符“s”时,停止持续发送“hello windows!”; 当接收到1个字符“t”时,持续发送“hello windows!”(提示:采用一个全局标量做信号灯)

代码如下:

main.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include "Serial.h"
#include "Delay.h"
#include <string.h>

int main(void)
{
	Serial_Init();
	while(1)
	{
		if(a==1)
		{         
			Serial_SendString("hello windows!\n");
					Delay_ms(800);					
		}
			else if(a==0)
			{
				
			}
	}
} 

Serial.c

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include <stdio.h>
#include <string.h>

uint8_t Serial_RxFlag;//定义一个接收标志位
int a=0;

void Serial_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	
	//输出
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;   //复用推挽输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	//串口输入
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//串口初始化
	USART_InitStruct.USART_BaudRate = 9600;//设置波特率
	USART_InitStruct.USART_HardwareFlowControl =USART_HardwareFlowControl_None; 
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送 | 接收
	USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStruct);
	
	//开启中断
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	
	//开启外设
	USART_Cmd(USART1,ENABLE);
}



uint8_t Serial_GetRxFlag(void) //用来返回一个是否接收完一个字符串的标志
{
	if(Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}


void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	
}
//发送字符串
void Serial_SendString(char *String) 
{
	uint8_t i;
	for(i=0;String[i]!=0;i++)
	{
		Serial_SendByte(String[i]);
	}
	
}

//中断函数
void USART1_IRQHandler()
{
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
	{
		u8 RxData = USART_ReceiveData(USART1);
		if(RxData=='s')//如果是s,则返回全局变量0
		{a=0;
		}
		if(RxData=='t')//如果是t,则返回全局变量1
		{a=1;
		}
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

Serial.h

#ifndef _Serial_H
#define _Serial_H
extern int a;//这里的a是全局变量,所以要加extern
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendString(char *String);
uint8_t Serial_GetRxFlag(void);
#include <stdio.h>
#endif

实验现象:

题目:当stm32接收到字符“stop stm32!”时,停止持续发送“hello windows!”; 当接收到字符“go stm32!”时,持续发送“hello windows!”(提示:要将接收到的连续字符保存到一个字符数组里,进行判别匹配。写一个接收字符串的函数。 

main.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include "Serial.h"
#include "Delay.h"
#include <string.h>

int main(void)
{
	Serial_Init();
	while(1)
	{
		if(Serial_GetRxFlag()==1)//数组存入完毕标志
		{         
			if(strcmp(Serial_RxPacket,"go stm32!")==0)
			{
				while(1)
				{
					Serial_SendString("hello windows!\n");
					Delay_ms(800);
					
					if(strcmp(Serial_RxPacket,"stop stm32!")==0)
					{
						break;
					}
				}
				
			}
		}
		
	}
} 

Serial.c

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include <stdio.h>
#include <string.h>

uint8_t Serial_RxFlag;
char Serial_RxPacket[100];

void Serial_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//打开PA9和PA10的时钟
	
	
	//输出
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;   //复用推挽输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	//串口输入
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//串口初始化
	USART_InitStruct.USART_BaudRate = 9600;//波特率
	USART_InitStruct.USART_HardwareFlowControl =USART_HardwareFlowControl_None;  
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//TX | RX
	USART_InitStruct.USART_Parity = USART_Parity_No;
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStruct);
	
	//开启中断
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	
	//开启外设
	USART_Cmd(USART1,ENABLE);
}



uint8_t Serial_GetRxFlag(void) //用来返回一个是否接收完一个字符串的标志
{
	if(Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}


void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	
}
//发送字符串
void Serial_SendString(char *String) 
{
	uint8_t i;
	for(i=0;String[i]!=0;i++)
	{
		Serial_SendByte(String[i]);
	}
	
}

//中断函数
void USART1_IRQHandler()
{
	static u8 number = 0;//数组下标
	static u8 Rxstate = 0; //判断是否读取完毕
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
	{
		u8 RxData = USART_ReceiveData(USART1);
		
		if(Rxstate==0)
		{
			if(RxData=='[')//起始标志符为'['
			{
				memset(Serial_RxPacket,'\0',sizeof(Serial_RxPacket));
				number = 0;
				Rxstate=1;
			}
		}
		else if(Rxstate==1)
		{
			if(RxData==']')//结束标志符为']'
			{
				Rxstate=0;	
				Serial_RxFlag=1;
			}
			else
			{
				Serial_RxPacket[number] = RxData;
				number++;
			}	
		}
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

实验现象:

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值