找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 23785|回復(fù): 9
收起左側(cè)

讓12864液晶模塊顯示更小的文字 5*8點(diǎn)陣

[復(fù)制鏈接]
ID:51090 發(fā)表于 2014-9-17 02:13 | 顯示全部樓層 |閱讀模式
今天,世界末日的大年初三.一年到頭就這么幾天最寶貴的時(shí)間,在家琢磨著怎樣整一個(gè)5×8的ASCII字符庫,用于我后面要設(shè)計(jì)的溫度記錄儀.本人比較喜歡LCD12864(ST920)這個(gè)模塊,它本身的字符庫給我們帶來了很多方便,可是也有些缺陷,就是個(gè)子太大,一個(gè)ASCII字符都占用了8×16的空間,可見如果我們用于繪圖時(shí)當(dāng)需要顯示某些提示內(nèi)容的時(shí)候可想而知,那文字都比圖像還要大.于是,筆者經(jīng)過多番的修改和更正后,完成了以下函數(shù)庫,在今后的使用中,字符庫可以任意更改,字體也能任意寬高,便于移植到更大的LCD模塊上使用,接下來我就以一個(gè)簡單的C語言程序讓它顯示到LCD模塊上.經(jīng)過使用自定義的字符庫,單個(gè)12864能夠顯示7行字符.
效果如圖:





   本程序使用IAR AVR編譯器,芯片使用M128.
先來看看我的主程序吧,很簡單,就幾行內(nèi)容,基于之前我設(shè)計(jì)的FrameAVR,多任務(wù)調(diào)用.使用LCD12864的class類和BMP的class類組合實(shí)現(xiàn).
#include "mydef.h"
#include "d:\inc\io.h"
#include "mydevice.h"
#include "myglobar.h"
#include "myfunction.h"
#include "myvect.h"

void FrameAVR::init()
{
  lcd= LCD12864_SPI;//     使用SPI串行模式
bmp.create(128,64);//     創(chuàng)建一個(gè)128×64像素的BMP類
}

void FrameAVR::Run()
{
DDRB = 0xFF;
PORTB ^= 0x0F//          測試信號,可用示波器觀察判斷程序的運(yùn)行狀況;
}

void FrameAVR::Timer()
{
bmp.clear();//                                清除緩沖區(qū)
  bmp= tabAscii5_8;//                         設(shè)定字符庫
bmp.drawText("#include <stdio.h>");//            開始寫入C程序
bmp.drawText("int main(void)",0,9);
bmp.drawText("{",0,18);
bmp.drawText("  for(chari=0;i<128;i++)",0,27);
bmp.drawText("    sprintf(\"i=%3d\",i);",0,36);
bmp.drawText("  return0;",0,45);
bmp.drawText("}",0,54);
  lcd= bmp;//                                 向LCD模塊發(fā)送數(shù)據(jù)
}
首先我建立的是一個(gè)BMP類,內(nèi)容如下:(經(jīng)過數(shù)天設(shè)計(jì)的可怕的程序,功能強(qiáng)大!慢慢來就會(huì)看得懂,重點(diǎn)看看下面這兩個(gè))


bool drawText(char* pStr,int dx = 0,int dy = 0)//         使用指定的字體繪字
  {
   while(*pStr != '\0')
    {
     if(dx + font.Width() >= width)//  判斷當(dāng)前行能否容納下一個(gè)要填充的字符
     {
       dx = 0;
       dy += font.Height() + 1;
       if(dy > height)
         return false;
     }
     for(int i = 0 ; i < font.Height(); i++)
     {
       fillLine(tabAscii5_8 + *pStr * 8 + 16 + i * font.WidthBytes(),1,dx,dy +i);
     }
     pStr++;
     dx += font.Width();
    }
   return true;
  }

bool fillLine(char *pSource,int nbytes,int dx,int dy)//   從pSource指向的地址向dx,dy指定的地址橫向填充(自動(dòng)換行)
  {
   char i = 0;
   while(nbytes--)
    {
     if(dx % 8 == 0)//                   當(dāng)前字節(jié)尚能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource;
     }
     else//                                       當(dāng)前字節(jié)不能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
       sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
     }
     dx += 8;
     i++;
    }
   return true;
  }



