实验三 STM32外中断控制数码管显示

利用已经建立的工程模板,在Keil for ARM V5中编写代码,使用数码管动态显示电路,实现如下功能:配置相应的GPIO口为外中断口,数码管初始显示数字000,按下KEY1,使数字加1,加到999为止;按下KEY2使数字减1,减到0为止;按下KEY3,数码管显示位置左移一位,直到顶格为止;按下KEY4,数码管显示位置右移一位,直到顶格为止。

main.c

int table[] = {0 , 0 , 0};
uint8_t size[] = {0x3f , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d , 0x7d , 0x07 , 0x7f , 0x6f};
int wei = 0;
uint8_t v[] = {0x20 , 0x10 , 0x08 , 0x04};

int main()
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	ST_Init();
	int temp;
	while(1)
	{
		temp = ~(v[wei]);
		for(int i = 0 ; i < 3 ; i++)
		{	
			GPIO_Write(GPIOB , temp);
			GPIO_Write(GPIOC , size[table[i]]);
			temp = temp >> 1;
			delay(100);
		}
	}
}

st.c

#include "st.h"

extern int table[];
extern int wei;

void ST_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA , ENABLE); 
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC, &GPIO_InitStructure); 

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3); 
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4);
	
	EXTI_InitTypeDef EXTI_InitStructure;
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line1 | EXTI_Line2 | EXTI_Line3 | 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);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
	NVIC_Init(&NVIC_InitStructure);
	
}	

void EXTI1_IRQHandler()
{
	if(table[0] != 9 || table[1] != 9 || table[2] != 9)
	{
		if(table[2] == 9)
		{
			if(table[1] == 9)
			{
				table[0]++;
				table[1] = 0;
				table[2] = 0;
			}
			else
			{
				table[1]++;
				table[2] = 0;
			}
		}
		else table[2]++;
	}
	EXTI_ClearITPendingBit(EXTI_Line1);
}

void EXTI2_IRQHandler()
{
	if(table[0] != 0 || table[1] != 0 || table[2] != 0)
	{
		if(table[2] == 0)
		{
			if(table[1] == 0)
			{
				table[0]--;
				table[1] = 9;
				table[2] = 9;
			}
			else
			{
				table[1]--;
				table[2] = 9;
			}
		}
		else table[2]--;
	}
	EXTI_ClearITPendingBit(EXTI_Line2);
}

void EXTI3_IRQHandler()
{
	if(wei != 0)
	{
		wei--;
	}
	EXTI_ClearITPendingBit(EXTI_Line3);
}

void EXTI4_IRQHandler()
{
	if(wei != 3)
	{
		wei++;
	}
	EXTI_ClearITPendingBit(EXTI_Line4);
}

st.h

#ifndef __ST_H
#define __ST_H

#include "sys.h"

void ST_Init(void);

#endif

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值