Program to drive ST7565P LCD screen based on SPI of ATMEGA8515

Publisher:CelestialGardenLatest update time:2020-03-30 Source: eefocusKeywords:ATMEGA8515 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is the source code for driving 12864 LCD by SPI. The control chip of the LCD is ST7565P. After testing, the SPI clock can reach the 2nd frequency of the oscillation frequency. It is much faster than using ordinary IO port to simulate serial. . . 

The source code is posted below with detailed comments. . . . 

This program is simple and practical and can be used as a simple reference. . . . 

Compilation environment: GCC+AVR STUDIO 

Microcontroller: ATMEGA 8515 

Crystal: 3.6864M 

#include //Include header file 

#include  

#define COMMAND 1 

#define DATA 0 

///////////////////////////////////////// 

//Data line definition: 

// SI==========PB5 // 

// A0==========PC0 

// CS==========PC2 

//RES=========PC1 

// SCL=========PB7 

// 

///////////////////////////////////////// 

unsigned char gushi[8][112]PROGMEM= 

{0x10,0x10,0x92,0x92,0x92,0xF2,0x9E,0x92,0x92,0x92,0x92,0x92,0xFE,0x10,0x10,0x00, // You asked when he would return, but there was no date 

0x00,0x00,0xF8,0x01,0x06,0xE0,0x22,0x22,0x22,0x22,0xE2,0x02,0x02,0xFE,0x00,0x00, 

0x00,0xF8,0x00,0x00,0xFF,0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0xFC,0x00,0x00, 

0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00, 

0x40,0x40,0x48,0x48,0x48,0x48,0xC8,0xFF,0x48,0x48,0x48,0x48,0x48,0x40,0x40,0x00, 

0x00,0x04,0x84,0x44,0xE4,0x34,0x2C,0x27,0x24,0x24,0x24,0xE4,0x04,0x04,0x04,0x00, 

0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00}, 

{0x40,0x20,0x10,0x0C,0xFE,0x45,0x44,0x44,0x44,0x44,0x44,0x44,0xFD,0x00,0x00,0x00, 

0x00,0x00,0xFF,0x00,0x00,0x03,0x02,0x02,0x02,0x02,0x03,0x40,0x80,0x7F,0x00,0x00, 

0x00,0x87,0x40,0x30,0x0F,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x00,0x00, 

0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00, 

0x20,0x20,0x10,0x10,0x08,0x06,0x01,0xFF,0x01,0x02,0x04,0x08,0x18,0x30,0x10,0x00, 

0x02,0x01,0x00,0x00,0xFF,0x09,0x09,0x09,0x29,0x49,0xC9,0x7F,0x00,0x00,0x00,0x00, 

0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00}, 

{0x00,0x00,0xFE,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00, //The night rain in Bashan makes the pond swell 

0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00, 

0x04,0x04,0x84,0xC4,0x3C,0x14,0x85,0x66,0xBC,0x24,0x24,0x24,0xE4,0x24,0x04,0x00, 

0x02,0x02,0xF2,0x32,0x52,0x92,0x12,0xFE,0x32,0x52,0x92,0x12,0xF2,0x02,0x02,0x00, 

0x10,0x61,0x86,0x60,0xF2,0x12,0x12,0x1E,0x80,0xFF,0xA0,0x90,0x88,0x86,0x80,0x00, 

0x24,0x24,0xE4,0xFC,0xA2,0x22,0x22,0x80,0x70,0x00,0xFF,0x40,0x20,0x18,0x00,0x00, 

0x10,0x60,0x02,0x8C,0x60,0x80,0xF8,0x40,0x40,0xFE,0x20,0x20,0x10,0xF0,0x00,0x00}, 

{0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x78,0x20,0x00, 

0x00,0x20,0x7F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00, 

0x02,0x01,0x00,0xFF,0x00,0x82,0x41,0x22,0x14,0x09,0x14,0x23,0x40,0xC0,0x40,0x00, 

0x00,0x00,0xFF,0x01,0x02,0x04,0x00,0x7F,0x01,0x02,0x44,0x80,0x7F,0x00,0x00,0x00, 

0x04,0xFC,0x03,0x00,0x01,0x41,0x81,0x7F,0x00,0xFF,0x42,0x0C,0x30,0x60,0x20,0x00, 

0x02,0x01,0x00,0xFF,0x00,0x83,0x40,0x20,0x18,0x07,0x01,0x06,0x18,0xE0,0x40,0x00, 

0x08,0x08,0x7C,0x03,0x00,0x00,0x3F,0x40,0x40,0x5F,0x40,0x42,0x44,0x43,0x70,0x00}, 