以下為ASCII_5_8.h
橫向取模,未定義的字符全部為空(0x00)
數(shù)組開始的幾個(gè)字節(jié)具有某些特殊的用途,可以靈活調(diào)配,為了不誤人子弟,本人已親自測試

#ifndef _ASCII_5_8_H_
#define _ASCII_5_8_H_


__flash char tabAscii5_8[] =
{
//   0     1     2    3     4      5    6     7     8    9     A     B    C     D     E    F   
//                   每個(gè)字符
//              起始 占用的
//   寬度  高度 地址 字節(jié)數(shù)  保留  保留  保留   名稱
    5,    8,   0x10, 8,    0,     0,   0,     'A',  'S', 'C', 'I',  'I',  '5', '*',  '8',  '\0',
//                                                             0x00
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x01
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x02
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x03
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x04
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x05
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                              0x06
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x07
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x08
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x09
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x0A
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x0B
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x0C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                              0x0D
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x0E
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x0F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,



//                                                             0x10
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x14
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x15
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                              0x16
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x17
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x18
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x19
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x1A
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x1B
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x1C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                              0x1D
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x1E
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                             0x1F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,



//                                                             0x20
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//                                                              0x21 !
0x00,0x60,0x60,0x60,0x60,0x00,0x60,0x60,
//                                                             0x22 "
0xA0,0xA0,0xA0,0x00,0x00,0x00,0x00,0x00,
//                                                             0x23 #
0x00,0x50,0x50,0xF8,0x50,0xF8,0x50,0x50,
//                                                             0x24 $
0x00,0x20,0xF0,0x80,0xF0,0x10,0xF0,0x40,
//                                                             0x25 %
0x90,0x10,0x20,0x20,0x40,0x40,0x80,0x90,
//                                                             0x26 &
0x60,0x90,0x90,0x60,0x48,0xB8,0x90,0x68,
//                                                             0x27 '
0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,
//                                                              0x28(
0x00,0x20,0x40,0x80,0x80,0x80,0x40,0x20,
//                                                             0x29 )
0x00,0x40,0x20,0x10,0x10,0x10,0x20,0x40,
//                                                              0x2A *
0x50,0x20,0x50,0x00,0x00,0x00,0x00,0x00,
//                                                             0x2B +
0x00,0x00,0x20,0x20,0xF8,0x20,0x20,0x00,
//                                                             0x2C ,
0x00,0x00,0x00,0x00,0x60,0x60,0x20,0x40,
//                                                             0x2D -
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
//                                                             0x2E .
0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,
//                                                             0x2F /
0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80,



//                                                             0x30   0
0x00,0xF0,0x90,0x90,0xB0,0xD0,0x90,0xF0,
//                                                              0x31   1
0x00,0x20,0x60,0x20,0x20,0x20,0x20,0x70,
//                                                             0x32   2
0x00,0xF0,0x10,0x10,0xF0,0x80,0x80,0xF0,
//                                                              0x33   3
0x00,0xF0,0x10,0x10,0xF0,0x10,0x10,0xF0,
//                                                             0x34   4
0x00,0x90,0x90,0x90,0xF0,0x10,0x10,0x10,
//                                                             0x35   5
0x00,0xF0,0x80,0x80,0xF0,0x10,0x10,0xF0,
//                                                             0x36   6
0x00,0xF0,0x80,0x80,0xF0,0x90,0x90,0xF0,
//                                                             0x37   7
0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,
//                                                             0x38   8
0x00,0xF0,0x90,0x90,0xF0,0x90,0x90,0xF0,
//                                                             0x39   9
0x00,0xF0,0x90,0x90,0xF0,0x10,0x10,0xF0,
//                                                             0x3A   :
0x00,0x00,0x00,0x60,0x60,0x00,0x60,0x60,
//                                                             0x3B   ;
0x00,0x00,0x00,0x60,0x60,0x00,0x20,0x60,
//                                                              0x3C   <
0x00,0x10,0x20,0x40,0x80,0x40,0x20,0x10,
//                                                             0x3D   =
0x00,0x00,0xF0,0x00,0x00,0xF0,0x00,0x00,
//                                                             0x3E   >
0x00,0x80,0x40,0x20,0x10,0x20,0x40,0x80,
//                                                             0x3F   ?
0x60,0x90,0x10,0x10,0x60,0x00,0x60,0x60,





//                                                             0x40  @
0x60,0x90,0x90,0xB0,0xA0,0x80,0x90,0x60,
//                                                             0x41  A
0x60,0x90,0x90,0x90,0xF0,0x90,0x90,0x90,
//                                                             0x42  B
0xE0,0x90,0x90,0x90,0xE0,0x90,0x90,0xE0,
//                                                             0x43  C
0x60,0x90,0x80,0x80,0x80,0x80,0x90,0x60,
//                                                             0x44  D
0xE0,0xA0,0x90,0x90,0x90,0x90,0xA0,0xE0,
//                                                             0x45  E
0xF0,0x80,0x80,0x80,0xF0,0x80,0x80,0xF0,
//                                                             0x46  F
0xF0,0x80,0x80,0xF0,0x80,0x80,0x80,0x80,
//                                                              0x47  G
0x60,0x90,0x80,0x80,0xB0,0x90,0x90,0x70,
//                                                             0x48  H
0x90,0x90,0x90,0x90,0xF0,0x90,0x90,0x90,
//                                                             0x49  I
0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0xE0,
//                                                             0x4A  J
0xF0,0x20,0x20,0x20,0x20,0x20,0xA0,0x40,
//                                                             0x4B  K
0x90,0x90,0xA0,0xC0,0xC0,0xA0,0x90,0x90,
//                                                             0x4C  L
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xF0,
//                                                             0x4D  M
0xD8,0xF8,0xA8,0xA8,0x88,0x88,0x88,0x88,
//                                                             0x4E  N
0x90,0xD0,0xD0,0xD0,0xB0,0xB0,0xB0,0x90,
//                                                             0x4F  O
0x60,0x90,0x90,0x90,0x90,0x90,0x90,0x60,




//                                                              0x50  P
0xE0,0x90,0x90,0xE0,0x80,0x80,0x80,0x80,
//                                                             0x51  Q
0x60,0x90,0x90,0x90,0x90,0xB0,0xE0,0x70,
//                                                             0x52  R
0x60,0x90,0x90,0x90,0xE0,0xA0,0x90,0x90,
//                                                             0x53  S
0x60,0x90,0x80,0x40,0x20,0x10,0x90,0x60,
//                                                              0x54  T
0xF8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
//                                                             0x55  U
0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x60,
//                                                             0x56  V
0x90,0x90,0x90,0x90,0x90,0xA0,0xC0,0x80,
//                                                             0x57  W
0x88,0x88,0x88,0x88,0xA8,0xA8,0xD8,0xD8,
//                                                             0x58  X
0x90,0x90,0x90,0x60,0x60,0x90,0x90,0x90,
//                                                             0x59  Y
0x88,0x88,0x50,0x70,0x20,0x20,0x20,0x20,
//                                                             0x5A  Z
0xF0,0x10,0x10,0x20,0x60,0x40,0x80,0xF0,
//                                                             0x5B  [
0xE0,0x80,0x80,0x80,0x80,0x80,0x80,0xE0,
//                                                             0x5C  反斜杠
0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,
//                                                              0x5D  ]
0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x70,
//                                                             0x5E  ^
0x40,0x40,0xA0,0xA0,0x00,0x00,0x00,0x00,
//                                                             0x5F  _
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,



//                                                             0x60  '
0x70,0x70,0x20,0x00,0x00,0x00,0x00,0x00,
//                                                              0x61  a
0x00,0x00,0x60,0x10,0x70,0x90,0xB0,0x50,
//                                                             0x62  b
0x00,0x00,0x80,0x80,0xE0,0x90,0x90,0xE0,
//                                                             0x63  c
0x00,0x00,0x00,0x70,0x80,0x80,0x80,0x70,
//                                                             0x64  d
0x00,0x10,0x10,0x10,0x70,0x90,0x90,0x70,
//                                                             0x65  e
0x00,0x00,0x00,0x60,0x90,0xF0,0x80,0x70,
//                                                             0x66  f
0x00,0x20,0x50,0x40,0xF0,0x40,0x40,0x40,
//                                                             0x67  g
0x00,0x00,0x70,0x90,0x90,0x70,0x10,0x60,
//                                                              0x68  h
0x00,0x80,0x80,0x80,0xE0,0x90,0x90,0x90,
//                                                             0x69  i
0x00,0x00,0x40,0x00,0xC0,0x40,0x40,0xE0,
//                                                              0x6A  j
0x00,0x00,0x20,0x00,0x60,0x20,0x20,0xE0,
//                                                             0x6B  k
0x00,0x00,0x80,0x90,0xA0,0xC0,0xA0,0x90,
//                                                             0x6C  l
0x00,0x00,0x40,0x40,0x40,0x40,0x50,0x60,
//                                                             0x6D  m
0x00,0x00,0x00,0x70,0xA8,0xA8,0xA8,0xA8,
//                                                              0x6E  n
0x00,0x00,0x00,0xE0,0x90,0x90,0x90,0x90,
//                                                             0x6F  o
0x00,0x00,0x00,0x60,0x90,0x90,0x90,0x60,




//                                                             0x70  p
0x00,0x00,0xE0,0x90,0x90,0xE0,0x80,0x80,
//                                                             0x71  q
0x00,0x00,0x70,0x90,0x90,0x70,0x10,0x10,
//                                                             0x72  r
0x00,0x00,0x00,0xD0,0x50,0x60,0x40,0x40,
//                                                             0x73  s
0x00,0x00,0x00,0x70,0x80,0x60,0x10,0xE0,
//                                                             0x74  t
0x00,0x00,0x40,0x40,0xF0,0x40,0x50,0x60,
//                                                              0x75  u
0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x60,
//                                                             0x76  v
0x00,0x00,0x00,0x90,0x90,0xB0,0xE0,0x80,
//                                                              0x77  w
0x00,0x00,0x00,0x00,0x88,0xA8,0xA8,0xD8,
//                                                             0x78  x
0x00,0x00,0x00,0x90,0x90,0x60,0x90,0x90,
//                                                             0x79  y
0x00,0x00,0x00,0x90,0x90,0x70,0x10,0x70,
//                                                             0x7A  z
0x00,0x00,0x00,0xF0,0x10,0x60,0x80,0xF0,
//                                                              0x7B  {
0x00,0x30,0x40,0x40,0xC0,0x40,0x40,0x30,
//                                                             0x7C  |
0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
//                                                             0x7D  }
0x00,0xC0,0x20,0x20,0x30,0x20,0x20,0xC0,
//                                                             0x7E  ~
0x00,0x00,0x00,0x40,0xA8,0x10,0x00,0x00,
//                                                             0x7F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};



#endif



以下為bmp.h

#ifndef _BMP_H_
#define _BMP_H_

#include "d:\inc\array.h"
#include "d:\inc\font.h"


__flash char tab_pixel[8] ={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

class BMP
{

protected:

Array sbuffer;

private:

  intx;
  inty;

protected:

UINT nWidths;  //  橫向數(shù)組寬度
  intwidth;//       橫向像素點(diǎn)數(shù)
  intheight;//      豎向像素點(diǎn)數(shù)
Font font;//       內(nèi)部字符庫

public:


BMP(int width = 0,int height = 0)
  {
   create(width,height);
  }

bool create(int width,int height)//                    創(chuàng)建一個(gè)實(shí)例
{   
   this->width = width;
   this->height = height;
   nWidths = width / 8 + (width % 8 ? 1 : 0);
   if(sbuffer.create(nWidths * height))
     return true;
   this->width = 0;
    this->height= 0;
   nWidths = 0;
   return false;
  }

  intfabs(int i)
  {
   return i >= 0 ? i : -i;
  }

void clear()//                                        清除緩沖區(qū)為0x00
  {
   sbuffer.format(0);
  }

bool drawText(char* pStr,int dx = 0,int dy = 0)//         使用指定的字體繪字
  {
   while(*pStr != '\0')
    {
     if(dx + font.Width() >= width)//  判斷當(dāng)前行能否容納下一個(gè)要填充的字符
     {
       dx = 0;
       dy += font.Height() + 1;
       if(dy > height)
         return false;
     }
     for(int i = 0 ; i < font.Height(); i++)
     {
       fillLine(tabAscii5_8 + *pStr * 8 + 16 + i * font.WidthBytes(),1,dx,dy +i);
     }
     pStr++;
     dx += font.Width();
    }
   return true;
  }

bool fillLine(char *pSource,int nbytes,int dx,int dy)//   從pSource指向的地址向dx,dy指定的地址橫向填充(自動(dòng)換行)
  {
   char i = 0;
   while(nbytes--)
    {
     if(dx % 8 == 0)//                   當(dāng)前字節(jié)尚能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource;
     }
     else//                                       當(dāng)前字節(jié)不能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
       sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
     }
     dx += 8;
     i++;
    }
   return true;
  }


bool fillLine(char __flash *pSource,int nbytes,int dx,int dy)//   從pSource指向的地址向dx,dy指定的地址橫向填充(自動(dòng)換行)
  {
   char i = 0;
   while(nbytes--)
    {
     if(dx % 8 == 0)//                   當(dāng)前字節(jié)尚能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource;
     }
     else//                                       當(dāng)前字節(jié)不能容納nPixs像素
     {
       sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
       sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
     }
     dx += 8;
     i++;
    }
   return true;
  }

