1、定义一位数码管 “显示共用体” 和 “段位转换共用体”
typedef union{
struct{
uchar a :1;
uchar b :1;
uchar c :1;
uchar d :1;
uchar e :1;
uchar f :1;
uchar g :1;
uchar dp :1;
}Bits;
uchar Value;
}unionByte;//一位数码管显示共用体
typedef union{
struct{
uchar _0 :1;
uchar _1 :1;
uchar _2 :1;
uchar _3 :1;
uchar _4 :1;
uchar _5 :1;
uchar _6 :1;
uchar _7 :1;
}Bits;
uchar Value;
}unionSegByte;//一位数码管段位转换共用体
2、定义LCD驱动数据【根据LCD段数确定数据多少】
//定义LCD屏驱动数据 根据驱动芯片 四位数内容数据
unionSegByte unionSegByte0;
unionSegByte unionSegByte1;
unionSegByte unionSegByte2;
unionSegByte unionSegByte3;
unionSegByte unionSegByte4;
unionSegByte unionSegByte5;
unionSegByte unionSegByte6;
unionSegByte unionSegByte7;
unionSegByte unionSegByte8;
unionSegByte unionSegByte9;
unionSegByte unionSegByte10;
unionSegByte unionSegByte11;
unionSegByte unionSegByte12;
unionSegByte unionSegByte13;
unionSegByte unionSegByte14;
unionSegByte unionSegByte15;
unionSegByte unionSegByte16;
unionSegByte unionSegByte17;
unionSegByte unionSegByte18;
unionSegByte unionSegByte19;
3、 根据真值表,写“显示”和“段位”转换函数
unionByte Byte[17];//定义四位数显示码值 如第一位显示‘0’则将码表数组中第0个元素0x3f赋给Byte[0]
uchar LCD_DisplayBuf[20];
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
uchar table[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; //0-F 码表值 最后一个是清0 共阴 因为点亮其中一段需要写1
//uchar table[]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00}; //0-F 码表值 最后一个是清0 共阳 因为点亮其中一段需要写0
void lcd_data_load_conver(uchar *ppdata)
{
Byte[0].Value = table[ppdata[0]]; //0
Byte[1].Value = table[ppdata[1]];
Byte[2].Value = table[ppdata[2]];
Byte[3].Value = table[ppdata[3]];
Byte[4].Value = table[ppdata[4]];
Byte[5].Value = table[ppdata[5]];
Byte[6].Value = table[ppdata[6]];
Byte[7].Value = table[ppdata[7]];
Byte[8].Value = table[ppdata[8]];
Byte[9].Value = table[ppdata[9]]; //9
Byte[10].Value = table[ppdata[10]]; //A
Byte[11].Value = table[ppdata[11]];
Byte[12].Value = table[ppdata[12]];
Byte[13].Value = table[ppdata[13]];
Byte[14].Value = table[ppdata[14]];
Byte[15].Value = table[ppdata[15]]; //F
Byte[16].Value = table[ppdata[16]];
Byte[16].Value = 0x00;//
unionSegByte0.Bits._0 = 0; //0 3F
unionSegByte0.Bits._1 = 1; //0
unionSegByte0.Bits._2 = 1; //1
unionSegByte0.Bits._3 = 1; //1
unionSegByte0.Bits._4 = 0; //1
unionSegByte0.Bits._5 = 0; //1
unionSegByte0.Bits._6 = 0; //1
unionSegByte0.Bits._7 = 0; //1
unionSegByte1.Value = 0x00; //清零显示数据
unionSegByte1.Bits._0 = 0; //
unionSegByte1.Bits._1 = Byte[2].Bits.a; //3A
unionSegByte1.Bits._2 = Byte[2].Bits.b; //3B
unionSegByte1.Bits._3 = Byte[2].Bits.g; //3G
unionSegByte1.Bits._4 = Byte[2].Bits.c; //3C
unionSegByte1.Bits._5 = Byte[2].Bits.d; //3D
unionSegByte1.Bits._6 = Byte[2].Bits.e; //3E
unionSegByte1.Bits._7 = Byte[2].Bits.f; //3F
unionSegByte2.Value = 0x00; //清零显示数据
unionSegByte2.Bits._0 = 1; //
unionSegByte2.Bits._1 = Byte[1].Bits.b; // 2B
unionSegByte2.Bits._2 = Byte[16].Bits.f; //
unionSegByte2.Bits._3 = 1; //
unionSegByte2.Bits._4 = Byte[1].Bits.c; // 2C
unionSegByte2.Bits._5 = 1; //
unionSegByte2.Bits._6 = Byte[16].Bits.c; //
unionSegByte2.Bits._7 = 1; //
unionSegByte3.Value = 0x00; //清零显示数据
unionSegByte3.Bits._0 = Byte[16].Bits.a; //
unionSegByte3.Bits._1 = Byte[16].Bits.a; //
unionSegByte3.Bits._2 = Byte[16].Bits.f; //
unionSegByte3.Bits._3 = Byte[16].Bits.g; //
unionSegByte3.Bits._4 = Byte[16].Bits.e; //
unionSegByte3.Bits._5 = Byte[16].Bits.d; //
unionSegByte3.Bits._6 = 1; //
unionSegByte3.Bits._7 = Byte[8].Bits.b; //9B
unionSegByte4.Value = 0x00; //清零显示数据
unionSegByte4.Bits._0 = Byte[8].Bits.c; //9C
unionSegByte4.Bits._1 = Byte[8].Bits.d; //9D
unionSegByte4.Bits._2 = Byte[8].Bits.e; //9E
unionSegByte4.Bits._3 = Byte[8].Bits.g; //9G
unionSegByte4.Bits._4 = Byte[8].Bits.f; //9F
unionSegByte4.Bits._5 = 1; //
unionSegByte4.Bits._6 = Byte[8].Bits.a; //9A
unionSegByte4.Bits._7 = Byte[7].Bits.b; //8B
unionSegByte5.Value = 0x00; //清零显示数据
unionSegByte5.Bits._0 = Byte[7].Bits.c; //8C
unionSegByte5.Bits._1 = Byte[7].Bits.d; //8D
unionSegByte5.Bits._2 = Byte[7].Bits.e; //8E
unionSegByte5.Bits._3 = Byte[7].Bits.g; //8G
unionSegByte5.Bits._4 = Byte[7].Bits.f; //8F
unionSegByte5.Bits._5 = Byte[7].Bits.a; //8A
unionSegByte5.Bits._6 = Byte[6].Bits.b; //7B
unionSegByte5.Bits._7 = Byte[6].Bits.c; //7C
unionSegByte6.Value = 0x00; //清零显示数据
unionSegByte6.Bits._0 = Byte[6].Bits.d; //7D
unionSegByte6.Bits._1 = Byte[16].Bits.a; //
unionSegByte6.Bits._2 = Byte[6].Bits.e; //7E
unionSegByte6.Bits._3 = Byte[6].Bits.g; //7G
unionSegByte6.Bits._4 = Byte[16].Bits.e; //
unionSegByte6.Bits._5 = Byte[6].Bits.f; //7F
unionSegByte6.Bits._6 = Byte[6].Bits.a; //7A
unionSegByte6.Bits._7 = Byte[5].Bits.b; //6B
unionSegByte7.Value = 0x00; //清零显示数据
unionSegByte7.Bits._0 = Byte[5].Bits.c; //6C
unionSegByte7.Bits._1 = Byte[5].Bits.d; //6D
unionSegByte7.Bits._2 = Byte[5].Bits.e; //6E
unionSegByte7.Bits._3 = Byte[5].Bits.g; //6G
unionSegByte7.Bits._4 = Byte[5].Bits.f; //6F
unionSegByte7.Bits._5 = Byte[5].Bits.a; //6A
unionSegByte7.Bits._6 = Byte[4].Bits.a; //5A
unionSegByte7.Bits._7 = Byte[4].Bits.b; //5B
unionSegByte8.Value = 0x00; //清零显示数据
unionSegByte8.Bits._0 = Byte[4].Bits.g; //5G
unionSegByte8.Bits._1 = Byte[4].Bits.c; //5C
unionSegByte8.Bits._2 = Byte[4].Bits.d; //5D
unionSegByte8.Bits._3 = Byte[4].Bits.e; //5E
unionSegByte8.Bits._4 = Byte[4].Bits.f; //5F
unionSegByte8.Bits._5 = Byte[16].Bits.d; //
unionSegByte8.Bits._6 = Byte[16].Bits.c; //
unionSegByte8.Bits._7 = Byte[16].Bits.b; //
unionSegByte9.Value = 0x00; //清零显示数据
unionSegByte9.Bits._0 = 0; //
unionSegByte9.Bits._1 = Byte[3].Bits.c; //4C
unionSegByte9.Bits._2 = Byte[3].Bits.d; //4D
unionSegByte9.Bits._3 = Byte[3].Bits.e; //4E
unionSegByte9.Bits._4 = Byte[3].Bits.g; //4G
unionSegByte9.Bits._5 = 1; //
unionSegByte9.Bits._6 = Byte[16].Bits.c; //
unionSegByte9.Bits._7 = Byte[16].Bits.b; //
unionSegByte10.Value = 0x00; //清零显示数据
unionSegByte10.Bits._0 = Byte[3].Bits.f; //4F
unionSegByte10.Bits._1 = Byte[3].Bits.a; //4A
unionSegByte10.Bits._2 = Byte[3].Bits.b; //4B
unionSegByte10.Bits._3 = Byte[1].Bits.d; //2D
unionSegByte10.Bits._4 = Byte[1].Bits.e; //2E
unionSegByte10.Bits._5 = Byte[1].Bits.g; //2G
unionSegByte10.Bits._6 = Byte[1].Bits.f; //2F
unionSegByte10.Bits._7 = Byte[1].Bits.a; //2A
unionSegByte11.Value = 0x00; //清零显示数据
unionSegByte11.Bits._0 = Byte[0].Bits.b; //1B
unionSegByte11.Bits._1 = Byte[0].Bits.c; //1C
unionSegByte11.Bits._2 = Byte[0].Bits.d; //1D
unionSegByte11.Bits._3 = Byte[0].Bits.e; //1E
unionSegByte11.Bits._4 = Byte[0].Bits.g; //1G
unionSegByte11.Bits._5 = Byte[0].Bits.f; //1F
unionSegByte11.Bits._6 = Byte[0].Bits.a; //1A
unionSegByte11.Bits._7 = Byte[16].Bits.a; //
unionSegByte12.Value = 0x00; //清零显示数据
unionSegByte12.Bits._0 = 1;; //
unionSegByte12.Bits._1 = 1; //
unionSegByte12.Bits._2 = 1; //
unionSegByte12.Bits._3 = 1; //
unionSegByte12.Bits._4 = 1; //
unionSegByte12.Bits._5 = 1; //
unionSegByte12.Bits._6 = 1; //
unionSegByte12.Bits._7 = 1; //
//以下用不到,写0x00
unionSegByte13.Value = 0x00;//清零显示数据
unionSegByte14.Value = 0x00;//清零显示数据
unionSegByte15.Value = 0x00;//清零显示数据
unionSegByte16.Value = 0x00;//清零显示数据
unionSegByte17.Value = 0x00;//清零显示数据
unionSegByte18.Value = 0x00;//清零显示数据
unionSegByte19.Value = 0x00;//
LCD_DisplayBuf[0] = unionSegByte0.Value;
LCD_DisplayBuf[1] = unionSegByte1.Value;
LCD_DisplayBuf[2] = unionSegByte2.Value;
LCD_DisplayBuf[3] = unionSegByte3.Value;
LCD_DisplayBuf[4] = unionSegByte4.Value;
LCD_DisplayBuf[5] = unionSegByte5.Value;
LCD_DisplayBuf[6] = unionSegByte6.Value;
LCD_DisplayBuf[7] = unionSegByte7.Value;
LCD_DisplayBuf[8] = unionSegByte8.Value;
LCD_DisplayBuf[9] = unionSegByte9.Value;
LCD_DisplayBuf[10] = unionSegByte10.Value;
LCD_DisplayBuf[11] = unionSegByte11.Value;
LCD_DisplayBuf[12] = unionSegByte12.Value;
LCD_DisplayBuf[13] = unionSegByte13.Value;
LCD_DisplayBuf[14] = unionSegByte14.Value;
LCD_DisplayBuf[15] = unionSegByte15.Value;
LCD_DisplayBuf[16] = unionSegByte16.Value;
}
4、与数组对应的,LCD段逻辑真值表
5、几位数码管同时显示“0~16”
void LCD_Display(uchar *buf)
{
lcd_data_load_conver(buf);//把要显示的数据通过换算更新到buf里
//写入屏幕显存
LCD_Driver_WriteData(LCD_DisplayBuf);//
}
void main(void)
{
uchar i;
printf("LCD_driveR \r\n");
for(i=0;i<16;i++)//16
{
LCD_buffer[0] = i;
LCD_buffer[1] = i;
LCD_buffer[2] = i;
LCD_buffer[3] = i;
LCD_buffer[4] = i;
LCD_buffer[5] = i;
LCD_buffer[6] = i;
LCD_buffer[7] = i;
LCD_buffer[8] = i;
LCD_Display(LCD_buffer);
delay_ms(500);
}
}