单片机实验报告1.doc

上传人:laozhun 文档编号:4151286 上传时间:2023-04-07 格式:DOC 页数:24 大小:236KB
返回 下载 相关 举报
单片机实验报告1.doc_第1页
第1页 / 共24页
单片机实验报告1.doc_第2页
第2页 / 共24页
单片机实验报告1.doc_第3页
第3页 / 共24页
单片机实验报告1.doc_第4页
第4页 / 共24页
单片机实验报告1.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《单片机实验报告1.doc》由会员分享,可在线阅读,更多相关《单片机实验报告1.doc(24页珍藏版)》请在三一办公上搜索。

1、单片机实验报告 姓名: 钟林春 班级: 0801205 学号: 2012212963 任课教师: 张开碧 上课地点: S411 实验一 流水灯实验一、实验目的及要求1、闪烁的LED;2、从左到右的流水灯;3、8只LED左右来回点亮;4、LED模拟交通灯。要求1:LED按设定的时间间隔闪烁要求2:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果要求3:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果;二、实验原理电路图1、 电路图2、 三、源程序 要求1:#include#define uchar unsigned char#define uint unsigned i

2、ntsbit LED=P10;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()while(1)LED=LED;DelayMS(150);要求2:#include #include /包含程序中的_cror_(P0,y) #define uchar unsigned char #define uint unsigned intuchar led;void delay(unsigned int i)uint j;uchar k;for(j=i;j0;j-)for(k=125;k0;k-);void ma

3、in()led=0x7f;/只亮第一个delay(1000);while(1)P0=led;delay(500);led=_cror_(led,1); /P0逐步右移一位。要求3:#include #include /包含程序中的_crol_(P0,y) #define uchar unsigned char #define uint unsigned intsbit led=P00;uchar i;void delay(unsigned int i)uint j;uchar k;for(j=i;j0;j-)for(k=125;k0;k-);void main()P0=0xfe;delay(1

4、000);while(1) for(i=0;i8;i+)P0=led;delay(500);led=_crol_(led,1); /P0逐步左移一位。for(i=0;i8;i+)P0=led;delay(500);led=_cror_(led,1); /P0逐步右移一位 实验二 独立键盘实验一、实验目的1、K1-K4 按键状态显示;2、K1-K4 控制LED移位;3、单只数码管循环显示09;4、K1-K4 控制数码管移位显示。 要求1:K1、K2按下时LED点亮,松开时熄灭,K3、K4按下并释放时LED点亮, 再次按下并释放时熄灭; 要求2: 按下K1时,P0口LED上移一位; 按下K2时,P

5、0口LED下移一位; 按下K3时,P2口LED上移一位;按下K4时,P2口LED下移一位;要求3:主程序中的循环语句反复将09的段码送至P0口,使数字09循环显示; 要求4:按下K1时加1计数并增加显示位, 按下K2时减1计数并减少显示位,按下K3时清零二、实验原理电路图1、 电路图2、 电路图3、 电路图4、三、源程序要求1:#include#define uchar unsigned char#define uint unsigned intsbit LED1=P00;sbit LED2=P01;sbit LED3=P02;sbit LED4=P03;sbit K1=P10;sbitK2=

6、P11;sbit K3=P12;sbit K4=P13;/延时voidDelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/主程序void main()P0=0xff;P1=0xff;while(1)LED1=K1;LED2=K2;if(K3=0)while(K3=0);LED3=LED3;if(K4=0)while(K4=0);LED4=LED4;DelayMS(10);要求2:#include#define uchar unsigned char#define uint unsigned intuchar led1;uchar led2;sb

7、it K1=P10;sbitK2=P11;sbit K3=P12;sbit K4=P13;/延时voidDelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/主程序void main()led1=0xfe;Led2=0xfe;while(1) if(K1=0)while(K1=0);P0=led1;led=_crol_(led1,1); /P0口LED上移一位。if(K2=0)while(K2=0);P0=led1;led=_cror_(led1,1); /P0口LED下移一位。if(K3=0)while(K3=0);P2=led2;Led1=_

8、crol_(led2,1); /P0口LED上移一位。if(K4=0)while(K4=0);P2=led2;Led2=_cror_(led2,1); /P0口LED下移一位。要求3:#include#define uchar unsigned char#define uint unsigned intuchar code display10=0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f;/延时Void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/主程序void main()uch

9、ar i;while(1) for(i=0;i10;i+)P0=displayi; DelayMS(500) i=0;要求4:#include#define uchar unsigned char#define uint unsigned intsbit K1=P10;Sbit K2=P11;sbit K3=P12;uchar code display10=0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f;/延时Void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/主程序void ma