void format(char fmt)//                               格式化
  {
   sbuffer.format(fmt);
  }

void drawpixel(int x,int y)//                          畫一個(gè)點(diǎn)
  {
   sbuffer[y * nWidths + x / 8] |= tab_pixel[x % 8];
  }

void lineX(int x,const int y,int length)//                            畫橫線
  {
   if(length > 0)
    {
     while(length != 0)
     {
       if(x % 8)
       {
         drawpixel(x++,y);
          length--;
       }
       else
       {
         if(length >= 8)
         {
           sbuffer[y * nWidths + x / 8] = 0xFF;
           length -= 8;
           x += 8;
         }
         else
         {
           drawpixel(x++,y);
           length--;
         }
       }
     }
    }
   else if(length < 0)
    {
     length = fabs(length);
     while(length != 0)
     {
       if(x % 8)
       {
         drawpixel(x--,y);
         length--;
       }
       else
       {
         if(length >= 8)
         {
           sbuffer[y * nWidths + x / 8 -1] = 0xFF;
           length -= 8;
           x -= 8;
         }
         else
         {
           drawpixel(x--,y);
           length--;
         }
       }
     }
    }
  }

void lineY(const int x,int y,int length)//                             畫豎線
  {
   char pixel = tab_pixel[x % 8];
   if(length > 0)
    {
     while(length != 0)
     {
       sbuffer[y++ * nWidths + x / 8] |= pixel;
       length--;
     }
    }
   else if(length < 0)
    {
     length = fabs(length);
     while(length != 0)
     {
       sbuffer[y-- * nWidths + x / 8] |= pixel;
       length--;
     }
    }
  }