{0x80,0x40,0xF0,0x1C,0x07,0xE2,0x24,0x24,0x24,0xE4,0x04,0x04,0xFC,0x04,0x04,0x00, //When will we cut the candlelight in the west window together? 

0x00,0x00,0x40,0x42,0x5C,0x48,0x40,0x40,0x7F,0x40,0x50,0x4E,0x44,0xC0,0x00,0x00, 

0x00,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x00,0x00, 

0x00,0x04,0xF4,0x54,0x55,0x56,0x54,0xF4,0x04,0x04,0xE6,0x05,0x04,0xF4,0x04,0x00, 

0x02,0xF2,0x12,0x12,0x12,0xFE,0x12,0x12,0x12,0x12,0xFE,0x12,0x12,0x12,0x12,0xF2,0x02,0x00, 

0x00,0x4C,0x44,0xE4,0x54,0x44,0x64,0xD5,0x46,0x44,0x4C,0x54,0xD4,0x24,0x2C,0x00, 

0x80,0x60,0x00,0xFF,0x20,0x18,0xF0,0x10,0x10,0xFF,0x12,0x10,0xF0,0x10,0x00,0x00}, 

{0x00,0x00,0x7F,0x00,0x00,0x07,0x02,0x02,0x02,0x07,0x20,0x40,0x3F,0x00,0x00,0x00,  

0x00,0x00,0x20,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x7F,0x00,0x00, 

0x02,0x02,0x82,0x42,0x32,0x1F,0x0A,0x02,0x02,0x06,0x0B,0x32,0xE2,0x42,0x02,0x00, 

0x00,0x08,0x8F,0x89,0x49,0x2B,0x1D,0x0B,0x08,0x08,0x08,0x4A,0x8C,0x7B,0x00,0x00, 

0x00,0x7F,0x28,0x24,0x22,0x21,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x7F,0x00,0x00, 

0x00,0x00,0x00,0xFF,0x44,0x62,0x55,0x49,0x55,0x43,0x41,0x40,0xFF,0x00,0x00,0x00, 

0x40,0x20,0x18,0x07,0x04,0x28,0x63,0x21,0x21,0x3F,0x11,0x15,0x1B,0x30,0x00,0x00}, 

{0x80,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00, //Talking about the night rain in Bashan 

0x40,0x41,0x4E,0xC4,0x00,0x20,0x24,0x24,0x24,0x24,0xFC,0x22,0x22,0x22,0x20,0x00, 

0x00,0x00,0xFE,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00, 

0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00, 

0x04,0x04,0x84,0xC4,0x3C,0x14,0x85,0x66,0xBC,0x24,0x24,0x24,0xE4,0x24,0x04,0x00, 

0x02,0x02,0xF2,0x32,0x52,0x92,0x12,0xFE,0x32,0x52,0x92,0x12,0xF2,0x02,0x02,0x00, 

0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00}, 

{0x00,0x10,0x3C,0x13,0x10,0x08,0x0A,0x1C,0x08,0xFF,0x00,0x08,0x10,0x0F,0x00,0x00, 

0x00,0x00,0x00,0x7F,0x20,0x10,0x00,0x7E,0x22,0x22,0x23,0x22,0x22,0x7E,0x00,0x00, 

0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x78,0x20,0x00, 

0x00,0x20,0x7F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00, 

0x02,0x01,0x00,0xFF,0x00,0x82,0x41,0x22,0x14,0x09,0x14,0x23,0x40,0xC0,0x40,0x00, 

0x00,0x00,0xFF,0x01,0x02,0x04,0x00,0x7F,0x01,0x02,0x44,0x80,0x7F,0x00,0x00,0x00, 

0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00}, 

}; 

//////////////////////////////// 

//Send a byte to ST7565P and wait for the sending to complete 

//Entry: Bytes to be sent 

//Return value: None 

//////////////////////////////// 

