五脚18.8数码管显示
今天抽空用将五脚的18.8数码管驱动程序研究了一下,将其过程记录如下:
数字1到5表示对应的数码管引脚,利用二极管的单向导通性来点亮单独的LED
数码管需要亮灯的高低电平点亮,不亮灯的IO口一定要设为高阻。
例如:点亮B3段,数码管2脚(单片机P3.3设置为推挽输出模式)高电平;数码管1脚(单片机P3.2设置为准双向口模式)低电平;在将P3.2设置为低电平输出就可以点亮B3段了; 自己总结: 高电平推挽输出模式; 低电平准双向口模式; 其余不亮的设置高阻输入模式;
用笨办法,一段一段点亮``,
以下是测试段位的测试程序:
# include <STC8G.H>
//3位6脚的,原理差不多,用笨办法,一段一段点亮,用switch,组成段码,小数点,STC8G1K08,接的P32-P36
/************************************************************************************/
#define DELAY 1 //设置每一个点显示的时间长度(1~20)
#define uchar unsigned char
#define uint unsigned int
void delay (uint a)//延时
{
uint i;
while( a-- != 0)
{
for(i = 0; i < DELAY; i++);
}
}
/****测试段码函数*****/
void DG1()
{
P3M1 = 0xCF; P3M0 = 0x10; P3 = 0xDF; delay(x); //1B
P3M1 = 0xD7; P3M0 = 0x08; P3 = 0xDF; delay(x); //1C
P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xB7; P3M0 = 0x40; P3 = 0xF7; delay(x); //2E
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x04; P3 = 0xDF; delay(x); //3E
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
P3M1 = 0xAF; P3M0 = 0x10; P3 = 0xBF; delay(x); //I
P3M1 = 0xB7; P3M0 = 0x08; P3 = 0xBF; delay(x); //J
P3M1 = 0x9F; P3M0 = 0x20; P3 = 0xBF; delay(x); //H
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
}
void main (void){ //主程序
init();
while (1)
{
DG1(); //测试代码
}
}
下面是将其用switch,组成段码,小数点; 从18.8倒减,
# include <STC8G.H>
单片机STC8G1K08,PIN1-PIN5接的P32-P36
/************************************************************************************/
#define DELAY 1 //设置每一个点显示的时间长度(1~20)
#define uchar unsigned char
#define uint unsigned int
uint TIME_SS=188;
uint C=0;
uint x=100;
void delay (uint a)//延时
{
uint i;
while( a-- != 0)
{
for(i = 0; i < DELAY; i++);
}
}
/************************************************************************************/
void init (void){ //上电初始化
TMOD = 0x01; // 定时/计数器0,1工作于方式1
TH0 = 0xcd; // 预置产生50ms时基信号
TL0 = 0xd4;
EA = 1; // 开总中断
ET0 = 1; // 定时/计数器0允许中断
TR0 = 1; // 开闭定时/计数器0
}
/**第一位显示,只显示1**/
void DG1(int a)
{
P3M1 = 0xCF; P3M0 = 0x10; P3 = 0xDF; delay(x); //1B
P3M1 = 0xD7; P3M0 = 0x08; P3 = 0xDF; delay(x); //1C
}
/**第二位显示含小数点,只显示0.-9.**/
void DG2(int a)
{
switch(a)
{
case 0:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //0.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xB7; P3M0 = 0x40; P3 = 0xF7; delay(x); //2E
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 1:{P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B //1.
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 2:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //2.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xB7; P3M0 = 0x40; P3 = 0xF7; delay(x); //2E
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 3:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //3.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 4:{P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B //4.
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 5:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //5.
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 6:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //6.
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xB7; P3M0 = 0x40; P3 = 0xF7; delay(x); //2E
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 7:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //7.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 8:{P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //8.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xB7; P3M0 = 0x40; P3 = 0xF7; delay(x); //2E
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
case 9:{ P3M1 = 0xE7; P3M0 = 0x08; P3 = 0xEF; delay(x); //2A //9.
P3M1 = 0xE7; P3M0 = 0x10; P3 = 0xF7; delay(x); //2B
P3M1 = 0xCF; P3M0 = 0x20; P3 = 0xEF; delay(x); //2C
P3M1 = 0xD7; P3M0 = 0x20; P3 = 0xF7; delay(x); //2D
P3M1 = 0xAF; P3M0 = 0x40; P3 = 0xEF; delay(x); //2F
P3M1 = 0x9F; P3M0 = 0x40; P3 = 0xDF; delay(x); //2G
P3M1 = 0xBB; P3M0 = 0x04; P3 = 0xBF; delay(x); //DP
break;}
}
}
/**第三位显示,只显示0-9**/
void DG3(int a)
{
switch(a)
{
case 0:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //0
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x04; P3 = 0xDF; delay(x); //3E
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
break;}
case 1:{
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B //1
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
break;}
case 2:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //2
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x04; P3 = 0xDF; delay(x); //3E
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 3:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //3
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 4:{
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B //4
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 5:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //5
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 6:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //6
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x04; P3 = 0xDF; delay(x); //3E
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 7:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //7
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
break;}
case 8:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //8
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x04; P3 = 0xDF; delay(x); //3E
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
case 9:{
P3M1 = 0xF3; P3M0 = 0x04; P3 = 0xE7; delay(x); //3A //9
P3M1 = 0xF3; P3M0 = 0x08; P3 = 0xFB; delay(x); //3B
P3M1 = 0xEB; P3M0 = 0x04; P3 = 0xEF; delay(x); //3C
P3M1 = 0xEB; P3M0 = 0x10; P3 = 0xFB; delay(x); //3D
P3M1 = 0xDB; P3M0 = 0x20; P3 = 0xFB; delay(x); //3F
P3M1 = 0xBB; P3M0 = 0x40; P3 = 0xFB; delay(x); //3G
break;}
}
}
/**第四位显示,点亮H段**/
void DG4()
{
P3M1 = 0x9F; P3M0 = 0x20; P3 = 0xBF; delay(x); //H
}
/**第五位显示,点亮I段**/
void DG5()
{
P3M1 = 0xAF; P3M0 = 0x10; P3 = 0xBF; delay(x); //I
}
/**第六位显示,点亮J段**/
void DG6()
{
P3M1 = 0xB7; P3M0 = 0x08; P3 = 0xBF; delay(x); //J
}
void main (void){ //主程序
init();
while (1)
{
DG1(TIME_SS/100);
DG2(TIME_SS%100/10);
DG3(TIME_SS%100%10);
DG4();
DG5();
DG6();
}
}
/***********************************************************************************/
void tiem0(void) interrupt 1 // 中断服务程序(产生500ms时基信号)
{ C++;
if(C>50){
C = 0;
TIME_SS--;
if(TIME_SS <0){
TIME_SS = 188;
}
}
TH0 = 0xcd;
TL0 = 0xd4;
}
|