void moveLeft(int line,UINT size)//    整行向左移動(dòng)
  {
   int i,source,des;
   size = fabs(size);
   des = nWidths * line;
   while(size >= 8)
    {
     source = des + 1;
     for( i = 0; i != nWidths - 1 ; i++)
     {
       sbuffer[des] = sbuffer[source];
       sbuffer[source] = 0x00;  
       des++;
       source++;
     }
     size -= 8;
    }
   des = nWidths * line;
    x= 8 - size;
   for(i = 0 ; i < nWidths - 1 ; i++)
    {
     sbuffer[des] = (sbuffer[des] << size) | (sbuffer[des + 1] >>x);
     des++;
    }
   sbuffer[des] <<= size;
  }

void moveRight(int line,UINT size)//    整行向右移動(dòng)
  {
   int i,source,des;
   size = fabs(size);
   des = nWidths * (line + 1) -1;
   while(size >= 8)
    {
     source = des - 1;
     for( i = 0; i != nWidths - 1 ; i++)
     {
       sbuffer[des] = sbuffer[source];
       sbuffer[source] = 0x00;  
       des--;
       source--;
     }
     size -= 8;
    }
   des = nWidths * line;
    x= 8 - size;
   for(i = 0 ; i < nWidths - 1 ; i++)
    {
     sbuffer[des] = (sbuffer[des] >> size) | (sbuffer[des - 1] <<x);
     des--;
    }
   sbuffer[des] >>= size;
  }

