www.pudn.com > LM240160-lcd-driver.rar > TOPWAY-GraphicLCM-ST7586-AppPro-v0.2.c, change:2013-03-18,size:21406b


//---------------------------------------------------- 
// 
//  拓普微内置ST7586液晶显示模块系列应用程序包-V0.2 
//           (应用液晶显示控制器ST7586) 
// 
//    本程序文件是应用手册中各功能程序的应用演示 
// 使用时要先根据使用环境修改接口地址和信号端口定义 
//             (以LM240160c为例) 
//         深圳市拓普微科技开发有限公司 
//      Shenzhen TOPWAY Technology Co.,Ltd. 
// 
//       by Guo Qiang           2012-01-12 
// note: update PIN define of serial interface 
//-------------------------------------------------- 
 
#include <stdio.h> 
#include <reg52.h> 
#include <intrins.h> 
 
#define uchar unsigned char // 0~255 
#define uint  unsigned int  // 0~65535 
#define ulong unsigned long // 32bit, 0 ~ 4,294,967,295 
 
uint   ContrastLevel;     // for contrast setting level  
//  数据格式转换表 
uchar ChangeTab[]={0x00,0x03,0x1c,0x1f,0xe0,0xe3,0xfc,0xff}; 
// 横向排列ASCII字符字模数据 
uchar code ASCIITAB[]= 
{ 
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 
  0x04,0x04,0x04,0x04,0x00,0x00,0x04,0x00,//! 
  0x0a,0x0a,0x0a,0x0a,0x00,0x00,0x00,0x00,//" 
  0x0a,0x0a,0x1f,0x0a,0x1f,0x0a,0x0a,0x00,//# 
  0x04,0x0f,0x14,0x0e,0x05,0x1e,0x04,0x00,//$ 
  0x18,0x19,0x02,0x04,0x08,0x13,0x03,0x00,//% 
  0x0c,0x12,0x14,0x08,0x15,0x12,0x0d,0x00,//& 
  0x0c,0x04,0x08,0x00,0x00,0x00,0x00,0x00,//' 
  0x02,0x04,0x08,0x08,0x08,0x04,0x02,0x00,//( 
  0x08,0x04,0x02,0x02,0x02,0x04,0x08,0x00,//) 
  0x00,0x04,0x15,0x0e,0x15,0x04,0x00,0x00,//* 
  0x00,0x04,0x04,0x1f,0x04,0x04,0x00,0x00,//+ 
  0x00,0x00,0x00,0x00,0x0c,0x04,0x08,0x00,//, 
  0x00,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,//- 
  0x00,0x00,0x00,0x00,0x00,0x0c,0x0c,0x00,//. 
  0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,/// 
  0x0e,0x11,0x13,0x15,0x19,0x11,0x0e,0x00,//0 
  0x04,0x0c,0x04,0x04,0x04,0x04,0x0e,0x00,//1 
  0x0e,0x11,0x01,0x02,0x04,0x08,0x1f,0x00,//2 
  0x1f,0x02,0x04,0x02,0x01,0x11,0x0e,0x00,//3 
  0x02,0x06,0x0a,0x12,0x1f,0x02,0x02,0x00,//4 
  0x1f,0x10,0x1e,0x01,0x01,0x11,0x0e,0x00,//5 
  0x06,0x08,0x10,0x1e,0x11,0x11,0x0e,0x00,//6 
  0x1f,0x01,0x02,0x04,0x04,0x04,0x04,0x00,//7 
  0x0e,0x11,0x11,0x0e,0x11,0x11,0x0e,0x00,//8 
  0x0e,0x11,0x11,0x0e,0x01,0x02,0x0c,0x00,//9 
  0x00,0x0c,0x0c,0x00,0x0c,0x0c,0x00,0x00,//: 
  0x00,0x0c,0x0c,0x00,0x0c,0x04,0x08,0x00,//; 
  0x02,0x04,0x08,0x10,0x08,0x04,0x02,0x00,//< 
  0x00,0x00,0x1f,0x00,0x1f,0x00,0x00,0x00,//= 
  0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x00,//> 
  0x0e,0x11,0x01,0x02,0x04,0x00,0x04,0x00,//? 
  0x0e,0x11,0x01,0x0d,0x15,0x15,0x0e,0x00,//@ 
  0x0e,0x11,0x11,0x11,0x1f,0x11,0x11,0x00,//a 
  0x1e,0x09,0x09,0x0e,0x09,0x09,0x1e,0x00,//b 
  0x0e,0x11,0x10,0x10,0x10,0x11,0x0e,0x00,//c 
  0x1e,0x09,0x09,0x09,0x09,0x09,0x1e,0x00,//d 
  0x1f,0x10,0x10,0x1f,0x10,0x10,0x1f,0x00,//e 
  0x1f,0x10,0x10,0x1e,0x10,0x10,0x10,0x00,//f 
  0x0e,0x11,0x10,0x13,0x11,0x11,0x0f,0x00,//G 
  0x11,0x11,0x11,0x1f,0x11,0x11,0x11,0x00,//H 
  0x0e,0x04,0x04,0x04,0x04,0x04,0x0e,0x00,//I 
  0x07,0x02,0x02,0x02,0x02,0x12,0x0c,0x00,//J 
  0x11,0x12,0x14,0x18,0x14,0x12,0x11,0x00,//K 
  0x10,0x10,0x10,0x10,0x10,0x10,0x1f,0x00,//L 
  0x11,0x1b,0x15,0x11,0x11,0x11,0x11,0x00,//M 
  0x11,0x19,0x19,0x15,0x13,0x13,0x11,0x00,//N 
  0x0e,0x11,0x11,0x11,0x11,0x11,0x0e,0x00,//O 
  0x1e,0x11,0x11,0x1e,0x10,0x10,0x10,0x00,//P 
  0x0e,0x11,0x11,0x11,0x15,0x12,0x0b,0x00,//Q 
  0x1e,0x11,0x11,0x1e,0x14,0x12,0x11,0x00,//R 
  0x0e,0x11,0x10,0x0e,0x01,0x11,0x0e,0x00,//S 
  0x1f,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//T 
  0x11,0x11,0x11,0x11,0x11,0x11,0x0e,0x00,//U 
  0x11,0x11,0x11,0x11,0x11,0x0a,0x04,0x00,//V 
  0x11,0x11,0x11,0x15,0x15,0x1b,0x11,0x00,//W 
  0x11,0x11,0x0a,0x04,0x0a,0x11,0x11,0x00,//X 
  0x11,0x11,0x11,0x0a,0x04,0x04,0x04,0x00,//Y 
  0x1f,0x01,0x02,0x04,0x08,0x10,0x1f,0x00,//Z 
  0x0e,0x08,0x08,0x08,0x08,0x08,0x0e,0x00,//[ 
  0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,//"\" 
  0x0e,0x02,0x02,0x02,0x02,0x02,0x0e,0x00,//] 
  0x04,0x0a,0x11,0x00,0x00,0x00,0x00,0x00,//^ 
  0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x00,//_ 
  0x10,0x08,0x04,0x00,0x00,0x00,0x00,0x00,//' 
  0x00,0x00,0x0e,0x01,0x0f,0x11,0x0f,0x00,//a 
  0x10,0x10,0x16,0x19,0x11,0x11,0x1e,0x00,//b 
  0x00,0x00,0x0e,0x11,0x10,0x11,0x0e,0x00,//c 
  0x01,0x01,0x0d,0x13,0x11,0x11,0x0f,0x00,//d 
  0x00,0x00,0x0e,0x11,0x1f,0x10,0x0e,0x00,//e 
  0x02,0x05,0x04,0x0e,0x04,0x04,0x04,0x00,//f 
  0x00,0x0d,0x13,0x13,0x0d,0x01,0x0e,0x00,//g 
  0x10,0x10,0x16,0x19,0x11,0x11,0x11,0x00,//h 
  0x04,0x00,0x0c,0x04,0x04,0x04,0x0e,0x00,//i 
  0x02,0x00,0x06,0x02,0x02,0x12,0x0c,0x00,//j 
  0x08,0x08,0x09,0x0a,0x0c,0x0a,0x09,0x00,//k 
  0x0c,0x04,0x04,0x04,0x04,0x04,0x0e,0x00,//l 
  0x00,0x00,0x1a,0x15,0x15,0x15,0x15,0x00,//m 
  0x00,0x00,0x16,0x19,0x11,0x11,0x11,0x00,//n 
  0x00,0x00,0x0e,0x11,0x11,0x11,0x0e,0x00,//o 
  0x00,0x16,0x19,0x19,0x16,0x10,0x10,0x00,//p 
  0x00,0x0d,0x13,0x13,0x0d,0x01,0x01,0x00,//q 
  0x00,0x00,0x16,0x19,0x10,0x10,0x10,0x00,//r 
  0x00,0x00,0x0f,0x10,0x1e,0x01,0x1f,0x00,//s 
  0x08,0x08,0x1c,0x08,0x08,0x09,0x06,0x00,//t 
  0x00,0x00,0x12,0x12,0x12,0x12,0x0d,0x00,//u 
  0x00,0x00,0x11,0x11,0x11,0x0a,0x04,0x00,//v 
  0x00,0x00,0x11,0x11,0x15,0x15,0x0a,0x00,//w 
  0x00,0x00,0x11,0x0a,0x04,0x0a,0x11,0x00,//x 
  0x00,0x00,0x11,0x11,0x13,0x0d,0x01,0x0e,//y 
  0x00,0x00,0x1f,0x02,0x04,0x08,0x1f,0x00,//z 
  0x02,0x04,0x04,0x08,0x04,0x04,0x02,0x00,//{ 
  0x02,0x02,0x02,0x00,0x02,0x02,0x02,0x00,//| 
  0x08,0x04,0x04,0x02,0x04,0x04,0x08,0x00,//} 
  0x08,0x15,0x02,0x00,0x00,0x00,0x00,0x00,//~ 
  0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,// 
}; 
 
