51微控制器(V51)————靜態數碼管和動態數碼管

2020-09-28 12:02:15

一、基本知識

(1)鎖存

在這裡插入圖片描述

鎖存晶片的作用:儲存上一狀態值先給他賦值1,【使能端為高】,讓我們要顯示的數值通過,再給他賦值0,【使能端為低】,就可以鎖住數值

(2)電路圖看段選和位選

在BSTV51微控制器中 P2^6 這個串列埠是段選,控制那一個數位的具體顯示,可根據字形表對應輸出 P2^7 是位選,選擇控制要哪一位亮燈
在這裡插入圖片描述

(3)共陽極數碼管與共陰極數碼管

  • 共陰極–輸出低電平0,點亮
  • 共陽極–輸出高電平點亮
  • 共有十個引腳,2個接地GND
    在這裡插入圖片描述

二、靜態數碼管

  • 靜態顯示的連線方式、靜態顯示的優缺點(優點:不需要動態重新整理;缺點:佔用IO口線多)
  • 動態顯示的連線方式(所有位數碼管的段選線並聯在一起,由 位選線控制是哪一位數碼管有效)、動態顯示的優缺點(缺點:需要動態重新整理;優點:佔用IO口線少)

在這裡插入圖片描述

#include<reg52.h>

sbit DUAN = P2^6;
sbit WEI  = P2^7;



void main()
{
	P0 = 0x7d; // 1111 1110
	DUAN = 1;
	DUAN = 0;

	P0 = 0x00; 
	WEI = 1;
	WEI = 0;

/*	P0 = 0x7c; // 1111 1110
	DUAN = 1;
	DUAN = 0;

	P0 = 0xfd; 
	WEI = 1;
	WEI = 0;	  */
	 
}

三、動態數碼管

在這裡插入圖片描述
在這裡插入圖片描述
用延遲函數

#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI  = P2^7;

void delay(unsigned int xms)
{
	unsigned int i,j;
	for(i=xms;i>0;i--)
		for(j=112;j>0;j--);
}

unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
  						 0x7d,0x07,0x7f,0x6f,0x77,0x7c,
						 0x39,0x5e,0x79,0x71,0x00
};
void main()
{
	while(1)
	{
		P0 = sz[1];		// 數位1
		DUAN = 1;		// 一端上電
		DUAN = 0;		//	 斷開
		P0 = 0xfe;
		WEI  = 1;		//	另一端上高電平
		WEI  = 0;		//	另一端斷開
		delay(200);

		P0 = sz[2];		// 數位2
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfd;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[3];		// 數位3
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfb;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[4];		// 數位4   
		DUAN = 1;
		DUAN = 0;
		P0 = 0xf7;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[5];		// 數位5
		DUAN = 1;
		DUAN = 0;
		P0 = 0xef;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[6];		// 數位6	 
		DUAN = 1;
		DUAN = 0;
		P0 = 0xdf;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[7];		// 數位7
		DUAN = 1;
		DUAN = 0;
		P0 = 0xbf;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[8];		// 數位8
		DUAN = 1;
		DUAN = 0;
		P0 = 0x7f;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[8];		// 數位8
		DUAN = 1;
		DUAN = 0;
		P0 = 0x7f;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[7];		// 數位7
		DUAN = 1;
		DUAN = 0;
		P0 = 0xbf;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[6];		// 數位6	 
		DUAN = 1;
		DUAN = 0;
		P0 = 0xdf;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[5];		// 數位5
		DUAN = 1;
		DUAN = 0;
		P0 = 0xef;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[4];		// 數位4   
		DUAN = 1;
		DUAN = 0;
		P0 = 0xf7;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[3];		// 數位3
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfb;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[2];		// 數位2
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfd;
		WEI  = 1;
		WEI  = 0;
		delay(200);

		P0 = sz[1];		// 數位1
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfe;
		WEI  = 1;
		WEI  = 0;
		delay(200);

	
	
	}
}

不用延遲函數

#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI  = P2^7;



unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
  						 0x7d,0x07,0x7f,0x6f,0x77,0x7c,
						 0x39,0x5e,0x79,0x71,0x00
};
void main()
{
	while(1)
	{
		P0 = sz[1];		// 數位1
		DUAN = 1;		// 一端上電
		DUAN = 0;		//	 斷開
		P0 = 0xfe;
		WEI  = 1;		//	另一端上高電平
		WEI  = 0;		//	另一端斷開
		P0 = 0xff;		// 不用延遲函數需要開一個關一個才能保證不亂碼
		WEI  = 1;
		WEI  = 0;
		

		P0 = sz[2];		// 數位2
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfd;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		

		P0 = sz[3];		// 數位3
		DUAN = 1;
		DUAN = 0;
		P0 = 0xfb;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		P0 = sz[4];		// 數位4   
		DUAN = 1;
		DUAN = 0;
		P0 = 0xf7;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		P0 = sz[5];		// 數位5
		DUAN = 1;
		DUAN = 0;
		P0 = 0xef;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		P0 = sz[6];		// 數位6	 
		DUAN = 1;
		DUAN = 0;
		P0 = 0xdf;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		P0 = sz[7];		// 數位7
		DUAN = 1;
		DUAN = 0;
		P0 = 0xbf;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;

		P0 = sz[8];		// 數位8
		DUAN = 1;
		DUAN = 0;
		P0 = 0x7f;
		WEI  = 1;
		WEI  = 0;
		P0 = 0xff;
		WEI  = 1;
		WEI  = 0;
	}
}