【vb精品源码栏目提醒】:以下是网学会员为您推荐的vb精品源码-MSP430程序库九数码管显示 - 大学课件,希望本篇文章对您学习有所帮助。
数码管也是单片机系统最常用的输出设备之一还有液晶、 发光二极管等。
七段这里用的是8 段,有小数点数码管可以完成显示 0-9 数字和一部分的英文字符如:A、b。
本文实现的程序完成显示数字和可显示的英文字符;同时完成数码管显示的 printf 函数的移植,以支持printf 的格式化字符等好用的特点(我用的数码管 8 个排为一排,方便数字等的显示)。
1. 硬件介绍: 这里所用到的硬件资源包括 8 个数码管、 msp430 单片机的两个 8 位 IO 口这 和 里用的是 P3 和 P5 口,如有改变,可以通过宏定义更改。
数码管是 8 个共阴的数码管,a-h 8 段通过一个 200 的电阻接到 430 单片机 的 P5 口。
共阴端是由单片机的 P3 口控制,单片机的一位 IO 通过一个三极管 接到数码管的共阴端,以完成位选。
单片机的 P3 口时数码管的位选口,某位为高则选中;P5 口时段选口;要数码 管显示时,通过 P3 位选,选中某个数码管亮,P5 段选选择 8 段(a-h)中的 那些亮,从而控制某一位显示数字或字符。
要同时显示多个数码管,就要动态扫描;动态扫描时,本程序选用的是由看门 狗的中断扫描显示:每 1.9ms 显示其中的一位,动态扫描显示每一位,从而让 数码管看起来是同时亮的。
2. 程序实现: 数码管显示首先要有一个数码管显示的断码表(完成数字和字符到数码管段值 的表),程序中采用了《MSP430 系列单片机系统工程设计与实践》这本书推 荐的方式实现的这个数码表:先用宏定义定义每段对应的单片机要输出的段 值,然后再实现是个表,当硬件改变时,只需更改前面的每段的段值定义即可, 改动的地方少了很多,代码如下: /宏定义,数码管 a-h 各段对应的比特,更换硬件只用改动以下 8 行/ define a 0x01 // AAAA define b 0x02 // F B define c 0x04 // F B define d 0x08 // GGGG define e 0x10 // E C define f 0x20 // E Cdefine g 0x40 // DDDDHHdefine h 0x80 //小数点/用宏定义自动生成段码表,很好的写法,值得学习//更换硬件无需重写段码表/const char Tab a b c d e f // Displays 0 b c // Displays 1 a b d e g // Displays 2 a b c d g // Displays 3 b c f g // Displays 4 a c d f g // Displays 5 a c d e f g // Displays 6 a b c // Displays 7 a b c d e f g // Displays 8 a b c d f g // Displays 9 a b c e f g // Displays A c d e f g // Displays B a d e f // Displays C b c d e g // Displays D a d e f g // Displays Ea e f g // Displays Fa c d e f // Displays Gb c e f g // Displays He f // Displays Ib c d e // Displays Jb d e f g // Displays Kd e f // Displays La c e g // Displays Ma b c e f // Displays Nc e g // Displays nc d e g // Displays oa b c d e f // Displays Oa b e f g // Displays Pa b c f g // Displays Qe g // Displays ra c d f g // Displays Sd e f g // Displays ta e f // Displays Tb c d e f // Displays Uc d e // Displays
vb d f g // Displays Wb c d f g // Displays Y a b d e g // Displays Z g // Displays - h // Displays . 0 // Displays undef aundef bundef cundef dundef eundef fundef g0-9 的位置对应显示 0-9,之后的是 A 开始往后显示,为了方便访问这个表格,定义了 AA 等一系列的常量,方便访问这个表。
define AA 10define BB AA1define CC BB1define DD CC1define EE DD1define FF EE1define GG FF1define HH GG1define II HH1define JJ II1define KK JJ1define LL KK1define mm LL1define NN mm1define nn NN1define oo nn1define OO oo1define PP OO1define QQ PP1define rr QQ1define SS rr1define tt SS1define TT tt1define UU TT1define VV UU1define WW VV1define YY WW1define ZZ YY1define NEG ZZ1 / - / //负号define DOT NEG1 / . / //小数点define SP DOT1 / 空白 / //空格A 从 10 开始访问这个表格,如果要显示 A 只需这样用 TabAA,即可得到需要的段值,AA-空格的宏定义放在 H 文件里,方便其他文件访问(当要调用显示函数的时候需要 AA 等宏定义)。
为什么是 AA 而不是 A 呢?主要原因是单字母的有几个已经在单片机 430 的头文件里定义了,为了访问的时候一致,就都用两个字母的了。
为了动态扫描,这里定义了一个全局数组数码管的程序可以访问Nixie8在这个里面的 8 个 char 对应 8 个数码管要显示的段值。
初始值是 8 个数码管都不显示:char Nixie8 00000000 //初始状态 不显示动态扫描时,函数每 1.9ms设的看门狗定时中断调用一次显示函数,每次显示一位为了让中断占用更少的时间,这样中断里只需赋值即可。
函数如下:void Display static char i 0 //记录扫描显示到哪位 CTRL_OUT 1 0i-- Nixie Nixiei - 1 //已显示字符左移一位 Nixie0 Tabch这个也是先判断小数点,小数点直接放到 h 段,其他的,则要已显示的左移再覆盖最右一位,源程序的注释很详细,可具体才、可以下载附件的程序库。
数码管清除函数,这个函数把数码管全部显示去掉,即把缓存数组内每项都置为 0:void NixiettubeClear forint i 0i 8i Nixie TabSP //显示空格程序比较简单,这里就不多解释了。
数码管的程序就这么多了,所有函数都列出来了。
下面开始介绍printf的移植,具体过程不再详细说了,详细过程参考:MSP430 程序库printf和scanf函数移植。
这里主要介绍所需程序。
单片机 printf 使用需要用户提供底层驱动-putchar 函数,printf 完成格式化等一系列活动后调用 putchar 输出字符流。
只要实现 putchar,包含 stdio.h 文件,就可以使用 printf 函数。
移植的数码管的 putchar 函数如下:includeinclude ctype.h /isdigit 函数需要该头文件/include Nixietube.hint putcharint ch //f表示走纸翻页,相当于清除显示 ifchnchr NixiettubeClear //数字和对应 ASCII 字母之间差 0x30 10x31 20x32... //isdigit 也是 C 语言标准函数 ifisdigitch NixiettubeInsertCharch-0x30 //若字符是数字则显示数字 else //否则,不是数字,是字母 switchch //根据字母选择程序分支 case A: case a:NixiettubeInsertCharAAbreak //字符 A case B: case b:NixiettubeInsertCharBBbreak //字符 B case C: case c:NixiettubeInsertCharCCbreak //... case D: case d:NixiettubeInsertCharDDbreak case E: case e:NixiettubeInsertCharEEbreak case F: case f:NixiettubeInsertCharFFbreak case G: case g:NixiettubeInsertCharGGbreak case H: case h:NixiettubeInsertCharHHbreak case I: case i:NixiettubeInsertCharIIbreak case J: case j:NixiettubeInsertCharJJbreak case K: case k:NixiettubeInsertCharKKbreak case L: case l:NixiettubeInsertCharLLbreak case M: case m:NixiettubeInsertCharmmbreak case N:NixiettubeInsertCharNNbreak case n:NixiettubeInsertCharnnbreak case O:NixiettubeInsertCharOObreak case o:NixiettubeInsertCharoobreak case P: case p:NixiettubeInsertCharPPbreak case Q: case q:NixiettubeInsertCharQQbreak case R: case r:NixiettubeInsertCharrrbreak case S: case s:NixiettubeInsertCharSSbreak case T: case t:NixiettubeInsertCharttbreak case U: case v:NixiettubeInsertCharUUbreak case V: case u:NixiettubeInsertCharVVbreak case W: case w:NixiettubeInsertCharWWbreak case Y: case y:NixiettubeInsertCharYYbreak //... case Z: case z:NixiettubeInsertCharZZbreak //字符 Z case -:NixiettubeInsertCharNEGbreak//字符- case .:NixiettubeInsertCharDOTbreak//小数点,直接显示在右下角 case :NixiettubeInsertCharSPbreak //空格 default :NixiettubeInsertCharSPbreak//显示不出来的字母用空格替代 returnch //返回显示的字符putchar 函数标准格式要求返回显示字符头文件必须包含 stdio.h,这样告诉编译器 printf 调用时,用这里的 putchar 函数。
然后判断字符,分类进行显示,不能显示的空一格。
数码管的程序就完成了,如果需要可以自己添加改写函数,如:当和键盘共同使用时,如果键盘移植了 scanf 函数,并且支持退格;可以改写函数-让数码管的 putchar 支持退格操作。
或者用的是我的键盘程序, 需要 10 多 ms 调用一次键盘处理函数,这样可以和这个数码管扫描公用一个中断: void Display static char i 0 //记录扫描显示到哪位 CTRL_OUT 1
上一篇:
前言传统的计算机程序设计教学方 .【推荐
下一篇:
电咖汽车入选2018独角兽企业榜百强 发布“天际”品牌将推高估值