at89c51单片机的七段显示数码管和流水灯的编程
2015-12-29
展开全部
这个可以借鉴网上的一些资料,具体的看你的原理图,可以仿写
最重要的是自己掌握方法
可以到一些贴吧论坛讨论,相互学习
#include <reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7; //数码管位声明
sbit dula = P2^6; //数码管段选声明
sbit FM = P2^3;//蜂鸣器声明
uchar counter = 0, counter1 = 0, j = 0, flag = 0, flag1 = 0;
uchar num = 6, num1 = 0;
//数码管段选编码
uchar code table_du [] = {
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, //熄灭
0x00 //自定义
};
//数码管位选编码
uchar code table_we[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
void init();
void display();
void main()
{
init();
while(1)
{
if(counter1 == 30)
{
counter1 = 0;
display();
num--;
num1++;
if(num == 0)
{
num = 6;
}
if(num1 == 6)
{
num1 = 0;
}
}
if(counter == 200)//定时到4秒后,流水灯向右流动
{
counter = 0;//counter清零,
//关闭定时器0
TR0 = 0;
//ET0 = 0;
flag = 1; //标志变量置1后,定时器重新初始化
TH0 = 0xB8; //定时器0定时20毫秒
TL0 = 0x00;
TR0 = 1; //重新打开定时器0
//ET0 = 1;
j = 0;
}
}
}
//定时器初始化程序
void init()
{
//定时器0和定时器1初始化函数
TMOD = 0x11;//定义定时器1和0在工作方式1下
TH0 = 0xB8; //定时器0定时20毫秒
TL0 = 0x00;
TH1 = 0xFC; //定时器1定时20毫秒
TL1 = 0x67;
TR0 = 1; //打开定时器0
TR1 = 1; //打开定时器1
EA = 1; //打开总中断
ET0 = 1; //打开定时器0中断
ET1 = 1; //打开定时器1中断
}
//数码管显示子程序
void display()
{
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
}
//中断定时器0
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(flag == 0)
{
if(counter % 10 == 0)//定时200毫秒
{
P1 = ~(1 << j++);//流水灯左移流动
if(j == 8)
{
j = 0;
}
}
}
else
{
if(counter % 5 == 0)
{
//counter = 0;
//流水灯右移流动
P1 = ~(0x80 >> j++);
FM = ~FM;
if(j == 8)
j = 0;
}
}
}
//中断定时器1
void int_time1() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x67;
counter1++;
}
参考
最重要的是自己掌握方法
可以到一些贴吧论坛讨论,相互学习
#include <reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7; //数码管位声明
sbit dula = P2^6; //数码管段选声明
sbit FM = P2^3;//蜂鸣器声明
uchar counter = 0, counter1 = 0, j = 0, flag = 0, flag1 = 0;
uchar num = 6, num1 = 0;
//数码管段选编码
uchar code table_du [] = {
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, //熄灭
0x00 //自定义
};
//数码管位选编码
uchar code table_we[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
void init();
void display();
void main()
{
init();
while(1)
{
if(counter1 == 30)
{
counter1 = 0;
display();
num--;
num1++;
if(num == 0)
{
num = 6;
}
if(num1 == 6)
{
num1 = 0;
}
}
if(counter == 200)//定时到4秒后,流水灯向右流动
{
counter = 0;//counter清零,
//关闭定时器0
TR0 = 0;
//ET0 = 0;
flag = 1; //标志变量置1后,定时器重新初始化
TH0 = 0xB8; //定时器0定时20毫秒
TL0 = 0x00;
TR0 = 1; //重新打开定时器0
//ET0 = 1;
j = 0;
}
}
}
//定时器初始化程序
void init()
{
//定时器0和定时器1初始化函数
TMOD = 0x11;//定义定时器1和0在工作方式1下
TH0 = 0xB8; //定时器0定时20毫秒
TL0 = 0x00;
TH1 = 0xFC; //定时器1定时20毫秒
TL1 = 0x67;
TR0 = 1; //打开定时器0
TR1 = 1; //打开定时器1
EA = 1; //打开总中断
ET0 = 1; //打开定时器0中断
ET1 = 1; //打开定时器1中断
}
//数码管显示子程序
void display()
{
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
dula = 1;
P0 = table_du[num];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
}
//中断定时器0
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(flag == 0)
{
if(counter % 10 == 0)//定时200毫秒
{
P1 = ~(1 << j++);//流水灯左移流动
if(j == 8)
{
j = 0;
}
}
}
else
{
if(counter % 5 == 0)
{
//counter = 0;
//流水灯右移流动
P1 = ~(0x80 >> j++);
FM = ~FM;
if(j == 8)
j = 0;
}
}
}
//中断定时器1
void int_time1() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x67;
counter1++;
}
参考
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询