利用已经建立的工程模板,在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