//-----中文字库---------------------------------- 
// 汉字数据格式:汉字左右字节排序,16行数据顺序排列 
uchar code CCTAB[]={ 
/*--  文字:  深  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x47,0xFE,0x24,0x02,0x15,0x12,0x05,0x08,0x82,0x04,0x64,0x44, 
0x30,0x40,0x0F,0xFE,0x10,0x60,0x10,0xD0,0x21,0x58,0x22,0x4C,0x2C,0x42,0x40,0x40, 
/*--  文字:  圳  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x7A,0x22,0x22,0x22, 
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x2E,0x22,0x12,0x22,0x64,0x22,0x04,0x22, 
/*--  文字:  市  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x7F,0xFC,0x01,0x00,0x1F,0xF8,0x21,0x04, 
0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x64,0x00,0x18, 
/*--  文字:  拓  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x10,0x00,0x10,0x00,0x13,0xFE,0x10,0x40,0x38,0x80,0x10,0x80,0x10,0x80, 
0x14,0xFE,0x19,0x82,0x72,0x82,0x12,0x82,0x14,0x82,0x10,0x82,0x10,0x82,0x70,0xFC, 
/*--  文字:  普  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x08,0x10,0x08,0x10,0x7F,0xFE,0x04,0x40,0x24,0x44,0x14,0x48,0x14,0x48, 
0x7F,0xFE,0x00,0x00,0x1F,0xF8,0x20,0x08,0x3F,0xF8,0x20,0x08,0x20,0x08,0x1F,0xF8, 
/*--  文字:  微  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x11,0x00,0x11,0x50,0x29,0x50,0x29,0x5E,0x57,0xD2,0x10,0x12,0x17,0xF2, 
0x20,0x14,0x67,0x94,0x24,0x8C,0x24,0x8C,0x24,0x84,0x24,0xAC,0x24,0xD2,0x28,0x22, 
/*--  文字:  科  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x7C,0x84,0x10,0x44,0x10,0x24,0x10,0x14,0x7F,0x0C,0x18,0x84, 
0x14,0x44,0x34,0x24,0x32,0x04,0x50,0x0E,0x13,0xF4,0x10,0x04,0x10,0x04,0x10,0x04, 
/*--  文字:  技  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x10,0x40,0x13,0xFE,0x10,0x40,0x7C,0x40,0x10,0x40,0x13,0xFC, 
0x11,0x04,0x19,0x08,0x30,0x88,0x50,0x50,0x10,0x60,0x10,0xD0,0x13,0x08,0x7C,0x06, 
/*--  文字:  开  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x3F,0xFC,0x08,0x10,0x08,0x10,0x08,0x10,0x08,0x10,0x08,0x10, 
0x7F,0xFE,0x08,0x10,0x08,0x10,0x08,0x10,0x08,0x10,0x10,0x10,0x10,0x10,0x20,0x10, 
/*--  文字:  发  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x01,0x00,0x11,0x30,0x21,0x08,0x22,0x00,0x3F,0xFE,0x02,0x00,0x02,0x00, 
0x07,0xFC,0x05,0x08,0x08,0x88,0x08,0x90,0x10,0x60,0x20,0x60,0x41,0x98,0x0E,0x06, 
/*--  文字:  有  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x01,0x00,0x02,0x00,0x7F,0xFE,0x04,0x00,0x08,0x00,0x1F,0xFC,0x70,0x04, 
0x90,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x3C, 
/*--  文字:  限  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x7B,0xFC,0x4A,0x04,0x4A,0x04,0x4B,0xFC,0x52,0x04,0x4A,0x04, 
0x4B,0xFC,0x4A,0x40,0x4A,0x44,0x7A,0x28,0x42,0x30,0x42,0x10,0x42,0x08,0x43,0xC6, 
/*--  文字:  公  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x04,0x40,0x04,0x20,0x08,0x10,0x10,0x10,0x10,0x08,0x21,0x04, 
0x42,0x02,0x02,0x20,0x04,0x10,0x08,0x10,0x08,0x08,0x10,0x08,0x1F,0xFC,0x00,0x04, 
/*--  文字:  司  --*/ 
/*--  幼圆12;  此字体下对应的点阵为:宽x高=16x16   --*/ 
0x00,0x00,0x00,0x00,0x3F,0xFC,0x00,0x04,0x00,0x04,0x3F,0xF4,0x00,0x04,0x3F,0xE4, 
0x20,0x24,0x20,0x24,0x20,0x24,0x20,0x24,0x20,0x24,0x1F,0xC4,0x00,0x04,0x00,0x7C, 
}; 
 
