STC单片机直接驱动4位数码管
利用STC单片机IO口推挽输出方式直接驱动4位数码管,最大限度地节省外围电路,电路原理图如下所示。P3.2 - P3.5推挽输出,每位驱动1位数码管;P1口每个时序驱动1段数码管,降低单片机的输出负载。如果需要降低显示亮度,还可以对P3.2 - P3.5增加软件PWM。源代码中Digits_Buf[4]是显示缓冲区,设置好显示内容以后,只要以固定间隔调用Digits_Refresh()就能正常显示。调用Digits_Refresh()的间隔时间不稳定时,会数码管各字符显示亮度不均匀并伴随闪烁。
Digits.h代码
#define DIGITS_DP 20 extern unsigned char Digits_Buf[4]; void Digits_Refresh();
Digits.c代码
#include "Hardware.h" /* sbit Digits_K1 = ...; sbit Digits_K2 = ...; sbit Digits_K3 = ...; sbit Digits_K4 = ...; */ unsigned char code DIGITS_SEG_TAB[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0x3e,0x1a,0x7a,0x9e,0x8e,0x00,0xee,0x1c,0x02, //0 1 2 3 4 5 6 7 8 9 a b c d e fA L - 0xfd,0x61,0xdb,0xf3,0x67,0xb7,0xbf,0xe1,0xff,0xf7,0xef,0x3f,0x1b,0x7b,0x9f,0x8f,0x01,0xef,0x1d,0x03}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. a. b. c. d. e. f. .A. L. -. unsigned char Digits_Buf[4]; //显存4字节,4个数码管 unsigned char Digits_SendBuff; unsigned char Digits_BufPos; unsigned char Digits_BitPos; void Digits_Refresh() { if (Digits_BitPos == 0) { if (Digits_BufPos == 3) Digits_BufPos = 0; else Digits_BufPos ++; Digits_SendBuff = DIGITS_SEG_TAB[Digits_Buf[Digits_BufPos]]; Digits_BitPos = 0x80; P1 = 0xFF; P3 &= 0xC3; if (Digits_BufPos == 0) Digits_K1 = 1; else if (Digits_BufPos == 1) Digits_K2 = 1; else if (Digits_BufPos == 2) Digits_K3 = 1; else Digits_K4 = 1; } P1 = ~(Digits_SendBuff & Digits_BitPos); Digits_BitPos >>= 1; }