Keil下,基于STM32F103单片机的按键中断的几种LED实例

Keil下,基于STM32F103单片机的按键中断的几种LED实例(流水、奇亮偶灭等)

这里是通过实验室的单片机实现的(皮赛公司的)
由于Keil版本的问题,5.15版本以下的版本使用C/C++开发单片机需要在option for target里的C/C++栏里对标准固件库进行宏定义(USE_STDPERIPH_DRIVER),不然无法识别stm32f10x.h的头文件
新建工程、选择stm32f103zc芯片,勾选core,startup,framework,rcc,gpio,然后添加C源文件。下面是源码。
****1、拨动实验箱上的拨码开关 DIP0、 DIP1、 DIP2、 DIP3 、 DIP4、 DIP5、 DIP6、DIP7 来控制对应的 LED 灯 D0、 D1、 D2、 D3 ,D4、 D5、D6、 D7 的亮灭。

#include"stm32f10x.h"
#define ON   Bit_SET     //GPIO的布尔变量,用于GPIO_WriteBit函数
#define OFF  Bit_RESET   //GPIO的布尔变量,用于GPIO_WriteBit函数
#define LED0(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_11, x)
#define LED1(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_10, x)
#define LED2(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_9, x)
#define LED3(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_7, x)
#define LED4(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_3, x)
#define LED5(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_2, x)
#define LED6(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_13, x)
#define LED7(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_12, x)
#define DIP0  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define DIP1  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5)
#define DIP2  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)
#define DIP3  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_15)
#define DIP4  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_0)
#define DIP5  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_1)
#define DIP6  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_2)
#define DIP7  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_3)
void GPIO_Configuration(void);
void DIP_Init(void);

int main(void)
{  

		GPIO_Configuration();
   	DIP_Init();
  while(1)
  {  
					if(DIP0==1)
					{
								LED0(ON);
					}else
						LED0(OFF);
					if(DIP1==1)
					{
								LED1(ON);
					}else
						LED1(OFF);
					if(DIP2==1)
					{
								LED2(ON);
					}else
						LED2(OFF);
					if(DIP3==1)
					{
								LED3(ON);
					}else
						LED3(OFF);
					if(DIP4==1)
					{
								LED4(ON);
					}else
						LED4(OFF);
					if(DIP5==1)
					{
								LED5(ON);
					}else
						LED5(OFF);
					if(DIP6==1)
					{
								LED6(ON);
					}else
						LED6(OFF);
					if(DIP7==1)
					{
								LED7(ON);
					}else
						LED7(OFF);
  }
}

void DIP_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOF, ENABLE);//使能PA,PF端口时钟

  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4 | GPIO_Pin_5 ;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成上拉输入
  GPIO_Init(GPIOE, &GPIO_InitStructure);
	
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_14 | GPIO_Pin_15 ;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成上拉输入
  GPIO_Init(GPIOC, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成上拉输入
  GPIO_Init(GPIOF, &GPIO_InitStructure);

}
void GPIO_Configuration(void)
{
   GPIO_InitTypeDef  GPIO_InitStructure;
  //按键初始化函数  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOD, ENABLE);//使能PA,PB,PC,PG,PF端口时钟 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10 | GPIO_Pin_9 | GPIO_Pin_2 | GPIO_Pin_3;				 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
 GPIO_Init(GPIOG, &GPIO_InitStructure);					 
 GPIO_ResetBits(GPIOC,GPIO_Pin_11 | GPIO_Pin_10 | GPIO_Pin_7 | GPIO_Pin_9 | GPIO_Pin_2 | GPIO_Pin_3);//LED1,LED2,LED3,LED4,LED5

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 |GPIO_Pin_12 | GPIO_Pin_13;				 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
 GPIO_Init(GPIOD, &GPIO_InitStructure);				 //根据设定参数初始
 GPIO_ResetBits(GPIOD, GPIO_Pin_7 |GPIO_Pin_12|GPIO_Pin_13);                  //LED6,LED7		 

}

