⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd_12864.txt

📁 LCD12864的源码,采用IO口控制,包含了完整的驱动函数,及测试程序.
💻 TXT
字号:
#include<lcd.h>

//复位函数实现
void rst_bit ()
{
RST = 0;   //低电瓶有效

}

//延时子函数
void delay(uchar ms)   ////number=1 延迟100ms
{
int i,j;
while(ms--)
{
  for(i = 0; i <= OUTCOUNT; i++)
  {
  for(j = 0; j <= INCOUNT; j++)
{
}
  }
}
}

//LCD初始化
void interlize(uchar model)
{
RST = 1;
_nop_();
if(model == PAR)
{
PSW = 1;
}
else
{
PSW = 0;
}
}

//检测忙标志
void check_busy()
{
LCD_DATA = 0xff;
RS = 0; //开通写指令
RW = 1;   //读取端口的状态
E = 1;   //开通读写信号
while(LCD_DATA &= 0x80);
E = 0;
}

//写指令子函数
void write_code(uchar com)
{
check_busy();
RS = 0;   //选择写数据
RW = 0; //写操作
LCD_DATA = com;
E = 1;
_nop_();
_nop_();
E = 0;
}

//写数据子函数
void write_data(uchar dat)
{
check_busy();
RS = 1;
RW = 0;
LCD_DATA = dat;
E = 1;
_nop_();
_nop_();
E = 0;
}
//取字符字符子函数
void take_data( uchar *Cdata/*字符串地址*/, uchar icount/*intalized 16*/,uchar com/*intalized lcd address*/,int i/*intalize 0*/)
{
write_code(com);
while(icount--)
{
  if(Cdata != '\0')
{
      write_data(Cdata[i++]);
  delay(1);
      }
  else
  {
  return 
      }
}
}

//清屏子函数
void clc()
{
write_code(0x01);
write_code(0x34);
write_code(0x30);
}

//重新设置屏幕属性换子函数
void flash()
{
write_code(0x08);   //关闭显示
delay(1);
write_code(0x0c);   //开显示关光标 不闪烁
delay(1);
write_code(0x08);   //关显示 
delay(1);
write_code(0x0c);   //关显示 关光标 不闪烁
delay(1);
write_code(0x08);   //关显示
delay(1);
}

//图形显示子函数
void dis_grah()
{

}

//对点阵图形清屏
void dis_dot(uchar send_data1, uchar send_data2, uchar LCD_X, uchar LCD_Y, uchar icount1, uchar icount2, uchar icount3, uchar flag)
{
write_code(0x01);   //清屏
icount3 = 2;
write_code(LCD_X);
//
while(icount3--)
  {
  icount2 = 32;
  flag = 0;
LCD_Y = 0x80;
while(icount2--)
{
    icount1 = 16;
  write_code(0x34);
  write_code(LCD_Y);
  write_code(LCD_X);
  write_code(0x30);
  if(flag == 0)
  {
  while(icount1--)
  {
      write_data(send_data1);
  }
    LCD_Y++;
  flag = 1;
  }
  else
  {
  while(icount1--)
    {
    write_data(send_data2);
    }
    LCD_Y++;
    flag = 0;
  }
}

LCD_X = 0x88;

}
//
write_code(0x36);
write_code(0x30);
}
//画水漆直线子函数
void draw_xline(uchar x, uchar y, uchar line_data, uchar icount)
{
write_code(0x34);
write_code(y);
delay(1);
write_code(x);
write_code(0x30);
while(icount--)
{
    write_data(line_data);
  //delay(1);
}
write_code(0x36);
write_code(0x30);

}

/********************************************
* 功能:整数到字符串的转换
* 参数:
*     str --- 转换串的存储首地址
*     n   --- 待转换的整数
*     base -- 按不同的数制转换,支持2,8,10,16
* 返回:
*     正常返回转换后的字符串首地址
*     错误返回空指针
*********************************************/
/*uchar * Itoa(uchar * str, uint n, uchar base)
{

  char * q = str;
  char * p = str; 

  // 只接受 2、8、10、16 进制数
  if(base != 2 && base != 8 && base != 10 && base != 16)
    return NULL;

  while(n > 0)
  {
    uint tmp = n % base;
    if(tmp >= 10)
        *p++ = tmp - 10 + 'A';
    else
        *p++ = tmp + '0';

    n /= base;
  }
  *p-- = '\0';

  // 按求余转换的定义,需要将余数反序
  while(q < p)
  {
    *p ^= *q ^= *p ^= *q;
    ++q;
    --p;
  }
  
  return str;
}*/