// ----图片数组-----------------------------  
// 单色数据,横向排列 
uchar code BMP[]={}; 
 
//----------------------------------- 
// Delay Routine 
//----------------------------------- 
void delayms(uint m)    // 12MHz Xtal, close to ms value 
{ 
    uint j; 
    uint i; 
  
    for(i=0; i<m; i++) 
        for(j=0; j<109; j++) 
           _nop_(); 
} 
 
//----------------------------------- 
// 总线寻址方式驱动程序 
//----------------------------------- 
//--指令代码传送函数-------- 
void SdCmd(uchar Command) 
{ 
   uchar xdata  *com_addr; 
   com_addr = 0x8000;              // 指令通道地址 
   *com_addr = Command;          //写指令操作 
} 
//--显示数据传送函数--------- 
void SdData(uchar Ddata) 
{ 
    uchar xdata  *data_addr; 
    data_addr = 0x8100;           // 数据通道地址 
    *data_addr = Ddata;            //写数据操作 
} 
//--显示数据读取函数--------- 
void RdData() 
{ 
    uchar Ddata; 
    uchar xdata  *data_addr; 
    data_addr = 0x8100;           // 数据通道地址 
    Ddata = *data_addr;            // 读数据操作 
    return (Ddata); 
} 
 
//----------------------------------- 
// I/O寻址驱动程序 
//----------------------------------- 
#define LCDBUS    P1 
sbit _CS  = P3^4;       // connections to LCD module 
sbit _RST = P3^5; 
sbit RS   = P3^1; 
sbit _WR  = P3^7; 
sbit _RD  = P3^6; 
//--指令写入函数---------- 
void SdCmd(uchar Command)    
{ 
    RS = 0;                 // 选择指令通道 
    LCDBUS = Command;       // 将指令送至数据接口 
    _CS = 0;                // 选择模块 
    _WR = 0;                // 写信号为低电平 
    _WR = 1;                // 写信号为高电平 
    _CS = 1;                // 封锁模块 
} 
//--数据写入函数---------- 
void SdData(uchar Ddata)   
{ 
    RS = 1;                 // 选择数据通道 
    LCDBUS = Ddata;         // 将指令送至数据接口 
    _CS = 0;                // 选择模块 
    _WR = 0;                // 写信号为低电平  
    _WR = 1;                // 写信号为高电平 
    _CS = 1;                // 封锁模块 
} 
 