频率设置为8MHZ,creat HEX files,链接然后编译。
将 USB 线一端连接至计算机,一端连接至实验箱 USB 调试口。
打开 Keil-MDK,设置好 J-Link 下载模式。 FLASH->configure flash tools->Debug,选择 J-Link/J-trace Cortex。
在这里插入图片描述
点击Settings,进行设置。
在这里插入图片描述
点击Utilities :选择J-LINK/J-Trace cortex。在这里插入图片描述
然后点击Settings,进行下图设置。
在这里插入图片描述
插上电源线和JTAG转USB下载线,打开试验箱上开关,将BOOT0和BOOT1置零,并且按下复位键。点击下载按钮下载程序下好程序后点击仿真按钮 ,然后点击全速运行按钮 ,就可以在实验箱上看到对应的实验现象了。
在这里插入图片描述
下面是定义了流水灯,奇数灯,偶数灯亮等的源代码
实现流水灯:
在上述代码预编译当中进行延时子程序的函数声明,然后将延时函数添加到代码中。将流水灯程序写到主函数当中,这里是用DIPO0控制流水灯。
奇数灯亮(偶数灯亮同理,改一下LED口就行)

#include"stm32f10x.h"
#define ON   Bit_SET     //GPIO的布尔变量,用于GPIO_WriteBit函数
#define OFF  Bit_RESET   //GPIO的布尔变量,用于GPIO_WriteBit函数
#define LED0(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_11, x)
#define LED1(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_10, x)
#define LED2(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_9, x)
#define LED3(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_7, x)
#define LED4(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_3, x)
#define LED5(x)  GPIO_WriteBit(GPIOG, GPIO_Pin_2, x)
#define LED6(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_13, x)
#define LED7(x)  GPIO_WriteBit(GPIOD, GPIO_Pin_12, x)
#define DIP0  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define DIP1  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5)
#define DIP2  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)
#define DIP3  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_15)
#define DIP4  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_0)
#define DIP5  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_1)
#define DIP6  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_2)
#define DIP7  GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_3)
void delay_ms(unsigned short Number);    //延时子程序的函数声明
void GPIO_Configuration(void);
void DIP_Init(void);

