#循循渐进学51单片机#变量进阶与点阵LED#not.6

1、掌握变量的作用域及存储类别。

局部变量

函数内部声明的变量,只在函数内部有效,在本函数以外是不能使用的,叫局部变量。

全局变量

在函数外部声明的变量就是全局变量,一个源程序可以包含一个或多个函数,全局变量的作用范围是从它开始声明的位置一直到程序结束。

全局变量的副作用

1)降低函数的独立性,对任何一个函数的修改都可能影响到其他函数。

2)降低函数的通用性,不利于函数的重复利用。

3)降低程序的清晰度,每个函数执行都有可能改变全局变量值,无法清楚判断每个时刻的全局变量的值。

4)全局变量永久占用内存单元。

原则:能用局部变量就不用全局变量

全局变量和局部变量同名,在局部变量作用域范围内,局部变量有效。

自动变量

函数中的局部变量,如果不加static关键词修饰,都属于自动变量,也叫动态变量。

静态变量

所有的全局变量都属于静态变量,局部变量加了static关键词修饰的话,也是静态变量

2、了解点阵的显示原理,理解点阵动画显示原理。

点阵的显示原理是多个LED小灯组合在一起,点亮不同的LED小灯集合下来组成不同的图案,点阵的动画显示实际上就是不同图案的高帧变化。

3、独立完成点阵显示I❤U向下移动的程序。

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、独立完成点阵显示I❤U向右移动的程序。

#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、用点阵做一个9到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、尝试实现流水灯、数码管和点阵的同时显

#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;
	}
	}
相关推荐
Bearnaise14 分钟前
PointMamba: A Simple State Space Model for Point Cloud Analysis——点云论文阅读(10)
论文阅读·笔记·python·深度学习·机器学习·计算机视觉·3d
lucy1530275107933 分钟前
【青牛科技】GC5931:工业风扇驱动芯片的卓越替代者
人工智能·科技·单片机·嵌入式硬件·算法·机器学习
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
infiniteWei2 小时前
【Lucene】原理学习路线
学习·搜索引擎·全文检索·lucene
follycat2 小时前
[极客大挑战 2019]PHP 1
开发语言·学习·网络安全·php
weixin_518285053 小时前
深度学习笔记11-神经网络
笔记·深度学习·神经网络
并不会6 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
龙鸣丿6 小时前
Linux基础学习笔记
linux·笔记·学习
Nu11PointerException8 小时前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
亦枫Leonlew10 小时前
三维测量与建模笔记 - 3.3 张正友标定法
笔记·相机标定·三维重建·张正友标定法