唐山学院单片机实验考试
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
外驱。
程序:利用8051单片机串行口,和并行输出串行移位寄存器74LS164,扩展I/O口,在数码显示器上循环显示0-9这十个数字。
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
main()
{
uchar i;
while(1)
{
i=P1;
i=i>>4;
P1=i|0xf0;
}
}
外部中断实验:P1.0-P1.7接L0-L7,P3.2接单脉冲输出端“凹”。
#include<reg51.h>
#define uchar unsigned char
uchar data t=0,m1s=0;
uchar code seg[]={0x05,0x0a,0x50,0xa0,0x55,0xaa,0xff,0x00};
void time 1(void)interrupt 3
led=0;
}
}
main()
{
uchar i,a;
led=0;
IT0=1;
EX0=1;
EA=1;
while(1)
{
a=0x01;
for(i=0;i<8;i++)
{P1=a;
delay(50000):
a=a<<1;
}
}
}
定时器实验:P1.0-P1.7接L0-L7.
程序:8051内部定时器T1,按方式一工作,即作为十六位定时器使用每0.05秒溢出一次。开机后,第一秒L0,L2亮,第二秒L1,L3亮,第三秒4,6亮,第四秒5,7,第五秒0,2,4,6,第六秒1,3,5,7,第七秒全亮,第八秒全亮,循环。
for(j=0;j<111;j++);
}
disp()
{
uchar i,n=1,*ptr;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
{ptr=seg;
SBUF=*(ptr+disbuf[i]);
void delay(unsigned int N)
{
int i;
for(i=0;i<N;i++);
}
main()
{
uchar i,a;
while(1)
{
a=0x01;
for(i=0;i<8;i++)
{
P1=a;
delay(50000);
a=a<<1;
}
}
}
程序(2):P1低四位接L0-L3,高四位接K0-K3,将开关的状态在二极管上显示。
P1口:(1)P1.0-P1.7接L0-L7;(2)P1.0-P1.3接L0-L3,P1.4-P1.7接k0-k3.
程序(1):P1做输出口,接八只发光二极管,使其循环点亮。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
{
p=0x8004;
ptr=seg;
a=*(ptr+disbuf[i]);
*p=a;
p=0x8002;
*p=n;
n=n<<1;
DelayMs(10);
}
}
ad()
{
uchar xdata *p;
uchar i,n=1;
p=0x9000;
*p=0x00;
while(int0==0);
i=*p;
disbuf[2]=i/100;
n=i%100;
disbuf[1]=n/10;
disbuf[0]=n%10;
}
main()
{
while(1)
{
ad();
disp();
}
}
uchar data t=0,m1s=0;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar data disbuf[6]={0x01,0x02,0x03,0x04,0x05,0x06};
bit s1s=0;
{
TH1=0x3C;
TL1=0xB0;
t++;
if(t==20){
t=0;
m1s++;
if(m1s==8)m1s=0;
}
}
main()
{Leabharlann Baidu
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;
while(1)
{
P1=seg[m1s];
}
}
串行口:G1接P1.1; G0接P1.0; 164的Q0-Q7接H-A,CLK接P3.1,AB接P3.0.
TL1=0xb0;
t++;
if(t==20){s1s=1;
t=0;
s++;
if(s==60){
s=0;
m++;
if(m==60){
m=0;
h++;
if(h==24)h=0;
}
}
}
}
void DelayMs(uchar time)
{
uchar i,j;
for(i=0;i<time;i++)
for(j=0;j<111;j++);
uchar t=0;
uchar disbuf[6];
uchar s,m,h;
bit s1s=0;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void time1(void)interrupt 3
{
TH1=0x3C;
{
int i;
for(i=0;i<N;i++);
}
void pint0(void)interrupt 0 using 1
{
if(led==0){
P1=0xff;
delay(50000);
delay(50000);
led=1;
}
else{
P1=0x00;
delay(50000);
delay(50000);
while(TI==0);
TI=0;
P1=n;
n=n<<1;
DelayMs(10);
P1=0x00;
}
}
main()
{
uchar n;
SCON=0x00;
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;s1s=1;
while(1)
{
disp();
if(_testbit_(s1s)){
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit int0=P3^2;
uchar disbuf[6];
void DelayMs(uchar time)
{
uchar i,j;
disbuf[2]=m1s/100;
n=m1s%100;
disbuf[1]=n/10;
disbuf[0]=n%10;
}
}
}
数字电子钟:KEY/LED_CS接CS0(内驱)
程序:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
disbuf[0]=s%10;
disbuf[3]=m/10;
disbuf[2]=m%10;
disbuf[5]=h/10;
disbuf[4]=h%10;
}
}
}
A/D转换:CS0接KEY/LED_CS; CS1接AD_CS; IN0接电位器输出;EOC接P3.2.
程序:实验台上的电位器提供模拟量输入,将模拟量转换为数字量。
void time1(void) interrupt 3
{
TH1=0x3C;
TL1=0xB0;
t++;
if(t==20){s1s=1;
t=0;
m1s++;
if(m1s==100)m1s=0;
}
}
void DelayMs(uchar time)
{
uchar i,j;
for(i=0;i<time;i++)
}
disp()
{
uchar xdata *p;
uchar i,n=1,*ptr,a;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
{
p=0x8004;
ptr=seg;
a=*(ptr+disbuf[i]);
程序:二极管循环点亮,第一次外部中断全亮,延时1秒返回;第二次全灭,延时1秒返回,一直循环。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
bit led=0;
void delay(unsigned int N)
for(i=0;i<time;i++)
for(j=0;j<111;j++);
}
disp()
{
uchar xdata *p;
uchar i,n=1,*ptr,a;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
*p=a;
p=0x8002;
*p=n;
n=n<<1;
DelayMs(10);
*p=0x00;
}
}
main()
{
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;
while(1)
{
disp();
if(_testbit_(s1s)){
disbuf[1]=s/10;
程序:利用8051单片机串行口,和并行输出串行移位寄存器74LS164,扩展I/O口,在数码显示器上循环显示0-9这十个数字。
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
main()
{
uchar i;
while(1)
{
i=P1;
i=i>>4;
P1=i|0xf0;
}
}
外部中断实验:P1.0-P1.7接L0-L7,P3.2接单脉冲输出端“凹”。
#include<reg51.h>
#define uchar unsigned char
uchar data t=0,m1s=0;
uchar code seg[]={0x05,0x0a,0x50,0xa0,0x55,0xaa,0xff,0x00};
void time 1(void)interrupt 3
led=0;
}
}
main()
{
uchar i,a;
led=0;
IT0=1;
EX0=1;
EA=1;
while(1)
{
a=0x01;
for(i=0;i<8;i++)
{P1=a;
delay(50000):
a=a<<1;
}
}
}
定时器实验:P1.0-P1.7接L0-L7.
程序:8051内部定时器T1,按方式一工作,即作为十六位定时器使用每0.05秒溢出一次。开机后,第一秒L0,L2亮,第二秒L1,L3亮,第三秒4,6亮,第四秒5,7,第五秒0,2,4,6,第六秒1,3,5,7,第七秒全亮,第八秒全亮,循环。
for(j=0;j<111;j++);
}
disp()
{
uchar i,n=1,*ptr;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
{ptr=seg;
SBUF=*(ptr+disbuf[i]);
void delay(unsigned int N)
{
int i;
for(i=0;i<N;i++);
}
main()
{
uchar i,a;
while(1)
{
a=0x01;
for(i=0;i<8;i++)
{
P1=a;
delay(50000);
a=a<<1;
}
}
}
程序(2):P1低四位接L0-L3,高四位接K0-K3,将开关的状态在二极管上显示。
P1口:(1)P1.0-P1.7接L0-L7;(2)P1.0-P1.3接L0-L3,P1.4-P1.7接k0-k3.
程序(1):P1做输出口,接八只发光二极管,使其循环点亮。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
{
p=0x8004;
ptr=seg;
a=*(ptr+disbuf[i]);
*p=a;
p=0x8002;
*p=n;
n=n<<1;
DelayMs(10);
}
}
ad()
{
uchar xdata *p;
uchar i,n=1;
p=0x9000;
*p=0x00;
while(int0==0);
i=*p;
disbuf[2]=i/100;
n=i%100;
disbuf[1]=n/10;
disbuf[0]=n%10;
}
main()
{
while(1)
{
ad();
disp();
}
}
uchar data t=0,m1s=0;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar data disbuf[6]={0x01,0x02,0x03,0x04,0x05,0x06};
bit s1s=0;
{
TH1=0x3C;
TL1=0xB0;
t++;
if(t==20){
t=0;
m1s++;
if(m1s==8)m1s=0;
}
}
main()
{Leabharlann Baidu
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;
while(1)
{
P1=seg[m1s];
}
}
串行口:G1接P1.1; G0接P1.0; 164的Q0-Q7接H-A,CLK接P3.1,AB接P3.0.
TL1=0xb0;
t++;
if(t==20){s1s=1;
t=0;
s++;
if(s==60){
s=0;
m++;
if(m==60){
m=0;
h++;
if(h==24)h=0;
}
}
}
}
void DelayMs(uchar time)
{
uchar i,j;
for(i=0;i<time;i++)
for(j=0;j<111;j++);
uchar t=0;
uchar disbuf[6];
uchar s,m,h;
bit s1s=0;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void time1(void)interrupt 3
{
TH1=0x3C;
{
int i;
for(i=0;i<N;i++);
}
void pint0(void)interrupt 0 using 1
{
if(led==0){
P1=0xff;
delay(50000);
delay(50000);
led=1;
}
else{
P1=0x00;
delay(50000);
delay(50000);
while(TI==0);
TI=0;
P1=n;
n=n<<1;
DelayMs(10);
P1=0x00;
}
}
main()
{
uchar n;
SCON=0x00;
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;s1s=1;
while(1)
{
disp();
if(_testbit_(s1s)){
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit int0=P3^2;
uchar disbuf[6];
void DelayMs(uchar time)
{
uchar i,j;
disbuf[2]=m1s/100;
n=m1s%100;
disbuf[1]=n/10;
disbuf[0]=n%10;
}
}
}
数字电子钟:KEY/LED_CS接CS0(内驱)
程序:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
disbuf[0]=s%10;
disbuf[3]=m/10;
disbuf[2]=m%10;
disbuf[5]=h/10;
disbuf[4]=h%10;
}
}
}
A/D转换:CS0接KEY/LED_CS; CS1接AD_CS; IN0接电位器输出;EOC接P3.2.
程序:实验台上的电位器提供模拟量输入,将模拟量转换为数字量。
void time1(void) interrupt 3
{
TH1=0x3C;
TL1=0xB0;
t++;
if(t==20){s1s=1;
t=0;
m1s++;
if(m1s==100)m1s=0;
}
}
void DelayMs(uchar time)
{
uchar i,j;
for(i=0;i<time;i++)
}
disp()
{
uchar xdata *p;
uchar i,n=1,*ptr,a;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
{
p=0x8004;
ptr=seg;
a=*(ptr+disbuf[i]);
程序:二极管循环点亮,第一次外部中断全亮,延时1秒返回;第二次全灭,延时1秒返回,一直循环。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
bit led=0;
void delay(unsigned int N)
for(i=0;i<time;i++)
for(j=0;j<111;j++);
}
disp()
{
uchar xdata *p;
uchar i,n=1,*ptr,a;
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
for(i=0;i<6;i++)
*p=a;
p=0x8002;
*p=n;
n=n<<1;
DelayMs(10);
*p=0x00;
}
}
main()
{
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
ET1=1;
TR1=1;
EA=1;
t=0;
while(1)
{
disp();
if(_testbit_(s1s)){
disbuf[1]=s/10;