山东大学单片机实验 硬件实验.docx

上传人:b****5 文档编号:5225251 上传时间:2022-12-14 格式:DOCX 页数:10 大小:72.04KB
下载 相关 举报
山东大学单片机实验 硬件实验.docx_第1页
第1页 / 共10页
山东大学单片机实验 硬件实验.docx_第2页
第2页 / 共10页
山东大学单片机实验 硬件实验.docx_第3页
第3页 / 共10页
山东大学单片机实验 硬件实验.docx_第4页
第4页 / 共10页
山东大学单片机实验 硬件实验.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

山东大学单片机实验 硬件实验.docx

《山东大学单片机实验 硬件实验.docx》由会员分享,可在线阅读,更多相关《山东大学单片机实验 硬件实验.docx(10页珍藏版)》请在冰豆网上搜索。

山东大学单片机实验 硬件实验.docx

山东大学单片机实验硬件实验

 

信息科学与工程学院

2016-2017学年第二学期

实验报告

课程名称:

单片机原理与应用

 

实验名称:

4.1声光报警实验

4.58255并行I/O口扩展及交通灯信号控制实验

4.77279键盘扫描及动态LED显示实验

 

实验报告

【实验源程序】

实验4.1:

按键声光报警实验

源程序:

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitbell=P1^1;//P1.1口位赋值给铃声

sbitled=P1^0;//P1.0口位赋值给灯

sbitkey0=P1^2;//P1.2口位赋值给按键

uintcount;

/**************************主程序**********************************/

voidmain()//按键按下后就触发中断

{EA=1;//开放中断

EX0=1;//允许外部中断0中断

IT0=0;//外部中断0设置为电平触发方式

while

(1);//循环执行

}

voiddelay(uintcount)//延时1ms

{

uintx,y;

for(x=count;x>0;x--)

for(y=120;y>0;y--);

}

voidshow(uintcount)

{uinti;

for(i=0;i<=count;i++)

{

led=0;bell=0;//开灯响铃

delay(500);//延时0.5s

led=1;bell=1;//关灯灭铃

delay(500);

}

}

voids_timer0()interrupt0using0

{

EA=0;//屏蔽所有中断请求

show(count);//调用灯铃子程序

count++;//每按一次键,记一次数

delay(50);

if(count>=10)//到10清零按键数

count=0;

EA=1;//开放中断

}

实验4.5:

8255并行I/O扩展及交通信号灯控制实验

#include

#include

#include//可使用其中定义的宏来访问绝对地址

#defineucharunsignedchar

#defineuintunsignedint

#definePAXBYTE[0x7FFC]//PA指代外部数据存储器7FFCh的地址

#defineCOMXBYTE[0x7FFF]//由P0P2口联合输出地址

sbitP30=P3^0;

sbitP31=P3^1;

uinttemp,shi,ge,t;

uchara;

voidshow(uinttemp);

voiddelay();

voiddelays();

voidinit_8255();

voidinit_8255()

{

COM=0x80;//c=out,a=out,b=out

}

voiddelay()//延时500MS子程序

{

uinti,j;

for(i=500;i>0;i--)

for(j=120;j>0;j--);

}

voiddelays()//延时1S子程序

{

uinti,j;

for(i=1000;i>0;i--)

for(j=120;j>0;j--);

}

/***************************主程序*****************************/

voidmain()