void moveUp(UINT line)//          整體向上移動(dòng)
  {
   int des,source;
   des = 0x00;
   source = line * nWidths;
   while(source != sbuffer.Size())
    {
     sbuffer[des++] = sbuffer[source++];
    }
   while(des != sbuffer.Size())
    {
     sbuffer[des++] = 0x00;
    }
  }

void moveDown(UINT line)//        整體向下移動(dòng)
{  
   int des,source;
   des = sbuffer.Size() - 1;
   source = sbuffer.Size() - line * nWidths - 1;
   while(source != 0)
    {
     sbuffer[des--] = sbuffer[source--];
    }
   while(des != 0)
    {
     sbuffer[des++] = 0x00;
    }
}

void move(int rx,int ry)//                    移動(dòng)圖像
  {
   if(rx > 0)
    {
     for(int i = 0; i < height ; i++)
     {
       moveRight(i,rx);
     }
    }
   else if(rx < 0)
    {
     for(int i = 0; i < height ; i++)
     {
       moveLeft(i,rx);
     }
    }
   else if(ry > 0)
    {
     moveDown(ry);
    }
   else if(ry < 0)
    {
     moveUp(fabs(ry));
    }
  }

void MoveTo(int X,int Y)//                              移動(dòng)起始點(diǎn)
  {
    x= X;
    y= Y;
  }