//画点子函数
void show_dot(uchar x, uchar y)
{
uchar line_data;
if (x <= 15)
{
if( x<= 7)
{
  line_data = 0x80>>x;   
  draw_xline(0x80,0x90-y,line_data,1);
}
else
{
  line_data = 0x01<<(15-x);
  draw_xline(0x80,0x90-y,line_data,2);
  draw_xline(0x80,0x90-y,0x00,1);
  //line_data_two(0x00,0x01,0x80,0x90-y,0,0,0);
}
}
else if(x > 15 && x <= 31 )
  {
if(x <= 23)
{   
    line_data = 0x01<<(23-x);   
        draw_xline(0x81,0x80+(8-(23-y)),line_data,1);
}   
else
{
  line_data = 0x01<<(31-x);
  draw_xline(0x81,0x90-(31-y),line_data,2);
  draw_xline(0x81,0x90-(31-y),0x00,1);
}
  }
else if(x > 31 && x <= 47)
{
if(x <= 39)
{
    line_data = 0x01<<(39-x);   
  draw_xline(0x92,0x90+(8-(39-x)),line_data,1);
}
else
{
  line_data = 0x01<<(47-x);
  draw_xline(0x92,0x9f-(47-y-1),line_data,2);
  draw_xline(0x92,0x9f-(47-y-1),0x00,1);
}
}
else if(x > 47 && x <= 63)
{
if(x <= 55)
{
  line_data = 0x01<<(55-x);   
  draw_xline(0x93,0x9f-(8-(55-x)-1),line_data,1);
}
else
{
  line_data = 0x01<<(63-x);
  draw_xline(0x93,0x90+(63-y),line_data,2);
  draw_xline(0x93,0x90+(63-y),0x00,1);
}
}
}

//发送两个8位数画县
/*void line_data_two(uchar send_data1, uchar send_data2, uchar LCD_X, uchar LCD_Y, uchar icount1, uchar icount2, uchar icount3, uchar flag)
{
write_code(0x01);   //清屏
icount3 = 1;
write_code(LCD_X);
//
while(icount3--)
  {
  icount2 = 2;
  flag = 0;
LCD_Y = 0x80;
while(icount2--)
{
    icount1 = 1;
  write_code(0x34);
  write_code(LCD_Y);
  write_code(LCD_X);
  write_code(0x30);
  if(flag == 0)
  {
  while(icount1--)
  {
      write_data(send_data1);
  }
    LCD_Y++;
  flag = 1;
  }
  else
  {
  while(icount1--)
    {
    write_data(send_data2);
    }
    LCD_Y++;
    flag = 0;
  }
}

LCD_X = 0x88;

}
//
write_code(0x36);
write_code(0x30);
}*/
//////////////////////////////////////////////////////////////////////////////////////////////LCD.C结束
/*******************************************************************************
* 标题:1286 液晶驱动(控制器TS7920)
* 文件:lcd_12864_driver.h
* 日期:2005-8-26
* 版本:v.0
* 作者:辛万江
/********************************************************************************/
// 描述:
// 采用并口方式控制数据输入输出
/********************************************************************************/
#ifndef LCD_12864_DRIVER

#include<reg51.h>
#include<intrins.h>
#include<STDIO.H>
#include<STRING.H>
#include <STDLIB.H>
#include <CTYPE.H>
#include <FLOAT.H>
//控制引脚定义
sbit RS = P2^0;   //数据/指令选择
sbit RW = P2^1;   //读写选择
sbit E = P2^2;   //读写适鼓?
sbit PSB = P2^3;   //选择数据传输模式并口或串口高并低串
sbit RST = P2^5;