//--数据读取函数---------- 
uchar RdData()   
{ 
   uchar Ddata; 
    LCDBUS = 0xff; 
    RS = 1;                 // 选择数据通道 
    _CS = 0;                // 选择模块 
    _RD = 0;                // 写信号为低电平  
    Ddata = LCDBUS;         // 将指令送至数据接口 
    _RD = 1;                // 写信号为高电平 
    _CS = 1;                // 封锁模块 
    return (Ddata); 
} 
 
//----------------------------------- 
//串行接口驱动函数(4线) 
//----------------------------------- 
//---串行接口定义--- 
sbit _RST    = P3^5; 
sbit _CS     = P3^4; 
sbit RS      = P1^1; 
sbit SID     = P1^0; 
sbit SCLK    = P3^1; 
uchar bdata transdata;     //该变量可为位操作之变量 
sbit transbit  = transdata^7; 
//--指令写入函数------------------- 
void SdCmd(uchar Command) 
{ 
   uchar j; 
   transdata=Command;          // 指令送位变量 
   SCLK = 1;                   // 初始化SCLK 
    RS = 0;                    // 选择指令通道 
   _CS = 0;                   // 选通模块 
   for(j=0;j<8;j++) 
     { 
      SID=transbit;            // 位变量D7位送数据口 
      SCLK=0;                  // 产生移位脉冲 
      SCLK=1;                  // 上升沿有效 
      transdata=transdata<<1;  // 位变量数据左移一位 
     } 
   _CS=1;                      // 封锁模块 
} 
//-----数据传送函数-------------------------- 
void SdData(uchar Ddata) 
{ 
   uchar j; 
   transdata=Ddata;            // 指令送位变量 
   SCLK = 1;                   // 初始化SCLK 
   RS  = 1;                    // 选择数据通道 
   _CS = 0;                    // 选通模块 
   for(j=0;j<8;j++) 
     {                          
      SID=transbit;            // 位变量D7位送数据口 
      SCLK=0;                  // 产生移位脉冲 
      SCLK=1;                  // 上升沿有效 
      transdata=transdata<<1;  // 位变量数据左移一位 
     } 
   _CS=1;                      // 封锁模块 
} 
 
