上面是电路图,J16和J15相连,J6的321对应P2^0--P2^2,这样连有利于节省单片机的资源。其中P1^0和LE相连,段选(决定数码管显示什么东东),位选(哪一个数码管亮)由138译码器决定。我们先写程序吧:
#include<reg51.h> #define uint unsigned int #define uchar unsigned char //加上code表示将这个存放在ROM当中,否则存放在RAM当中
//RAM存储空间很小,51的只有128Byte uchar code table_smg[8] = {0x3f, 0x06, 0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07}; uchar temp; uchar flag = 0; void initTimer0(); void main() { int i; initTimer0(); P1 = 0x01; temp = 0x00; while(1) { for(i = 0; i < 8; i++) { while(flag != 1); if(flag == 1) { flag = 0; if(temp == 0x08) { temp = 0x00; } P2 = temp; P0 = table_smg[i]; temp = temp + 0x01; } } } } void initTimer0() { TMOD = 0x01; TH0 = (65536-5000)/256; TL0 = (65536-5000)%256; ET0 = 1; EA = 1; TR0 = 1; } void timer0() interrupt 1 { TH0 = (65536-5000)/256; TL0 = (65536-5000)%256; flag++; }要注意锁存器和138译码器的使用,及其定时器中断。
原文地址:http://blog.csdn.net/kotei_88_luluc_66/article/details/40146417