Homemade 51 MCU common header files (st7920 serial mode)

Publisher:WhisperingWindLatest update time:2016-07-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*------------------------------------------------ --------------------------

ST7920.H

The user function is C51.
Copyright (c) 1988-2004 Keil Elektronik GmbH sum zhaojun
All rights reserved.
---------------------------- --------------------------------------------------*/
// Serial mode
#ifndef __ST7920_H__
#define __ST7920_H__

#define uint unsigned int
#define uchar unsigned char

//Pin definition
#define LCD_CS P3_2 // Chip select high level is valid. When using a single-chip LCD, the high level can be fixed.
#define LCD_SID P3_3 // Data
#define LCD_CLK P3_4 // Clock
#define LCD_PSB P3_5 // Low level indicates that it is driven by the serial port. The low level can be fixed.
#define LCD_RST P3_6 // LCD reset. The LCD module has its own reset circuit. It can be left unconnected.
//#define LCD_BACK P2_6 // LCD backlight control

/******************************************************** 
 Function name: void Delay_LCD(uint Number)
 Function: LCD delayDescription
 : If the BF flag is not checked before sending an instruction, a longer delay must be made between the previous instruction and this instructionEntry
 parameter: NumberReturn
 value: None 
 ****************************************************/
void Delay_LCD(uint Number) 
{
  uint i,j;

  for (i=0; i   {
      for(j=0; j<10; j++)
        {
   ;
  }
 }
}

/******************************************************** 
 Function name: void Send_byte(uchar Byte) 
 Function: Send a byte
 Input parameter: Byte
 Return value: None 
 ********************************************************/
void Send_byte(uchar Byte) 
{
 uchar i;

  for (i=0; i<8; i++)
    {
     LCD_SID = Byte&0x80; // Take out the highest bit and write serial data
     LCD_CLK = 1; // Serial synchronous clock signal
     LCD_CLK = 0;
    Byte <<= 1; // Shift left
   }  
}
/********************************************************* 
 Function name: void WriteCommandLCM()
 Function: Write command
 entry to LCMParameter: WCLCMReturn
 value: None  
 ****************************************************/
void WriteCommandLCM(uchar WCLCM)
{
   uchar Start_data,Hdata,Ldata;

 Start_data = 0xf8; // write instruction 11111000
  
   Hdata = WCLCM&0xf0; // take the upper four bits DDDD0000
   Ldata = (WCLCM << 4) & 0xf0; // take the lower four bits 0000DDDD

   Send_byte(Start_data); // Send the first byte of the start signal - format: 1111ABC
   Delay_LCD(5); // Delay is required

   Send_byte(Hdata); // Send the upper four bits of the second byte - format: DDDD0000
   Delay_LCD(1); // Delay is required

   Send_byte(Ldata); // Send the third byte of the lower four bits - format: 0000DDDD
   Delay_LCD(1); // Delay is required
}
/******************************************************** 
 Function name: void WriteDataLCM()
 Function: Write data to LCM1602Description
 : Write the data in the parameter WDLCM to LCMEntry
 parameter: WDLCMReturn 
 value: None  
 ****************************************************/
void WriteDataLCM(uchar WDLCM)
{
   uchar Start_data,Hdata,Ldata;

 Start_data = 0xfa; // write data 11111010 

   Hdata = WDLCM & 0xf0; // Take the upper four bits DDDD0000
   Ldata = (WDLCM << 4) & 0xf0; // Take the lower four bits 0000DDDD

   Send_byte(Start_data); // Send the first byte of the start signal - format: 1111ABC
   Delay_LCD(5); // Delay is required

   Send_byte(Hdata); // Send the upper four bits of the second byte - format: DDDD0000
   Delay_LCD(1); // Delay is required

   Send_byte(Ldata); // Send the lower four bits 3rd byte - format: 0000DDDD
   Delay_LCD(1); // Delay is required
}

/******************************************************** 
 Function name: void Lcdinit(void)
 Function: LCD initialization
 entry Parameter: NoneReturn
 value: None 
 ********************************************************/
void Lcdinit(void)  
{
   Delay_LCD(10); // Start waiting, wait for LCM to enter working status
   LCD_PSB = 0; // Serial port drive mode
   LCD_RST = 0;

 Delay_LCD(1); 
 LCD_RST = 1; // Reset LCD
   LCD_CS = 1; // Chip select

   WriteCommandLCM(0x30); // 8-bit interface, basic command set
   WriteCommandLCM(0x0c); // Display on, cursor off, reverse white off
   WriteCommandLCM(0x01); // Clear the screen, reset the DDRAM address counter to zero  
}

/******************************************************** 
 Function name: void DisplayListChar()
 Function: write string to the pointed address
 Entry parameter: x- horizontal coordinate, y- vertical coordinate, s- string
 Return value: None  
 ****************************************************/
void DisplayListChar(uchar x, uchar y, uchar code *s)
{
 uchar add; // display address

 switch (y) // Display address count
 {
  case 0: add = x + 0x80; break; // Address of the first line
  case 1: add = x + 0x90; break; // Address of the second line  
  case 2: add = x + 0x88; break; // Address of the third line
  case 3: add = x + 0x98; break; // Address of the fourth line
  default: break; 
 }

 WriteCommandLCM(0x30); // 8-bit interface, basic command set
 WriteCommandLCM(add); // write address

    while (*s > 0) // Write string
    {
     WriteDataLCM(*s);
        s++;
        Delay_LCD(50);
    }
}

/******************************************************** 
 Function name: void DisplayPicture(uint code *img)
 Function: Display graphics on LCDDescription
 : When displaying graphics on LCD, due to the particularity of ST7920, two screens should be usedControl 
 entry parameter: *imgReturn
 value: None 
 ********************************************************/
//Graphics modevoid
DisplayPicture(uint code *img) 

    uchar Line,Row; // Line is the row address; Row is the column addressuint
    regist = 0; // Graphics address register

 WriteCommandLCM(0x36); // Graphics mode, extended command
    // -------------- Upper half screen----------------
   for (Line=0; Line<32; Line++)     
    { 
       WriteCommandLCM(0x80 + Line); // Write row address
       WriteCommandLCM(0x80); // Write column

       for (Row=0; Row<16; Row++)
  {
       WriteDataLCM(img[regist++]); // Write image data
  }
    }
    // --------------- Lower half screen---------------
   regist=512; // Lower half screen starting data

   for (Line=0; Line<32; Line++)  
    { 
       WriteCommandLCM(0x80 + Line); // Write row address
    WriteCommandLCM(0x88); // Write column

       for (Row=0; Row<16; Row++)
  {
        WriteDataLCM(img[regist++]); // Write image data
  }
    }

 WriteCommandLCM(0x30); // Basic instructions
}

/******************************************************** 
 Function name: void main(void)
 Function: main function
 entry parameter: noneReturn
 value: none 
 ****************************************************/
void main(void)
{

   Lcdinit(); // Initialize LCD

   while(1)
   {

  DisplayListChar(0,0,"zhaojun");
   }

}

#endif

Reference address:Homemade 51 MCU common header files (st7920 serial mode)

Previous article:Homemade 51 MCU common header files (DS18B20)
Next article:Homemade 51 MCU common header files (st7920 parallel mode)

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号