Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ST7565R LCD Controller Low-level driver

This is a low level driver for the ST7565R LCD controller.

It provides basic functions for initializing and writing to the LCD controller. In addition to hardware control and use of the LCD controller internal functions .

Before writing data to the display call st7565r_init() which will set up the physical interface and the LCD. A file named conf_st7565r.h is needed to define which interface to use. For more information see the Interface selection section. In addition one also need to define the pins ST7565R_A0_PIN, ST7565R_CS_PIN and ST7565R_RESET_PIN and the display ST7565R_CLOCK_SPEED.

Warning
This driver is not reentrant and can not be used in interrupt\ service routines without extra care.

As a safety feature one also need to set the max and min contrast levels that the st7565r_set_contrast using the defines ST7565R_DISPLAY_CONTRAST_MAX and ST7565R_DISPLAY_CONTRAST_MIN to protect the display from too high voltage, please see the st7565r_set_contrast function for more details.

An example conf_st7565r.h file could look like

// interface selection
#define ST7565R_USART_SPI_INTERFACE
#define ST7565R_USART_SPI &USARTD0
// minimum clock period is 50ns@3.3V -> max frequency is 20MHz
#define ST7565R_CLOCK_SPEED 1000000
#define ST7565R_DISPLAY_CONTRAST_MAX 40
#define ST7565R_DISPLAY_CONTRAST_MIN 30
#define ST7565R_A0_PIN NHD_C12832A1Z_REGISTER_SELECT
#define ST7565R_CS_PIN NHD_C12832A1Z_CSN
#define ST7565R_RESET_PIN NHD_C12832A1Z_RESETN

Dependencies

This driver depends on the following modules:

Macros

#define ST7565R_SERIAL_INTERFACE
 

Command defines

#define ST7565R_CMD_DISPLAY_ON   0xAF
 
#define ST7565R_CMD_DISPLAY_OFF   0xAE
 
#define ST7565R_CMD_START_LINE_SET(line)   (0x40 | (line))
 
#define ST7565R_CMD_PAGE_ADDRESS_SET(page)   (0xB0 | (page))
 
#define ST7565R_CMD_COLUMN_ADDRESS_SET_MSB(column)   (0x10 | (column))
 
#define ST7565R_CMD_COLUMN_ADDRESS_SET_LSB(column)   (0x00 | (column))
 
#define ST7565R_CMD_ADC_NORMAL   0xA0
 
#define ST7565R_CMD_ADC_REVERSE   0xA1
 
#define ST7565R_CMD_DISPLAY_NORMAL   0xA6
 
#define ST7565R_CMD_DISPLAY_REVERSE   0xA7
 
#define ST7565R_CMD_DISPLAY_ALL_POINTS_OFF   0xA4
 
#define ST7565R_CMD_DISPLAY_ALL_POINTS_ON   0xA5
 
#define ST7565R_CMD_LCD_BIAS_1_DIV_5_DUTY33   0xA1
 
#define ST7565R_CMD_LCD_BIAS_1_DIV_6_DUTY33   0xA2
 
#define ST7565R_CMD_NORMAL_SCAN_DIRECTION   0xC0
 
#define ST7565R_CMD_REVERSE_SCAN_DIRECTION   0xC8
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_0   0x20
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_1   0x21
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_2   0x22
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_3   0x23
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_4   0x24
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_5   0x25
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_6   0x26
 
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_7   0x27
 
#define ST7565R_CMD_POWER_CTRL_ALL_ON   0x2F
 
#define ST7565R_CMD_SLEEP_MODE   0xAC
 
#define ST7565R_CMD_NORMAL_MODE   0xAD
 
#define ST7565R_CMD_RESET   0xE2
 
#define ST7565R_CMD_NOP   0xE3
 
#define ST7565R_CMD_ELECTRONIC_VOLUME_MODE_SET   0x81
 
#define ST7565R_CMD_ELECTRONIC_VOLUME(volume)   (0x3F & (~volume))
 
