# step by step learning 51 microcontroller#Variable advancement and dot matrix LED#not.6

1. Master the scope and storage category of variables.

local variables

Variables declared inside a function are only valid within the function and cannot be used outside this function. They are called local variables.

global variables

Variables declared outside a function are global variables. A source program can contain one or more functions. The scope of a global variable is from the location where it is declared until the end of the program.

Side effects of global variables

1) Reduce the independence of functions. Modifications to any function may affect other functions.

2) Reduce the versatility of the function, which is not conducive to the reuse of the function.

3) Reduce the clarity of the program. Each function execution may change the value of the global variable, making it impossible to clearly judge the value of the global variable at each moment.

4) Global variables occupy memory units permanently.

Principle: If you can use local variables, don’t use global variables.

Global variables and local variables have the same name, and local variables are valid within the scope of the local variable.

automatic variable

Local variables in a function, if not modified with the static keyword, are automatic variables, also called dynamic variables.

static variable

All global variables are static variables. If local variables are modified with the static keyword, they are also static variables.


2. Understand the display principle of dot matrix and the display principle of dot matrix animation.

The display principle of the dot matrix is ​​that multiple small LED lights are combined together, and different small LED lights are lit to form different patterns. The animation display of the dot matrix is ​​actually a high-frame change of different patterns.


3. Independently complete the program of moving the dot matrix display I❤U downwards.

clude <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedBuff[] = {
0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,
0xE7,0xC3,0x81,0x00,0x00,0x99,0xFF,0xC3,
0x81,0x99,0x99,0x99,0x99,0x99,0xFF,0xFF,
};

unsigned int flag1s = 0;
unsigned int cnt = 0,i = 0;

void main()
{
	ENLED = 0;
	addr3 = 0;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	TR0 = 1;
  while(1) 
{
 if(TF0 == 1)
 {
	 TF0 = 0;
	 cnt++;
	 if(cnt >= 5)
	 {
	 cnt = 0;
	flag1s++;
	 }
 }
	 P0 = 0xff;
		 switch (i)
	{
		    case 0 :addr0 = 0;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s + 7];i++;break;
				case 1 :addr0 = 1;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s + 6];i++;break;
				case 2 :addr0 = 0;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s + 5];i++;break;
				case 3 :addr0 = 1;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s + 4];i++;break;
				case 4 :addr0 = 0;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s + 3];i++;break;
				case 5 :addr0 = 1;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s + 2];i++;break;
				case 6 :addr0 = 0;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s + 1];i++;break;
				case 7 :addr0 = 1;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s + 0];i=0;break;
		default:break;
	 }
	if(flag1s >= 16)
	{
	flag1s = 0;
	}
 	
}
}


4. Independently complete the program of moving the dot matrix display I❤U to the right.

#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedBuff[] = {
 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
};

unsigned int flag1s = 0;
unsigned int cnt = 0,i = 0;

void main()
{
	ENLED = 0;
	addr3 = 0;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	TR0 = 1;
  while(1) 
{
 if(TF0 == 1)
 {
	 TF0 = 0;
	 cnt++;
	 if(cnt >= 5)
	 {
	 cnt = 0;
	flag1s++;
	 }
 }
	 P0 = 0xff;
		 switch (i)
	{
		    case 0 :addr0 = 0;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s + 7];i++;break;
				case 1 :addr0 = 1;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s + 6];i++;break;
				case 2 :addr0 = 0;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s + 5];i++;break;
				case 3 :addr0 = 1;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s + 4];i++;break;
				case 4 :addr0 = 0;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s + 3];i++;break;
				case 5 :addr0 = 1;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s + 2];i++;break;
				case 6 :addr0 = 0;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s + 1];i++;break;
				case 7 :addr0 = 1;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s + 0];i=0;break;
		default:break;
	 }
	if(flag1s >= 32)
	{
	flag1s = 0;
	}
 	
}
}


5. Use dot matrix to make a countdown card display from 9 to 0.

#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedBuff[][8] = {
0xFF,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xFF,
	0xFF,0xE3,0xDF,0xDF,0xEF,0xF7,0xC3,0xFF,
