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 uns
2、igned intsbit 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
3、=125;k0;k-);void main() 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
4、-);void main() P0=0xfe; delay(1000); 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点亮, 再次按下并
5、释放时熄灭; 要求2: 按下K1时,P0口LED上移一位; 按下K2时,P0口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 LED
6、3=P02;sbit LED4=P03;sbit K1=P10;sbitK2=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 unsigne
7、d char#define uint unsigned intuchar led1;uchar led2;sbit 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
8、; led=_cror_(led1,1); /P0口LED下移一位。 if(K3=0) while(K3=0); P2=led2; Led1=_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
9、;/延时Void DelayMS(uint x) uchar i; while(x-) for(i=0;i120;i+); /主程序void main() uchar 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,
10、0x07,0x7f,0x6f;/延时Void DelayMS(uint x) uchar i; while(x-) for(i=0;i120;i+); /主程序void main() 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、数码管显示4
11、X4矩阵键盘按键号;要求1:运行本例时,按下的按键值越大点亮的LED越多要求2:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的值,从而得到按键的序号二、实验原理电路图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
12、,0x84,0x88;/延时voidDelayMS(uint x) uchar i; while(x-) for(i=0;i120;i+); /键盘扫描ucharKeys_Scan() 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
13、+) /查表得到按键序号并返回 if(kCode=KeyCodeTablei) return(i); else sCode=_crol_(sCode,1); return(-1);/主程序void main() uchar i,P2_LED,P3_LED; ucharKeyNo=-1; /按键序号,-1表示无按键 while(1) KeyNo=Keys_Scan(); /扫描键盘获取按键序号KeyNo if(KeyNo!=-1) P2_LED=0xff; P3_LED=0xff; for(i=0;i=KeyNo;i+) /键值越大,点亮的LED越多 if(i=1; else P2_LED=1;
14、 P3=P3_LED; /点亮条形LED P2=P2_LED; 要求2:#include#include#define uchar unsigned char#define uint unsigned intunsigned char code table=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
15、(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,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=
16、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=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=te
17、mp&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=temp&0xf0; P1=0xf7; temp=P1; temp=temp&0xf0; if (temp!=0xf0) delay(10); temp=P1; temp=temp&0xf0; if (temp!=0x
18、f0) 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) matrixkeyscan(); P0=tablekey; 实验四 外部中断实验一、实验目的及要求1、INT0中断计数;2、外部INT0中断控制LED;3、INT0及INT1中断计数要
19、求1:每次按下计数键时触发INT0中断,中断程序累加计数, 计数值显示在3只数码管上,按下清零键时数码管清零要求2:每次按键都会触发INT0中断,中断发生时将LED状态取反,产生LED状态由按键控制的效果要求3:每次按下第1个计数键时,第1组计数值累加并显示在右边3只数码管上, 每次按下第2个计数键时,第2组计数值累加并显示在左边3只数码管上,后两个按键分别清零。二、实验原理电路图1、 三、源程序要求1:#include#define uchar unsigned char#define uint unsigned int/09的段码uchar code DSY_CODE=0x3f,0x06,
20、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_Buffer0=Count%10; if(DSY_Buffer2=0) /高位为0时不显示 DSY_Buffer2=0x0a; if(DSY_Buffer1=0) /高位为0,若
21、第二位为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; /清0 Show_Count_ON_DSY(); /INT0中断函数void EX_INT0() interrupt 0 Count+; /计数值递增要求2:#include#de
22、fine uchar unsigned char#define uint unsigned intSbit LED=P00;/主程序void main() IE=0x81; /允许INT0中断 IT0=1; /下降沿触发 while(1); /INT0中断函数void EX_INT0() interrupt 0 LED=LED; /控制LED亮灭要求3:#include#define uchar unsigned char#define uint unsigned intunsigned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0
23、x07,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总中断,允许INT1和INT0中断,1000 0101 IT0=1; /下降沿触发IT1=1; /下降沿触发 w
24、hile(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 0 num1+; /右边的数字加1void EX_INT0() interrupt 0 num2+; / 左边的数字加1Void display(int num2,int num1) bai=num1/100;/右边num1百位shi=(num1/10)%10;/右边num1十位ge=num1%10;swan=num2/100;/左边num2百位,数码管十万位wan=
25、(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; 实验五 定时器中断实验一、实验目的1、定时器控制单只LED;2、TIMER0控制流水灯;3、定时器控制4个LED滚动闪烁; 要求1:LED在定时器的中断例程控制下不断闪烁; 要求2: 定时器控制P0、P2口的LED滚动显示,本例未使用中断函数;要
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1