#include
#define uchar unsigned char
#define uint unsigned int
uchar Bits=0,Ten=0,Hundred=0,Thousand=0;
uchar code ledshow[10]={ 0x3F,/*0*/
0x06,/*1*/
0x5B,/*2*/
0x4F,/*3*/
0x66,/*4*/
0x6D,/*5*/
0x7D,/*6*/
0x07,/*7*/
0x7F,/*8*/
0x6F,/*9*/} ; //共阴数码管代码
uchar code jz_code[17]={0xff,
0xee,0xed,0xeb,0xe7,
0xde,0xdd,0xdb,0xd7,
0xbe,0xbd,0xbb,0xb7,
0x7e,0x7d,0x7b,0x77}; //键盘代码
uint code Data_square[]={0,
1,4,9,16,
25,36,49,64,
81,100,121,144,
169,196,225,256}; //平方
void delay(unsigned char a) //延时
{
unsigned char i;
while(a--)
for(i=100;i>0;i--);
}
uchar kbscan(void) //键盘扫描
{
unsigned char hang,lie,key;
P1=0xf0;
delay(5);
lie=P1;
P1=0x0f;
delay(5);
hang=P1;
for(key=16;key>0;key--)
{
if(jz_code[key]==(hang|lie)) break;
}
return (key);
}
void data_bcd(void) //分解平方后的结果
{
uchar i;
i= kbscan();
if(i!=0)
{
Bits=Data_square[i]%10; //个位
Ten=(Data_square[i]%100)/10; //十位
Hundred=(Data_square[i]%1000)/100;//百位
Thousand=Data_square[i]/1000; //千位
}
}
void display(void)//数码管显示,调试方法:数码管间隔跳动函数中 delay(5);四个,调小
{
data_bcd();
P0=ledshow[Thousand];
P2_0=0;
delay(5);
P2_0=1;
P0=ledshow[Hundred];
P2_1=0;
delay(5);
P2_1=1;
P0=ledshow[Ten];
P2_2=0;
delay(5);
P2_2=1;
P0=ledshow[Bits];
P2_3=0;
delay(5);
P2_3=1;
}
void main(void)
{
P2=0xff;
while(1)
{
display();
}
}
#include
#define uchar unsigned char
#define uint unsigned int
sbit beep=P3^6;
sbit elre=P3^7;
sbit K1=P1^4;
sbit K2=P1^5;
sbit K3=P1^6;
sbit K4=P1^7;
uchar code tabled[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delay(uint a)
{
uint i,j;
for(j=a;j>0;j--)
for(i=110;i>0;i--);
}
void key_scan()
{
if(K1==0)
{
uint a;
delay(10);
if(K1==0)
{
while(!K1);
for(a=100;a>0;a--)
{
beep=0;
delay(1);
beep=1;
delay(1);
}
}
}
if(K2==0)
{
uchar j;
delay(10);
if(K2==0)
{
while(!K2);
for(j=0;j<16;j++)
{
P2=0;
P0=tabled[j];
delay(1000);
P0=0xff;
}
}
}
if(K3==0)
{
delay(10);
if(K3==0)
{
while(!K3);
elre=0;
delay(1000);
elre=1;
delay(1000);
}
}
if(K4==0)
{
delay(10);
if(K4==0)
{
while(!K4);
P1=0xaa;
P0=~0xaa;
delay(10000);
P1=0xff;
P0=0xff;
}
}
}
void main()
{
while(1)
{
key_scan();
}
}
这是我自己写的,肯定对的,控制部分是用的三极管
如果你得是锁存器我也可以发给你