int main(void)
{  

		GPIO_Configuration();
     	DIP_Init();
	
  while(1)
		
  {  
					if(DIP0==1)
					{
						LED0(ON);
		                delay_ms(50);
				 		LED0(OFF);
						LED1(ON);
						delay_ms(50);
						LED1(OFF);
						LED2(ON);
						delay_ms(50);
						LED2(OFF);
						LED3(ON);
						delay_ms(50);
						LED3(OFF);
						LED4(ON);
						delay_ms(50);
						LED4(OFF);
						LED5(ON);
						delay_ms(50);
						LED5(OFF);
						LED6(ON);
						delay_ms(50);
						LED6(OFF);
						LED7(ON);
						delay_ms(50);
						LED7(OFF);
						LED0(ON);
					}else
						LED0(OFF);
					//上述代码实现DIPO按键控制LED0~7从左向右流水工作


					if(DIP1==1)
					{
						LED7(ON);
		                delay_ms(50);
						LED7(OFF);
						LED6(ON);
						delay_ms(50);
						LED6(OFF);
						LED5(ON);
						delay_ms(50);
						LED5(OFF);
						LED4(ON);
						delay_ms(50);
						LED4(OFF);
						LED3(ON);
						delay_ms(50);
						LED3(OFF);
						LED2(ON);
						delay_ms(50);
						LED2(OFF);
						LED1(ON);
						delay_ms(50);
						LED1(OFF);
						LED0(ON);
						delay_ms(50);
						LED0(OFF);
						LED7(ON);
					}else
						LED1(OFF);
					//DIP1按键控制LED0~7从右向左流水工作


					if(DIP2==1)
					{
						LED0(ON);
						LED2(ON);
						LED4(ON);
						LED6(ON);
					}else
						LED2(OFF);
					//DIP2按键控制LED0\2\4\6偶数灯亮

					if(DIP3==1)
					{
						LED1(ON);
						LED3(ON);
						LED5(ON);
						LED7(ON);	
					}else
						LED3(OFF);
					//DIP3按键控制LED1\3\5\7奇数灯亮

					if(DIP4==1)
					{
						LED1(ON);
						LED3(ON);
						delay_ms(500);
						LED1(OFF);
						LED3(OFF);
						delay_ms(500);
						LED1(ON);
						LED4(ON);
						delay_ms(500);
						LED1(OFF);
						LED4(OFF);
						delay_ms(500);
						LED5(ON);
						LED2(ON);
						LED0(ON);
						delay_ms(1000);	
					}else
						LED4(OFF);
					//DIP4实现13-14-520
						
					if(DIP5==1)
					{
						LED0(ON);
						LED1(ON);
						LED2(ON);
						delay_ms(500);
						LED3(ON);
						LED4(ON);
						LED5(ON);
						delay_ms(500);
						LED0(OFF);
						LED1(OFF);
						LED2(OFF);
						LED3(OFF);
						LED4(OFF);
						LED5(OFF);
						delay_ms(50);
						
						LED0(ON);
						delay_ms(50);
						LED0(OFF);
						LED1(ON);
						delay_ms(50);
						LED1(OFF);
						LED2(ON);
						delay_ms(50);
						LED2(OFF);
						LED3(ON);
						delay_ms(50);
						LED3(OFF);
						LED4(ON);
						delay_ms(50);
						LED4(OFF);
						LED5(ON);
						delay_ms(50);
						LED5(OFF);
						LED6(ON);
						delay_ms(50);
						LED6(OFF);
						LED7(ON);
						delay_ms(50);
						LED7(OFF);
						delay_ms(450);
						
						LED7(ON);
		                delay_ms(50);
						LED7(OFF);
						LED6(ON);
						delay_ms(50);
						LED6(OFF);
						LED5(ON);
						delay_ms(50);
						LED5(OFF);
						LED4(ON);
						delay_ms(50);
						LED4(OFF);
						LED3(ON);
						delay_ms(50);
						LED3(OFF);
						LED2(ON);
						delay_ms(50);
						LED2(OFF);
						LED1(ON);
						delay_ms(50);
						LED1(OFF);
						LED0(ON);
						delay_ms(50);
						LED0(OFF);
						LED7(ON);
						delay_ms(50);
                        LED7(OFF);
                        delay_ms(450);
					}else
						LED5(OFF);
					//DIP5实现012-345-左到右流水-右到左流水
						
					if(DIP6==1)
					{
						LED1(ON);
						LED3(ON);
						LED5(ON);
						LED7(ON);
                        delay_ms(450);
						LED1(OFF);
						LED3(OFF);
						LED5(OFF);
						LED7(OFF);
						delay_ms(450);
                        LED0(ON);
						LED2(ON);
						LED4(ON);
						LED6(ON);	
						delay_ms(450);
						LED0(OFF);
						LED2(OFF);
						LED4(OFF);
						LED6(OFF);		
					}else
						LED6(OFF);
					//按键DIP6实现1357-0246

					if(DIP7==1)
					{
						LED7(ON);
						delay_ms(100);
						LED7(OFF);
						delay_ms(100);
							
					}else
						LED7(OFF);
					//按键DIP7实现LED7闪烁
  }
}
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Keil5中使用STM32F103的RS485通信,可以使用USART的中断函数来实现。以下是一个简单的例子: 1. 首先需要设置USART的参数,可以参考如下代码: ```c void USART_Config(void) { /* Enable GPIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* Enable USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure USART Tx and Rx as alternate function push-pull */ 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); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART */ USART_InitStructure.USART_BaudRate = 9600; 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); /* Enable USART interrupts */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable USART */ USART_Cmd(USART1, ENABLE); } ``` 2. 然后需要实现中断函数,可以参考如下代码: ```c void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { /* Clear RXNE interrupt flag */ USART_ClearITPendingBit(USART1, USART_IT_RXNE); /* Read data from USART */ uint16_t data = USART_ReceiveData(USART1); /* Process received data */ // ... } } ``` 在中断函数中,首先要判断中断源是否为接收数据,然后读取USART接收寄存器中的数据,并进行处理。 需要注意的是,RS485通信需要在发送数据前将控制引脚置为高电平,发送完成后再将控制引脚置为低电平。此外,还需要根据具体的通信协议实现数据处理部分。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值