|
今天解決了幾個(gè)困惑繼續(xù)上路,先把所有基礎(chǔ)的東西用源代碼跑一遍。
順便認(rèn)識(shí)一下各個(gè)元器件,和很多陌生的名詞。
下面是我寫的一個(gè)數(shù)碼管顯示數(shù)字。嘗試了一下C語(yǔ)言的抽象, C++程序員的陋習(xí)^_^
- Interface.h
- #ifndef __INTERFACE_H__
- #define __INTERFACE_H__
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef unsigned long u32;
- /**
- * 接口說(shuō)明 : 顯示一個(gè)數(shù)字
- * 參數(shù) :
- * x : 顯示位置的X坐標(biāo)
- * y : 顯示位置的Y坐標(biāo)
- * val : 具體需要顯示的內(nèi)容
- * 返回 :
- * 無(wú)返回值
- */
- typedef void(*pfnDisplayNumber)(u8 x, u8 y, u8 val);
- /**
- * 接口說(shuō)明 : 清除屏幕
- */
- typedef void(*pfnClearScreen)(void);
- /**
- * 結(jié)構(gòu)體說(shuō)明 : 負(fù)責(zé)顯示
- * 例如數(shù)碼管、或者液晶屏等等
- */
- typedef struct _DisplayInterface {
- pfnDisplayNumber displayerNumber; // 用于顯示一個(gè)數(shù)字的接口
- pfnClearScreen clear; // 用于清除的一個(gè)接口
- }DisplayInterface;
- #endif
- ---------------------------------------------------------
- NixieTubeDisplay.h
- #ifndef __NIXIETUBEDISPLAY_H__
- #define __NIXIETUBEDISPLAY_H__
- #include "reg52.h"
- #include <intrins.h>
- #include "Interface.h"
- // 初始化一個(gè)數(shù)碼管,例如外部創(chuàng)建的一個(gè)局部 DisplayInterface
- // 將地址傳進(jìn)來(lái)由InitNixieTube初始化.
- // 因?yàn)槭褂胢alloc不正常所以只設(shè)計(jì)了一個(gè)Init
- DisplayInterface * InitNixieTube(DisplayInterface * );
- #endif
- ---------------------------------------------------------
- NixieTubeDisplay.c
- #include "NixieTubeDisplay.h"
- sbit LSA = P2 ^ 2;
- sbit LSB = P2 ^ 3;
- sbit LSC = P2 ^ 4;
- // 0 - 9
- static const u8 numberHex[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
- // 將一個(gè)數(shù)字顯示到數(shù)碼管
- void displayNumberToNixieTube(u8 x, u8 y, u8 val)
- {
- if((val >= 0 && val < 10) && (x >= 0 && x < 8))
- {
- // 設(shè)置顯示內(nèi)容
- P0 = numberHex[val];
-
- // 設(shè)置顯示索引
- LSA = (x >> 0 ) % 2;
- LSB = (x >> 1 ) % 2;
- LSC = (x >> 2 ) % 2;
- }
- }
- // 清除顯示
- void clearNixieTube()
- {
- P0 = 0;
- }
- DisplayInterface * InitNixieTube(DisplayInterface * pDisplay)
- {
- if(pDisplay) {
- pDisplay->displayerNumber = displayNumberToNixieTube;
- pDisplay->clear = clearNixieTube;
- return pDisplay;
- }
- return 0;
- }
- ---------------------------------------------------------
- main.c
- #include "NixieTubeDisplay.h"
- typedef u8(*pfnCallback)(DisplayInterface * displayObject);
- void delay(u16 i)
- {
- while(i--);
- }
- void display(u8 index, u8 number,
- DisplayInterface * displayObject, pfnCallback callback)
- {
- if(displayObject)
- {
- displayObject->displayerNumber(index, 0, number);
- if(callback) { callback(displayObject); }
- }
- }
- void message(DisplayInterface * displayObject)
- {
- delay(100);
- // 當(dāng)接收到這個(gè)消息的時(shí)候,說(shuō)明已經(jīng)顯示完了
- if(displayObject)
- {
- displayObject->clear();
- }
- }
- void main()
- {
- u8 n = 0;
- u8 i = 0;
- DisplayInterface v;
- InitNixieTube(&v);
- while(1)
- {
- for(i = 0; i < 10; i++)
- {
- display(i, i + 2, &v, message);
- }
- }
- }
復(fù)制代碼 |
|