单片机学习笔记-day02_数码管显示

Day2 2025.8.7

配置vscode实现51单片机开发

  1. 安装EIDE拓展
  2. 配置EIDE使用keil5作为编译器
  3. pip安装serial,不然不能烧录

按钮控制LED灯

#include <Atmel/REGX52.H>

void Delay(unsigned int xms)
{
    unsigned char i, j;
    while (xms--) {
        i = 2;
        j = 239;
        do {
            while (--j);
        } while (--i);
    }
}

/* void main()
{
    while (1) {  //按下开,再按下关
        if (P3_1 == 0) {
            Delay(20); // 消除按键抖动
            while (P3_1 == 0);
            Delay(20);
            P2_0 = ~P2_0;
        }
    }
} */

/* void main()
{
    unsigned char LEDNum = 0; // 占八位,正好对应一个八位寄存器
    while (1) {
        if (P3_1 == 0) { // 实现二进制计数
            Delay(20);
            while (P3_1 == 0);
            Delay(20);

            LEDNum++;
            P2 = ~LEDNum; // 取反
        }
    }
} */

void main()
{
    unsigned char LedNum = 0;
    P2                   = ~(0x01 << LedNum);

    while (1) { 
        if (P3_1 == 0) {
            Delay(20);
            if (LedNum >= 7) {
                LedNum = 0;
            } else {
                LedNum++;
            }
            P2 = ~(0x01 << LedNum);
            while (P3_1 == 0);
            Delay(20);
        } else if (P3_0 == 0) {
            Delay(20);
            if (LedNum <= 0) {
                LedNum = 7;
            } else {
                LedNum--;
            }
            P2 = ~(0x01 << LedNum);
            while (P3_0 == 0);
            Delay(20);
        }
    }
}

数码管控制

静态显示

数码管分为供阴极&供阳极,阳极1亮0灭,阴极相反。

#include <Atmel/REGX52.H>
//数码管段码表
unsigned char NixieTable[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void Nixie(unsigned char Location,Number) //Location表示第几个,Number表示显示几
{
    switch (Location) {
        case 1: P2_4 = 1; P2_3 = 1; P2_2 = 1; break;
        case 2: P2_4 = 1; P2_3 = 1; P2_2 = 0; break;
        case 3: P2_4 = 1; P2_3 = 0; P2_2 = 1; break;
        case 4: P2_4 = 1; P2_3 = 0; P2_2 = 0; break;
        case 5: P2_4 = 0; P2_3 = 1; P2_2 = 1; break;
        case 6: P2_4 = 0; P2_3 = 1; P2_2 = 0; break;
        case 7: P2_4 = 0; P2_3 = 0; P2_2 = 1; break;
        case 8: P2_4 = 0; P2_3 = 0; P2_2 = 0; break;
    }
    P0 = NixieTable[Number];
}

// 静态数码管
void main()
{
    Nixie(2,3);
    while (1) {
    }
}

动态显示

#include <Atmel/REGX52.H>
//数码管段码表
unsigned char NixieTable[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,Number); //数码管显示
void Delay(unsigned char xms);             //延迟

void main()
{
    while (1) 
    {
        Nixie(1,1);
        Nixie(2,2);
        Nixie(3,3);
        Nixie(4,4);
        Nixie(5,5);
        Nixie(6,6);
        Nixie(7,7);
        Nixie(8,8);
    }
}

void Nixie(unsigned char Location,Number)
{
    switch (Location) 
    {
        case 1: P2_4 = 1; P2_3 = 1; P2_2 = 1; break;
        case 2: P2_4 = 1; P2_3 = 1; P2_2 = 0; break;
        case 3: P2_4 = 1; P2_3 = 0; P2_2 = 1; break;
        case 4: P2_4 = 1; P2_3 = 0; P2_2 = 0; break;
        case 5: P2_4 = 0; P2_3 = 1; P2_2 = 1; break;
        case 6: P2_4 = 0; P2_3 = 1; P2_2 = 0; break;
        case 7: P2_4 = 0; P2_3 = 0; P2_2 = 1; break;
        case 8: P2_4 = 0; P2_3 = 0; P2_2 = 0; break;
    }
    P0 = NixieTable[Number];
    Delay(200);   //消影:解决数码管串位问题
    P0 = 0x00;
}
void Delay(unsigned char xms)
{
    unsigned char i, j;
    while (xms--) 
    {
        i = 2;
        j = 239;
        do {
            while (--j);
        } while (--i);
    }
}
posted @ 2025-08-08 01:10  Acholl  阅读(28)  评论(0)    收藏  举报