第七章 变量进阶和点阵LED

这里我们讲一讲什么是点阵LED,所谓点的点阵LED就是我们平时在大街上看到的电子滚动屏幕,用以显示店家需要展示的消息。
而深入来看,它是一个LED组成的点阵,如图所示
![点阵LED原理](https://img-blog.csdn.net/20170813102704299?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGhjaGVuY2hvbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
和我们中学学习的坐标类似,如果要想形成文字,便将点阵LED中的某些点电亮,展现各种各样的图形。
我们所看见的电子屏幕的滚动,实际上是图片的动态刷新。一次只移动一个LED,形成我们所看到的滚动屏幕。
下面是宋雪松老师提供的代码,详情访问www.kingst.org
#include<reg52.h>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

unsigned char code image[] = {
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7D,0x01,0x01,0x7D,0xFF,0xFF,0xE3,0xC1,
0x81,0x03,0x03,0x81,0xC1,0xE3,0xFF,0xFF,0x81,0x01,0x3F,0x3F,0x3F,0x01,0x81,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    };  
void main()
{
    EA = 1;
    ENLED = 0;
    ADDR3 = 0;
    TMOD = 0x01;
    TH0 = 0xFC;
    TL0 = 0x67;
    ET0 = 1;
    TR0 = 1;
    while(1);
}

void InterruptTimer0() interrupt 1
{
    static unsigned char i = 0;
    static unsigned char index = 0;
    static unsigned char tmr = 0;
    TH0 = 0xFC;
    TL0 = 0x67;

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

    tmr++;
    if(tmr >= 250)
    {
        tmr = 0;
        index++;
        if(index >= 32)
        {
            index = 0;
        }
    }


}
#include<reg52.h>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

    unsigned char code image[30][8] = {
    {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},  //动画帧1
    {0xFF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F},  //动画帧2
    {0xFF,0x3F,0x7F,0x7F,0x7F,0x7F,0x7F,0x3F},  //动画帧3
    {0xFF,0x1F,0x3F,0x3F,0x3F,0x3F,0x3F,0x1F},  //动画帧4
    {0xFF,0x0F,0x9F,0x9F,0x9F,0x9F,0x9F,0x0F},  //动画帧5
    {0xFF,0x87,0xCF,0xCF,0xCF,0xCF,0xCF,0x87},  //动画帧6
    {0xFF,0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3},  //动画帧7
    {0xFF,0xE1,0x73,0x73,0x73,0xF3,0xF3,0xE1},  //动画帧8
    {0xFF,0x70,0x39,0x39,0x39,0x79,0xF9,0xF0},  //动画帧9
    {0xFF,0x38,0x1C,0x1C,0x1C,0x3C,0x7C,0xF8},  //动画帧10
    {0xFF,0x9C,0x0E,0x0E,0x0E,0x1E,0x3E,0x7C},  //动画帧11
    {0xFF,0xCE,0x07,0x07,0x07,0x0F,0x1F,0x3E},  //动画帧12
    {0xFF,0x67,0x03,0x03,0x03,0x07,0x0F,0x9F},  //动画帧13
    {0xFF,0x33,0x01,0x01,0x01,0x03,0x87,0xCF},  //动画帧14
    {0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7},  //动画帧15
    {0xFF,0xCC,0x80,0x80,0x80,0xC0,0xE1,0xF3},  //动画帧16
    {0xFF,0xE6,0xC0,0xC0,0xC0,0xE0,0xF0,0xF9},  //动画帧17
    {0xFF,0x73,0x60,0x60,0x60,0x70,0x78,0xFC},  //动画帧18
    {0xFF,0x39,0x30,0x30,0x30,0x38,0x3C,0x7E},  //动画帧19
    {0xFF,0x9C,0x98,0x98,0x98,0x9C,0x1E,0x3F},  //动画帧20
    {0xFF,0xCE,0xCC,0xCC,0xCC,0xCE,0x0F,0x1F},  //动画帧21
    {0xFF,0x67,0x66,0x66,0x66,0x67,0x07,0x0F},  //动画帧22
    {0xFF,0x33,0x33,0x33,0x33,0x33,0x03,0x87},  //动画帧23
    {0xFF,0x99,0x99,0x99,0x99,0x99,0x81,0xC3},  //动画帧24
    {0xFF,0xCC,0xCC,0xCC,0xCC,0xCC,0xC0,0xE1},  //动画帧25
    {0xFF,0xE6,0xE6,0xE6,0xE6,0xE6,0xE0,0xF0},  //动画帧26
    {0xFF,0xF3,0xF3,0xF3,0xF3,0xF3,0xF0,0xF8},  //动画帧27
    {0xFF,0xF9,0xF9,0xF9,0xF9,0xF9,0xF8,0xFC},  //动画帧28
    {0xFF,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE},  //动画帧29
    {0xFF,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF}   //动画帧30
};

void main()
{
    EA = 1;
    ENLED = 0;
    ADDR3 = 0;
    TMOD = 0x01;
    TH0 = 0xFC;
    TL0 = 0x67;
    ET0 = 1;
    TR0 = 1;
    while(1);
}

void InterruptTimer0() interrupt 1
{
    static unsigned char i = 0;
    static unsigned char tmr = 0;
    static unsigned char index = 0;

    TH0 = 0xFC;
    TL0 = 0x67;

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

    tmr++;
    if(tmr >= 250)
    {   
        tmr = 0;
        index++;
        if(index >= 30)
        {
            index = 0;
        }
    }

}
变量进阶主要包括变量的命名规则和作用域、变量的内存管理以及变量的类型转换等内容。首先,变量的命名规则和作用域非常重要。在Python中,变量名必须以字母或下划线开头,后面可以跟字母、数字或下划线,变量名是大小写敏感的,同时要避免使用Python的关键字作为变量名。此外,变量的作用域分为全局作用域和局部作用域,全局变量在整个程序中都可以访问,而局部变量只能在其所在的函数或代码块中使用。 其次,变量的内存管理也是变量进阶的重点内容。在Python中,变量是指向对象的引用,而不是存储对象本身。当我们创建一个变量并将其赋值为某个对象时,实际上是将变量名和对象之间的引用关系保存到内存中。当一个变量不再被使用时,Python会自动回收该变量所指向的对象的内存空间,这个过程称为垃圾回收。 最后,变量的类型转换也是变量进阶的重要内容。在Python中,变量的类型可以动态改变,我们可以使用内置的函数来进行类型转换,比如int()、float()、str()等函数可以将变量转换为整型、浮点型、字符串类型。此外,我们还可以使用isinstance()函数来判断变量的类型,帮助我们在程序中更加灵活地操作变量。 综上所述,变量进阶内容涉及到变量的命名规则和作用域、变量的内存管理以及变量的类型转换,这些内容对于提高Python编程的效率和质量都具有重要的意义。希望大家在学习Python变量进阶内容时能够认真对待,加深理解,提高编程水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值