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.






Tuesday 11 April 2017

Programming STM32F103C8T6 using Keil

Programming Basic Peripherals of STM32F103C8T6 using Keil 
STM32F103C8T6 falls under medium density ARM-Cortex M3 based microcontroller with on chip RAM of 20KB and Flash of 64KB, with decent number of on chip peripherals, Here i will show how this controller can be programmed using keil.
In my earlier post I covered how to add Secondary bootloader to STM32F103C8T6 you can find it from here.

In this post i will show how to program GPIO of STM32F103C8T6.
On board LED is connected to Port C -13pin. Basic things required for programming are
1. Keil IDE - Download
2. Common Files - Download
3. Manuals - Hardware -  Download
                     Peripherals- Download

Just create one folder and extract common files in to that folder and open keil and create project
in that folder with out any extension give some name say led
Select Project->New Project->led.uvproj->STM32F103C8
allow startup file to be added to project
Right click on source group and add System_Stm32f10x.c
and create one app.c and add it to source group
In app.c just include stm32f10x.h
write main function
Steps to Program GPIO
1) Power on GPIO by enabling Power to Port-c by setting bit 4 of APB2ENR register
this is part  Reset  and Clock Control (RCC) structure

2) Default Function of Port-C 13 pin is GPIO (from Hardware manual you can get this information)

3) Configure relevant registers of GPIO
Set Direction of GPIO as output  push pull mode
by writing 1 to 21st bit of CRH register

4) then turn Port-C.13 pin on and off by writing in to BSRR and BRR register 13th bit with software delay in between

Select target options and click C/C++ and under define type  STM32F10X_MD

Then click on Save and Rebuild It should give zero errors and zero warnings
click on debug and select peripherals-> GPIO C and click on run

After connecting STM32F103C8T6  board through USB you should see following class of USB DFU



Do small settings in program.
first under target change start to 0x8003000

then under linker check select from target options

in System_stm32f10x.c change Vector Table Offset to 0x3000

in app.c add SystemInit() and increase delay

output generate hex and using dfu manager convert hex to dfu file
then upload dfu in to hard ware using dfuse software
after uploading press reset.led should toggle for 1sec




for interfacing lcd to stm32f103c8t6 click here