40个经典单片机实验,单片机实验设计
单片机流水灯+数码管实验
额,程序之前没仿真过,有点错误,现在可以了
#includereg52.h
#define?uchar?unsigned?char
#define?uint?unsigned?int
unsigned?a=0,b=0,counter=0;
unsigned?char?SEG1[?]?=?{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned?char?SEG2[?]?=?{0x03,0x06,0x0C,0x18,0x30,0x60,0x0C,0x81};
unsigned?char?SEG3[?]?=?{0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned?char?SEG4[?]?=?{0x01,0x02,0x04,0x08,0x30,0x60,0x0C,0x81};
sbit?P31?=?P3^1;
sbit?P30?=?P3^0;
void?Timer0Init(void)?interrupt?3?//定时器1
{
counter++;
if(counter==2000)???//定时器定时250us,故4000次中断就表示1秒钟到达
{
counter=0;
a++;
b++;
if(a==8)?a=0;
if(b==256)b=0;
EA=1;
}
}
void?init()
{
TMOD?=?0x20;
EA=1;
TH1=6;
TL1=6;
TR1=1;
ET1=1;
}
void?disp()
{
if?(P30==0P31==0)?{P1?=?SEG1?[a];}
if?(P30==1P31==0)?{P1?=?SEG2?[a];}
if?(P30==0P31==1)?{P1?=?SEG3?[a];}
if?(P30==1P31==1)?{P1?=?SEG4?[a];}
P2=b;
}
main()
{
init();
while(1)
{
disp();
}
}
单片机大一19个实验代码?
实验一
实验(一):本程序的功能是点亮一个发光二极管
#includereg51.h
sbit led=P1^0;
void main()
{
led=0;
while(1);
}
实验(二):本程序的功能是点亮一个发光二极管
#includereg51.h
Void main()
{
P1=0xfe;
While(1)
;
}
实验(三):本程序的功能是点亮三个发光二极管
#include reg51.h
Sbit LED0=P1^0;
Sbit LED1=P1^1;
Sbit LED2=P1^2;
Void main()
{
LED0=0;
LED1=0;
LED2=0;
While(1);
}
实验(四):本程序的功能是点亮三个发光二极管
#include reg51.h
Void main()
{
P1=0xf8;
While(1)
;
}
实验二
实验(一):本程序的功能是使D0交替闪烁,频率始终一致
#includereg51.h
sbit LED0=P1^0;
void Delay(unsigned int t);
void main()
{
while(1)
{
LED0=0;
Delay(10000);
LED0=1;
Delay(10000);
}
}
void Delay(unsigned int t)
{
while(--t);
}
实验(二):本程序的功能是使D0交替闪烁,前10次的闪烁频率与后10次不同
#includereg51.h
sbit LED0=P1^0;
void Delay(unsigned int t);
void main()
{
Unsigned char i;
while(1)
{
For(i=0;i10;i++)
{
LED0=0;
Delay(10);
LED0=1;
Delay=(10);
}
For(i=0;i10;i++)
{
LED0=0;
Delay(6000);
LED0=1;
Delay(6000);
}
}
}
Void Delay(unsigned int t)
{
Unsigned i,j;
For(i=0;it;i++)
For(j=0;j1000;j++)
;
}
实验(三):本程序的功能是使D0、D2、D4交替闪烁,前10次的闪烁频率与后10次不同
#includereg51.h
sbit LED0=P1^0;
sbit LED2=P1^2;
sbit LED4=P1^4;
void Delay(unsigned int t);
void main()
{
Unsigned char i;
while(1)
{
For(i=0;i10;i++)
{
LED0=0;
LED2=0;
LED4=0;
Delay(100);
LED0=1;
LED2=1;
LED4=1;
Delay=(100);
}
For(i=0;i10;i++)
{
LED0=0;
LED2=0;
LED4=0;
Delay(1000);
LED0=1;
LED2=1;
LED4=1;
Delay(1000);
}
}
}
Void Delay(unsigned int t)
{
Unsigned i,j;
For(i=0;it;i++)
For(j=0;j1000;j++)
;
}
实验(四):本程序的功能是使D0、D2、D4交替闪烁,前10次的闪烁频率与后10次不同
#includereg51.h
void Delay(unsigned int t);
void main()
{
Unsigned char i;
while(1)
{
For(i=0;i10;i++)
{
P1=0xff;
Delay(100);
P1=0xea;
Delay=(100);
}
For(i=0;i10;i++)
{
P1=0xff;
Delay(1000);
P1=0xea;
Delay(1000);
}
}
}
Void Delay(unsigned int t)
{
Unsigned i,j;
For(i=0;it;i++)
For(j=0;j1000;j++)
;
}
实验三
实验(一):Proteus本程序的功能是使D0-D7从右至左依次被点亮
#includereg51.h
void Delay(unsigned int t);
void main()
{
unsigned char i;
Delay(50000);
P1=0xff;
for(i=0;i8;i++)
{
Delay(50000);
P1=1;
}
while(1)
;
}
void Delay(unsigned int t)
{
while(--t);
}
实验(二):本程序的功能是使D0-D7从左至右依次被点亮
#includereg51.h
void Delay(unsigned int t);
void main()
{
unsigned char i;
Delay(50000);
P1=0xff;
for(i=0;i8;i++)
{
Delay(50000);
P1=1;
}
while(1)
;
}
void Delay(unsigned int t)
{
while(--t);
}
实验四
实验(一):本程序的功能是使D0-D7从有至左被点亮,某时刻只有一个灯亮,最终D7在亮
#includereg51.h
#includeintrins.h
void delay(unsigned int t);
void main()
{
unsigned char i;
P1=0xfe;
delay(50000);
for(i=0;i7;i++)
{
P1=crol_(P1,1);
delay(50000);
}
while(1)
;
}
void delay(unsigned int t)
{
while(--t);
}
实验(二):本程序的功能是使D0-D7从有至左被点亮,某时刻只有一个灯亮,最终D0在亮
#includereg51.h
#includeintrins.h
void delay(unsigned int t);
void main()
{
unsigned char i;
P1=0x7f;
delay(50000);
for(i=0;i7;i++)
{
P1=_crol_(P1,1);
delay(50000);
}
while(1);
}
void delay(unsigned int t)
{
while(--t);
}
实验五
#includereg51.h
sbit K0=P1^0;
sbit K1=P1^1;
sbit LED0=P1^2;
sbit LED1=P1^3;
void main()
{
while(1)
{
if(K0==1)
LED0=1;
else
LED0=0;
if(K1==1)
LED1=1;
else
LED1=0;
}
}
实验六
(1)实验箱程序清单
#includereg51.h
sbit K0=P0^0;
sbit lemp=P1^0;
void main()
{
while(1)
{
if(K0==0)
lemp=0;
else
lemp=1;
}
}
2.Proteus 仿真程序清党
#includereg51.h
sbit K0=P0^0;
sbit lamp=P3^0;
void main()
{
While(1)
{
if(K0==0)
lamp=0;
else
lamp=1;
}
}
实验七
#includereg51.h
#define uint unsigned int
#define uchar unsigned char
sbit K0=P1^0;
void delay(uint x)
{
uint i,j;
for(i=0;ix;i++)
for(j=0;j1000;j++)
;
}
void main()
{
while(1)
{
K0=1;
delay(50);
K0=0;
delay(50);
}
}
实验十
(1)仿真一程序清单
#includereg51.h
sbit sdata=P1^0;
sbit clk=P1^1;
void delay(unsigned int x)
{
unsigned int I,j;
for(i=0;ix;i++)
for(j=0;i60000;i++)
;
}
void write_74ls164(unsigned char dat)
{
unsigned char i;
for(i=0;i8;i++)
{
Clk=0;
sdata=dat0x01;
clk=1;
delay(50000);
dat=dat1;
}
}
void main()
{
write_74ls164(0x01);
while(1);
}
(2)仿真二程序清单
#include reg51.h
#define uchar unsigned char
#define uint unsigned int
sbit sda=P1^0;
sbit clk=P1^1;
void write_741s164(uchar dat)
{
uchar i;
for(i=0;i8;i++)
{
clk=0;
sda=dat0x01;
clk=1;
dat=dat1;
}
}
void delay(uint x)
{
uchar y;
for(;x0;x--)
for(y=1000;y0;y--);
}
void main()
{
while(1)
{
write_741s164(0xff);
delay(1000);
}
}
(3)仿真三程序清单
#include reg51.h
#define uchar unsigned char
#define uint unsigned int
sbit sda=P1^0;
sbit clk=P1^1;
uchar a[2]={0xff,0xf6};
void write_741s164(uchar dat)
{
uchar i;
for(i=0;i8;i++)
{
clk=0;
sda=dat0x01;
clk=1;
dat=dat1;
}
}
void delay(uint x)
{
uchar y;
for(;x0;x--)
for(y=1000;y0;y--);
}
void main()
{
while(1)
{
write_741s164(a[0]);
write_741s164(a[1]);
delay(1000);
}
}
(4)实验箱程序清单
#includereg51.h》
#define uchar unsigned char
#define uint unsigned int
sbit sda=P1^0;
sbit clk=P1^1;
uchar a[5]={0xfe,0xfc,0x9c,0xb6,0x60};
void write_74ls164(uchar dat)
{
uchar I;
for(i=0;i8;i++)
{
clk=0;
sda=dat0x01;
clk=1;
dat=dat1;
}
}
void delayls(uint x)
{
uchar y;
for(;x0;x--)
for(y=1000;y0;y--)
;
}
void main()
{
while(1)
{
write_74ls164(a[0]);
write_74ls164(a[1]);
write_74ls164(a[2]);
write_74ls164(a[3]);
write_74ls164(a[4]);
delayls(3000);
}
}
实验十一
#include reg51.h
#define uchar unsigned char
#define uint unsigned int
uchar duanma[6]={0x06,0x7d,0x7f,0x06,0x7d,0x7f};
uchar weima[6]={0x20,0x10,0x08,0x04,0x02,0x01};
void dealy(uint x)
{
uint i,j;
for(i=0;ix;i++)
for(j=0;j1000;j++);
}
void main()
{
uchar i;
while (1)
{
for(i=0;i6;i++)
{
P0=duanma[i];
P1=weima[i];
dealy(80);
}
}
}
实验十二
#includereg51.h
void main(){
unsigned char x;
while(1)
{
x=P1;
switch(x)
{
case 0xfe:P2=0x3f;break;
case 0xfd:P2=0x06;break;//84218421
case 0xfb:P2=0x5b;break;//2 01011011
case 0xf7:P2=0x4f;break;//3 01001111
case 0xef:P2=0x66;break;//4 01100110
case 0xdf:P2=0x6b;break;//5 01101101
case 0xbf:P2=0x7d;break;//6 01111101
case 0x7f:P2=0x07;break;
}
}
}
实验十三
#includereg51.h
#define GPIO_KEY P1
unsigned char m[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char KeyValue;
void ys(unsigned int c);
void KeyDown();
void main()
{
while(1)
{
KeyDown();
P2=m[KeyValue];
ys(25);
}
}
void KeyDown()
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
ys(1);
if(GPIO_KEY!=0x0f)
{
GPIO_KEY=0xf0;
switch(GPIO_KEY)
{
case(0xe0): KeyValue=0;break;
case(0xd0): KeyValue=1;break;
case(0xb0): KeyValue=2;break;
case(0x70): KeyValue=3;break;
}
GPIO_KEY=0x0f;
switch(GPIO_KEY)
{
case(0x0e): KeyValue=KeyValue;break;
case(0x0d): KeyValue=KeyValue+4;break;
case(0x0b): KeyValue=KeyValue+8;break;
case(0x07): KeyValue=KeyValue+12;break;
}
while((a50)(GPIO_KEY!=0x0f))
{
ys(1);
a++;
}
}
}
}
void ys(unsigned int c)
{
unsigned char a,b;
for(;c0;c--)
{
for(b=38;b0;b--)
{
for(a=130;a0;a--)
;
}
}
}
实验十四
#includereg51.h
sbit sz=P3^4;
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;ix;i++)
for(j=0;j1000;j++);
}
void main()
{
TMOD=0X05;
TH0=0;
TL0=0;
TR0=1;
while(1)
{
P1=TL0;
delay(0);
}
}
实验十五
(1)中断方式
#includereg51.h
sbit led=P1^0;
unsigned int counter;
void timer0_inter()interrupt 1
{
counter++;
}
void main()
{
led=1;
TH0=0x9c;
TL0=0x9c;
TMOD=0X02;
ET0=1;
EA=1;
TR0=1;
while(1)
{
if(counter==10000)
{
counter=0;
led=~led;
}
}
}
(2)非中断方式:
#includereg51.h
sbit led=P1^0;
void timer0(unsigned int x)
{
unsigned int i=0;
TH0=0x9c;
TL0=0x9c;
TR0=1;
while(ix)
{
while(TF0==0)
;
i++;
TF0=0;
}
}
void main()
{
Led=1;
TMOD=0x02;
while(1)
{
led=0;
timer0(10000);
led=1;
timer0(10000);
}
}
实验十六
#includereg51.h
sbit P1_0=P1^0;
sbit P1_1=P1^1;
void delay();
void main()
{
P1_0=0;
P1_1=0;
EA=1;
IT0=1;
EX0=1;
while(1)
{
P1_1=0;
delay();
P1_1=1;
delay();
}
}
void int0() interrupt 0
{
bit saveled;
EA=0;
saveled=P1_1;
P1_0=!P1_0;
delay();
delay();
delay();
P1_1=saveled;
EA=1;
}
void delay()
{
unsigned int m,n;
for(m=0;m250;m++)
for(n=0;n255;n++)
;
}
实验十七
#includereg51.h
void delay();
void right();
void main()
{
P1=0xff;
EA=1;
IT0=1;
EX0=1;
while(1)
{
right();
}
}
void int0() interrupt 0
{
unsigned char saveled,i;
EA=0;
saveled=P1;
P1=0xff;
for(i=0;i10;i++)
{
P1=~P1;
delay();
}
P1=saveled;
EA=1;
}
void right()
{
unsigned char i,k;
k=0x80;
for(i=0;i8;i++)
{
P1=~k;
delay();
k=1;
}
}
void delay()
{
unsigned int m,n;
for(m=0;m250;m++)
for(n+0;n255;n++)
;
}
实验十八
#includereg51.h
sbit led1=P1^0;
sbit led2=P1^1;
void delayls();
void main()
{
EA=1;
IT0=1;
EX0=1;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(1)
{
led1=0;
delayls();
led1=1;
led2=0;
delayls();
led2=1;
}
}
void int0() interrupt 0
{
unsigned char saveled;
EA=0;
saveled=P1;
led1=1;
led2=0;
delayls();
delayls();
delayls();
delayls();
led2=1;
P1=saveled;
EA=1;
}
void delayls()
{
unsigned char i=0;
while(i!=20)
{
if(TF0==1)
{
i++;
TH0=0x3c;
TL0=0xb0;
TF0=0;
}
}
}
实验十八
#includereg51.h
sbit led1=P1^0;
sbit led2=P1^1;
void delayls();
void main()
{
EA=1;
IT0=1;
EX0=1;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(1)
{
P1_0=0;
delayls();
P1_0=1;
P1_1=0;
delayls();
P1_1=1;
}
}
void int0() interrupt 0
{
unsigned char saveled;
EA=0;
saveled=P1;
P1_0=1;
P1_1=0;
delayls();
delayls();
delayls();
delayls();
P1_1=1;
P1=saveled;
EA=1;
}
void delayls()
{
unsigned char i=0;
while(i!=20)
{
if(TF0==1)
{
i++;
TH0=0x3c;
TL0=0xb0;
TF0=0;
}
}
}
实验二十
(1)串口以工作方式1实现数据收发
#include reg51.h
#define uint unsigned int
#define uchar unsigned char
void delay(uint x)
{
uint y,z;
for(y=x;y0;y--)
for(z=250;z0;z--)
;
}
void timer1_int()
{
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
TR1=1;
}
void serial_int()
{
PCON=0;
SCON=0x40;
}
void main()
{
uchar send_data=0;
uchar i=0;
timer1_int();
serial_int();
IE=0x00;
while(1)
{
send_data='A';
for(i=0;i26;i++)
{
SBUF=send_data;
while(TI==0)
;
TI=0;
send_data++;
delay(1000);
}
delay(6000);
}
}
(2)89c51点对点通讯
#includereg51.h
#define uint unsigned int
#define uchar unsigned char
sbit P2_0=P2^0;
sbit P2_1=P2^1;
uchar led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar num;
void delay(uchar x)
{
uchar y,z;
for(y=x;y0;y--)
for(z=250;z0;z++)
;
}
void display(uchar num)
{
P2_0=0;
P0=~led[num/10];
delay(5);
P2_0=1;
P2_1=0;
P0=~led[num%10];
delay(5);
P2_1=1;
}
void TAKE_SBUF(uchar dat)
{
ES=0;
SUBF=dat;
while(~TI)
;
TI=0;
ES=1;
}
void main()
{
SCON=0x90;
EA=1;
ES=1;
P2=0xff;
num=0;
TAKE_SUBF(num+1);
while(1)
;
}
void intorupt()interrupt 4
{
uchar num;
Uchar i;
RI=0;
num=SUBF;
if(num99)
Num=0;
for(i=100;i0;i--)
display(num);
TAKE_SBUF(num+1);
}
实验二十三
#includereg51.h
#define ZHK P0
#define ZLK P1
#define uchar unsigned char
#define uint unsigned int
uchar h1[]={0xf7,0xef};
uchar l1[]={0x18,0x18};
uchar h2[]={0xfb,0xf7,0xef,0xdf};
uchar l2[]={0x3c,0x24,0x24,0x3c};
uchar h3[]={0xfd,0xfb,0xf7,0xef,0xdf,0xbf};
uchar l3[]={0x7e,0x42,0x42,0x42,0x42,0x7e};
uchar h4[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar l4[]={0xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff};
void delay(uchar x)
{
uchar y,z;
for(y=x;y0;y--)
for(z=250;z0;z--)
;
}
void main()
{
uchar i;
while(1)
{
for(i=0;i2;i++)
{
ZHK=h1[i];
ZLK=l1[i];
delay(5);
}
delay(100);
for(i=0;i4;i++)
{
ZHK=h2[i];
ZLK=l2[i];
delay(5);
}
delay(100);
for(i=0;i6;i++)
{
ZHK=h3[i];
ZLK=l3[i];
delay(5);
}
delay(100);
for(i=0;i8;i++)
{
ZHK=h4[i];
ZLK=l4[i];
delay(5);
}
delay(100);
}
}
实验二十四
#includereg51.h
#define uint unsigned int
#define uchar unsigned char
sbit lcde=P1^0;
sbit lcdrs=P1^1;
uchar i;
uchar a1[]="Welcome to ";
uchar a2[]="an zhi yuan";
void chushihua();
void write_c(uchar c);
void write_d(uchar d);
void delay(uint x);
void main()
{
chushihua();
write_c(0x80+0x00);
for(i=0;i11;i++)
{
write_d(a1[i]);
delay(10);
}
write_c(0x80+0x41);
for(i=0;i11;i++)
{
write_d(a2[i]);
delay(10);
}
for(i=0;i3;i++)
{
write_c(0x18);
delay(10);
}
while(1)
;
}
void chushihua()
{
lcde=0;
write_c(0x38);
write_c(0x0f);
write_c(0x01);
write_c(0x06);
}
void write_c(uchar c)
{
lcdrs=0;
P2=c;
delay(2);
lcde=1;
delay(5);
lcde=0;
delay(2);
}
void write_d(uchar d)
{
lcdrs=1;
P2=d;
delay(2);
lcde=1;
delay(5);
lcde=0;
delay(2);
}
void delay(uint x)
{
uint i,j;
for(i=x;i0;i--)
for(j=120;j0;j--)
;
}
单片机控制共阴数码管显示0到60,俩个按键,按下一个显示0到60,按下另一个显示28,求大神解答!
这个是99跑马表吧,很简单啊,建设楼主在面度搜《单片机40个实验》,下载来看看,很不错地
10. 00-99计数器
1. 实验任务
利用AT89S51单片机来制作一个手动计数器,在AT89S51单片机的P3.7管脚接一个轻触开关,作为手动计数的按钮,用单片机的P2.0-P2.7接一个共阴数码管,作为00-99计数的个位数显示,用单片机的P0.0-P0.7接一个共阴数码管,作为00-99计数的十位数显示;硬件电路图如图19所示。
2. 电路原理图
图4.10.1
3. 系统板上硬件连线
(1. 把“单片机系统”区域中的P0.0/AD0-P0.7/AD7端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个a-h端口上;要求:P0.0/AD0对应着a,P0.1/AD1对应着b,……,P0.7/AD7对应着h。
(2. 把“单片机系统”区域中的P2.0/A8-P2.7/A15端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个数码管的a-h端口上;
(3. 把“单片机系统”区域中的P3.7/RD端口用导线连接到“独立式键盘”区域中的SP1端口上;
4. 程序设计内容
(1. 单片机对按键的识别的过程处理
(2. 单片机对正确识别的按键进行计数,计数满时,又从零开始计数;
(3. 单片机对计的数值要进行数码显示,计得的数是十进数,含有十位和个位,我们要把十位和个位拆开分别送出这样的十位和个位数值到对应的数码管上显示。如何拆开十位和个位我们可以把所计得的数值对10求余,即可得个位数字,对10整除,即可得到十位数字了。
(4. 通过查表方式,分别显示出个位和十位数字。
5. 程序框图
图4.10.2
6. 汇编源程序
Count EQU 30H
SP1 BIT P3.7
ORG 0
START: MOV Count,#00H
NEXT: MOV A,Count
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOVC A,@A+DPTR
MOV P2,A
WT: JNB SP1,WT
WAIT: JB SP1,WAIT
LCALL DELY10MS
JB SP1,WAIT
INC Count
MOV A,Count
CJNE A,#100,NEXT
LJMP START
DELY10MS: MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END
7. C语言源程序
#include AT89X51.H
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Count;
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i0;i--)
for(j=248;j0;j--);
}
void main(void)
{
Count=0;
P0=table[Count/10];
P2=table[Count%10];
while(1)
{
if(P3_7==0)
{
delay10ms();
if(P3_7==0)
{
Count++;
if(Count==100)
{
Count=0;
}
P0=table[Count/10];
P2=table[Count%10];
while(P3_7==0);
}
}
}
}
单片机原理及接口技术的实验,求大神!!!!!!!!
1:
MOV R0,#40H
MOV R2,#30
LP:
MOV @R0,#0
INC R0
DJNZ R2,LP
2:
MOV DPTR,#7000H
MOV R2,#30
LP:
CLR A
MOVX @DPTR,A
INC DPTR
DJNZ R2,LP
3:
MOV R0,#20H
MOV R2,#40
LP:
MOV @R0,#0FEH
INC R0
DJNZ R2,LP
4:
MOV A,30H
ANL A,#0FH
XCH A,30H
MOV A,40H
ANL A,#0FH
SWAP A
ORL A,30H
MOV 50H,A
5:
MOV DPTR,#7000H
MOVX A,@DPTR
ANL A,#0F0H
SWAP A
MOV B,A
INC DPTR
MOVX A,@DPTR
ANL A,#0FH
SWAP A
ORL A,B
INC DPTR
MOVX @DPTR,A
6:
MOV A,30H
ANL A,#0F0H
MOV 31H,A
MOV A,30H
ANL A,#0FH
MOV 32H,A
7:
MOV DPTR,#7000H
MOVX A,@DPTR
PUSH ACC
ANL A,#0F0H
SWAP A
INC DPTR
MOVX @DPTR,A
POP ACC
ANL A,#0FH
INC DPTR
MOVX @DPTR,A
8:
MOV R0,#30H
MOV R1,#40H
MOV R2,#7
LP:
MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,LP
单片机实验编程啊
P0口8255的数据口RS RW接8255的RW RS A1 A0 CS 接地就OK了
#includereg51.h
#includeabsacc.h
#includestdlib.h
#define PA8255 XBYTE [0xe000]
#define PB8255 XBYTE [0xe001]
#define PC8255 XBYTE [0xe002]
#define COM8255 XBYTE [0xe003]
#define uchar unsigned char
uchar show[]={0X01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void init8255(void)
{
COM8255=0x00;//80
}
void main()
{
uchar count=0;
int H=50000;
PB8255=0x00;
while(1)
{
PA8255=show[count];
// PB8255=0X01;
P1= show[count];
count++;
while(H--);
if(count==8)
{
count=0;
}
}
}
51单片机实验,急
#includereg52.h
sbit Start = P3^0; //引脚声明
sbit Pause = P3^1;
sbit Clear = P3^2;
unsigned char code Ledchar[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};//共阳极 0 - 9
unsigned char Ledbuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};//显示缓冲区
unsigned char hour = 0;
unsigned char min = 0;
unsigned int sec = 0;
unsigned char Bms = 0;
unsigned char ?XH = 12; //学号 后两位 ?, 你可以更改
unsigned char ?num = 18; //计数
char cnt = 0; //数码管点亮位置
unsigned char index = 0;
void ConfigTimer0()
{
TMOD = 0x01;
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时重载值
ET0 = 1; //打开定时器0 中断允许
TR0 = 1; //启动定时器0
EA = 1; //打开总中断
}
void main()
{
ConfigTimer0();
while(1)
{
Ledbuf[0] = 0xFF; //转换成BCD码 放入显示缓冲区
Ledbuf[1] = Ledchar[num%10];
Ledbuf[2] = Ledchar[num/10%10];; // "-"
Ledbuf[3] = Ledchar[num/100];
Ledbuf[4] = 0xBF; // "-"
Ledbuf[5] = Ledchar[XH%10]; //学号
Ledbuf[6] = Ledchar[XH/10];
Ledbuf[7] = 0xFF;
}
}
void StartScan()
{
static unsigned char t = 0;
static bit Lock = 0;
if(Start == 1)
{
t = 0;
Lock = 0;
}
else if(Lock == 0)
{
t++;
if(t = 10)
{
if(index == 0)
{
num += 2;
if(num = 22)
{
num = 22;
}
}
else if(index == 2)
{
cnt++;
if(cnt = 8)
{
cnt = 0;
}
}
Lock = 1;
}
}
}
void PauseScan()
{
static unsigned char t = 0;
static bit Lock = 0;
if(Pause == 1)
{
t = 0;
Lock = 0;
}
else if(Lock == 0)
{
t++;
if(t = 10)
{
if(index == 0)
{
num -= 2;
if(num = 14)
{
num = 14;
}
}
else if(index == 2)
{
cnt--;
if(cnt = 0)
{
cnt = 7;
}
}
Lock = 1;
}
}
}
void ClearScan()
{
static unsigned char t = 0;
static bit Lock = 0;
if(Clear == 1)
{
t = 0;
Lock = 0;
}
else if(Lock == 0)
{
t++;
if(t = 10)
{
index++;
if(index = 3)
{
index = 0;
}
Lock = 1;
}
}
}
void KeyScan()
{
ClearScan();
PauseScan();
StartScan();
}
void LedScan()
{
static unsigned char i = 0;
P0 = 0xFF;
if(index == 0)
{
switch(i)
{
case 0: P2 = 0x01; P0 = Ledbuf[7]; i++; break;
case 1: P2 = 0x02; P0 = Ledbuf[6]; i++; break;
case 2: P2 = 0x04; P0 = Ledbuf[5]; i++; break;
case 3: P2 = 0x08; P0 = Ledbuf[4]; i++; break;
case 4: P2 = 0x10; P0 = Ledbuf[3]; i++; break;
case 5: P2 = 0x20; P0 = Ledbuf[2]; i++; break;
case 6: P2 = 0x40; P0 = Ledbuf[1]; i++; break;
case 7: P2 = 0x80; P0 = Ledbuf[0]; i=0; break;
default : break;
}
}
else
{
switch(cnt)
{
case 0: P2 = 0x01; P0 = Ledbuf[7]; ?break;
case 1: P2 = 0x02; P0 = Ledbuf[6]; ?break;
case 2: P2 = 0x04; P0 = Ledbuf[5]; ?break;
case 3: P2 = 0x08; P0 = Ledbuf[4]; ?break;
case 4: P2 = 0x10; P0 = Ledbuf[3]; ?break;
case 5: P2 = 0x20; P0 = Ledbuf[2]; ?break;
case 6: P2 = 0x40; P0 = Ledbuf[1]; ?break;
case 7: P2 = 0x80; P0 = Ledbuf[0]; ?break;
default : break;
}
}
}
void interruptTimer0() interrupt 1
{
static unsigned int ?tt = 0;
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时重载值
KeyScan();
LedScan();
if(index == 1)
{
tt++;
if(tt = 500)
{
tt = 0;
cnt++;
if(cnt = 8)
{
cnt = 0;
}
}
}
}
用的共阳极数码管,, 满足要求, 最下面的按键按一下 ?进行 数码管切换点亮, 再按一下, 可以通过上面两个按键选择显示那个数码管
再按最下面的 ?就会回到刚开始显示页面,就是学号什么的,全部显示, 此时 上面两个按键 就是对计数值进行加减2 ,