{

init_8255();

TMOD=0x01;//设定工作模式1

EA=1;//开放中断

ET0=1;//允许T0中断

TF0=0;//定时器0溢出标志位

while

(1)

{

a=0x26;//变量a赋初值

for(temp=25;temp>19;temp--)//绿灯倒计时25s

{

a=a-1;

PA=0x69;//东西绿灯亮

show(a);//调用数码管显示子程序

delays();//延时1s

}

a=0x1a;//变量a赋值

for(temp=19;temp>9;temp--)//倒计时

{

a=a-1;

PA=0x69;//东西绿灯亮

show(a);

delays();

}

a=0x0a;

for(temp=9;temp>0;temp--)

{

a=a-1;

PA=0x69;

show(a);

delays();

}

for(t=5;t>0;t--)//绿灯闪烁5s

{

a=0;//当计时值为0时闪烁

show(a);

PA=0xeb;//东西绿灯灭

delay();//延时0.5s

PA=0x69;//东西绿灯亮

delay();//延时0.5s

}

a=0x26;

for(temp=25;temp>19;temp--)//绿灯倒计时25s

{

a=a-1;

PA=0x96;//南北绿灯亮

show(a);

delays();

}

a=0x1a;

for(temp=19;temp>9;temp--)

{

a=a-1;

PA=0x96;

show(a);

delays();

}

a=0x0a;

for(temp=9;temp>0;temp--)

{

a=a-1;

PA=0x96;

show(a);

delays();

}

for(t=5;t>0;t--)//绿灯闪烁5s

{

a=0;//当计时值为0时闪烁

show(a);

PA=0xbe;//南北绿灯灭

delay();//延时0.5s

PA=0x96;//南北绿灯亮

delay();//延时0.5s

}

}

}

voidshow(uinta)//显示子程序

{

SCON=0x00;//工作模式0

SBUF=a;//将变量a的值送给缓存寄存器

while(!

TI)//等待发送是否完成

TI=0;//中断标志位清零

}

实验4.7:

7279键盘扫描及动态LED显示实验

#include//***函数定义***

#include

#defineucharunsignedchar

#defineuintunsignedint

#defineRESET0xa4//复位指令

#defineRL0xa1//左移指令

#defineDECODE10xc8//译码方式1

#defineREAD0x15//读键盘指令

voiddelay1(void);//长延时

voiddelay2(void);

voidwrite7279(uchar,uchar);//写入到7279

ucharread7279(uchar);//从7279读出

voidsend_byte(uchar);//发送一个字节

ucharreceive_byte(void);//接收一个字节

ucharkeyma[]={0x1b,0x13,0x0b,0x03,0x1a,0x12,0x0a,0x02,0x19,0x11,0x09,0x01,0x18,0x10,0x08,0x00};

//***变量及I/O口定义***

sbitcs=P1^0;//cs连接于P1.0

sbitclk=P1^1;//clk连接于P1.1

sbitdio=P1^2;//dio连接于P1.2

sbitkey=P1^3;//key连接于P1.3

voidmain()

{

ucharanjian,i,num;

send_byte(0xa4);//全部复位指令

while

(1){

if(key==0){//如果按键按下

send_byte(0x15);//读键盘指令

anjian=receive_byte();//接收键盘数据

for(i=0;i<16;i++){

if(anjian==keyma[i])

{

num=i;

break;

}

}

send_byte(0xa1);

write7279(0xc8,num);

while(key==0);

}

}

}

voidwrite7279(ucharcmd,ucharnum)

{

send_byte(cmd);

send_byte(num);

}

ucharread7279(ucharcommand)

{

send_byte(command);

return(receive_byte());

}

voidsend_byte(unsignedcharout_byte)

{

unsignedchari;

cs=0;//芯片使能

delay1();

for(i=0;i<8;i++)//分8次移入数据

{

if(out_byte&0x80)//先传高位

{

dio=1;

}

else

{

dio=0;

}

clk=1;

delay2();

clk=0;

delay2();

out_byte=out_byte*2;//数据左移

}

dio=0;

}

ucharreceive_byte(void)

{

uchari,in_byte;

dio=1;//设置传输口打开

delay1();

for(i=0;i<8;i++)//分8次读入数据高位在前

{

clk=1;

delay2();

in_byte=in_byte*2;//数据左移

if(dio)

{

in_byte=in_byte|0x01;

}

clk=0;

delay2();

}

dio=0;

return(in_byte);

}

voiddelay1(void)

{

uchari;

for(i=0;i<0x30;i++);

}

voiddelay2(void)

{

uchari;

for(i=0;i<8;i++);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 书信模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1