數碼管驅動
單片機源程序如下:
Main.c
- #include "reg51.h"
- #include "DisplayUnit.h"
- void main(void)
- {
- DISP_Init();
- DISP_Number(1234);
- EA = 1;
- while(1)
- {
- }
- }
- Display。C
- #include "reg51.h"
- char code cSegments[16] =
- {
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
- 0x7F,0x6F,0x40,0x08,0x00,0x5E,0x79,0x71
- };
- char code cBits[4] =
- {
- 0xFE,0xFD,0xFB,0xF7
- };
- char vSegValue[4];
- char vIndex;
- void DISP_Init(void)
- {
- int i;
- //初始化本單元的全局變量
- for(i=0;i<4;i++)
- {
- vSegValue[i] = 0;
- }
- vIndex = 0;
- //初始化定時器T0
- TMOD = 0x01;
- ET0 = 1 ;
- EA = 1 ;
- TR0 = 1 ;
- TH0 = 0xEC;
- TL0 = 0x78;
- }
- void DISP_T0_ISR(void) interrupt 1
- {
- //重新預置初值
- TH0 = 0xEC;
- TL0 = 0x78;
- //根據索引的值,操作端口,顯示一位數字
- P1 = cBits[vIndex];
- P0 = vSegValue[vIndex];
- //改變索引,當前正在顯示的位的編號。0~4之間
- vIndex++;
- if( vIndex >= 4 )
- vIndex = 0;
- }
- void DISP_BitNum(char WhichBit,char WhichNum)
- {
- //根據當前位和數字的參數
- //把該數字對應的段的字模,存入指定位的數組中 vsegValue[0] = cSegments[1];
- vSegValue[WhichBit] = cSegments[WhichNum];
- }
- void DISP_Number(int Value)
- {
- char Number;
- //提取Vlue的個十百千位的數字
- //調用 DISP_BitNum (調用4遍)函數,把需要顯示的數字的字模,放入全局變量數組中
- while (Value >= 1000)
- {
- Number++;
- Value -= 1000;
- }
- DISP_BitNum(0,Number);
- Number = 0;
- while (Value >= 100)
- {
- Number++;
- Value -= 100;
- }
- DISP_BitNum(1,Number);
- Number = 0;
- while (Value >= 10)
- {
- Number++;
- Value -= 10;
- }
- DISP_BitNum(2,Number);
- DISP_BitNum(3,Value);
- }
- void DISP_Low2(char Value)
- {
- //提取Value的個位十位
- //調用 DISP_BitNum的函數兩遍
- char Number=0;
- while(Value >= 10)
- {
- Number++;
- Value -= 10;
- }
- DISP_BitNum(2,Number);
- DISP_BitNum(3,Value);
- }
- void DISP_High2(char Value)
- {
- //提取Value的個位十位
- //調用 DISP_BitNum的函數兩遍
- char Number=0;
- while(Value >= 10)
- {
- Number++;
- Value -= 10;
- }
- DISP_BitNum(0,Number);
- DISP_BitNum(1,Value);
- }
- D.h
- #ifndef __MY__Display__Unit_H___
- #define __MY__Display__Unit_H___
- void DISP_Init(void);
- void DISP_BitNum(char WhichBit,char WhichNum);
- void DISP_Number(int Value);
- void DISP_Low2(char Value);
- void DISP_High2(char Value);
- #endif
復制代碼
所有資料51hei提供下載:
數碼管驅動.docx
(16.18 KB, 下載次數: 4)
2017-12-4 16:52 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|