10、in()int num;uchar i;P2=1;while(1) If(K1=0)while(K1=0); num+; If(num=10) Num=0;If(K2=0)while(K2=0); Num-; If(num=-1) Num=0;If(K3=0)while(K3=0); Num=0;P0=displaynum; 实验三 矩阵键盘实验一、实验目的及要求1、4X4矩阵键盘控制条形LED显示;2、数码管显示4X4矩阵键盘按键号;要求1:运行本例时,按下的按键值越大点亮的LED越多要求2:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同

11、的值,从而得到按键的序号二、实验原理电路图1、 电路图2、三、 源程序要求1:#include#include#define uchar unsigned char#define uint unsigned int/矩阵键盘按键特征码表uchar code KeyCodeTable=0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88;/延时voidDelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/键盘扫描ucharKeys_Scan()

12、Uchar sCode,kCode,i,k;/低4位置0,放入4行P1=0xf0;/若高4位出现0,则有键按下if(P1&0xf0)!=0xf0)DelayMS(2);if(P1&0xf0)!=0xf0)sCode=0xfe;/行扫描码初值for(k=0;k4;k+)/对4行分别进行扫描P1=sCode;if(P1&0xf0)!=0xf0)kCode=P1;for(i=0;i16;i+)/查表得到按键序号并返回if(kCode=KeyCodeTablei)return(i);elsesCode=_crol_(sCode,1); return(-1);/主程序void main()uchar i

