代码实现
#include <REGX52.H>
#include <intrins.h>
#define SMG_PORT P0
#define Bit_Choose P2
#define PORT P1/ * 按键引脚 */
typedef unsigned char uint8_t;
uint8_t row = 0, column = 0;
unsigned char i,X,Y;
int gsmg_code[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0xff,0x39,0xbf,0x79,0x71};
void Delay100us(void);
void Delay100ms(void);
void smg_display(unsigned long j)
{
unsigned long x = 1;
uint8_t num[8] = {0};
uint8_t digit = 0;
unsigned long lat = j;
do{
lat = lat / 10;
digit++;
}
while(lat != 0);
for(i = 0;i <digit;i++)
{
num[i] = j % (10 * x) / x;
x *= 10;
}
for(i=0;i<digit;i++)
{
Bit_Choose = i << 2;
SMG_PORT = gsmg_code[num[i]];
Delay100us();
SMG_PORT = 0x00;
}
}
void scan_key()
{
PORT = 0xF0;
if(PORT != 0xF0){
Delay100us();
X = PORT & 0xF0;
PORT = 0x0F;
Y = PORT & 0x0F;
switch(X)
{
case 0x70:
row = 0;
break;
case 0xB0:
row = 1;
break;
case 0xD0:
row = 2;
break;
case 0xE0:
row = 3;
break;
}
if(X != 0xF0){
switch(Y)
{
case 0x07 :
column = 0;
break;
case 0x0b :
column = 1;
break;
case 0x0d :
column = 2;
break;
case 0x0e :
column = 3;
break;
}
}
}
smg_display(row * 4 + column);
}
void main()
{
while(1)
{
scan_key();
}
}
void Delay100us(void)
{
unsigned char data i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
void Delay100ms(void)
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}