单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:3865|回复:0

LCD12864液晶显示程序

[复制链接]
ID:82781发表于 2015-6-13 23:30|显示全部楼层
                                                                 /***********************************************************************
工   程:ST7920驱动的12864液晶的3线串行驱动模式
创建日期:2008年11月23日
创 建 人:lqg
邮   箱:lqg_k@163.com     QQ:253076338
引脚定义:RS(CS)=====>  PD3   //PB0
   RW(SID)====> PD4     //PB1
       EN(SCLK)===>   PD6//   PB2
   PSB为硬件控制,接高电平为8位或4位的并行模式,接低电平为串行模式
************************************************************************/
#include< iom16v.h>
#include< macros.h>
#define uchar unsigned char
#define uint unsigned int
#define nop()  NOP()
#define xtal 8
#define Set_CS() DDRD |= (1<<3);PORTD |= (1<<3)
#define Set_SID() DDRD |= (1<<4);PORTD |= (1<<4)
#define Set_SCLK() DDRD |= (1<<6);PORTD |= (1<<6)
#define Clr_CS() DDRD |= (1<<3);PORTD& =~(1<<3)
#define Clr_SID() DDRD |= (1<<4);PORTD& =~(1<<4)
#define Clr_SCLK() DDRD |= (1<<6);PORTD& =~(1<<6)


//====================================================================
//函数声明
void Delay(uint ms);     //延时子程序
void W_1byte(uchar RW, uchar RS, uchar W_data);
void Write_8bits(uint W_bits);
void LCD_Init(void);
/********************************************************************
********************************************************************/
//===================================================================
const uchar mynew1[]={"欢迎你来到我的家"};
const uchar mynew2[]={"Create by:LQG  "};
const uchar mynew3[]={"海内存知己"};
const uchar mynew4[]={"天涯若比邻"};
/********************************************************************
********************************************************************/
void main()
{ uchar i = 0;
DDRD& = ~BIT(7);
PORTD& = ~BIT(7);
DDRC |=BIT(6);
PORTC& = ~BIT(6);
DDRC& = ~BIT(7);
PORTC& = ~BIT(7);
Clr_CS();
Clr_SID();
Clr_SCLK();
LCD_Init();
while(1)
{
   nop();
   nop();
   W_1byte(0,0,0x80);   //显示的地址0x80
   nop();
   for(i=0;mynew1[ i]!='\0';i++)
   {
   W_1byte(0,1,mynew1);
   }
   W_1byte(0,0,0x90);     //显示的地址0x90
   for(i=0;mynew2!='\0';i++)
   {
   W_1byte(0,1,mynew2);
   }
   W_1byte(0,0,0x88);   //显示的地址0x88
   for(i=0;mynew3!='\0';i++)
   {
   W_1byte(0,1,mynew3);
   }
   W_1byte(0,0,0x98+3);     //显示的地址0x98  +3是空格三个字开始写,否则乱码
   for(i=0;mynew4!='\0';i++)
   {
   W_1byte(0,1,mynew4);
   }
   nop();
   for(;;)
   {
   continue;
   }
}
}
/******************************************************************/
void LCD_Init(void)
{
   uchar cmd;
   cmd=0x30;   //功能设置 8位数据,基本指令
W_1byte(0,0,cmd);
Delay(2);
cmd=0x0C;   //显示状态 ON,游标OFF,反白OFF
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x01;   //清除显示
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x02;   //地址归位
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x80;   //设置DDRAM地址
W_1byte(0,0,cmd); //写指令
Delay(2);   //延时
}
/*******************************************************************
函 数 名:W_1byte
入口参数:RW、RS、W_data
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:写一个字节的数据到12864液晶,包括指令和数据
说   明:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
   (一般RW都设为0,即只向液晶写数据,不读数据)
       RS=1,写入的是数据;RS=0,写入的是指令;
   一般模式:RW=0,RS=1;写数据
     RW=0,RS=0;写指令
********************************************************************/
void W_1byte(uchar RW, uchar RS, uchar W_data)
{
uint H_data,L_data,S_ID = 0xf8;  //11111RWRS0
if(RW == 0)
{
   S_ID& =~ 0x04;
}
else     //if(RW==1)
{
   S_ID |= 0X04;
}
if(RS == 0)
{
   S_ID& =~ 0x02;
}
else     //if(RS==1)
{
   S_ID |= 0X02;
}
H_data = W_data;
H_data& = 0xf0;   //屏蔽低4位的数据
L_data = W_data;     //xxxx0000格式
L_data& = 0x0f;   //屏蔽高4位的数据
L_data<< = 4;   //xxxx0000格式
Set_CS();
Write_8bits(S_ID);   //发送S_ID
Write_8bits(H_data); //发送H_data
Write_8bits(L_data); //发送L_data
Clr_CS();
}
/********************************************************************
函 数 名:Write_8bits
入口参数:W_bits
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:负责串行输出8个bit位
说   明:
********************************************************************/
void Write_8bits(uint W_bits)
{
uint i,Temp_data;
for(i=0; i<8; i++)
{
   Temp_data = W_bits;
   Temp_data<< = i;
   if((Temp_data&0x80)==0)  //bit7 is zero
   {
   Clr_SID();
   nop();
   Set_SCLK();
   nop();
   nop();
   Clr_SCLK();
   nop();
   Clr_SID();
   }
   else       //bit7 is one
   {
   Set_SID();
   nop();
   Set_SCLK();
   nop();
   nop();
   Clr_SCLK();
   nop();
   Clr_SID();
   }
}
}
/********************************************************************
函 数 名:Delay
入口参数:ms
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:毫秒级的延时程序,当晶振为1Mhz时,xtal=1;
说   明:
********************************************************************/
void Delay(uint ms)
{
   uint i;
   while(ms--)  
   {
     for(i=1;i<(uint)(xtal*143-2);i++)
       ;
   }  
}
//===================================================================*/

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网