《微机实验》报告 LED数码管显示实验 指导教师: 专业班级: 姓名 : 学号 : 联系方式 :
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
一、 任务要求
实验目的:理解LED七段数码管的显示控制原理,掌握数码管与MCU的接口技术,能够
编写数码管显示驱动程序;熟悉接口程序调试方法。 实验内容:利用C8051F310单片机控制数码管显示器 基本要求:
利用末位数码管循环显示数字0-9,显示切换频率为1Hz。 提高要求: 在4位数码管显示器上依次显示当天时期和时间,显示格式如下: yyyy (年份)
mm.dd(月份.日) hh.mm(小时.分钟) 思考题:
数码管采用动态驱动方式时刷新频率应如何选择,为什么, 二、 设计思路
C8051F310单片机片上晶振为24.5MHz,采用8分频后为3.0625MHz ,输入时钟信号采用48个机器周期。
0到9对应的断码为:FCH、60H、DAH、F2H、66H、B6H、BEH、E0H、FEH、F6H 基础部分:
由于只需要用末位数码管显示,不需要改变位码,所以只需要采用LED的静态显示。采用查表的方法,通过循环结构,每次循环查找数据表下一地址,循环十次后重新开始循环。每次循环延时1s,采用定时器0定时方式1。
提高部分:
四个数码管都要显示,所以采用LED的动态显示。由于数码管的位选由P0.7、P0.6控制,P0端口的其他引脚都没用到,所以对P0端口初始化赋00H,每次循环加40H、选中下一位,四次后十六进制溢出,P0端口变又为00H回到第一个数码管。
每位数码管显示一个段码后都延时1ms(否则数码管太亮,刺眼)采用定时器0定时方式1,依然采用查表法改变段码值。通过循环:
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down.
Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
DJNZ R5,BACK MOV R5,#250 DJNZ R4,BACK MOV R4,#8
来控制每种模式的切换时间,我采用2s切换一次(8*250*1ms=2s)。 切换模式,可以采用改变查表法的偏移量来实现,没切换一次模式,偏移量加04H,
三次后回到初始偏移量,来实现三种模式的循环显示。 三、 资源分配 基础部分:
P0.7、P0.6:控制数码管的位选 P1:控制数码管段码的显示 R0:控制段选 提高部分:
P0.7、P0.6:控制数码管的位选 1:控制数码管段码的显示 P R0:控制位选 R1:控制段选
R3:用于改变偏移量来切换模式
R4、R5:控制循环次数,控制模式切换时间
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in
cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
四、 流程图 基础部分: 开始 初始化
选中第一个数码管 R0,,00H 查表取段码 段码赋给显示P1 等待中断
延时1s查表偏移指针R0加一 N R0=0AH Y ,,00H R0
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down.
Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
提高部分 开始 初始化 R000H,, ,,00HR1 位选 P0R0,, ,,R0+40H R0
加偏移量查表 取段码 P1,,A显示段码 查表指针R0加一 04H清零满 等待中断 1ms定时
循环达到2s偏移量加04H(用于切换模式) 满三次回到 初始偏移量
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody
will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
五、源代码 (含文件头说明、资源使用说明、语句行注释) 基础部分: ;********************************************************* ;Filename:shumaguan.asm
;Description: 利用末位数码管循环显示数字0-9,显示切换频率为1Hz。 ;Designed by:gxy
;Date:2012/11/7
;********************************************************* $include (C8051F310.inc) ORG 0000H ;复位入口 AJMP MAIN
ORG 000BH ;定时器0中断入口 AJMP TIME0
MAIN: ACALL Init_Device ;初始化配置 MOV P0,#00H ;位选中第一个数码管 MOV R0,#00H ;偏移指针初值 CLR PSW.1 ;标志位清零 SETB EA ;允许总中断 SETB ET0 ;允许定时器0中断
MOV TMOD,#01H ;定时器0选工作方式1 MOV TH0,#06H
MOV TL0,#0C6H ;赋初值,定时1s
LOOP: MOV A,R0 ADD A,#0BH ;加偏移量 MOVC A,@A+PC ;查表取,段码 MOV P1,A ;段码给P1显示 SETB TR0 ;开定时
LOOP1: JNB PSW.1,LOOP1 ;等待中断 CLR PSW.1
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
INC R0 ;偏移指针加一 CJNE R0,#0AH,LOOP3
MOV R0,#00H ;偏移指针满10清零 AJMP LOOP ;返回
DB 0FCH,60H,0DAH,0F2H,66H ;段码数据表:0、1、2、3、4 DB 0B6H,0BEH,0E0H,0FEH,0F6H; 5、6、7、8、9
;***************************************************************** ; 定时器0中断
;***************************************************************** TIME0: SETB PSW.1 ;标志位置一 MOV TH0,#06H ;定时器重新赋值
MOV TL0,#0C6H
LOOP3: CLR TR0 ;关定时 RETI
;***************************************************************** ;初始化配置
;***************************************************************** PCA_Init:
anl PCA0MD, #0BFh mov PCA0MD, #000h ret Timer_Init: mov TMOD, #001h mov CKCON, #002h ret
Port_IO_Init:
; P0.0 - Unassigned, Open-Drain, Digital ; P0.1 - Unassigned, Open-Drain, Digital ; P0.2 - Unassigned, Open-Drain, Digital
; P0.3 - Unassigned, Open-Drain, Digital Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese
military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
; P0.4 - Unassigned, Open-Drain, Digital ; P0.5 - Unassigned, Open-Drain, Digital ; P0.6 - Unassigned, Open-Drain, Digital ; P0.7 - Unassigned, Open-Drain, Digital ; P1.0 - Unassigned, Open-Drain, Digital ; P1.1 - Unassigned, Open-Drain, Digital ; P1.2 - Unassigned, Open-Drain, Digital ; P1.3 - Unassigned, Open-Drain, Digital ; P1.4 - Unassigned, Open-Drain, Digital ; P1.5 - Unassigned, Open-Drain, Digital ; P1.6 - Unassigned, Open-Drain, Digital ; P1.7 - Unassigned, Open-Drain, Digital ; P2.0 - Unassigned, Open-Drain, Digital ; P2.1 - Unassigned, Open-Drain, Digital ; P2.2 - Unassigned, Open-Drain, Digital ; P2.3 - Unassigned, Open-Drain, Digital mov XBR1, #040h ret
Interrupts_Init: mov IE, #002h ret
Init_Device:
lcall PCA_Init lcall Timer_Init lcall Port_IO_Init lcall Interrupts_Init ret end
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
提高部分:
;********************************************************* ;Filename: shumaguan2.asm ;Description:在4位数码管显示器上依次显示当天时期和时间,显示格式如下: ; 2012 (年份)
; 12.07(月份.日) ; 12.34(小时.分钟) ;Designed by:gxy ;Date:2012/11/7
;********************************************************* $include (C8051F310.inc) ORG 0000H AJMP MAIN
ORG 000BH AJMP TIME0
MAIN: ACALL Init_Device MOV R0,#00H ;用于位选 MOV R1,#00H ;用于段选
MOV R2,#22H ;置偏移量,用于控制模式 MOV R4,#8 MOV R5,#250
CLR PSW.1 ;标志位清零 SETB EA ;允许总中断 SETB ET0 ;允许定时器0中断
MOV TMOD,#01H ;定时器0选工作方式1 MOV TH0,#0FFH
MOV TL0,#0C0H ;定时器赋初值1ms BACK: MOV P0,R0 ;位选 MOV A,R0
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
ADD A,#40H ;选下一位 MOV R0,A
MOV A,R1
;加偏移量 ADD A,R2 MOVC A,@A+PC ;查表取段码 MOV P1,A ;段码给P1显示 LOOP: SETB TR0 ;开定时 HERE: JNB PSW.1,HERE ;等待中断 CLR PSW.1 DJNZ R5,BACK MOV R5,#250 DJNZ R4,BACK
MOV R4,#8 ;循环2000次(2s) MOV A,R2
ADD A,#04H ;偏移量加04H,到下一模式段码初值地址 MOV R2,A
CJNE R2,#2EH,LOOP2
MOV R2,#22H ;加三次后偏移量回到初值 LOOP2: AJMP BACK ;返回进入下一模式
;段码数据表:
DB 0DAH,60H,0FCH,0DAH ; 2102 DB 0E0H,0FCH,61H,60H ; 701. 1 DB 66H,0F2H,0DBH,60H ; 432. 1
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody
will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
;***************************************************************** ; 定时器0中断
;***************************************************************** TIME0: MOV TH0,#0FFH MOV TL0,#0C0H CLR TR0 SETB PSW.1
INC R1 ;偏移指针加一 CJNE R1,#04H,LOOP
MOV R1,#00H ;偏移指针满04H清零 RETI
;***************************************************************** ; 初始化配置
;***************************************************************** PCA_Init:
anl PCA0MD, #0BFh mov PCA0MD, #000h ret Timer_Init: mov TMOD, #001h mov CKCON, #002h
ret
Port_IO_Init:
; P0.0 - Unassigned, Open-Drain, Digital ; P0.1 - Unassigned, Open-Drain, Digital ; P0.2 - Unassigned, Open-Drain, Digital ; P0.3 - Unassigned, Open-Drain, Digital ; P0.4 - Unassigned, Open-Drain, Digital ; P0.5 - Unassigned, Open-Drain, Digital ; P0.6 - Unassigned, Open-Drain, Digital ; P0.7 - Unassigned, Open-Drain, Digital
; P1.0 - Unassigned, Open-Drain, Digital Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
; P1.1 - Unassigned, Open-Drain, Digital ; P1.2 - Unassigned, Open-Drain, Digital ; P1.3 - Unassigned, Open-Drain, Digital ; P1.4 - Unassigned, Open-Drain, Digital ; P1.5 - Unassigned, Open-Drain, Digital ; P1.6 - Unassigned, Open-Drain, Digital ; P1.7 - Unassigned, Open-Drain, Digital
; P2.0 - Unassigned, Open-Drain, Digital ; P2.1 - Unassigned, Open-Drain, Digital ; P2.2 - Unassigned, Open-Drain, Digital ; P2.3 - Unassigned, Open-Drain, Digital mov XBR1, #040h ret
Interrupts_Init: mov IE, #002h ret
Init_Device: lcall PCA_Init lcall Timer_Init lcall Port_IO_Init lcall Interrupts_Init ret end
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
六、程序测试方法与结果、软件性能分析 软件调试总体截图:
基础部分:
软件运行时,我们发现P0端口为00H,P1端口以依次为FCH、60H、DAH、F2H、66H、B6H、BEH、E0H、FEH、F6H。说明第一个数码管依次显示0、1、2、3、4、5、6、7、8、9
如图,这是程序运行时的一个截图,P0端口为00H,位选第一个数码管,P1端口为BEH,表示第一个数码管显示数字“6”
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down.
Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
0到9显示如下:
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
所以,软件调试时,运行正常。
Program Size: data=8.0 xdata=0 code=112 硬件调试时
C8051F310单片机开发板上第一个数码管循环显示数字0-9,显示切换频率为1Hz。
运行正常,数码管亮度适中,如图:
综上:基础部分代码,无论是在软件上还是硬件上都运行正常,达到了实验的要求。
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down.
Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
提高部分:
软件调试时,P0端口不断在00H、40H、80H、C0H中循环跳动,说明每1ms换一个位码,即每1ms亮下一个数码管,软件截图如下:
P1端口也不断在0DAH,60H,0FCH,0DAH(2102 )间跳动 ; 过2s后切换到在0E0H,0FCH,61H,60H(701. 1)间跳动; 在过2s在66H,0F2H,0DBH,60H(432. 1)间跳动; 再过2s后又切换到第一种模式 所以,软件调试时,运行正常。
Program Size: data=8.0 xdata=0 code=141 硬件调试时
C8051F310单片机开发板上4位数码管显示器上依次显示年份、月日和时分,亮度适中 每2s切换一次,切换正常,截图如下:
分别显示2012年、11月07日、12点34分。
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
综上:提高部分代码,无论是在软件上还是硬件上都运行正常,达到了实验的要求。
七、思考题
数码管采用动态驱动方式时刷新频率应如何选择,为什么,
答:每位数码管点亮时间(亮度)与扫描频率成反比。扫描频率最好大于50Hz,即每一轮时间不超过20ms。每位数码管显示时间不能太长,这样会影响刷新频率,产生闪烁现象;时间太短会使亮度降低,所以每位数码点亮时间为1ms左右。刷新频率为10ms左右为最佳。这样亮度适中,也没有闪烁情况。
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
八、实验小结
通过本次试验,我们理解了LED七段数码管的显示控制原理和数码管与MCU的接口技术, ,LED数码管是由七段条形发光二极管组成,外加小数点dp,通过控制其引脚电位来控制数码管的显示。数字0到9分别对应段码FCH、60H、DAH、F2H、66H、B6H、BEH、E0H、FEH、F6H。
我们通过编写数码管显示驱动程序,掌握了数码管的静态扫描和动态扫描的原理和方法。基础部分只用到一个数码管所以用静态扫描的方法;提高部分要用到4位数码管所以用动态扫描的方法,动态扫描刷新频率的大小会影响数码管的亮暗程度和闪烁情况。位码变换控制在1ms,整体刷新频率控制在4到10ms,数码管整体显示亮度适中。
通过程序的调试,我们熟悉了接口程序调试方法。程序写完后,调试是一定会出现很多问题,在软件调试时,我们一定要掌握利用断点来单步调试的方法,这样比较容易找出错误,而且节约大量时间,在代码量较多时,效果尤为明显。写代码时,一定要合理布局,及时写上注释,这样也避免写下面程序时出现错误,同时也方便调试时查错。
Shen Longbao look in the eyes, for a chance to kill him. Shen Longbao behind smart to keep gendarmes, picked up a brick reservoir in cotton vest, walked into a small alley corner, seeing four weeks nobody will step up and pulled out bricks Asahi on military police knocked down. Unsuspecting Japanese military police was flaccid paralysis. Shen Longbao picked the Japanese military police gun in cotton vest, leave
因篇幅问题不能全部显示,请点此查看更多更全内容