共阳极数码管是指八段数码管的八段发光二极管的阳极(正极)都连在一起,而阴极对应的各段可分别控制,如图(B)所示:
共阴极数码管是指八段数码管的八段发光二极管的阴极(负极)都连在一起,而阳极对应的各段分别控制,如图(C)所示:
共阴级输入高电平有效,共阳级输入低电平有效。
驱动程序如下:
/*seg.c*/
#include "./delay/delay.h"
#define SEGPORT P0
sbit bit_select = P2^0; //定义位码位为P2^0口
sbit seg_select = P2^1; //定义断码位为P2^1口
/*共阴极数码管的0~F的显示*/
unsigned char seg_data[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char bit_data[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void main()
{
unsigned char i;
SEGPORT = 0;
/*消除数码管刷新时的重影*/
while(1)
{
for(i = 0; i < 8; i++)
{
SEGPORT = 0x0;
seg_select = 1;
seg_select = 0;
SEGPORT = 0xff;
bit_select = 1;
bit_select = 0;
SEGPORT = seg_data[i];
seg_select = 1;
seg_select = 0;
SEGPORT = bit_data[i];
bit_select = 1;
bit_select = 0;
delay_ms(2);
}
<pre name="code" class="html">}
/*延时函数*/
/*delay.c*/
<pre name="code" class="html">void delay_us(unsigned char t)
{
while(--t);
}
void delay_ms(unsigned char t)
{
while(t--)
{
delay_us(245);
delay_us(245);
}
}
void delay_s(unsigned char t)
{
while(t--)
{
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
}
}
/*delay.h*/
#ifndef _DELAY_H_
#define _DELAY_H_
extern void delay_us(unsigned char t);
extern void delay_ms(unsigned char t);
extern void delay_s(unsigned char t);
#endif