8路LED的二进制加法器
本文对应的A51代码,请参考http://blog.csdn.net/fly928sky/article/details/7666037
代码如下:
regs.h
#ifndef _REGS_H #define _REGS_H #include <reg52.h> #include <intrins.h> #define U8 unsigned char #define U16 unsigned int #define U32 unsigned long int U8 code tableLightWater[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE}; U8 code ZtableaddL[]={0xFF,0x7F,0xBF,0x3F,0xDF,0x5F,0x9F,0x1F,0xEF,0x6F,0xAF,0x2F,0xCF,0x4F,0x8F,0x0F}; //正着看对应开发板上右边4个LED灯的0~15 U8 code ZtableaddH[]={0xFF,0xF7,0xFB,0xF3,0xFD,0xF5,0xF9,0xF1,0xFE,0xF6,0xFA,0xF2,0xFC,0xF4,0xF8,0xF0}; //正着看对应开发板上左边4个LED灯的0~15 U8 code DtableaddL[]={0xFF,0xFE,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6,0xF5,0xF4,0xF3,0xF2,0xF1,0xF0}; //倒着看对应开发板上右边4个LED灯的0~15 U8 code DtableaddH[]={0xFF,0xEF,0xDF,0xCF,0xBF,0xAF,0x9F,0x8F,0x7F,0x6F,0x5F,0x4F,0x3F,0x2F,0x1F,0x0F}; //倒着看对应开发板上左边4个LED灯的0~15 #endif
说明:
1.tableLightWater[ ]属于流水灯需要用到的table,请在本程序中忽略存在。
2.ZtableaddL[ ]和DtableaddL[ ]对应真实开发板上的起始位置不同。ZtableaddL[ ]的对应H位为DS9,对应L位为DS2;DtableaddL[ ]则正好相反。
电路图请参考http://blog.csdn.net/fly928sky/article/details/7645591
led_add.c
#include "regs.h" sbit Wei = P1^1; sbit Duan = P1^0; sbit Line = P1^3; sbit LedEn = P1^2; void init(); void delayMS(U16); void displayAdd1(); void displayAdd2(); void main() { init(); //displayAdd1(); displayAdd2(); } void init() //初始化函数 { Wei=1; //关闭数码管 P0=0xFF; Wei=0; Line=1; //关闭点阵 P0=0x00; Line=0; } void delayMS(U16 z) //延时函数 { U16 x,y; for(x=z;x>0;x--) for(y=124;y>0;y--); } void displayAdd1() { int flagH,flagL; //标志位 int num,numH,numL; //二进制对应数值 int i; flagH=0; num=0; while(1) { numH=num/16; numL=num%16; if(numH==16) //加满判断,全部初始化 { num=0; flagH=0; } if(!flagH) //高4位为零执行 { for(i=0;i<16;i++) { LedEn=1; P0=DtableaddL[i]; LedEn=0; delayMS(500); num++; if(num==16) { flagL=0; flagH=1; } } } else { if(!flagL) { LedEn=1; P0=DtableaddH[numH]; LedEn=0; num++; delayMS(500); flagL=1; } else { for(i=1;i<16;i++) { LedEn=1; P0=DtableaddH[numH] & DtableaddL[i]; LedEn=0; delayMS(500); num++; if(num%16==0) { flagL=0; } } } } } } void displayAdd2() { int num,numH,numL; //二进制对应数值 num=0; while(1) { numH=num/16; numL=num%16; if(numH==16) //加满判断,全部初始化 { num=numH=numL=0; } for(;numL<16;numL++) { LedEn=1; P0=ZtableaddH[numH] & ZtableaddL[numL]; LedEn=0; delayMS(500); num++; } } }