单片机中不带字库LCD液晶屏显示少量汉字

单片机中不带字库LCD液晶屏如何显示少量汉字,一般显示汉字的方法有1.使用带字库的LCD屏,2.通过SD

卡或者外挂spi flash存中文字库,3.直接将需要的汉字取模存入mcu的flash中。

第一种方式最方便,带字库的液晶屏价格稍贵,第二种方式电路复杂价格也不便宜,第三种方法可以存储少

量必要的汉字,经济实用。将汉字字模数据存储到大数组中,通过偏移值来取出需要的汉字显示。

typedef struct _GB16  // 汉字字模数据结构
{
  char Index[2];  // 汉字内码索引
  char Msk[32];             // 点阵码数据
}GB16_Typedef;


const GB16_Typedef GB16x16[] = {//创建字库,阴码,顺向,逐行式取模

  {"",0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,
        0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,
        0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,
        0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06},

  {"",0x10,0x20,0x10,0x20,0x10,0x20,0x11,0x20,
        0x11,0x2C,0xFD,0x34,0x11,0x64,0x13,0xA4,
        0x11,0x24,0x11,0x34,0x11,0x28,0x1D,0x22,
        0xE1,0x22,0x41,0x02,0x00,0xFE,0x00,0x00},

  {"",0x00,0x00,0x7F,0xFC,0x00,0x80,0x00,0x80,
        0x01,0x00,0x01,0x00,0x03,0x40,0x05,0x20,
        0x09,0x10,0x11,0x08,0x21,0x04,0x41,0x04,
        0x81,0x00,0x01,0x00,0x01,0x00,0x01,0x00},

  {"",0x08,0x00,0x08,0x00,0x08,0x00,0x13,0xFC,
        0x10,0x00,0x30,0x00,0x30,0x00,0x50,0x00,
        0x90,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
        0x17,0xFE,0x10,0x00,0x10,0x00,0x10,0x00},

}

/*
其中定义的汉字结构体元素中Index[2]存放汉字,而Msk用于存放点阵码。
具体程序如下:
*/

void show_chinese(uint16_t x, uint16_t y, uint8_t *pstr, uint16_t pointColor, uint16_t backColor)
{
  
  uint8_t bit_cnt, byte_cnt, wordNum;
  uint16_t color, word_index;
  const GB16_Typedef* p_tab;
  
  p_tab = GB16x16; 
  wordNum = sizeof(GB24x24) / sizeof(GB24_Typedef);//汉字的个数

  while(*pstr != '\0')
  {
    for(word_index=0; word_index<wordNum; word_index++)
    {
      if(*pstr==p_tab[word_index].Index[0] && *(pstr+1)==p_tab[word_index].Index[1])
      { 
        tft_set_window(x, y, x+15, y+15);//设置要操作的窗口范围
         
        for(byte_cnt=0; byte_cnt<32; byte_cnt++)
        {
          uint8_t color = p_tab[word_index].Msk[byte_cnt];
          for (bit_cnt=0; bit_cnt<8; bit_cnt++) 
          {
            if((color&0x80) == 0x80)
            {
              tft_wrdat(pointColor);//LCD写数据
            } 
            else
            {
              tft_wrdat(backColor);//LCD写数据
            } 
            color = color<<1;
          }
         
        }
        pstr+=2;
        x += 16;
        if(x > 225)   
        {
          x = 0;
          y += 16; 
        }
      } 
    }
  }
}



int main()
{ 
  TFT_Init();  //TFT彩屏初始化 
  LED_Init();  //LED初始化
  tft_clear_screen(BLACK); //清屏
  show_chinese(6, 20, "天地不仁", MAGENTA, YELLOW);//YELLOW
  
  while(1)
  { 
    KeyTask();//按键扫描
  } 

}

 

posted @ 2018-06-27 14:24  eruca520  阅读(3957)  评论(0编辑  收藏  举报