#define ST7565R_CMD_BOOSTER_RATIO_SET   0xF8
 
#define ST7565R_CMD_BOOSTER_RATIO_2X_3X_4X   0x00
 
#define ST7565R_CMD_BOOSTER_RATIO_5X   0x01
 
#define ST7565R_CMD_BOOSTER_RATIO_6X   0x03
 
#define ST7565R_CMD_STATUS_READ   0x00
 
#define ST7565R_CMD_END   0xEE
 
#define ST7565R_CMD_READ_MODIFY_WRITE   0xE0
 

Interface selection

The LCD controller support both serial and parallel mode, that means there is a number of possible ways of interfacing the controller using different AVR peripherals.

The different interfaces can be selected using different defines. This driver supports the serial communication mode using an USART in Master SPI mode by defining ST7565R_USART_SPI_INTERFACE, and a normal SPI in Master Mode by defining ST7565R_SPI_INTERFACE.

Note
The current driver only support serial mode.
#define ST7565R_USART_SPI_INTERFACE
 Select the a USART SPI interface. More...
 
#define ST7565R_SPI_INTERFACE
 Select the normal SPI peripheral interface. More...
 

LCD controller write and read functions

static void st7565r_write_command (uint8_t command)
 Writes a command to the display controller. More...
 
static void st7565r_write_data (uint8_t data)
 Write data to the display controller. More...
 
static uint8_t st7565r_read_data (void)
 Read data from the controller. More...
 
static uint8_t st7565r_get_status (void)
 Read status from the controller. More...
 

LCD Controller reset

static void st7565r_soft_reset (void)
 Perform a soft reset of the LCD controller. More...
 
static void st7565r_hard_reset (void)
 Perform a hard reset of the LCD controller. More...
 

Sleep control

static void st7565r_sleep_enable (void)
 Enable the LCD sleep mode. More...
 
static void st7565r_sleep_disable (void)
 Disable the LCD sleep mode. More...
 

Address setup for the LCD

static void st7565r_set_page_address (uint8_t address)
 Set current page in display RAM. More...
 
static void st7565r_set_column_address (uint8_t address)
 Set current column in display RAM. More...
 
static void st7565r_set_display_start_line_address (uint8_t address)
 Set the display start draw line address. More...
 

Display hardware control

static void st7565r_display_on (void)
 Turn the LCD display on. More...
 
static void st7565r_display_off (void)
 Turn the LCD display off. More...
 
static void st7565r_set_all_pixels (bool pixels_on)
 Sets all LCD pixels on. More...
 
static uint8_t st7565r_set_contrast (uint8_t contrast)
 Set the LCD contrast level. More...
 
static void st7565r_display_invert_enable (void)
 Invert all pixels on the device. More...
 
static void st7565r_display_invert_disable (void)
 Disable invert of all pixels on the device. More...
 

Initialization

void st7565r_init (void)
 Initialize the LCD controller. More...
 

#define ST7565R_CMD_ADC_NORMAL   0xA0

Referenced by st7565r_init().

#define ST7565R_CMD_ADC_REVERSE   0xA1
#define ST7565R_CMD_BOOSTER_RATIO_2X_3X_4X   0x00

Referenced by st7565r_init().

#define ST7565R_CMD_BOOSTER_RATIO_5X   0x01
#define ST7565R_CMD_BOOSTER_RATIO_6X   0x03
#define ST7565R_CMD_BOOSTER_RATIO_SET   0xF8

Referenced by st7565r_init().

#define ST7565R_CMD_COLUMN_ADDRESS_SET_LSB (   column)    (0x00 | (column))
#define ST7565R_CMD_COLUMN_ADDRESS_SET_MSB (   column)    (0x10 | (column))
#define ST7565R_CMD_DISPLAY_ALL_POINTS_OFF   0xA4

Referenced by st7565r_set_all_pixels().

