目录
(1)例程1:数码管显示某一位(某一杠)。以点亮8段数码管最上面的横杠为例。
(3)例程3:数码管显示某一个数值。(本例以点亮8段数码管显示数组“1”为例)
(4)例程4:数码管依次显示0-9、-、全部熄灭,显示间隔为500MS;
(5)例程5:数码管依次显示:0-9、-、全部熄灭,每隔数码管显示间隔500MS;要求数码管码值使用数组,功能函数使用for循环。
(6)例程6:按键控制数码管显示。S7、S6、S5、S4按键分别控制数码管显示1、2、3、4数字。
(7)例程7:按键控制数码管正及时、倒计时。本例以S7按键按下数码管开始0->9正计时、S6按键按下数码管开始9->0倒计时。注意:按键采用松手检测
一、补充 (code)
unsigned char code SEG[] = {0XF0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char SEG[] = {0XF0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
区别:不写code(程序读取快),代表所创建的数组被单片机从程序存储器中读取,存入单片机的内存中(IM);写code(程序读取较慢,不影响数码管)代表所创建的数组直接存储到程序存储器中,单片机不会把数组存入IM中;但是如果编写点阵等,是推荐写code。
二、例程
(1)例程1:数码管显示某一位(某一杠)。以点亮8段数码管最上面的横杠为例。
//头文件声明区域
#include <STC15F2K60S2.H>
//程序主体
void main()
{
P2 = 0XA0;P0 = OX00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
P0 = 0XFE;//1111 1110
while(1);
}
(2)例程2:数码管的8个段依次点亮(其他保持熄灭)
//头文件声明区域
#include <STC15F2K60S2.H>
#include<intrins.h>
//变量声明区域
unsigned char SEG = 0;
//函数定义区域
void Delay(unsigned int time) //@11.0592MHz
{
unsigned char i, j;
while(time--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
}
void SEG_Left(void)
{
for(SEG = 0;SEG < 8;SEG++)
{
P0 = ~(0x01<<i);
Delay(500);
}
}
//程序主体
void main()
{
P2 = 0XA0;P0 = OX00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
while(1)
{
SEG_Left();
}
}
(3)例程3:数码管显示某一个数值。(本例以点亮8段数码管显示数组“1”为例)
//头文件声明区域
#include <STC15F2K60S2.H>
//程序主体
void main()
{
P2 = 0XA0;P0 = OX00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
P0 = 0XF9;
while(1);
}
(4)例程4:数码管依次显示0-9、-、全部熄灭,显示间隔为500MS;
#include <STC15F2K60S2.H>
void main()
{
P2 = 0XA0;P0 = OX00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
while(1)
{
P0 = 0XC0; Delay(500);//0
P0 = 0XF9; Delay(500);//1
P0 = 0XA4; Delay(500);//2
P0 = 0XB0; Delay(500);//3
P0 = 0X99; Delay(500);//4
P0 = 0X92; Delay(500);//5
P0 = 0X82; Delay(500);//6
P0 = 0XF8; Delay(500);//7
P0 = 0X80; Delay(500);//8
P0 = 0X90; Delay(500);//9
P0 = 0XBF; Delay(500);//-
P0 = 0XFF; Delay(500);//全部熄灭
}
}
(5)例程5:数码管依次显示:0-9、-、全部熄灭,每隔数码管显示间隔500MS;要求数码管码值使用数组,功能函数使用for循环。
//头文件声明区域
#include <STC15F2K60S2.H>
#include<intrins.h>
//变量声明区域
unsigned char code SEG[] = {0XF0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned int i = 0;
//函数定义区域
void Delay(unsigned int time) //@11.0592MHz
{
unsigned char i, j;
while(time--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
//程序主体
void main()
{
P2 = 0XA0;P0 = 0X00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
while(1)
{
for(i = 0;i < 11;i++)
{
P0 = SEG[i];
Delay(500);
}
}
}
(6)例程6:按键控制数码管显示。S7、S6、S5、S4按键分别控制数码管显示1、2、3、4数字。
//头文件声明区域
#include <STC15F2K60S2.H>
#include<intrins.h>
//变量声明区域
unsigned char code SEG[] = {0XF0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char S7_Flag = 0,S6_Flag = 0,S5_Flag = 0,S4_Flag = 0;
//函数定义区域
void Delay(unsigned int time) //@11.0592MHz
{
unsigned char i, j;
while(time--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
//按键扫描函数
void KeyScan(void)
{
if(P30 == 0)
{
Delay(10);
if(P30 == 0) S7_Flag = 1;
while(!P30);
}
else if(P31 == 0)
{
Delay(10);
if(P31 == 0) S6_Flag = 1;
while(!P30);
}
else if(P32 == 0)
{
Delay(10);
if(P32 == 0) S5_Flag = 1;
while(!P30);
}
else if(P31 == 0)
{
Delay(10);
if(P33 == 0) S4_Flag = 1;
while(!P30);
}
}
//程序主体
void main()
{
P2 = 0XA0;P0 = 0X00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
while(1)
{
KeyScan();
if(P30 == 0) {S7_Flag = 0;P0 = SEG[1];}
if(P31 == 0) {S6_Flag = 0;P0 = SEG[2];}
if(P32 == 0) {S5_Flag = 0;P0 = SEG[3];}
if(P33 == 0) {S4_Flag = 0;P0 = SEG[4];}
}
}
(7)例程7:按键控制数码管正及时、倒计时。本例以S7按键按下数码管开始0->9正计时、S6按键按下数码管开始9->0倒计时。注意:按键采用松手检测
//头文件声明区域
#include <STC15F2K60S2.H>
#include<intrins.h>
//变量声明区域
unsigned char code SEG[] = {0XF0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char S7_Flag = 0,S6_Flag = 0,S5_Flag = 0,S4_Flag = 0;
unsigned char SEG_Flag = 0;
unsigned int i = 0;
//函数定义区域
void Delay(unsigned int time) //@11.0592MHz
{
unsigned char i, j;
while(time--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
//按键扫描函数
void KeyScan(void)
{
if(P30 == 0)
{
Delay(10);
if(P30 == 0) S7_Flag = 1;
while(!P30);
}
else if(P31 == 0)
{
Delay(10);
if(P31 == 0) S6_Flag = 1;
while(!P30);
}
else if(P32 == 0)
{
Delay(10);
if(P32 == 0) S5_Flag = 1;
while(!P30);
}
else if(P31 == 0)
{
Delay(10);
if(P33 == 0) S4_Flag = 1;
while(!P30);
}
}
//程序主体
void main()
{
P2 = 0XA0;P0 = 0X00;P2 = 0X80;P0 = 0XFF;//初始化程序
P2 = 0XC0;P0 = 0X01;P2 = 0XFF;P0 = 0XFF;//打开第一个数码管程序
while(1)
{
KeyScan();
if(P30 == 0) {S7_Flag = 0;SEG_Flag = 1;}
if(P31 == 0) {S6_Flag = 0;SEG_Flag = 2;}
if(SEG_Flag == 1) //正计时
{
for(i = 0;i < 10;i++)
{
P0 = SEG[i];
Delay(1000);
}
}
if(SEG_Flag == 2) //倒计时
{
for(i = 9;i >= 0;i--)
{
P0 = SEG[i];
Delay(1000);
}
}
}
}