//----------------------------------- 
// 对比度调节函数 
//----------------------------------- 
void LCD_Darker(void)              //  调深对比度 
{ 
  if (ContrastLevel<0x01f0)        // 限制上限值,根据应用环境设置允许值 
   { 
    ContrastLevel++; 
    SdCmd(0xc1);		           // darker command 
   } 
} 
//---------------------------------- 
void LCD_Lighter(void)            //  调浅对比度 
{ 
  if (ContrastLevel>0x0010)       // 限制下限值,根据应用环境设置允许值 
   { 
    ContrastLevel--; 
    SdCmd(0xc2);		          // lighter command 
   } 
} 
 
//----------------------------------- 
//    初始化函数 
//----------------------------------- 
void initLCDM() 
{ 
    _RST = 1;  
    _RST = 0;       // reset the LCD module 
    delayms(10); 
    _RST = 1; 
    delayms(800);     // wait for the init routine to finish 
//  read OPT data 
    SdCmd(0xd7);		// Disable Auto Read 
	SdData(0x9F);		 
	SdCmd(0xe0);		// Enable OTP Read 
	SdData(0x00);		 
	delayms(100);		 
	SdCmd(0xe3);		// OTP Up-Load 
	delayms(200);		 
	SdCmd(0xe1);		// OTP Control Out 
// initail set 
	SdCmd(0x11);		// Sleep Out 
	delayms(50);		 
	ContrastLevel=0x11f; 
	SdCmd(0xc0);		 // Vop  
	SdData(ContrastLevel);		// contrast value 
	SdData(ContrastLevel>>8);		//  
	SdCmd(0xc3);		// BIAS  
	SdData(0x02);		// 05 = 1/12 
	SdCmd(0xc4);		// Booster = x8 
	SdData(0x07);		 
	SdCmd(0xd0);		// Enable Analog Circuit 
	SdData(0x1d);		 
	SdCmd(0xb5);		// N-Line = 13 
	SdData(0x8d);		//8d 
	SdCmd(0x38);		// 4 Gray 
//	SdCmd(0x39);		// B/W Mode 
	SdCmd(0x3a);		// Enable DDRAM Interface 
	SdData(0x02);		 
	SdCmd(0x36);		// Dispaly Control 
	SdData(0xc8);       // MY=1,MX[1:0]=11;		 
	SdCmd(0xb0);		// Duty Setting 
	SdData(0x9f);		// 160 line 9f 
	SdCmd(0xb1);		// First COM out 
	SdData(0x00);		// com0 
	SdCmd(0x29);		// Display ON 
} 
 