void LineTo(int X,int Y)//                             按MoveTo指定的起始點(diǎn)畫線,自動(dòng)保存起始點(diǎn)到dx,dy
  {
   int relx = X - x;
   int rely = Y - y;
   float k;
   if(X < 0 || X > width || Y < 0 || Y > height)//   超過極限取消畫線
     return ;
   drawpixel(x,y);
   if(relx == 0)//                               畫豎線
    {
     lineY(x,y,rely);
    }
   else if(rely == 0)//                          畫橫線
    {
     lineX(x,y,relx);
    }
   else
    {
     if(fabs(relx) > fabs(rely))
     {
       float y1 = y + 0.5;
       k = (float)rely / (float)fabs(relx);
       if(relx > 0)
       {
         while(x != X && x < 128 && x >= 0)
         {
           y1 += k;
           drawpixel(++x,(int)(y1));
         }
       }
       else
       {
         while(x != X && x < 128 && x >= 0)
         {
           y1 += k;
           drawpixel(--x,(int)(y1));
         }
       }
     }
     else
     {
       float x1 = x + 0.5;
       k = (float)relx / (float)fabs(rely);
       if(rely > 0)
       {
         while(y != Y && y < 64 && y >= 0)
         {
           x1 += k;
           drawpixel((int)(x1) , ++y);
         }
       }
       else
       {
         while(y != Y && y < 64 && y >= 0)
         {
           x1 += k;
           drawpixel((int)(x1) , --y);
         }
       }
     }
    }
    x= X;
    y= Y;
  }

