//main.c
/***********************************************************
程序功能:独立按键控制数码管数字增加减小
KEY1 INT0控制数码管数字增加
KEY2 INT1控制数码管数字减小
*************************************************************/
#include <reg52.h>
#include "intrins.h"
#include "display.h"
#include "delay.h"
void EX0_Init();
void EX1_Init();
unsigned int NumCnt = 0; //四位数码管显示的数
void main()
{
EX0_Init();
EX1_Init();
EA = 1;
while(1)
{
ledbuf[0] = NumCnt/1000; //拆分NumCnt千位
ledbuf[1] = NumCnt/100%10; //拆分NumCnt百位
ledbuf[2] = NumCnt/10%10; //拆分NumCnt十位
ledbuf[3] = NumCnt%10; //拆分NumCnt个位
Display(); //数码管显示NumCnt
}
}
void EX0_Init()//外部中断0初始化
{
IT0 = 1;
EX0 = 1;
}
void EX1_Init()//外部中断1初始化
{
IT1 = 1;
EX1 = 1;
}
void EX0_ISR() interrupt 0//外部中断0中断服务函数
{
NumCnt++;
if(NumCnt>9999)
{
NumCnt = 0;
}
}
void EX1_ISR() interrupt 2//外部中断1中断服务函数
{
NumCnt--;
if(NumCnt>9999)
{
NumCnt = 9999;
}
}
//display.c 数码管显示程序
#include <reg52.h>
#include "intrins.h"
#include "display.h"
unsigned char code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
};
//数码管断码表
unsigned char ledbuf[]={1,2,3,4};//数据显示缓冲区
unsigned char code PLACE_CODE[]={0xFE,0xFD,0xFB,0xF7};//位选信号
void Display()
{
static unsigned char i=0;
//1.送段码
//2.送位码
//3.延时1ms小于10ms
//4.消隐
switch(i)
{
case 0:
GPI0_DIG = leddata[ledbuf[0]];
GPIO_PLACE = PLACE_CODE[0];
delay(1);
GPI0_DIG = 0x00;
i++;
break;
case 1:
GPI0_DIG = leddata[ledbuf[1]];
GPIO_PLACE = PLACE_CODE[1];
delay(1);
GPI0_DIG = 0x00;
i++;
break;
case 2:
GPI0_DIG = leddata[ledbuf[2]];
GPIO_PLACE = PLACE_CODE[2];
delay(1);
GPI0_DIG = 0x00;
i++;
break;
case 3:
GPI0_DIG = leddata[ledbuf[3]];
GPIO_PLACE = PLACE_CODE[3];
delay(1);
GPI0_DIG = 0x00;
i=0;
break;
default:break;
}
}
#include <reg52.h>
#include "delay.h"
void delay(unsigned int xms)
{
unsigned int x,y;
for(x=xms;x>0;x--)
{
for(y=127;y>0;y--);
}
}
执行代码后,通过Proteus仿真的显示界面如下: