数码管驱动
单片机源程序如下:
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)
|