加法器

2019-09-19 424点热度 2人点赞 0条评论

最近在温故一本书,为LPC1765编程打点基础,先用51单片机练练手吧

以下是一个简单的加法器程序
[highlight lanaguage="C"]

#include 
#include "macro_and_const"

sbit ADDR0 = P1^0;  //数码管片选
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

sbit KeyOut4 = P2^0;    //矩阵按键引脚
sbit KeyOut3 = P2^1;
sbit KeyOut2 = P2^2;
sbit KeyOut1 = P2^3;
sbit KeyIn1 = P2^4;
sbit KeyIn2 = P2^5;
sbit KeyIn3 = P2^6;
sbit KeyIn4 = P2^7;

uint8 code LedChar[] = { //数码管真值表
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E            
};

uint8 LedBuff[8] = {
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF;
};

uint8 KeySta[4][4] = {
    {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}  
}; 

uint8 code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
    { 0x31, 0x32, 0x33, 0x26 }, //数字键 1、数字键 2、数字键 3、向上键
    { 0x34, 0x35, 0x36, 0x25 }, //数字键 4、数字键 5、数字键 6、向左键
    { 0x37, 0x38, 0x39, 0x28 }, //数字键 7、数字键 8、数字键 9、向下键
    { 0x30, 0x1B, 0x0D, 0x27 }  //数字键 0、ESC 键、 回车键、 向右键
};

KeyDriver();

void main()
{
    EA = 1;      //开总中断
    TMOD = 0x01; //定时器模式1
    TH0 = 0xFC;  //装初值
    TL0 = 0x67;
    ET0 = 1;     //使能T0
    TR0 = 1;     //启动T0
    LedBuff[0] = LedChar[0];    //数码管默认显示0
    while(1)
    {
        KeyDriver();
    }
}


/*数字显示程序*/
void ShowNumber(uint32 num)
{
    uint8 buf[8];
    signed char i;   //注意i为有符号变量

    for(i=0; i<8; i++) { buf[i] = num % 10; num = num / 10; } for(i=7; i>=1; i--)
    {
        if(buf[i] == 0)
        {
            LedBuff[i] = 0xFF;
        }
        else 
            break;
    }

    for( ; i>0; i--)
    {
        LedBuff[i] = LedChar[buf[i]];       
    }
}

/*按键执行程序*/
void KeyAction(uint8 keycode)
{
    static uint32 addend = 0;
    static uint32 result = 0;

    if((keycode >= 0x30) && (keycode <= 0x39))
    {
        addend = (addend * 10 + (keycode - 0x30));
        ShowNumber(addend); 
    }
    else if(keycode == 0x26)
    {
        result += addend;
        addend = 0;
        ShowNumber(result);
    }
    else if(keycode == 0x0D)
    {
        result += addend;
        addend = 0;
        ShowNumber(result);     
    }
    else if(keycode == 0x1B)
    {
        addend = 0;
        result = 0;
        ShowNumber[addend];
    }
}

/*按键驱动程序*/
void KeyDriver()
{
    uint8 i, j;
    static uint8 KeyBackup[4][4] = {   //注意这里是static变量
        {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}  
    }; 

    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            if(KeySta[i][j] != KeyBackup[i][j])
            {
                if(Keybackup[i][j] != 0)
                {
                    KeyAction(KeyCodeMap[i][j]);
                }
                Keyback[i][j] = KeySta[i][j];
            }
        }
    }
}

/*数码管扫描程序*/
void LedScan()
{
    stastic uint8 i = 0;

    P0 = 0xFF;
    switch(i)
    {
        case 0: ADDR0=0; ADDR1=0; ADDR2=0; i++; P0 = LedBuff[0]; break;
        case 1: ADDR0=0; ADDR1=0; ADDR2=1; i++; P0 = LedBuff[1]; break;
        case 2: ADDR0=0; ADDR1=1; ADDR2=0; i++; P0 = LedBuff[2]; break;
        case 3: ADDR0=0; ADDR1=1; ADDR2=1; i++; P0 = LedBuff[3]; break;
        case 4: ADDR0=1; ADDR1=0; ADDR2=0; i++; P0 = LedBuff[4]; break;
        case 5: ADDR0=1; ADDR1=0; ADDR2=1; i++; P0 = LedBuff[5]; break;
        case 6: ADDR0=1; ADDR1=1; ADDR2=0; i++; P0 = LedBuff[6]; break;
        case 7: ADDR0=1; ADDR1=1; ADDR2=1; i=0; P0 = LedBuff[7]; break; 
        default: break;
    }         
}

/*按键扫描程序*/
void KeyScan()
{
    uint8 i;
    static uint8 keyout = 0;
    uint8 KeyBuff[4][4] = {
        {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}, 
        {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
    };
    
    KeyBuff[keyout][0] = (KeyBuff[keyout][0] << 1) | Key_In1;
    KeyBuff[keyout][1] = (KeyBuff[keyout][1] << 1) | Key_In2;
    KeyBuff[keyout][2] = (KeyBuff[keyout][2] << 1) | Key_In3;
    KeyBuff[keyout][3] = (KeyBuff[keyout][3] << 1) | Key_In4;

    for(i=0; i<4; i++)
    {
        if((KeyBuff[keyout][i] & 0x0F) == 0x0F)
        {
            KeySta[keyout][i] = 1;
        }
        else if((KeyBuff[keyout][i] & 0x0F) == 0x00)
        {
            KeySta[keyout][i] = 0;  
        }
    }
    keyout++;
    keyout = keyout & 0x03;
    switch(keyout)
    {
        case 0: KeyOut4 = 1; KeyOut1 = 0; break;
        case 1: KeyOut1 = 1; KeyOut2 = 0; break;
        case 2: KeyOut2 = 1; KeyOut3 = 0; break;
        case 3: KeyOut3 = 1; KeyOut4 = 1; break;
        default: break;
    }   
}

/*中断服务程序*/
void InterruptTimer0() interrupt 1
{
    TH0 = 0xFC;  //重装初值
    TL0 = 0x67;
    
    LedScan();
    KeyScan();
}
close

订阅博客,及时获取文章更新通知

订阅博客,及时获取文章更新通知

古月弧

文章评论