硬件介绍: 这里所用到的硬件资源包括8个数码管、和msp430单片机的两个8位IO口(这里用的是P3和P5口,如有改变,可以通过宏定义更改)。 数码管是8个共阴的数码管,a-h 8段通过一个200Ω的电阻接到430单片机的P5口。共阴端是由单片机的P3口控制,单片机的一位IO通过一个三极管接到数码管的共阴端,以完成位选。 单片机的P3口时数码管的位选口,某位为高则选中;P5口时段选口;要数码管显示时,通过P3位选,选中某个数码管亮,P5段选选择8段(a-h)中的那些亮,从而控制某一位显示数字或字符。 要同时显示多个数码管,就要动态扫描;动态扫描时,本程序选用的是由看门狗的中断扫描显示:每1.9ms显示其中的一位,动态扫描显示每一位,从而让数码管看起来是同时亮的。 程序实现: 数码管显示首先要有一个数码管显示的断码表(完成数字和字符到数码管段值的表),程序中采用了《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 C #define g 0x40 // DDDD HH #define 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 "E" a + e + f + g, // Displays "F" a + c + d + e + f, // Displays "G" b + c + e + f + g, // Displays "H" e + f, // Displays "I" b + c + d + e, // Displays "J" b + d + e + f + g, // Displays "K" d + e + f, // Displays "L" a + c + e + g, // Displays "M" a + b + c + e + f, // Displays "N" c + e + g, // Displays "n" c + d + e + g, // Displays "o" a + b + c + d + e + f, // Displays "O" a + b + e + f + g, // Displays "P" a + b + c + f + g, // Displays "Q" e + g, // Displays "r" a + c + d + f +g, // Displays "S" d + e + f + g, // Displays "t" a + e + f , // Displays "T" b + c + d + e + f, // Displays "U" c + d + e, // Displays "v" b + d + f + g, // Displays "W" b + c + d + f + g, // Displays "Y" a + b + d + e + g, // Displays "Z" g, // Displays "-" h, // Displays "." 0 // Displays " " }; #undef a #undef b #undef c #undef d #undef e #undef f #undef g
int putchar(int ch) { //'\f'表示走纸翻页,相当于清除显示 if(ch=='\n'||ch=='\r') NixiettubeClear();
//数字和对应ASCII字母之间差0x30 '1'=0x31 '2'=0x32... //isdigit也是C语言标准函数 if(isdigit(ch)) NixiettubeInsertChar(ch-0x30); //若字符是数字则显示数字 else //否则,不是数字,是字母 { switch(ch) //根据字母选择程序分支 { case 'A': case 'a': NixiettubeInsertChar(AA);break; //字符A case 'B': case 'b': NixiettubeInsertChar(BB);break; //字符B case 'C': case 'c': NixiettubeInsertChar(CC);break; //... case 'D': case 'd': NixiettubeInsertChar(DD);break; case 'E': case 'e': NixiettubeInsertChar(EE);break; case 'F': case 'f': NixiettubeInsertChar(FF);break; case 'G': case 'g': NixiettubeInsertChar(GG);break; case 'H': case 'h': NixiettubeInsertChar(HH);break; case 'I': case 'i': NixiettubeInsertChar(II);break; case 'J': case 'j': NixiettubeInsertChar(JJ);break; case 'K': case 'k': NixiettubeInsertChar(KK);break; case 'L': case 'l': NixiettubeInsertChar(LL);break; case 'M': case 'm': NixiettubeInsertChar(mm);break; case 'N': NixiettubeInsertChar(NN);break; case 'n': NixiettubeInsertChar(nn);break; case 'O': NixiettubeInsertChar(OO);break; case 'o': NixiettubeInsertChar(oo);break; case 'P': case 'p': NixiettubeInsertChar(PP);break; case 'Q': case 'q': NixiettubeInsertChar(QQ);break; case 'R': case 'r': NixiettubeInsertChar(rr);break; case 'S': case 's': NixiettubeInsertChar(SS);break; case 'T': case 't': NixiettubeInsertChar(tt);break; case 'U': case 'v': NixiettubeInsertChar(UU);break; case 'V': case 'u': NixiettubeInsertChar(VV);break; case 'W': case 'w': NixiettubeInsertChar(WW);break; case 'Y': case 'y': NixiettubeInsertChar(YY);break; //... case 'Z': case 'z': NixiettubeInsertChar(ZZ);break; //字符Z case '-': NixiettubeInsertChar(NEG);break;//字符- case '.': NixiettubeInsertChar(DOT);break;//小数点,直接显示在右下角 case ' ': NixiettubeInsertChar(SP);break; //空格 default : NixiettubeInsertChar(SP);break;//显示不出来的字母用空格替代 } } return(ch); //返回显示的字符(putchar函数标准格式要求返回显示字符) }