13、,P2_LED,P3_LED;ucharKeyNo=-1;/按键序号,-1表示无按键while(1)KeyNo=Keys_Scan();/扫描键盘获取按键序号KeyNoif(KeyNo!=-1)P2_LED=0xff;P3_LED=0xff;for(i=0;i=KeyNo;i+)/键值越大,点亮的LED越多if(i=1;elseP2_LED=1;P3=P3_LED; /点亮条形LEDP2=P2_LED;要求2:#include#include#define uchar unsigned char#define uint unsigned intunsigned char code table=

14、0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/0-F的码表void matrixkeyscan() /矩阵键盘P1=0xfe;temp=P1;temp=temp&0xf0;if (temp!=0xf0)delay(10);temp=P1;temp=temp&0xf0;if (temp!=0xf0)temp=P1;switch (temp)case 0xee:key=1,num=0;break;case 0xde:key=2,num=1;break;case 0xbe:key=3

15、,num=2;break;case 0x7e:key=4,num=3;break;while (temp!=0xf0)temp=P1;temp=temp&0xf0;P1=0xfd;temp=P1;temp=temp&0xf0;if (temp!=0xf0)delay(10);temp=P1;temp=temp&0xf0;if (temp!=0xf0)temp=P1;switch (temp)case 0xed:key=5,num=4;break;case 0xdd:key=6,num=5;break;case 0xbd:key=7,num=6;break;case 0x7d:key=8,num

16、=7;break;while (temp!=0xf0)temp=P1;temp=temp&0xf0;P1=0xfb;temp=P1;temp=temp&0xf0;if (temp!=0xf0)delay(10);temp=P1;temp=temp&0xf0;if (temp!=0xf0)temp=P1;switch (temp)case 0xeb:key=9,num=8;break;case 0xdb:key=9,num=9;break;case 0xbb:key=10;break; case 0x7b:key=11;break; while (temp!=0xf0)temp=P1;temp=

17、temp&0xf0;P1=0xf7;temp=P1;temp=temp&0xf0;if (temp!=0xf0)delay(10);temp=P1;temp=temp&0xf0;if (temp!=0xf0)temp=P1;switch (temp)case 0xe7:key=12;break; case 0xd7: key=13;break; case 0xb7:key=14;break; case 0x77:key=15;break; default: break;while (temp!=0xf0)temp=P1;temp=temp&0xf0; Void main()while(1)ma

18、trixkeyscan();P0=tablekey; 实验四 外部中断实验一、实验目的及要求1、INT0中断计数;2、外部INT0中断控制LED;3、INT0及INT1中断计数要求1:每次按下计数键时触发INT0中断,中断程序累加计数, 计数值显示在3只数码管上,按下清零键时数码管清零要求2:每次按键都会触发INT0中断,中断发生时将LED状态取反,产生LED状态由按键控制的效果要求3:每次按下第1个计数键时,第1组计数值累加并显示在右边3只数码管上, 每次按下第2个计数键时,第2组计数值累加并显示在左边3只数码管上,后两个按键分别清零。二、实验原理电路图1、 三、源程序要求1:#includ

19、e#define uchar unsigned char#define uint unsigned int/09的段码uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00;/计数值分解后各个待显示的数位uchar DSY_Buffer=0,0,0;uchar Count=0;sbit Clear_Key=P36;/数码管上显示计数值void Show_Count_ON_DSY()DSY_Buffer2=Count/100;/获取3个数DSY_Buffer1=Count%100/10;DSY_Buff

20、er0=Count%10;if(DSY_Buffer2=0) /高位为0时不显示DSY_Buffer2=0x0a;if(DSY_Buffer1=0) /高位为0,若第二位为0同样不显示DSY_Buffer1=0x0a;P0=DSY_CODEDSY_Buffer0;P1=DSY_CODEDSY_Buffer1;P2=DSY_CODEDSY_Buffer2;/主程序void main()P0=0x00;P1=0x00;P2=0x00;IE=0x81;/允许INT0中断IT0=1;/下降沿触发while(1)if(Clear_Key=0) Count=0;/清0Show_Count_ON_DSY()

21、;/INT0中断函数void EX_INT0() interrupt 0Count+; /计数值递增要求2:#include#define uchar unsigned char#define uint unsigned intSbit LED=P00;/主程序void main()IE=0x81;/允许INT0中断IT0=1;/下降沿触发while(1);/INT0中断函数void EX_INT0() interrupt 0LED=LED; /控制LED亮灭要求3:#include#define uchar unsigned char#define uint unsigned intunsi

22、gned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;int num1,num2;sbit K3=P34;/清零1sbit K4=P35;/清零2 sbit wela1=P20; sbit wela2=P21; sbit wela3=P22; sbit wela4=P23; sbit wela5=P24; sbit wela6=P25; Void display(int num2,int num1);void main()IE=0x85;/开EA

23、总中断,允许INT1和INT0中断,1000 0101IT0=1;/下降沿触发IT1=1;/下降沿触发while(1)if(K3=0)while(K3=0);num1=0;if(K4=0)while(K4=0);num2=0;Display(num2,num1);/INT0中断函数void EX_INT0() interrupt 0num1+; /右边的数字加1void EX_INT0() interrupt 0num2+; / 左边的数字加1Void display(int num2,int num1)bai=num1/100;/右边num1百位shi=(num1/10)%10;/右边num

24、1十位ge=num1%10;swan=num2/100;/左边num2百位,数码管十万位wan=(num2/10)%10;qian=num2%10;wela1=1;/打开最高位数码管的位选P1=tableswan;/输入段选数据,即NUM2的百位数,数码管的十万位wela1=0;/关闭最高位数码管的位选wela2=1;P1=tablewan;wela2=0;wela3=1;P1=tableqian;wela3=0;wela4=1;P1=tablebai;wela4=0;wela5=1;P1=tableshi;wela5=0;wela6=1;P1=tablege;wela6=0; 实验五 定时器

25、中断实验一、实验目的1、定时器控制单只LED;2、TIMER0控制流水灯;3、定时器控制4个LED滚动闪烁; 要求1:LED在定时器的中断例程控制下不断闪烁; 要求2: 定时器控制P0、P2口的LED滚动显示,本例未使用中断函数;要求3:4只LED在定时器控制下滚动闪烁; 二、实验原理电路图1、 三、源程序要求1:#include#define uchar unsigned char#define uint unsigned intsbit LED=P00;uchar T_Count=0;/主程序void main()TMOD=0x00; /定时器0工作方式0TH0=(8192-5000)/3

26、2;/5ms定时TL0=(8192-5000)%32;IE=0x82;/允许T0中断TR0=1;while(1);/T0中断函数void LED_Flash() interrupt 1TH0=(8192-5000)/32;/恢复初值TL0=(8192-5000)%32;if(+T_Count=100)/0.5s开关一次LEDLED=LED;T_Count=0; 要求2:#include#include#define uchar unsigned char#define uint unsigned int/主程序void main()uchar T_Count=0;P0=0xfe;P2=0xfe

27、;TMOD=0x01; /定时器0工作方式1TH0=(65536-40000)/256;/40ms定时TL0=(65536-40000)%256;TR0=1;/启动定时器while(1)if(TF0=1)TF0=0;TH0=(65536-40000)/256;/恢复初值TL0=(65536-40000)%256;if(+T_Count=5)P0=_crol_(P0,1);P2=_crol_(P2,1);T_Count=0;要求3:#include#define uchar unsigned char#define uint unsigned intsbit B1=P00;sbit G1=P01

28、;sbit R1=P02;sbit Y1=P03;uint i,j,k;/主程序void main()i=j=k=0;P0=0xff;TMOD=0x02; /定时器0工作方式2TH0=256-200;/200us定时TL0=256-200;IE=0x82;TR0=1;/启动定时器while(1);/T0中断函数void LED_Flash_and_Scroll() interrupt 1if(+k35)return; /定时中断若干次后执行闪烁k=0;switch(i)case 0:B1=B1;break;case 1:G1=G1;break;case 2:R1=R1;break;case 3:Y1=Y1;break;default:i=0;if(+j300) return; /每次闪烁持续一段时间j=0;P0=0xff; /关闭显示i+; /切换到下一个LED

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

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号