//--------------------------------- 
//   清屏函数 
//--------------------------------- 
void ClearRAM()			 
//   
{ 
 uchar i,j; 
	SdCmd(0x2a);		// 列地址区域设置 
	SdData(0x00);		  
	SdData(0x00);		  
	SdData(0x00);		 
	SdData(0x7f);       //		  
	SdCmd(0x2b);		// 行地址区域设置 
	SdData(0x00);		 
	SdData(0x00);		 
	SdData(0x00);		 
	SdData(0x9f); 
	 
	SdCmd(0x2c);        // 写数据指令 
	for(i=0;i<160;i++) 
	{ 
	 for(j=0;j<128;j++) 
	   { 
	    SdData(0x00); 
	   } 
	} 
} 
 
//-------------------------------------- 
//    西文字符写入函数 
//-------------------------------------- 
void PrintASCII(uchar x, uchar y, uchar *asciicode)  
// 坐标X为字符位,3像素点数据/字节  
// 坐标Y为像素点行数0-159 
// asciicode为西文字符代码,对应字库为ASCIITAB 
// 显示6*8点阵字符 
{  
    uint p; 
    uchar i,k,Ddata,temp; 
	while(*asciicode>0) 
	   { 
	    SdCmd(0x2a);		// Column Address Setting 
	    SdData(0x00);		//  
	    SdData(x);			//  
	    SdData(0x00);		 
	    SdData(x+1);	//	 
	    SdCmd(0x2b);		// Row Address Setting 
	    SdData(0x00);		//  
	    SdData(y);		 
	    SdData(0x00);		 
	    SdData(y+7); 
	    SdCmd(0x2c); 
        p=((*asciicode++)-0x20)*8;                      // 计算字模数据起始地址 
        for(i=0;i<8;i++) 
	       { 
	        Ddata=ASCIITAB[p+i]<<2; 
		    for(k=0;k<2;k++) 
		      { 
		       temp=Ddata&0xe0; 
		       SdData(ChangeTab[temp>>5]); 
               Ddata=Ddata<<3; 
  		      } 
           } 
		x=x+2; 
	   } 
}  
 
