一、所需两个代码--显示和延时
显示:
#include <REGX52.H>
#include "Delay.h"
unsigned char num[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x3E,0x40};
void a(unsigned char LOCATE,number)
{
switch(LOCATE)
{
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;
case 9:
P2_4=1;P2_3=1;P2_2=1;
break;
}
P0=num[number];
Delay(1);
P0=0x00; //将上一次显示的数据归0,则当该数据传入下一位时就是0x00,就不会显示上一位的数字(单片机数据传递时,上一位会先到下一位,后该位数据才到)
}
延时:
void Delay(unsigned int xms)
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
二、注意事项
1.消抖
Delay(20);
while(某一按键==0);
Delay(20);
2.转换和转换闪烁的处理
任务要求处于U1,U2,U3,U4时还可以通过按其它按钮来转换。所以要将数码管显示代码放进消抖的while循环中,这样转换时数码管就不会闪烁。同时通过在运行显示代码后面用if语句来满足在该情况下运行时转换情况的需求。
U1例子:
while(P3_1==0){
while(1){
a(9,10);
a(2,1);
a(7,0);
a(8,1);
if(P3_0==0){
U2();
}
if(P3_2==0)
{
U3();
}
if(P3_3==0){
U4();
}
}
}
3.模块化
将所有代码编写在同一个文件中,会很拥挤,该任务可以使用模块化编程。
三、U1
思路:
静态显示U,1,0,1,分别位于第1,2,7,8位,所以只需对应的位置显示对应的数字即可。
所以U显示对应数码管应是B,C,D,E,F这5位显示,对应0x3E,1对应B,C显示,所以是0x06。
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "U4.h"
#include "U2.h"
#include "U3.h"
void U1(){
Delay(20);
while(P3_1==0){
while(1){
a(9,10);
a(2,1);
a(7,0);
a(8,1);
if(P3_0==0){
U2();
}
if(P3_2==0)
{
U3();
}
if(P3_3==0){
U4();
}
}
}
Delay(20);
}
四、U2
动态显示可以额外设置一个变量c=1000,利用while(c--),直到c减到0循环停止刚好1s。
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "U1.h"
#include "U4.h"
#include "U3.h"
unsigned int n2=0,n3=0;
unsigned char c=1000;
void U2(){
Delay(20);
while(P3_0==0){
while(1)
{
n3++;
if(n2==1&&n3==1){
n2=0;
n3=0;
}
if(n2==0&&n3==10){
n2=1;
n3=0;
}
while(c--){
a(9,10);
a(2,2);
a(7,n2);
a(8,n3);
if(P3_1==0){
U1();
}
if(P3_2==0)
{
U3();
}
if(P3_3==0){
U4();
}
}
}
}
Delay(20);
}
五、U3
显示字符“——”,对应G,所以二进制表示为0x40,,闪烁0.5s后不闪烁,可以交替显示,即显示完后停止显示,0.5s后再显示,延时方式如上U2 。
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "U1.h"
#include "U4.h"
#include "U2.h"
unsigned int n0=0,n1=0;
unsigned char b=500;
void U3()
{
Delay(20);
while(P3_2==0){
while(1)
{
while(b--)
{
a(9,10);
a(2,3);
a(7,0);
a(8,3);
a(5,11);
a(6,11);
if(P3_1==0){
U1();
}
if(P3_0==0){
U2();
}
if(P3_3==0){
U4();
}
}
b=500;
while(b--)
{
a(9,10);
a(2,3);
a(7,0);
a(8,3);
a(5,0);
a(6,0);
if(P3_1==0){
U1();
}
if(P3_0==0){
U2();
}
if(P3_3==0){
U4();
}
}
}
}
Delay(20);
}
六、U4
思路:
按键按下时会显示上一次的数据,松开按键后会显示新的数据。即按键按下进入死循环会显示,然后,跳出循环后还会继续显示。
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "U1.h"
#include "U2.h"
#include "U3.h"
unsigned int n4=0,n5=0;
void U4(){
while(1)
{
if(P3_3==0){
Delay(20);
while(P3_3==0){
a(9,10);
a(2,4);
a(7,n4);
a(8,n5);
}
Delay(20);
n5++;
if(n4==9&&n5==10){
n4=0;
n5=0;
}
if(n5==10&&n4!=9){
n4++;
n5=0;
}
while(P3_3!=0){
a(9,10);
a(2,4);
a(7,n4);
a(8,n5);
if(P3_1==0){
U1();
}
if(P3_0==0){
U2();
}
if(P3_2==0)
{
U3();
}
}
}
}
Delay(20);
}