单片机源程序如下:
- #include <reg52.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- sbit seg_sel = P1^4;
- sbit bit_sel = P1^5;
- #define DATA P0
- u8 code seg_tab[]={
- 0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e
- };
- u8 code bit_tab[]={
- 0x10,0x20,0x40,0x80,
- 0x01,0x02,0x04,0x08
- };
- void delay(u16 num)
- {
- u16 x,y;
- for(x=num; x>0; x--)
- for(y=110; y>0; y--)
- {
- ;//延时1ms
- }
- }
- void display_led(u8 which_bit, u8 which_number)
- {
- bit_sel = 1;//Q[7..0]=D[7..0]
- DATA = bit_tab[which_bit];
- bit_sel = 0;//BIT[7..0]=0x80
- seg_sel = 1;//Q[7..0]=D[7..0]
- DATA = seg_tab[which_number];
- seg_sel = 0;
- delay(2);
- }
- u8 key_scan (void)
- {
- u8 temp,temp2;
- P2 = 0xf0;//让P2输出0xf0
- temp = P2;//读入P2的值
- if (temp != 0xf0) //当P2读入的值不等于0xf0
- {
- delay(5);//延迟5ms
- temp = P2;//再读入P2口的值
- if (temp != 0xf0)//如果P2读入的值不等于
- { //0xf0,就说明有按键按下
- temp2 = temp & 0xf0;//保留P2读入值得高四位
- P2 = 0x0f;//再让P2口输出0x0f
- delay(1);
- temp = P2;//再读入P2口的值
- temp2 |= temp;//保留此读入值的低四位
- return temp2;
- }
- }
- }
- u8 encode(u8 cod)
- {
- u8 num;
- switch(cod)
- {
- case 0xee: num = 0;break;
- case 0xde: num = 1;break;
- case 0xbe: num = 2;break;
- case 0x7e: num = 3;break;
- case 0xed: num = 4;break;
- case 0xdd: num = 5;break;
- case 0xbd: num = 6;break;
- case 0x7d: num = 7;break;
- case 0xeb: num = 8;break;
- case 0xdb: num = 9;break;
- case 0xbb: num = 10;break;
- case 0x7b: num = 11;break;
- case 0xe7: num = 12;break;
- case 0xd7: num = 13;break;
- case 0xb7: num = 14;break;
- case 0x77: num = 15;break;
- default : break;
- }
- return num;
- }
- void main (void)
- {
- u8 cod,num;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
详见文件:
LED Display.rar
(67.22 KB, 下载次数: 6)
|