Tuesday 18 April 2017

Interfacing LCD to STM32F103C8T6

LCD Interface with STM32F103C8T6

LCD most common debugging medium for embedded systems, pin-out of 16 x 2 lcd 
If you are new to STM32F103C8T6  for basics you can refer this Link 
Pins of  LCD can be categorized in to 3 types
1) Power
2) Control
3) Data

1) Power: Starting from left  Pin 1-GND must be connected to supply ground
  Pin 2-Vcc must be connected to 5v of supply Pin 3-Vee  for contrast adjustment of LCD usually a variable resistor of (500 to 1K ohm) is sufficient on this pin pull-down configuration is preferred i.e 
connect this pin to resistor (510 ohms) and other end of resistor to ground.
pin 15 - Anode must be connected to 5v through resistor of (330 ohms to 1k ohms) pull up configuration. (for Back light adjustment) 
pin 16 - Cathode must be connected to ground.
     
2) Control: Pin 4 Register select (RS  =0   value on   D0-D7 considered as command)
(RS  =1   value on   D0-D7 considered as data)
Pin 5 R/W (if R/W=0 write,  R/W=1 read) as LCD is meant for display purpose mostly write is preferred. To check status of lcd (busy/ready) read can be used but by following proper timing this can be avoided.
Pin 6 Enable  For write Falling transition is required (1 to 0) for read rising transition is required (0 to 1)

3 Data: Pin 7 to Pin 14 (D0 - D7) data pins through this command/data can be transferred to LCD

in 8 bit mode: 8 data + 3 control total 11 pins  of micro controller are required.
in 4 bit mode: 4 data + 3 control total 7 pins of micro controller are required. 
(D0-D3) are ignored in 4 bit mode.
also RW can be connected to ground. so only 6 pins of micro controller are required.

Steps to Interface LCD to STM32F103C8T6 in four bit mode.
1) Power on LCD  
2) Enable clock to GPIO i used Port A 8 to 11 for D4 to D7 and Port B 12 ,13  ( RS,  Enable) respectively
RCC->APB2ENR|=(1<<2)|(1<<3);
3) Give a delay greater than 15 ms say(16 ms ). For this i used software delay considering clock frequency of 72MHz one iteration of for loop takes 5 clk cycles for 1ms (72000/5)= 14400 iterations required for 1ms delay(approximately).

4) pass nibble of 0x03 then wait for delay of 5ms
5) pass nibble of 0x03 then wait for delay of 100ms
6) pass nibble of 0x03 then pass nibble of 0x02
LCD will come out of 8bit mode, 
7) Pass command 0x28 to change LCD to 4bit mode
8) Pass command 0x08 Display off cursor off
9) Pass command 0x01 to clear screen
10) Pass command 0x06 auto increment cursor 
11) Pass command 0x0C Display on cursor off 
that ends lcd initialization.
pass data on lcd  at particular position (x,y )
for line 1  pass command ((1<<7) |0x00|x) where bit 7 for enabling Display Data RAM, and setting position x in line 1, 
for line 2  pass command ((1<<7) |0x40|x) where bit 7 for enabling Display Data RAM, and setting position x in line 2, 
  

I used  Static data in Line 0 and Scrolling Data in Line 1.






1 comment:

  1. I want to learn stm32f103 programming plz suggest some sources other than data sheet ,reference manual, sources like video books websites.thanks.

    ReplyDelete