//数据线
#define LCD_DATA P0
//
#define uchar unsigned char
#define uint unsigned int
#define true 1   //忙标志
#define false 0   //闲标志
#define INCOUNT 10   //延时函数参数设置(注意所用的晶振?德剩?
#define OUTCOUNT 50       // 180
#define PAR 1   //并口模式
#define SER 0   //串口模式
/////////////////////////////////////////////////////////////////函数定义
//复位函数
void rst_bit();

//初始化函数
void interlize(uchar model);

//写指令子函数
void write_code(uchar com);

//写数据子函数
void write_data(uchar dat);
//忙判断
void check_busy ();

//延时子函数
void delay(uchar ms);

//清屏子函数
void clc();

//显示图形子函数
void dis_grah();

//显示数据子函数
void dis_char_mumber();

//取数据子函数
void take_data(uchar *Cdata/*字符串地址*/, uchar/**/icount, uchar com/**/,int i);

//重新设置屏幕属性换子函数
void flash();

//显示点阵子函数
void dis_dot(uchar send_data1, uchar send_data2, uchar LCD_X, uchar LCD_Y, uchar icount1, uchar icount2, uchar icount3, uchar flag);

//调用半脚字符显示
void show_half_char();

//调用汉字字库显示
void show_chinse();

//画横直线子函数
void draw_xline(uchar x, uchar y, uchar line_data, uchar icount);

//整型转蛔址?
uchar * Itoa(uchar * str, uint n, uchar base);

//画点子函数
void show_dot(uchar x, uchar y);

//发送两个8位数画县
/*void line_data_two(uchar send_data1, uchar send_data2, uchar LCD_X, uchar LCD_Y, uchar icount1, uchar icount2, uchar flag);*/

#endif
////////////////////////////////////////////////////////////////////////////////////LCD.H结束
///下面是我的测试程序
/*******************************************************************************
* 标题:1286 液晶驱动(控制器TS7920)
* 文件:lcd_12864_driver.h
* 日期:2005-8-26
* 版本:v.0
* 作者:辛万江
/********************************************************************************/
// 描述:
// 采用并口方式控制数据输入输出
/********************************************************************************/
#include<lcd.c>
/////////////////////////////////////////////////////////////////////////////////

uchar TAB[12] = "显示结果:";
uchar code TAB2[]= "X=       ";
uchar code TAB3[]= "   121111 ";
uchar code TAB4[]= "犯得上法121 ";
uchar buffer[3];
uchar buffer1[5];
float i = 16.89;
int j = 0;
void *pf;
uchar *fd;
//int i = 16;
uchar x = 0;
uint count;
/////////////////////////////////////////////////////////////////////////////////
main()
{
///////////////////////////////////////////////////准备工作设定显示方式
rst_bit();
delay(2);   //延时两微妙
interlize(1);   //采用并口模式
write_code(0x34); //34h扩充功能设定
write_code(0x30); //30h基本操作指令
write_code(0x01); //01h清楚显示指令
write_code(0x06); //06h指定在资料读仁? ,光标的移动方向
write_code(0x0c); //0ch开显示,关光标,不闪烁
////////////////////////////////////////////////////准备工作完毕
//显示汉字和字符
clc();
//点阵清屏
dis_dot(0x00,0x00,0x80,0x80,0,0,0,0);
//Itoa(temp, i, 10);
//strcat(TAB1,temp);
//char buffer[3];
//将FLOAT转化为字符
fd = buffer1;
pf = &i;
for(j = 0 j < 4; j++)
{
*(fd+j) = *((char *)pf+j);
}
//sprintf(buffer, "%f", i);
buffer1[4] = '\0';
strcat(TAB,buffer1);
take_data(TAB,16,0x88,0);
//sprintf(TAB1 + 4, "%d",i);
//TAB1[6]='\0'
for(x = 0; x < 64; x++)
show_dot(x,x);
draw_xline(0x90,0x90,0xff,16);
//draw_xline(0x9f,0x9f,0xff,16);
//draw_xline(0x80,0x80,0xff,16);
//draw_xline(0x9f-18,0x80,0xff,16);
while(1);
}
//////////////////////////////////////////////////////////////////////////main结束

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -