/*************************************************
实验名称:数码管实验一
实验目的:利用动态扫描方法在六位数码管上显示出稳定
的654321
*************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DUAN = P2^6;
sbit WEI = P2^7;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
void delay_1ms(int);
void main()
{
int i,j;
j =0;
while(1)
{
for(i = 7;i > 1; i--)
{
/* P0 = duan_table[i-1];
DUAN = 1;
DUAN = 0;
P0 = 0xff;
P0 = wei_table[j++];
WEI = 1;
WEI = 0;
delay_1ms(1);
以上代码将会产生余晖,改用以下代码消除余晖*/
DUAN = 1;
P0 = duan_table[i-1];
DUAN = 0;
P0 = 0xff;//消除余晖
WEI = 1;
P0 = wei_table[j++];
WEI = 0;
delay_1ms(1);
}
if(j == 6)
j = 0;
}
}
void delay_1ms(int x)
{
int i, j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
/**************************************************
实验名称:数码管实验二
实验目的:利用动态扫描方法和定时器1在数码管的前三位
显示出秒表,精确到1%秒,即后两位显示1%秒,
一直循环下去。(TH/TL填装9216)
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DUAN = P2^6;
sbit WEI = P2^7;
sbit SPK = P2^3;
uchar S_ms, B_ms, S;
uchar code duan_table0[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code duan_table1[]=
{0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
void delay_1ms(uchar);
void display(uchar,uchar,uchar);
void main()
{
S_ms = 0;
B_ms = 0;
S = 0;
TMOD = 0x10;
TH1 = (65535-9216)/256;
TL1 = (65535-9216)%256;
EA = 1;
ET1 = 1;
TR1 = 1;
while(1)
{
if(S_ms == 10)
{
S_ms = 0;
B_ms++;
if(B_ms == 10)
{
B_ms = 0;
S++;
SPK = 0; //每过一秒响一声
delay_1ms(10); //
SPK = 1; //
if(S == 10)
{
S=0;
}
}
}
display(S,B_ms,S_ms);
}
}
void delay_1ms(uchar x)
{
uchar i, j;
for(i = 0; i < x; i++)
for(j = 0; j <110; j++);
}
void Time1() interrupt 3
{
TH1 = (65535-9216)/256;
TL1 = (65535-9216)%256;
S_ms++;
}
void display(uchar i,uchar j,uchar k)
{
DUAN = 1;
P0 = duan_table1[i];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table0[j];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table0[k];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[2];
WEI = 0;
delay_1ms(1);
}
/**************************************************
实验名称:数码管实验三
实验目的:利用动态扫描和定时器1,在数码管上显示出从
765432开始以1%秒的速度往下递减直至764987,
并保持显示次数,与此同时利用定时器0以500ms
的速度进行流水灯从上至下移动,同时蜂鸣器
发声,当数码光上数减到停止时,实验板上流
水灯全部闪烁5次,数码管全熄灭。单片机进入
休眠状态。(0.01秒填装9216)
实验备注:该程序暂时解决不了因中断冲突而引起的数码管
显示延迟
**************************************************/
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DUAN = P2^6;
sbit WEI = P2^7;
sbit SPK = P2^3;
long num;
uchar swan,wan,qian,bai,shi,ge,flag_0,flag_1;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
void delay_1ms(uint);
void display(uchar,uchar,uchar,uchar,uchar,uchar);
void main()
{
uchar i;
flag_0 = 0;
flag_1 = 0;
num = 765432;
P1 = 0x7f;
TMOD = 0x11;
TH1 = (65536-9216)/256;
TL1 = (65536-9216)%256;
EA = 1;
ET0 = 1;
ET1 = 1;
// EX0 = 1;
// EX1 = 1;
TR0 = 1;
TR1 = 1;
while(1)
{
if(flag_0 == 10)
{
flag_0 = 0;
P1 = _cror_(P1,1);
SPK = 0;
delay_1ms(100);
SPK = 1;
}
if(flag_1 == 90)
{
flag_1 = 0;
num = num - 90;
swan = num/100000;
wan = num/10000%10;
qian = num/1000%10;
bai = num/100%10;
shi = num%100/10;
ge = num%10;
}
display(swan,wan,qian,bai,shi,ge);
if(num == 764987)
{
ET0 = 0;
ET1 = 0;
for(i = 0;i < 5;i++)
{
P1 = 0;
SPK = 0;
delay_1ms(500);
P1 = 1;
SPK = 1;
delay_1ms(500);
}
PCON = 0x2;
}
}
}
void delay_1ms(uint x)
{
int i,j;
for(i = 0;i < x;i++)
for(j = 0;j < 110;j++);
}
void display(uchar swan,uchar wan,uchar qian,
uchar bai,uchar shi,uchar ge)
{
DUAN = 1;
P0 = duan_table[swan];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[wan];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[qian];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[2];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[bai];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[3];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[shi];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[4];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[ge];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[5];
WEI = 0;
delay_1ms(1);
}
void Timer0() interrupt 1
{
TH0 = (65536-46080)/256;
TH0 = (65536-46080)%256;
flag_0++;
}
void Timer1() interrupt 3
{
TH1 = (65536-9216)/256;
TL1 = (65536-9216)%256;
flag_1++;
}
//void Ex_int0() interrupt 0
//{}
//void Ex_int1() interrupt 2
//{}