#define ST7565R_CMD_DISPLAY_ALL_POINTS_ON   0xA5

Referenced by st7565r_set_all_pixels().

#define ST7565R_CMD_DISPLAY_NORMAL   0xA6
#define ST7565R_CMD_DISPLAY_OFF   0xAE

Referenced by st7565r_display_off().

#define ST7565R_CMD_DISPLAY_ON   0xAF

Referenced by st7565r_display_on().

#define ST7565R_CMD_DISPLAY_REVERSE   0xA7
#define ST7565R_CMD_ELECTRONIC_VOLUME (   volume)    (0x3F & (~volume))

Referenced by st7565r_set_contrast().

#define ST7565R_CMD_ELECTRONIC_VOLUME_MODE_SET   0x81

Referenced by st7565r_set_contrast().

#define ST7565R_CMD_END   0xEE
#define ST7565R_CMD_LCD_BIAS_1_DIV_5_DUTY33   0xA1
#define ST7565R_CMD_LCD_BIAS_1_DIV_6_DUTY33   0xA2

Referenced by st7565r_init().

#define ST7565R_CMD_NOP   0xE3
#define ST7565R_CMD_NORMAL_MODE   0xAD

Referenced by st7565r_sleep_disable().

#define ST7565R_CMD_NORMAL_SCAN_DIRECTION   0xC0
#define ST7565R_CMD_PAGE_ADDRESS_SET (   page)    (0xB0 | (page))
#define ST7565R_CMD_POWER_CTRL_ALL_ON   0x2F

Referenced by st7565r_init().

#define ST7565R_CMD_READ_MODIFY_WRITE   0xE0
#define ST7565R_CMD_RESET   0xE2

Referenced by st7565r_soft_reset().

#define ST7565R_CMD_REVERSE_SCAN_DIRECTION   0xC8

Referenced by st7565r_init().

#define ST7565R_CMD_SLEEP_MODE   0xAC

Referenced by st7565r_sleep_enable().

#define ST7565R_CMD_START_LINE_SET (   line)    (0x40 | (line))
#define ST7565R_CMD_STATUS_READ   0x00
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_0   0x20
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_1   0x21

Referenced by st7565r_init().

#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_2   0x22
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_3   0x23
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_4   0x24
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_5   0x25
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_6   0x26
#define ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_7   0x27
#define ST7565R_SERIAL_INTERFACE
#define ST7565R_SPI_INTERFACE

Select the normal SPI peripheral interface.

#define ST7565R_USART_SPI_INTERFACE

Select the a USART SPI interface.

static void st7565r_display_invert_disable ( void  )
inlinestatic

Disable invert of all pixels on the device.

This function will disable invert on all pixels on the LCD

References ST7565R_CMD_DISPLAY_NORMAL, and st7565r_write_command().

Referenced by st7565r_init().

static void st7565r_display_invert_enable ( void  )
inlinestatic

Invert all pixels on the device.

This function will invert all pixels on the LCD

References ST7565R_CMD_DISPLAY_REVERSE, and st7565r_write_command().

static void st7565r_display_off ( void  )
inlinestatic

Turn the LCD display off.

This function will turn off the LCD.

References ST7565R_CMD_DISPLAY_OFF, and st7565r_write_command().

static void st7565r_display_on ( void  )
inlinestatic

Turn the LCD display on.

This function will turn on the LCD.

References ST7565R_CMD_DISPLAY_ON, and st7565r_write_command().

Referenced by st7565r_init().

static uint8_t st7565r_get_status ( void  )
inlinestatic

Read status from the controller.

Note
The controller does not support read in serial mode.
Return values
8bit status read from the controller
static void st7565r_hard_reset ( void  )
inlinestatic

Perform a hard reset of the LCD controller.

This functions will reset the LCD controller by setting the reset pin low.

Note
this functions should not be confused with the st7565r_soft_reset() function, this command will control the RST pin.

References delay_us, ioport_set_pin_high(), and ioport_set_pin_low().

