LCD12864 LCD display program

Publisher:数据探险家Latest update time:2019-11-19 Source: 51heiKeywords:LCD12864 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/***********************************************************************

Engineering: 3-wire serial drive mode of 12864 LCD driven by ST7920

Pin definition: RS(CS)=====> PD3 //PB0

    RW(SID)====> PD4     //PB1

          EN(SCLK)===> PD6// PB2

    PSB is hardware controlled. A high level is for 8-bit or 4-bit parallel mode, and a low level is for serial mode.

************************************************************************/

#include

#include

#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)



//====================================================================

// Function declaration

void Delay(uint ms); //Delay subroutine

void W_1byte(volat RW, volat RS, volat W_data);

void Write_8bits(uint W_bits);

void LCD_Init(void);

/********************************************************************

********************************************************************/ 

//===================================================================

const uchar mynew1[]={"Welcome to my home"};

const uchar mynew2[]={"Create by:LQG  "};

const uchar mynew3[]={"There are many friends in the world"};

const uchar mynew4[]={"The world is so small"};

/********************************************************************

********************************************************************/

void main()

{ fly 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); //Displayed address 0x80

  nop();

  for(i=0;mynew1[ i]!='';i++)

  {

   W_1byte(0,1,mynew1);

  }

  W_1byte(0,0,0x90); //Displayed address 0x90

  for(i=0;mynew2!='';i++)

  {

   W_1byte(0,1,mynew2); 

  }

  W_1byte(0,0,0x88); //Displayed address 0x88

  for(i=0;mynew3!='';i++)

  {

   W_1byte(0,1,mynew3);

  }

  W_1byte(0,0,0x98+3); //The displayed address 0x98 +3 is a space and starts with three characters, otherwise it will be garbled.

  for(i=0;mynew4!='';i++)

  {

   W_1byte(0,1,mynew4);

  }

  nop();

  for(;;)

  {

    continue;

  } 

}

}

/******************************************************************/

void LCD_Init(void)

{

  fly cmd;

  cmd=0x30; //Function setting 8-bit data, basic instructions

W_1byte(0,0,cmd);

Delay(2);

cmd=0x0C; //Display status ON, cursor OFF, highlight OFF

W_1byte(0,0,cmd); //write command

Delay(2);

cmd=0x01; //Clear display

W_1byte(0,0,cmd); //write command

Delay(2);

cmd=0x02; //Address return

W_1byte(0,0,cmd); //write command

Delay(2);

cmd=0x80; //Set DDRAM address

W_1byte(0,0,cmd); //write command

Delay(2); //Delay

}

/*******************************************************************

Function name: W_1byte

Entry parameters: RW, RS, W_data

Export parameters: None

Date of establishment: March 3, 2007

Modified Date:

Function: Write one byte of data to the 12864 LCD, including instructions and data

Description: RW=1, read data from LCD to MCU; RW=0, write data to LCD;

   (Generally, RW is set to 0, which means that data is only written to the LCD, not read)

          RS=1, data is written; RS=0, instructions are written;

    Normal mode: RW=0, RS=1; write data

       RW=0, RS=0; write instruction

********************************************************************/

void W_1byte(float RW, float RS, float 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; //Shield the lower 4 bits of data

L_data = W_data; //xxxx0000 format

L_data &= 0x0f; //Shield the high 4 bits of data

L_data <<= 4; //xxxx0000 format

Set_CS();

Write_8bits(S_ID); //Send S_ID

Write_8bits(H_data); //Send H_data

Write_8bits(L_data); //Send L_data

Clr_CS(); 

}

/********************************************************************

Function name: Write_8bits

Input parameter: W_bits

Export parameters: None

Date of establishment: March 3, 2007

Modified Date:

Function: responsible for serial output of 8 bits

illustrate:

********************************************************************/

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();

  } 

}

}

/********************************************************************

Function name: Delay

Entry parameter: ms

Export parameters: None

Date of establishment: March 3, 2007

Modified Date:

Function: millisecond delay program, when the crystal oscillator is 1Mhz, xtal=1;

illustrate:

********************************************************************/

void Delay(uint ms) 

    uint i; 

    while(ms--)    

   { 

     for(i=1;i<(uint)(xtal*143-2);i++) 

         ; 

   }   

}

//===================================================================*/


Keywords:LCD12864 Reference address:LCD12864 LCD display program

Previous article:8-color bitmap acquisition serial port based on AVR (experimental)
Next article:AVR microcontroller controlled relay

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号