0xFF,0xC3,0xDF,0xDF,0xE3,0xDF,0xDF,0xC3,
	0xFF,0xEF,0xE7,0xEB,0xC1,0xEF,0xEF,0xEF,
0xFF,0xC3,0xFB,0xC3,0xDF,0xDF,0xDF,0xE3,
	0xFF,0xC3,0xDF,0xDF,0xC3,0xDB,0xDB,0xC3,
0xFF,0xC3,0xDF,0xEF,0xF7,0xF7,0xF7,0xF7,
	0xFF,0xC3,0xDB,0xDB,0xE7,0xDB,0xDB,0xC3,
0xFF,0xC3,0xDB,0xDB,0xC3,0xDF,0xDF,0xEF,
	0xFF,0xE7,0xDB,0xDB,0xDB,0xDB,0xDB,0xE7,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};

unsigned int flag1s = 0;
unsigned int cnt = 0,i = 0;

void main()
{
	ENLED = 0;
	addr3 = 0;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	TR0 = 1;
  while(1) 
{
 if(TF0 == 1)
 {
	 TF0 = 0;
	 cnt++;
	 if(cnt >= 10)
	 {
	 cnt = 0;
	flag1s++;
	 }
 }
	 P0 = 0xff;
		 switch (i)
	{
		    case 0 :addr0 = 0;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s][0];i++;break;
				case 1 :addr0 = 1;addr1 = 0;addr2 = 0;P0 = LedBuff[flag1s][1];i++;break;
				case 2 :addr0 = 0;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s][2];i++;break;
				case 3 :addr0 = 1;addr1 = 1;addr2 = 0;P0 = LedBuff[flag1s][3];i++;break;
				case 4 :addr0 = 0;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s][4];i++;break;
				case 5 :addr0 = 1;addr1 = 0;addr2 = 1;P0 = LedBuff[flag1s][5];i++;break;
				case 6 :addr0 = 0;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s][6];i++;break;
				case 7 :addr0 = 1;addr1 = 1;addr2 = 1;P0 = LedBuff[flag1s][7];i=0;break;
		default:break;
	 }
	if(flag1s >= 10)
	{
	flag1s = 0;
	}
 	
}
}


6. Try to realize the simultaneous display of running lights, digital tubes and dot matrix.

#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned int i = 0;

void main()
{
	EA = 1;
	ENLED = 0;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	ET0 = 1;
	TR0 = 1;
  while(1) 
{
 
}	
}
void InterruptTimer0() interrupt 1
{
	TH0 = 0xfc;
	TL0 = 0x67;
	P0 = 0xff;
	switch (i)
	{
		    case 0 :addr3 = 0;addr0 = 0;addr1 = 0;addr2 = 0;P0 = 0;i++;break;
				case 1 :addr3 = 0;addr0 = 1;addr1 = 0;addr2 = 0;P0 = 0;i++;break;
				case 2 :addr3 = 0;addr0 = 0;addr1 = 1;addr2 = 0;P0 = 0;i++;break;
				case 3 :addr3 = 0;addr0 = 1;addr1 = 1;addr2 = 0;P0 = 0;i++;break;
				case 4 :addr3 = 0;addr0 = 0;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
				case 5 :addr3 = 0;addr0 = 1;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
				case 6 :addr3 = 0;addr0 = 1;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
				case 7 :addr3 = 0;addr0 = 1;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
		
				case 8 :addr3 = 1;addr0 = 0;addr1 = 0;addr2 = 0;P0 = 0;i++;break;
				case 9 :addr3 = 1;addr0 = 1;addr1 = 0;addr2 = 0;P0 = 0;i++;break;
				case 10 :addr3 = 1;addr0 =0;addr1 = 1;addr2 = 0;P0 = 0;i++;break;
				case 11 :addr3 = 1;addr0 = 1;addr1 = 1;addr2 = 0;P0 = 0;i++;break;
				case 12 :addr3 = 1;addr0 = 0;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
				case 13 :addr3 = 1;addr0 = 1;addr1 = 0;addr2 = 1;P0 = 0;i++;break;
		
				case 14 :addr3 = 1;addr0 = 0;addr1 = 1;addr2 = 1;P0 = 0;i=0;break;
		default:break;
	}
	}

おすすめ

転載: blog.csdn.net/2301_77479336/article/details/132845026