找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4196|回復(fù): 0
收起左側(cè)

LCD12864液晶顯示程序

[復(fù)制鏈接]
ID:82781 發(fā)表于 2015-6-13 23:30 | 顯示全部樓層 |閱讀模式
                                                                                                 /***********************************************************************
工    程:ST7920驅(qū)動(dòng)的12864液晶的3線串行驅(qū)動(dòng)模式
創(chuàng)建日期:2008年11月23日
創(chuàng) 建 人: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)


//====================================================================
//函數(shù)聲明
void Delay(uint ms);      //延時(shí)子程序
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[]={"海內(nèi)存知己"};
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是空格三個(gè)字開始寫,否則亂碼
  for(i=0;mynew4!='\0';i++)
  {
   W_1byte(0,1,mynew4);
  }
  nop();
  for(;;)
  {
    continue;
  }
}
}
/******************************************************************/
void LCD_Init(void)
{
  uchar cmd;
  cmd=0x30;   //功能設(shè)置 8位數(shù)據(jù),基本指令
W_1byte(0,0,cmd);
Delay(2);
cmd=0x0C;   //顯示狀態(tài) ON,游標(biāo)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;   //設(shè)置DDRAM地址
W_1byte(0,0,cmd); //寫指令
Delay(2);   //延時(shí)
}
/*******************************************************************
函 數(shù) 名:W_1byte
入口參數(shù):RW、RS、W_data
出口參數(shù):無
建立日期:2007年3月3日
修改日期:
函數(shù)作用:寫一個(gè)字節(jié)的數(shù)據(jù)到12864液晶,包括指令和數(shù)據(jù)
說    明:RW=1,從液晶讀數(shù)據(jù)到MCU;RW=0,寫一個(gè)數(shù)據(jù)到液晶;
   (一般RW都設(shè)為0,即只向液晶寫數(shù)據(jù),不讀數(shù)據(jù))
          RS=1,寫入的是數(shù)據(jù);RS=0,寫入的是指令;
    一般模式:RW=0,RS=1;寫數(shù)據(jù)
       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位的數(shù)據(jù)
L_data = W_data;     //xxxx0000格式
L_data &= 0x0f;   //屏蔽高4位的數(shù)據(jù)
L_data <<= 4;   //xxxx0000格式
Set_CS();
Write_8bits(S_ID);   //發(fā)送S_ID
Write_8bits(H_data); //發(fā)送H_data
Write_8bits(L_data); //發(fā)送L_data
Clr_CS();
}
/********************************************************************
函 數(shù) 名:Write_8bits
入口參數(shù):W_bits
出口參數(shù):無
建立日期:2007年3月3日
修改日期:
函數(shù)作用:負(fù)責(zé)串行輸出8個(gè)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();
  }
}
}
/********************************************************************
函 數(shù) 名:Delay
入口參數(shù):ms
出口參數(shù):無
建立日期:2007年3月3日
修改日期:
函數(shù)作用:毫秒級(jí)的延時(shí)程序,當(dāng)晶振為1Mhz時(shí),xtal=1;
說    明:
********************************************************************/
void Delay(uint ms)
{
    uint i;
    while(ms--)   
   {
     for(i=1;i<(uint)(xtal*143-2);i++)
         ;
   }   
}
//===================================================================*/
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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