//-------------------------------------- 
//        中文字符写入函数 
//-------------------------------------- 
//--中文写入以18*16点阵格式写入,其中第17、18像素数据为0,作为字间距出现 
void PrintGB(uchar x, uchar y, uchar *chinesecode)  
// 坐标X为字符位(字节),3像素点数据/字节 
// 坐标Y为像素点行数0-159 
// chinesecode为中文字符代码,代码从0x01开始,对应字库为CCTAB 
  
{   
   uint p; 
   uchar i,k,temp; 
   uint Ddata; 
	while(*chinesecode>0) 
	   { 
	    SdCmd(0x2a);		// Column Address Setting 
	    SdData(0x00);		//  
	    SdData(x);			//  
	    SdData(0x00);		 
	    SdData(x+5);	//	 
	    SdCmd(0x2b);		// Row Address Setting 
	    SdData(0x00);		//  
	    SdData(y);		 
	    SdData(0x00);		 
	    SdData(y+15); 
	    SdCmd(0x2c); 
        p=((*chinesecode++)-1)*32;      // 计算字模数据起始地址 
        for(i=0;i<16;i++) 
	       { 
	        Ddata=(CCTAB[p++]<<8)+CCTAB[p++]; 
		    for(k=0;k<6;k++) 
		      { 
		       temp=(Ddata&0xe000)>>8; 
		       SdData(ChangeTab[temp>>5]); 
               Ddata=Ddata<<3; 
  		      } 
           } 
		x=x+6; 
	   } 
}  
 
//------------------------------------ 
// 画图函数 
//------------------------------------ 
void ShowBMP(uchar x,uchar y,uchar width,uchar high,uchar *pstr) 
// 坐标(x,y),x为水平方向字符位(1个字符位占3个像素点);y为垂直方向像素行; 
// width:图片宽度,按照水平方向像素点计算;high:为图片高度,按照像素点计算,*pstr 图片数组名 
// 要求width以3的倍数设置。 
{ 
	uchar i,j,k,Ddata,temp; 
	SdCmd(0x2a);		// Column Address Setting 
	SdData(0x00);		//  
	SdData(x);			//  
	SdData(0x00);		 
	SdData((width/3)+x-1);	//	 
	SdCmd(0x2b);		// Row Address Setting 
	SdData(0x00);		//  
	SdData(y);		 
	SdData(0x00);		 
	SdData(y+high-1); 
	SdCmd(0x2c); 
	for(i=0;i<high;i++) 
	{ 
// 单色数据按照3个字节(24个像素点)转换成8个字节,每个字节3个像素点 
// 转换方法:使用查表方法,3位一组转换数据 
    for(j=0;j<width;j=j+24) 
	   { 
	    Ddata=*pstr++; 
		for(k=0;k<2;k++) 
		  { 
		   temp=Ddata&0xe0; 
		   SdData(ChangeTab[temp>>5]); 
           Ddata=Ddata<<3; 
  		  } 
	    Ddata=Ddata + ((*pstr)>>2);  
		for(k=0;k<2;k++) 
		  { 
		   temp=Ddata&0xe0; 
		   SdData(ChangeTab[temp>>5]); 
           Ddata=Ddata<<3; 
  		  } 
	    Ddata=Ddata + (((*pstr++)&0x03)<<4);   
        Ddata=Ddata +((*pstr)>>4); 
		for(k=0;k<2;k++) 
		  { 
		   temp=Ddata&0xe0; 
		   SdData(ChangeTab[temp>>5]); 
           Ddata=Ddata<<3; 
  		  } 
        Ddata=Ddata +(((*pstr++)&0x0f)<<2); 
		for(k=0;k<2;k++) 
		  { 
		   temp=Ddata&0xe0; 
		   SdData(ChangeTab[temp>>5]); 
           Ddata=Ddata<<3; 
  		  } 
       } 
	} 
} 
 
