1.代码:
#include <reg52.h>
typedef unsigned char u8;typedef unsigned int u16;sbit seg_sel = P1^4;sbit bit_sel = P1^5;#define DATA P0//code 在程序编译的时候 当作代买直接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 delay(120); seg_sel = 1;//Q[7..0]=D[7..0] DATA = seg_tab[which_number]; seg_sel = 0; delay(120); }void main (void){ u8 num; P0 = 0xff; P1 = 0xff;//端口初始化 seg_sel = 0;//LE禁止锁存 bit_sel = 0; while (1) { //8 个灯都亮 for(num=8; num>=1; num--) display_led(num, 5); }}2.电路图:
3.效果: