战舰LCD显示汉字

2019-07-20 22:38发布

在学习调试LCD实验的时候发现只能显示字符,可能是汉字字库太大,原子没有实现,所以自己把汉字显示加上了。
在font.h里面定义一个汉字结构体
typedef struct _chinese
{
    unsigned char *index;
    unsigned char word[24*3]; //取决于最大的汉字, 这里是24X24
}chinese;
接着定义汉字字库,提取方式与例程字符相同
const chinese ch_12_12[] =
{
    {"正",0x00,0x10,0x40,0x10,0x47,0xF0,0x40,0x10,0x40,0x10,0x7F,0xF0,0x42,0x10,0x42,0x10,0x42,0x10,0x42,0x10,0x00,0x10,0x00,0x00},/*0*/
    {"点",0x00,0x10,0x0F,0xA0,0x08,0x80,0x08,0xA0,0x08,0x90,0xF8,0x80,0x48,0xA0,0x48,0x90,0x48,0x80,0x4F,0xA0,0x40,0x10,0x00,0x00},/*1*/
    {"原",0x00,0x10,0x7F,0xE0,0x40,0x10,0x5F,0x20,0x55,0x40,0x75,0x10,0x55,0xF0,0x55,0x00,0x55,0x40,0x5F,0x20,0x40,0x10,0x00,0x00},/*2*/
    {"子",0x02,0x00,0x02,0x00,0x42,0x00,0x42,0x10,0x42,0x10,0x4F,0xF0,0x52,0x00,0x62,0x00,0x42,0x00,0x02,0x00,0x02,0x00,0x00,0x00},/*3*/
    ......
};
const chinese ch_16_16[] =
{
    {"正",0x00,0x02,0x40,0x02,0x40,0x02,0x43,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x40,0x02,0x00,0x02,0x00,0x00},/*0*/
    {"点",0x00,0x01,0x00,0x02,0x03,0xEC,0x02,0x20,0x02,0x20,0x02,0x28,0xFE,0x26,0x12,0x20,0x12,0x28,0x12,0x26,0x12,0x20,0x13,0xE0,0x10,0x08,0x10,0x07,0x00,0x00,0x00,0x00},/*1*/
    {"原",0x00,0x01,0x00,0x06,0x7F,0xF8,0x40,0x02,0x40,0x04,0x4F,0xE8,0x49,0x22,0x59,0x21,0x69,0x3E,0x49,0x20,0x49,0x20,0x4F,0xE8,0x40,0x04,0x40,0x02,0x40,0x00,0x00,0x00},/*2*/
    {"子",0x01,0x00,0x41,0x00,0x41,0x00,0x41,0x00,0x41,0x00,0x41,0x02,0x41,0x01,0x47,0xFE,0x45,0x00,0x49,0x00,0x51,0x00,0x61,0x00,0x41,0x00,0x01,0x00,0x01,0x00,0x00,0x00},/*3*/
    ......
};
const chinese ch_24_24[] =
{
    {"正",0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x08,0x10,0x00,0x08,0x10,0x00,0x08,0x10,0x00,0x08,0x10,0xFF,0xF8,0x10,0x40,0x08,0x10,0x00,0x08,0x10,0x00,0x08,0x10,0x00,0x08,0x10,0x00,0x08,
          0x1F,0xFF,0xF8,0x10,0x10,0x08,0x10,0x10,0x08,0x10,0x10,0x08,0x10,0x10,0x08,0x10,0x10,0x08,0x10,0x10,0x08,0x10,0x30,0x08,0x30,0x10,0x18,0x10,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00},        
    {"点",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x06,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x7F,0x80,0x00,0x41,0x00,0x00,0x41,0x20,0x00,0x41,0x1C,0x00,0x41,0x0C,0x7F,0xC1,0x00,
          0x24,0x41,0x00,0x04,0x41,0x20,0x04,0x41,0x18,0x04,0x41,0x0C,0x04,0x41,0x00,0x04,0x7F,0x80,0x04,0x7F,0xA0,0x0C,0x00,0x10,0x04,0x00,0x1C,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00},
    ......
}

接着在lcd.c中添加一个显示汉字的函数
void LCD_ShowChinese(u16 x,u16 y,u8 *num,u8 size,u8 mode)
{
    u8 temp,t1,t;
    u16 y0=y;
    u8 csize=(size/8+((size%8)?1:0))*size;  //得到字体一个字符对应点阵集所占的字节数
  
    int index = -1;
   
    if(size == 12)
    {
        for(t=0; t<sizeof(ch_12_12)/sizeof(ch_12_12[0]); t++)
        {
            if((num[0] == ch_12_12[t].index[0]) && (num[1] == ch_12_12[t].index[1]))
            {
                index = t;
                break;
            }
        }   
    }
    else if(size == 16)
    {
        for(t=0; t<sizeof(ch_16_16)/sizeof(ch_16_16[0]); t++)
        {
            if((num[0] == ch_16_16[t].index[0]) && (num[1] == ch_16_16[t].index[1]))
            {
                index = t;
                break;
            }
        }   
    }
    else if(size == 24)
    {
        for(t=0; t<sizeof(ch_24_24)/sizeof(ch_24_24[0]); t++)
        {
            if((num[0] == ch_24_24[t].index[0]) && (num[1] == ch_24_24[t].index[1]))
            {
                index = t;
                break;
            }
        }   
    }
    else
    {
        return;    // 没有对应的字库
    }
   
    if(index == -1)  return;  // 字库中没有找到要显示的汉字
   
for(t=0;t<csize;t++)
{   
        if(size == 12)       temp=ch_12_12[index].word[t];
        else if(size == 16) temp=ch_16_16[index].word[t];
        else if(size == 24) temp=ch_24_24[index].word[t];
        
  for(t1=0;t1<8;t1++)
  {      
          if(temp&0x80)LCD_Fast_DrawPoint(x,y,POINT_COLOR);
          else if(mode==0)LCD_Fast_DrawPoint(x,y,BACK_COLOR);
          temp<<=1;
          y++;
          if(y>=lcddev.height)return;  //超区域了
          if((y-y0)==size)
          {
              y=y0;
              x++;
              if(x>=lcddev.width)return; //超区域了
              break;
          }
      }   
}   
}
接着修改原来的显示字符串函数,添加显示汉字功能
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
{         
u8 x0=x;
width+=x;
height+=y;
   
    while((*p!=127)&&(*p>=' ')) // 127是del符号
    {
        if(x>=width){x=x0;y+=size;}
        if(y>=height)break;//退出
        
        if(*p >127)    // 汉字大于127
        {
            LCD_ShowChinese(x,y,p,size,0);
            x += size;
            p += 2;
        }
        else
        {
            LCD_ShowChar(x,y,*p,size,0);
            x += size >> 1;
            p++;            
        }
    }
}

验证功能
LCD_ShowString(0, 0,  240, 12, 12, "正点原子ALIENTEK战舰");
LCD_ShowString(0, 12, 240, 12, 12, "星翼电子科技有限公司");
LCD_ShowString(0, 24, 240, 16, 16, "正点原子ALIENTEK战舰");
LCD_ShowString(0, 40, 240, 16, 16, "星翼电子科技有限公司");
LCD_ShowString(0, 56, 240, 24, 24, "正点原子ALIENTEK战舰");
LCD_ShowString(0, 80, 240, 24, 24, "星翼电子科技有限公司");


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。