void DashTo(int X,int Y,char dash = 1)//   按MoveTo指定的起始點(diǎn)畫虛線,自動(dòng)保存起始點(diǎn)到dx,dy,虛線間隔為dsah設(shè)定的值
  {
   int relx = X - x;
   int rely = Y - y;
   int i = 0;
   drawpixel(x & 127,y & 63);
   if(relx == 0)//                              畫豎線
    {
     while(y!=Y&&y<64&&y>=0)
     {
       i++;
       rely>=0?++y:--y;
       if(i%dash==0)
         drawpixel(x,y);
     }

    }
   else if(rely==0)//                          畫橫線
    {
     while(x!=X&&x<128&&x>=0)
     {
       i++;
       relx>=0?++x:--x;
       if(i%dash==0)
         drawpixel(x,y);
     }
    }
   x=X;
   y=Y;
  }

void Rectangle(int x,int y,int width,int height,bool filler =false)//               畫一個(gè)矩形
  {
   if(filler)//             畫填充實(shí)心的矩形
    {
     while(height)
     {
       MoveTo(x,y);
       LineTo(x+width,y);
       if(height > 0)
         y++,height--;
       else
         y--,height++;
     }
    }
   else//                   畫空心的矩形
    {
     MoveTo(x,y);
     LineTo(x + width,y);
     LineTo(x + width,y + height);
     LineTo(x,y + height);
     LineTo(x,y);
    }
  }

void loadBmp(char __flash *bmp,char width,char height,char stX = 0,charstY = 0)//          從flash數(shù)組里提取圖像
  {
    char x,y;
    for(y = 0 ; y < height ; y++)
    {
      for(x = 0 ; x < width / 8 ; x++)
      {
        sbuffer[(stY + y) * nWidths + x + stX / 8] = bmp[y * width / 8 + x];
      }
    }
  }

char getByte(int x,int y)//                           獲取緩沖區(qū)里的數(shù)據(jù)
  {
   return sbuffer[y * nWidths + x];
  }

UINT getWidths()//                                   獲取橫向字節(jié)數(shù)
  {
   return nWidths;
  }

char* get(int x,int y)//                            獲取緩沖區(qū)地址
  {
   return sbuffer.Sbuffer() + y * nWidths + x;
  }

  intWidth()//                                     獲取寬度(像素)
  {
   return width;
  }

  intHeight()//                                    獲取高度(像素)
  {
   return height;
  }

void operator = (char* p)
  {

  }

void operator = (char __flash *pFont)
  {
   font = pFont;
  }

char& operator [] (UINT index)
  {
   return sbuffer[index];
  }

Array& Sbuffer()
  {
   return sbuffer;
  }

UINT Length()//                 返回總字節(jié)數(shù)
  {
   return width * height;
  }
};

BMP bmp;

#endif

結(jié)束語 : 經(jīng)過一番努力,我終于可以在液晶模塊任何地方顯示文字和圖形了.

回復(fù)

使用道具 舉報(bào)

ID:64696 發(fā)表于 2014-12-4 20:49 | 顯示全部樓層
謝謝分享!我頂!�。�!
回復(fù)

使用道具 舉報(bào)

ID:75483 發(fā)表于 2015-3-27 17:21 | 顯示全部樓層
貌似是C++。
回復(fù)

使用道具 舉報(bào)

ID:82476 發(fā)表于 2015-6-9 11:09 | 顯示全部樓層
有C語言的嗎?
回復(fù)

使用道具 舉報(bào)

ID:82588 發(fā)表于 2015-6-10 10:26 | 顯示全部樓層
你那個(gè)my****.h 很坑人,和沒有一樣
回復(fù)

使用道具 舉報(bào)

ID:94867 發(fā)表于 2016-6-4 21:03 | 顯示全部樓層
最近在弄這方面的
回復(fù)

使用道具 舉報(bào)

ID:94867 發(fā)表于 2016-6-9 00:29 | 顯示全部樓層
C++寫的,大神啊
回復(fù)

使用道具 舉報(bào)

ID:169756 發(fā)表于 2017-3-14 21:15 | 顯示全部樓層
學(xué)習(xí)了,謝謝!
回復(fù)

使用道具 舉報(bào)

ID:195884 發(fā)表于 2017-6-1 16:59 | 顯示全部樓層
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表