void SPIByte(unsigned char byte)   

     SPDR =byte; //write data 

    while(!(SPSR &(1<

//////////////////////////////////////////////// 

//Function to send a byte to ST7565P 

//Entry parameters: select ,data 

//select: Determine whether the data or command is sent to ST7565P based on this value 

//data: a byte to be sent to ST7565 

//Return value: None 

//////////////////////////////////////////////// 

void LCD_SPISend(unsigned char select,unsigned chardata) 

     PORTC&= ~(1<<2); // CS=0 chip enable signal 

    if(select == COMMAND) //The command is transmitted 

      {     

         PORTC &=~(1<<0); //According to the ST7565P data sheet, A0 should be low when transmitting commands 

         SPIByte(data);  

         PORTC |=1<<0; // A0=1   

     } 

    else 

    { 

        SPIByte(data); 

     } 

     PORTC|= 1<<2; //CS=recovery disabled 

//////////////////////////////////////////// 

//Write coordinate function 

//Entry parameters: xy 

//y: page address, this LCD display area is divided into 9 pages, the value range of y is 0~8 

//x: column address, value range: 0~127 

//Return value: None 

//////////////////////////////////////////// 

void LCD_XYSet(unsigned char x,unsigned char y) 

    LCD_SPISend(COMMAND,0xB0|y); //The value of y is 0~8 

    LCD_SPISend(COMMAND,0x10|(x>>4)); 

    LCD_SPISend(COMMAND,0x00|(x&0x0F)); 

///////////////////////////////////// 

//Delay function 

//Entry: ms When the delay is 100 milliseconds, the value of ms can be 100 

//Return value: None 

///////////////////////////////////// 

void DelayMs(volatile unsigned int ms) 

    volatile unsigned inti; 

    volatile unsigned intj; 

   for(i=0;i    { 

        j=280; 

       while(j--); 

     } 

////////////////////////////////////////////////////// 

//Function to initialize SPI and LCD screen 

//Entrance: None 

//Return value: None 

///////////////////////////////////////////////////// 

void LCD_Init(void) 

{   

     SPCR = 0x5C;  

     SPSR|= 1<//SPI function setting explanation: Enable SPI; use host mode; SPI transmission mode is mode 3; the clock frequency is two-frequency of the system clock, that is, //3.6864/2M 

    DelayMs(100); 

    SPIByte(0xFF); //It can be sent or not, meaningless 

     PORTC&= ~(1<<1); //Pull the reset pin low 

    DelayMs(5); //Delay 5ms, wait for the power supply to stabilize 

     PORTC|= 1<<1; //Pull the reset pin high 

    DelayMs(5); 

    LCD_SPISend(COMMAND,0xAE); //Turn off the LCD display 

    LCD_SPISend(COMMAND,0xA3); //Set bias ratio 

    LCD_SPISend(COMMAND,0xA0); //Set SEG output direction 

    LCD_SPISend(COMMAND,0xC8); //Set the common output scanning direction 

    LCD_SPISend(COMMAND,0x25); //Adjust the (1 +Rb/Ra) ratio value. Adjusting this value has a great impact. 

    LCD_SPISend(COMMAND,0x81); //Start setting the α command 

    LCD_SPISend(COMMAND,0x28); //Set the value of α. The larger the value, the stronger the contrast. Adjusting this value has little effect. It is a fine-tuning.

    DelayMs(5); 

    LCD_SPISend(COMMAND,0x2F); //Turn on related voltages 

    DelayMs(50);             

    LCD_SPISend(COMMAND,0xAF); //Display on 

    LCD_SPISend(COMMAND,0xF8);        

    LCD_SPISend(COMMAND,0x01); //These two commands are used to set the boost ratio 

    DelayMs(500); //I don't know if this delay can be omitted, but for safety reasons, it is better to keep it for the first debugging.

//////////////////////////////////////// 

//Clear screen function 

//Entrance: None 

//Return value: None 

/////////////////////////////////////// 

void Clear_Display(void) 

    unsigned char i; 

    unsigned char j; 

    for(i=0;i<8;i++) 

    { 

           LCD_XYSet(0,i); 

           for(j=0;j<128;j++) 

            { 

              LCD_SPISend(DATA,0); //Transmit data 

            } 

    } 

//////////////////////////////// 

// Display a poem by Li Shangyin: 

// I have no idea when I will return 

// The night rain in Bashan swells the autumn pond 

// When will we cut the candlelight in the west window together? 

// Let’s talk about the night rain in Bashan 

//Entrance: None 

//Return value: None 

//////////////////////////////// 

void Gushi_Display(void) 

[1] [2]
Keywords:ATMEGA8515 Reference address:Program to drive ST7565P LCD screen based on SPI of ATMEGA8515

Previous article:Converting ATmega128 to Arduino
Next article:Use Keil C software and AVR Atmega series downloader

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号