实
验
报
告
学生姓名:
学 号:
指导教师:
实验地点:
实验时间:2018年4月2日 实验 红绿灯显示实验
一、实验目的:
1.掌握程序控制指令。
2.掌握分支程序、子程序、循环程序的编写方法。
3.掌握软件延时的方法、了解机器周期和指令周期的概念。 4.了解“静态显示”的含义。
二、实验内容:
在显示实验的基础上,如果将第7位,第3位,第1位分别看成红灯、黄灯和绿灯,红灯5秒(对应位显示0)后,显示黄灯1秒(对应位显示0),再显示绿灯3秒,编写对应程序,程序要求,延时使用子程序,最好使用标志位。其它内容见显示实验
三、实验步骤:
1.程序清单:
a) DpyDisplay.c: #include
typedef unsigned char u8; typedef unsigned int u16;
#define Dpy P0 sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4;
u8 code array[17] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71,
0x00};
void ls(u8 c, u8 b, u8 a)
{ LSA = a; LSB = b; LSC = c; }
void delay(u16 i) { while(i>0) i--; }
void Dpy1Display(u8 i) { ls(1,1,1); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy2Display(u8 i) { ls(1,1,0); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy3Display(u8 i) { ls(1,0,1); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy4Display(u8 i) { ls(1,0,0); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy5Display(u8 i) { ls(0,1,1); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy6Display(u8 i) { ls(0,1,0); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy7Display(u8 i) { ls(0,0,1); Dpy = array[i]; delay(100); Dpy = 0x00; }
void Dpy8Display(u8 i) { ls(0,0,0); Dpy = array[i]; delay(100); Dpy = 0x00; }
void DpyDisplay(u8 a, u8 b, u8 c, u8 d, u8 e, u8 f, u8 g, u8 h) { Dpy1Display(a); Dpy2Display(b); Dpy3Display(c); Dpy4Display(d); Dpy5Display(e); Dpy6Display(f); Dpy7Display(g); Dpy8Display(h); } b) main.c: #include
typedef unsigned char u8; typedef unsigned int u16;
extern void DpyDisplay(u8 a, u8 b, u8 c, u8 d, u8 e, u8 f, u8 g, u8 h);
void main() { u16 i, j, n; n = 108;
while(1) { for(j=10;j>0;j--) {
for (i=0; i
DpyDisplay(j-1,16,16,16,16,16,16,16); } for(j=2;j>0;j--) {
for (i=0; i
DpyDisplay(16,16,j-1,16,16,16,16,16); } for(j=5;j>0;j--)
} } {
} for (i=0; i
2.实验现象:
开发板数码管第一位从9逐秒递减至0后,第三位从1逐秒递减至0,之后第五位从4逐秒递减至0。 3.实验原理:
a) 通过发送段码选定某位后向该位发送位码以点亮一根数码管。 b) 数码管以非常短的时间重复点亮,利用人的视觉暂留给人以稳定显示字符之感。
c) 利用for循环使数码管稳定显示,并使数字逐秒减少。 4.实验中的注意事项、遇到的问题以及总结:
a) 点亮数码管的循环函数中语句尽可能少,防止数码管显示过程中的闪烁。 b) 最好制作编码表,设置输入值与显示字符的关系,后续即可通过查表完成显示,可大大减小工作量。
c) 利用for控制每位显示可大大减少代码量。 5.实验改进方向与设想
可外接不同色LED模拟红绿灯。