//-----绘点函数-------------------------- 
void Draw_Dot(uint x,y) 
// 坐标(x,y),x为水平方向像素点列;y为垂直方向像素点行 
{ 
  uchar k,m; 
    m=x/3; 
 	SdCmd(0x2a);		// Column Address Setting 
	SdData(0x00);		//  
	SdData(m);			//  
	SdData(0x00);		 
	SdData(m+1);	//	 
	SdCmd(0x2b);		// Row Address Setting 
	SdData(0x00);		//  
	SdData(y);		 
	SdData(0x00);		 
	SdData(y+1); 
    SdCmd(0xb8); 
    k=RdData();                                   // 空读 
    k=RdData();                                   // 读数据 
    m=x%3; 
   switch (m) 
      { 
	   case 0 :   k=k|0xe0;break;                  // 写点 
       case 1 :   k=k|0x1c;break; 
	   case 2 :   k=k|0x03;break; 
	  } 
   SdData(k);                                    // 写数据 
   SdCmd(0xb9);                                   // 修改写模式结束 
 
} 
//-----绘线函数----------------------------------- 
void Draw_Line(uint x1,y1,x2,y2) 
//  x为水平方向像素列;y为垂直方向像素点行 
//  坐标(x1,y1)为线起始地址坐标;坐标(x2,y2)为线终止地址坐标。 
{ 
  uint temp; 
  int dalt_x,dalt_y,err=0; 
 if (y1>y2) 
    { 
    temp=x1; 
    x1=x2; 
    x2=temp; 
    temp=y1; 
    y1=y2; 
    y2=temp; 
    } 
 Draw_Dot(x1,y1); 
 dalt_x=x2-x1; 
 dalt_y=y2-y1; 
 if(dalt_x>=0) 
     { 
      if(dalt_y>dalt_x)//k>1 
        { 
         while(y1<y2) 
         { 
          if(err<0) 
          { 
           x1=x1+1; 
           y1=y1+1; 
           err=err+dalt_y-dalt_x; 
           } 
           else 
           { 
           y1=y1+1; 
           err=err-dalt_x; 
           } 
          Draw_Dot(x1,y1); 
         } 
       } 
     else  // 0<=k=<1 
      { 
       if (dalt_y==0) 
          y1=y1-1; 
       while(x1<x2) 
         { 
          if(err<0) 
          { 
           x1=x1+1; 
           err=err+dalt_y; 
           } 
           else 
           { 
           y1=y1+1; 
           x1=x1+1; 
           err=err+dalt_y-dalt_x; 
           } 
          Draw_Dot(x1,y1); 
         } 
     } 
      
  } 
else 
  {  
   dalt_x=x1-x2;   
    if(dalt_y>dalt_x)//k<-1 
     { 
      while(y1<y2) 
       { 
        if(err<0) 
         { 
          x1=x1-1; 
          y1=y1+1; 
          err=err+dalt_y-dalt_x; 
         } 
        else 
         { 
          y1=y1+1; 
          err=err-dalt_x; 
         } 
        Draw_Dot(x1,y1); 
      } 
    } 
   else       //0>k>=-1 
    { 
      if (dalt_y==0) 
          y1=y1-1;  
    while(x1>x2) 
      { 
       if(err<0) 
        { 
         x1=x1-1; 
         err=err+dalt_y; 
        } 
       else 
        { 
         x1=x1-1; 
         y1=y1+1; 
         err=err+dalt_y-dalt_x; 
        } 
       Draw_Dot(x1,y1); 
      } 
    }  
  } 
      
} 
 
//end of program