本文主要是介绍C代码,控制亮灯的代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这段代码的主要功能是通过控制位选锁存器和段选锁存器来实现对数码管的选择和显示。具体的显示内容由 leddata 数组中的值决定,根据 Location 和 Number 的不同,可以显示不同的字符或数字。
#include <REGX52.H>#define uchar unsigned char //宏定义
#define uint unsigned int //宏定义sbit we = P2^7; //位定义数码管位选锁存器接口
sbit du = P2^6; //位定义数码管段选锁存器接口uchar code leddata[] = { 0x3F, //"0"0x06, //"1"0x5B, //"2"0x4F, //"3"0x66, //"4"0x6D, //"5"0x7D, //"6"0x07, //"7"0x7F, //"8"0x6F, //"9"0x77, //"A"0x7C, //"B"0x39, //"C"0x5E, //"D"0x79, //"E"0x71, //"F"0x76, //"H"0x38, //"L"0x37, //"n"0x3E, //"u"0x73, //"P"0x5C, //"o"0x40, //"-"0x00, //熄灭};void Nixie(unsigned char Location, Number)
{switch (Location){ case 0 :0xFE;break;case 1 :0xFD;break;case 2 :0xFB;break;case 3 :0xF7;break;case 4 :0xEF;break;case 5 :0xDF;break;case 6 :0xBF;break;case 7 :0x7F;break;}P0 = leddata[Number];
}#include <REGX52.H>#define uchar unsigned char //宏定义
#define uint unsigned int //宏定义sbit we = P2^7; //位定义数码管位选锁存器接口
sbit du = P2^6; //位定义数码管段选锁存器接口uchar code leddata[] = { 0x3F, //"0"0x06, //"1"0x5B, //"2"0x4F, //"3"0x66, //"4"0x6D, //"5"0x7D, //"6"0x07, //"7"0x7F, //"8"0x6F, //"9"0x77, //"A"0x7C, //"B"0x39, //"C"0x5E, //"D"0x79, //"E"0x71, //"F"0x76, //"H"0x38, //"L"0x37, //"n"0x3E, //"u"0x73, //"P"0x5C, //"o"0x40, //"-"0x00, //熄灭};void Nixie(unsigned char Location, Number)
{switch (Location){ case 0 :0xFE;break;case 1 :0xFD;break;case 2 :0xFB;break;case 3 :0xF7;break;case 4 :0xEF;break;case 5 :0xDF;break;case 6 :0xBF;break;case 7 :0x7F;break;}P0 = leddata[Number];
}void main()
{du = 1; //打开段选P0 = leddata[1]; //显示1du = 0; //关闭段选Nixie(6,2);we = 1;//打开位选P0 = 0xfe;//左边第一位数码管显示we = 0; //关闭位选while(1);//程序停留
}
#include <REGX52.H>#define uchar unsigned char //宏定义
#define uint unsigned int //宏定义sbit we = P2^7; //位定义数码管位选锁存器接口
sbit du = P2^6; //位定义数码管段选锁存器接口void Delay(unsigned int xms){unsigned char i, j;while(xms--){i = 2;j = 239;do{while(--j);}while(--i);}
}uchar code leddata[] = { 0x3F, //"0"0x06, //"1"0x5B, //"2"0x4F, //"3"0x66, //"4"0x6D, //"5"0x7D, //"6"0x07, //"7"0x7F, //"8"0x6F, //"9"0x77, //"A"0x7C, //"B"0x39, //"C"0x5E, //"D"0x79, //"E"0x71, //"F"0x76, //"H"0x38, //"L"0x37, //"n"0x3E, //"u"0x73, //"P"0x5C, //"o"0x40, //"-"0x00, //熄灭};void Nixie(unsigned char Location, int Number)
{int key = 0xFE;du = 1; //打开段选P0 = leddata[Number]; //显示1du = 0; //关闭段选switch (Location){ case 0 :key = 0xFE;break;case 1 :key = 0xFD;break;case 2 :key = 0xFB;break;case 3 :key = 0xF7;break;case 4 :key = 0xEF;break;case 5 :key = 0xDF;break;case 6 :key = 0xBF;break;case 7 :key = 0x7F;break;}we = 1;//打开位选P0 = key;//左边第一位数码管显示we = 0; //关闭位选Delay(1000);
}void main()
{ int i; for(i=0;i<8;i++) Nixie(i,i+1);while(1);//程序停留
}
这篇关于C代码,控制亮灯的代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!