Referenced by st7565r_init().

void st7565r_init ( void  )
static uint8_t st7565r_read_data ( void  )
inlinestatic

Read data from the controller.

Note
The controller does not support read in serial mode.
Return values
8bit data read from the controller

Referenced by gfx_mono_st7565r_get_byte(), and gfx_mono_st7565r_get_page().

static void st7565r_set_all_pixels ( bool  pixels_on)
inlinestatic

Sets all LCD pixels on.

This function can be used to test the LCD by setting all pixels on, this will not affect the current LCD RAM.

Parameters
pixels_onif true all the LCD pixels are turned on, false the display is back in normal mode displaying what is in the display RAM.

References ST7565R_CMD_DISPLAY_ALL_POINTS_OFF, ST7565R_CMD_DISPLAY_ALL_POINTS_ON, and st7565r_write_command().

static void st7565r_set_column_address ( uint8_t  address)
inlinestatic
static uint8_t st7565r_set_contrast ( uint8_t  contrast)
inlinestatic

Set the LCD contrast level.

Warning
This will set the voltage for the LCD, settings this value too high may result in damage to the LCD. Hence the limit for these settings must be defined in the conf_st7565r.h file.

Contrast values outside the max and min values will be clipped to the defined ST7565R_DISPLAY_CONTRAST_MAX and ST7565R_DISPLAY_CONTRAST_MIN.

Parameters
contrasta number between 0 and 63 where the max values is given by the LCD.
Return values
contrastthe contrast value written to the LCD controller

References ST7565R_CMD_ELECTRONIC_VOLUME, ST7565R_CMD_ELECTRONIC_VOLUME_MODE_SET, and st7565r_write_command().

Referenced by st7565r_init().

static void st7565r_set_display_start_line_address ( uint8_t  address)
inlinestatic

Set the display start draw line address.

This function will set which line should be the start draw line for the LCD.

References ST7565R_CMD_START_LINE_SET, and st7565r_write_command().

Referenced by gfx_mono_st7565r_init().

static void st7565r_set_page_address ( uint8_t  address)
inlinestatic

Set current page in display RAM.

This command is usually followed by the configuration of the column address because this scheme will provide access to all locations in the display RAM.

Parameters
addressthe page address

References ST7565R_CMD_PAGE_ADDRESS_SET, and st7565r_write_command().

Referenced by gfx_mono_st7565r_get_byte(), gfx_mono_st7565r_get_page(), gfx_mono_st7565r_put_byte(), gfx_mono_st7565r_put_framebuffer(), and gfx_mono_st7565r_put_page().

static void st7565r_sleep_disable ( void  )
inlinestatic

Disable the LCD sleep mode.

References ST7565R_CMD_NORMAL_MODE, and st7565r_write_command().

static void st7565r_sleep_enable ( void  )
inlinestatic

Enable the LCD sleep mode.

References ST7565R_CMD_SLEEP_MODE, and st7565r_write_command().

static void st7565r_soft_reset ( void  )
inlinestatic

Perform a soft reset of the LCD controller.

This functions will reset the LCD controller by sending the reset command.

Note
this functions should not be confused with the st7565r_hard_reset() function, this command will not control the RST pin.

References ST7565R_CMD_RESET, and st7565r_write_command().

static void st7565r_write_command ( uint8_t  command)
inlinestatic
static void st7565r_write_data ( uint8_t  data)
inlinestatic

Write data to the display controller.

This functions sets the pin A0 before writing to the controller. Different data write function is called based on the selected interface.

Parameters
datathe data to write

References usart_spi_device::id, spi_device::id, ioport_set_pin_high(), ioport_set_pin_low(), spi_deselect_device(), spi_select_device(), spi_write_single(), usart_spi_deselect_device(), usart_spi_select_device(), and usart_spi_transmit().

Referenced by gfx_mono_st7565r_put_byte(), and gfx_mono_st7565r_put_page().