#include "stm8s.h"
#include "lcd.h"
#include "font.h"
/*************************************************
延时
************************************************/
void delay(u16 z)
{
u16 x,y,n;
n=1000*z;
for(x=n;x>0;x--)
for(y=124;y>0;y--);
}
//延时nms
void delay_1ms(void)//1ms延时函数
{
int i=108;
while(--i);
}
void delay_nms(nms)//nms延时函数
{
u8 i;
for(i=0;i<8;i++)
{
delay_1ms();
}
}
/******** 写“8bit”程序 ********/
void write_8bit(u8 dat)
{
u8 i;
for(i=0;i<8;i++)
{
if((dat&0x80)==0x80)
SDI_H();
else
SDI_L();
SCLK_L();
SCLK_H();
dat<<=1;
//delay_1ms();
}
}
/******** 读“数据”程序 ********/
u8 read_data(void)
{
u8 i,l,temp=0X00;
CONTROL_OUTPUT_MODE();
//CONTROL_SDO_OUTPUT_MODE();
//SDO_L();
CONTROL_SDO_INPUT_MODE();
l=GPIO_ReadInputPin(CONTROL_PORT, SDO_PIN);
SCS_L();
write_8bit(0x40);
for(i=0;i<8;i++)
{
temp<<=1;
SCLK_L();
if(l&0X01==1);
temp++;
SCLK_H();
}
SCS_H();
return temp;
}
/******** 读指令程序 ********/
u8 read_com(void)
{
u8 i,j,l,temp=0X00,dat=0xc0;
CONTROL_OUTPUT_MODE();
CONTROL_SDO_INPUT_MODE();
SCS_L();
write_8bit(0XC0);
for(i=0;i<8;i++)
{
temp<<=1;
SCLK_L();
if(GPIO_ReadInputPin(CONTROL_PORT, SDO_PIN)!=RESET)
temp++;
SCLK_H();
}
SCS_H();
return temp;
}
/******** 读“状态””程序 ********/
void busy(void)
{ u8 temp=0x00;
do{
temp=read_com();
}while(temp&0x80);
}
/******** 写“指令”程序 ********/
void write_com(u8 com)
{
busy();
SCLK_H();
SDI_H();
SCS_L();
write_8bit(0x80);
write_8bit(com);
SCS_H();
}
/******** 写“数据”程序 ********/
void write_data(u8 dat)
{
busy();
SCLK_H();
SDI_H();
SCS_L();
write_8bit(0x00);
write_8bit(dat);
SCS_H();
}
/******** 写“指令数据”程序 *********/
void write_comdat(u8 com,u8 dat)
{ write_com(com);
write_data(dat);
}
/******** 工作窗口设置程序 ********/
void active_window(u16 row0,u16 row1,u16 col0,u16 col1)
{
write_comdat(0x30,col0); //Horizontal Start Point
write_comdat(0x31,col0>>8);
write_comdat(0x32,row0); //Vertical Start Point
write_comdat(0x33,row0>>8);
write_comdat(0x34,col1); //Horizontal End Point
write_comdat(0x35,col1>>8);
write_comdat(0x36,row1); //Vertical End Point
write_comdat(0x37,row1>>8);
}
/******** 卷动窗口设置程序 ********/
void scroll_window(u16 row0,u16 row1,u16 col0,u16 col1)
{
write_comdat(0x38,col0); //Horizontal Start Point
write_comdat(0x39,col0>>8);
write_comdat(0x3a,row0); //Vertical Start Point
write_comdat(0x3b,row0>>8);
write_comdat(0x3c,col1); //Horizontal End Point
write_comdat(0x3d,col1>>8);
write_comdat(0x3e,row1); //Vertical End Point
write_comdat(0x3f,row1>>8);
}
/******** 卷动偏移量设置程序 ********/
void scroll_offset(u16 row,u16 col)
{
write_comdat(0x24,col); //Horizontal offset
write_comdat(0x25,col>>8);
write_comdat(0x26,row); //Vertical offset
write_comdat(0x27,row>>8);
}
/******** 浮动窗口设置程序 ********/
void floating_window(u16 row0,u16 col0,u16 height,u16 width,u16 row1,u16 col1)
{
write_comdat(0xD0,col0); //Start Address XA:Destination
write_comdat(0xD1,col0>>8);
write_comdat(0xD2,row0); //Start Address YA:Destination
write_comdat(0xD3,row0>>8);
write_comdat(0xD4,width); //Width
write_comdat(0xD5,width>>8);
write_comdat(0xD6,height); //Height
write_comdat(0xD7,height>>8);
write_comdat(0xD8,col1); //Display X Address:Source
write_comdat(0xD9,col1>>8);
write_comdat(0xDA,row1); //Display Y Address:Source
write_comdat(0xDB,row1>>8);
}
/******** 字体(内部)设置程序 ********/
void font_in_set(void)
{ //CGROM=0:内部CGROM; CGROM=1:外部CGROM
//size=0x00:字体16、size=0x40:字体24、size=0x80:字体32
write_comdat(0x40,0x80); //文本模式、光标不闪烁
write_comdat(0x44,0x00); //Blink Time闪烁时间
// write_comdat(0x4E,0x0f); //Font Write Cursor Horizontal Size
// write_comdat(0x4F,0x01); //Font Write Cursor Vertical Size
write_comdat(0x21,0x00); //CGROM
write_comdat(0x22,0x00); //字体不对齐、背景色、不旋转、正常大小
write_comdat(0x29,0x00); //字体行间距00
write_comdat(0x2E,0x80); // 字体大小size、字体列间距00
write_comdat(0x2F,0x00); //字库GT、ASCII、Normal
}
/******** 字体(外部)设置程序 ********/
void font_ex_set(void)
{ //CGROM=0:内部CGROM; CGROM=1:外部CGROM
//size=0x00:字体16、size=0x40:字体24、size=0x80:字体32
write_comdat(0x44,0x00); //Blink Time闪烁时间
// write_comdat(0x4E,0x0f); //Font Write Cursor Horizontal Size
// write_comdat(0x4F,0x01); //Font Write Cursor Vertical Size
write_comdat(0x20,0x00);
write_comdat(0x21,0x20); //CGROM
write_comdat(0x22,0x00); //字体不对齐、背景色、不旋转、正常大小
write_comdat(0x22,0x00); //字体不对齐、背景色、不旋转、正常大小
write_comdat(0x29,0x00); //字体行间距00
write_comdat(0x2E,0x00); // 字体大小size、字体列间距00
write_comdat(0x2F,0x81); //字库GT、ASCII、Normal
}
/******** 字体地址程序 ********/
void font_position(u16 row,u16 col)
{
write_comdat(0x2A,(u8)(col)); //Horizontal Position
write_comdat(0x2B,(u8)(col>>8));
write_comdat(0x2C,(u8)(row)); //Vertical Position
write_comdat(0x2D,(u8)(row>>8));
}
/******** 内存写入地址程序 ********/
void memory_write_position(u16 row,u16 col)
{
write_comdat(0x46,(u8)(col)); //Horizontal Position
write_comdat(0x47,(u8)(col>>8));
write_comdat(0x48,(u8)(row)); //Vertical Position
write_comdat(0x49,(u8)(row>>8));
}
/******** 内存读取地址程序 ********/
void memory_read_position(u16 row,u16 col)
{
write_comdat(0x4A,col); //Horizontal Position
write_comdat(0x4B,col>>8);
write_comdat(0x4C,row); //Vertical Position
write_comdat(0x4D,row>>8);
}
/******** BTE背景色前景色设置程序 ********/
void back_fore_color(u16 color_b,u16 color_f)
{
write_comdat(0x60,(u8)(color_b>>11)); //背景色
write_comdat(0x61,(u8)(color_b>>5));
write_comdat(0x62,(u8)(color_b));
write_comdat(0x63,(u8)(color_f>>11)); //前景色
write_comdat(0x64,(u8)(color_f>>5));
write_comdat(0x65,(u8)(color_f));
}
/********倍频设置程序********/
void PLL(void)
{ write_comdat(0x88,0x0a);
write_comdat(0x89,0x02);
}
/******** 清屏程序********/
void clr(void)
{
// write_comdat(0x8E,0xC0); //清除显示窗口
active_window(0,HD-1,0,WD-1); //工作窗口
write_comdat(0x8E,0x80); //清除工作窗口
}
/******** 全屏(文本)显示程序********/
void fill_txt(u8 dat)
{ u16 i;
font_position(0,0); //字体地址
write_com(0x02); //写数据
for(i=0;i<WD/8*HD/16;i++)
write_data(dat);
}
/******** 填充任意窗口(文本)显示程序********/
void fill2_txt(u16 x1,u16 y1,u16 x2,u16 y2,u16 dat)
{ u16 i;
write_comdat(0x40,0x80); //文本模式
active_window(x1,x2,y1,y2); //工作窗口
font_position(x1,y1); //字体地址
write_com(0x02); //写数据
for(i=0;i<((y2-y1+1)/8)*((x2-x1+1)/16);i++) //以8*16像素块填充数据dat,填充0x00:清屏
write_data(dat);
}
/******** 全屏(图形)显示程序********/
void fill_graph(u16 x,u16 y,u16 k,u8 dat)
{ u32 i;
memory_write_position(x,y); //内存地址
write_com(0x02); //写数据
for(i=0;i<WD*k;i++)
write_data(dat);
}
/******** 256色显示程序********/
void color256(void)
{ u16 i,j;
for(i=0;i<HD;i++)
{ memory_write_position(i,0); //内存地址
write_com(0x02); //写数据
for(j=0;j<WD;j++)
write_data(0x00+i);
}
}
/******** 字符(文本)显示程序 ********/
void chhzes_txt(u16 x,u16 y,u8 *s)
{
font_position(x,y); //字体地址
write_com(0x02); //写数据
while(*s!='\0')
write_data(*s++);
}
/******** 图片显示程序 ********/
void picture(u16 x,u16 y,u16 m,u16 n,u16 sum,u8 *s) //写入点阵n*m字符
{ u16 i;
active_window(x,x+m-1,y,y+n-1); //工作窗口
memory_write_position(x,y); //内存写入地址
write_com(0x02); //写数据
for(i=0;i<sum;i++)
write_data(*s++);
}
/******** 建立CGRAM自定义字符程序********/
void CGRAM_set(u8 *s,u8 k)
{ u8 i,j;
write_comdat(0x40,0x00); //图形模式
write_comdat(0x21,0x00);
write_comdat(0x41,0x04);
for(j=0;j<k;j++)
{ write_comdat(0x23,0x00+j); //CGRAM No
write_com(0x02); //写数据
for(i=0;i<16;i++)
write_data(*s++);
}
}
/******** 显示CGRAM自定义字符程序********/
void CGRAM_disp(u16 x,u16 y,u8 k,u8 cod)
{ u8 i;
write_comdat(0x40,0x80); //文本模式
write_comdat(0x21,0xa0);
write_comdat(0x41,0x00);
font_position(x,y); //字体地址
write_com(0x02); //写数据
for(i=0;i<k;i++)
write_data(cod+i);
}
/******** 画直线或矩形程序 ********/
/***** l_s=0:直线; l_s=1:矩形 *****/
void draw_line_square(u16 x1,u16 x2,u16 y1,u16 y2,u8 l_s)
{
write_comdat(0x91,(u8)(y1)); //Horizontal Start Address
write_comdat(0x92,(u8)(y1>>8));
write_comdat(0x93,(u8)(x1)); //Vertical Start Address
write_comdat(0x94,(u8)(x1>>8));
write_comdat(0x95,(u8)(y2)); //Horizontal End Address
write_comdat(0x96,(u8)(y2>>8));
write_comdat(0x97,(u8)(x2)); //Vertical End Address
write_comdat(0x98,(u8)(x2>>8));
if(l_s==0) //画直线
{ write_comdat(0x90,0x80); //Start
write_comdat(0x90,0x00); //End
}
else //画矩形
{ write_comdat(0x90,0x90); //Start,unfill
write_comdat(0x90,0x10); //End
//delay(time/2);
// write_comdat(0x90,0xb0); //Start,fill
// write_comdat(0x90,0x30); //End
}
}
/******** 画圆程序 ********/
void draw_circle(u16 x,u16 y,u16 r)
{
write_comdat(0x99,(u8)(y)); //Horizontal Address
write_comdat(0x9a,(u8)(y>>8));
write_comdat(0x9b,(u8)(x)); //Vertical Address
write_comdat(0x9c,(u8)(x>>8));
write_comdat(0x9d,(u8)(r)); //Center Address
//write_comdat(0x90,0x40); //开始画圆,不填充
// write_comdat(0x90,0x00); //结束画圆
//delay(time/2);
write_comdat(0x90,0x60); //Start,fill
write_comdat(0x90,0x20); //End
}
/******** 画椭圆或圆角矩形程序 ********/
/**** e_cs=0:椭圆; e_cs=1:圆角方形 ****/
void draw_ellipse_csquare(u16 x,u16 y,u16 l,u16 s)
{
write_comdat(0xa1,(u8)(l)); //Long axis
write_comdat(0xa2,(u8)(l>>8));
write_comdat(0xa3,(u8)(s)); //Short axis
write_comdat(0xa4,(u8)(s>>8));
write_comdat(0xa5,(u8)(y)); //Center Horizontal Address
write_comdat(0xa6,(u8)(y>>8));
write_comdat(0xa7,(u8)(x)); //Center Vertical Address
write_comdat(0xa8,(u8)(x>>8));
//write_comdat(0xa0,0x80); //Start,Unfill
//write_comdat(0xa0,0x80); //End
//delay(time/2);
write_comdat(0xa0,0xc0); //Start,fill
write_comdat(0xa0,0x40); //End
}
/******** 画圆角方形程序 ********/
void draw_circle_square(u16 x1,u16 x2,u16 y1,u16 y2,u16 l,u16 s)
{
write_comdat(0x91,(u8)(y1)); //Horizontal Start Address
write_comdat(0x92,(u8)(y1>>8));
write_comdat(0x93,(u8)(x1)); //Vertical Start Address
write_comdat(0x94,(u8)(x1>>8));
write_comdat(0x95,(u8)(y2)); //Horizontal End Address
write_comdat(0x96,(u8)(y2>>8));
write_comdat(0x97,(u8)(x2)); //Vertical End Address
write_comdat(0x98,(u8)(x2>>8));
write_comdat(0xa1,(u8)(l)); //Long axis
write_comdat(0xa2,(u8)(l>>8));
write_comdat(0xa3,(u8)(s)); //Short axis
write_comdat(0xa4,(u8)(s>>8));
write_comdat(0xa0,0xa0); //Start,Unfill
write_comdat(0xa0,0x20); //End
//delay(time/2);
//write_comdat(0xa0,0xe0); //Start,fill
// write_comdat(0xa0,0x60); //End
}
/******** 画三角形程序 ********/
void draw_triangle(u16 x1,u16 y1,u16 x2,u16 y2,u16 x3,u16 y3)
{
write_comdat(0x91,(u8)(y1)); //Point0 Horizontal Address
write_comdat(0x92,(u8)(y1>>8));
write_comdat(0x93,(u8)(x1)); //Point0 Vertical Address
write_comdat(0x94,(u8)(x1>>8));
write_comdat(0x95,(u8)(y2)); //Point1 Horizontal Address
write_comdat(0x96,(u8)(y2>>8));
write_comdat(0x97,(u8)(x2)); //Point1 Vertical Address
write_comdat(0x98,(u8)(x2>>8));
write_comdat(0xa9,(u8)(y3)); //Point2 Horizontal Address
write_comdat(0xaa,(u8)(y3>>8));
write_comdat(0xab,(u8)(x3)); //Point2 Vertical Address
write_comdat(0xac,(u8)(x3>>8));
// write_comdat(0x90,0x81); //Start,Unfill
//write_comdat(0x90,0x01); //End
//delay(time/2);
write_comdat(0x90,0xa1); //Start,fill
write_comdat(0x90,0x21); //End
}
/******** Serial Flash窗口设置程序 ********/
void Flash_window(u16 x,u16 y,u16 height,u16 width)
{
write_comdat(0x30,y); //Horizontal Start Point
write_comdat(0x31,y>>8);
write_comdat(0x32,x); //Vertical Start Point
write_comdat(0x33,x>>8);
write_comdat(0x34,(y+width-1)); //Horizontal End Point
write_comdat(0x35,(y+width-1)>>8);
write_comdat(0x36,(x+height-1)); //Vertical End Point
write_comdat(0x37,(x+height-1)>>8);
}
/********* DMA数据来源地址程序 *********/
void DMA_source(u32 address)
{
write_comdat(0xB0,(u8)(address)); //DMA Source Address
write_comdat(0xB1,(u8)(address>>8));
write_comdat(0xB2,(u8)(address>>16));
write_comdat(0xB3,(u8)(address>>24));
}
/********* DMA连续性传输量程序 *********/
void DMA_transfer_number(u32 number)
{
write_comdat(0xB4,(u8)(number)); //DMA传输量
write_comdat(0xB6,(u8)(number>>8));
write_comdat(0xB8,(u8)(number>>16));
}
/********* DMA区块宽度和高度程序 *********/
void DMA_width_height(u16 height,u16 width,u16 p)
{
write_comdat(0xB4,(u8)(width)); //DMA Width
write_comdat(0xB5,(u8)(width>>8));
write_comdat(0xB6,(u8)(height)); //DMA Height
write_comdat(0xB7,(u8)(height>>8));
write_comdat(0xB8,(u8)(p)); //DMA Source Picture Width
write_comdat(0xB9,(u8)(p>>8));
}
/********* DMA模式启动程序 *********/
void DMA_ON(u8 p)
{
write_comdat(0xBF,p); //01:连续性模式; 03:区块模式
write_com(0xBF);
while(read_data()&0x01);
}
/******** 初始化程序 ********/
void lcdinit()
{
CONTROL_OUTPUT_MODE();
CONTROL_SDO_OUTPUT_MODE();
SDO_L();
RST_L();
delay_nms();
RST_H();
delay_nms();
write_comdat(0x01,0x00); //Display OFF
PLL(); //倍频设置
write_comdat(0x04,0x03); //Pixel Clock:上升沿、1倍系统频率周期
write_comdat(0x05,0x00); //Serial Flash/ROM Configuration
write_comdat(0x06,0x03); //Serial Flash/ROM CLK Setting:1/4系统频率
write_comdat(0x10,0x00); //System Configuration:64K色、16bit接口
write_comdat(0x14,0x27); //LCD Horizontal Display Width:WD/8-1=27H
write_comdat(0x15,0x02); //Horizontal Non-Display Period Fine Tuning Option
write_comdat(0x16,0x03); //Horizontal Non-Display Period
write_comdat(0x17,0x01); //HSYNC Start Position
write_comdat(0x18,0x03); //HSYNC Pulse Width
write_comdat(0x19,0xef); //LCD Vertical Display Height:HD-1=EFH
write_comdat(0x1A,0x00);
write_comdat(0x1B,0x0a); //LCD Vertical Non-Display Period
write_comdat(0x1C,0x00);
write_comdat(0x1D,0x00); //VSYNC Start Position
write_comdat(0x1E,0x00);
write_comdat(0x1F,0x01); //VSYNC Pulse Width
write_comdat(0x20,0x00); //单图层
write_comdat(0x01,0x80); //82H:Display ON 、82H:睡眠模式
clr(); //清屏
}