You are on page 1of 9

Name Ubaid Ur Rehman

Reg No FA18-BEE-121-E
LAB #10
This Lab has been designed to familiarize with STM LCD display and
manipulation.
Objectives
To explain the working of 16x2 LCD interfacing with the STM32F407 using trainer board AND modify the
program and display its output on STM32F407 trainer board using C programming.
Methodology
This Lab consisted of interfacing of STM LCD its operation and programming. The basic objective
was to manipulate the LCD display to display a desired string message. The LCD is used widely in
different applications due to its cost, ease in programming and ability to display characters, numbers
as well as graphics. The STM LCD has two lines that can display 16 characters. The LCD has two
inbuilt registers namely the command registers and data register. The command register is used to
insert a special command into the LCD. While Data register is used to insert a data into the LCD.
Command is a special set of data which is used to give the internal command to LCD. Different
commands were to be learned and their functions were to be understood. The data register is used to
write data or read data. The register is used to display characters that are to be displayed on the LCD.
This Lab had a pre lab task and two lab tasks that were to be implemented and the results were to
be observed. The pre lab was read the pre lab and familiarize with different set of commands
regarding the LCD manipulation. The pre lab was to familiarize the use of the commands in
programming of the LCD. The pre lab task also included display of a string message. The next
tasks were the in-lab tasks. The first task was to display a string message on the LCD in such a
fashion that the message scrolls from left to right. The second task was to display two strings and
make the above and below stings scroll in opposite directions. The tasks were to be completed and
the results were to be verified.
Conclusion
The lab helped further understand the use of STM Trainer Board and the interfacing of LCD. The
Lab also helped in the understanding of different registers involved in the functioning of the LCD.
The lab also helped in interfacing and understanding of the LCD commands and their functioning.
The results after changing the codes were observed. All the Lab Tasks were completed.
Observations were made and the results were verified.
LAB TASK
TASK 1
#include <stdio.h>
#include <stm32f4xx.h>
void lcd_ini(void);
void lcd_data(char j);
void lcd_cmd(char i);

volatile uint32_t msTicks; /* counts 1ms timeTicks */


/*----------------------------------------------------------------------------
SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
msTicks++;
}
void Delay (uint32_t dlyTicks) {
uint32_t loop=0,dly=0,loope=0;
dly = dlyTicks ;
for(loop=0;loop<dly;loop++){
for(loope=0;loope<29000;loope++){
__nop();
}
}
}
unsigned long LCDDATA=0, i;

/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {
SystemCoreClockUpdate(); // Get Core Clock Frequency

RCC->AHB1ENR |= (1 << 1) ; // Enable GPIOB clock


RCC->AHB1ENR |= (1 << 4) ; // Enable GPIOE clock
GPIOB->MODER = 0x00000005;
GPIOB->OTYPER = 0x00000003;
GPIOB->OSPEEDR = 0xAAAAAAAA;
GPIOB->PUPDR = 0x00000000;
GPIOE->MODER = 0x55555555;
GPIOE->OTYPER = 0x0000FF00;
GPIOE->OSPEEDR = 0xAAAAAAAA;
GPIOE->PUPDR = 0x00000000;
GPIOB->BSRRH = ((1 << 0) ); // LCD RW -> 0 Read/Write Pin of LCD
lcd_ini(); //LCD inilization function calling
lcd_cmd(0x0); // Command for selecting line 1 for display, command function is called
lcd_data('M'); //data function is called for sending data
lcd_data('S');
lcd_data('I');
lcd_data(' ');
lcd_data('L');
lcd_data('A');
lcd_data('B');
lcd_cmd(0xC0); // // Command for selecting line 2 for display
lcd_data('L');
lcd_data('A');
lcd_data('B');
lcd_data(' ');
lcd_data('9');
lcd_data(' ');
lcd_data('L');
lcd_data('C');
lcd_data('D');
lcd_data(' ');
lcd_data('D');
lcd_data('E');
lcd_data('M');
lcd_data('O');
Delay(20);
while(1){
}
}

///////////////////////////////////////
void lcd_ini(void) //LCD initialization function
{
Delay(10);
lcd_cmd(0x38); //LCD command for function set 8-bit 2 line
lcd_cmd(0x0C); //LCD command for cursor off
lcd_cmd(0x01); //LCD command for clear display
Delay(10);
}

void lcd_cmd(char i) //LCD function for commands


{
unsigned long r=0;
char loop=0;
r |= i; // bitwise OR
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRH = ((1 << 1) ); //for setting the value on RS pin
LCDDATA = r;
GPIOE->ODR &= 0x000000FF; //bitwise and
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) ); //for sending high to low pulse on enable pin
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}

void lcd_data(char j)
{
unsigned long r=0;
char loop=0;
r |= j;
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRL = ((1 << 1) );
LCDDATA = r;
GPIOE->ODR &= 0x000000FF;
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) );
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}

TASK 2
#include <stdio.h>
#include <stm32f4xx.h>

void lcd_ini(void);
void lcd_data(char j);
void lcd_cmd(char i);

volatile uint32_t msTicks; /* counts 1ms timeTicks */


/*----------------------------------------------------------------------------
SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
msTicks++;
}

void Delay (uint32_t dlyTicks) {


uint32_t loop=0,dly=0,loope=0;
dly = dlyTicks ;
for(loop=0;loop<dly;loop++){
for(loope=0;loope<29000;loope++){
__nop();
}
}
}

unsigned long LCDDATA=0, i;


/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {

SystemCoreClockUpdate(); // Get Core Clock Frequency

RCC->AHB1ENR |= (1 << 1) ; // Enable GPIOB clock


RCC->AHB1ENR |= (1 << 4) ; // Enable GPIOE clock

GPIOB->MODER = 0x00000005;
GPIOB->OTYPER = 0x00000003;
GPIOB->OSPEEDR = 0xAAAAAAAA;
GPIOB->PUPDR = 0x00000000;

GPIOE->MODER = 0x55555555;
GPIOE->OTYPER = 0x0000FF00;
GPIOE->OSPEEDR = 0xAAAAAAAA;
GPIOE->PUPDR = 0x00000000;

Label:
GPIOB->BSRRH = ((1 << 0) ); // LCD RW -> 0 Read/Write Pin of LCD
lcd_ini(); //LCD inilization function calling
lcd_cmd(0x0); // Command for selecting line 1 for display, command function is called

lcd_data('M'); //data function is called for sending data


lcd_data('S');
lcd_data('I');
lcd_data(' ');
lcd_data('L');
lcd_data('A');
lcd_data('B');

lcd_cmd(0xC0); // // Command for selecting line 2 for display

lcd_data('L');
lcd_data('A');
lcd_data('B');
lcd_data(' ');
lcd_data('9');
lcd_data(' ');
lcd_data('L');
lcd_data('C');
lcd_data('D');
lcd_data(' ');
lcd_data('D');
lcd_data('E');
lcd_data('M');
lcd_data('O');
Delay(20);

for(i=0;i<=15;i++)
{
lcd_cmd(0x18); //command for "display entire shift left"
Delay(20);
}
goto Label;
while(1){

}
}

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

void lcd_ini(void) //LCD initialization function


{
Delay(10);
lcd_cmd(0x38); //LCD command for function set 8-bit 2 line
lcd_cmd(0x0C); //LCD command for cursor off
lcd_cmd(0x01); //LCD command for clear display
Delay(10);
}

void lcd_cmd(char i) //LCD function for commands


{
unsigned long r=0;
char loop=0;
r |= i; // bitwise OR
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRH = ((1 << 1) ); //for setting the value on RS pin
LCDDATA = r;
GPIOE->ODR &= 0x000000FF; //bitwise and
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) ); //for sending high to low pulse on enable pin
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}

void lcd_data(char j)
{
unsigned long r=0;
char loop=0;
r |= j;
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRL = ((1 << 1) );
LCDDATA = r;
GPIOE->ODR &= 0x000000FF;
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) );
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}
TASK 3
#include <stdio.h>
#include <stm32f4xx.h>
void lcd_ini(void);
void lcd_data(char j);
void lcd_cmd(char i);
volatile uint32_t msTicks; /* counts 1ms timeTicks */
/*----------------------------------------------------------------------------
SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
msTicks++;
}
void Delay (uint32_t dlyTicks) {
uint32_t loop=0,dly=0,loope=0;
dly = dlyTicks ;
for(loop=0;loop<dly;loop++){
for(loope=0;loope<29000;loope++){
__nop();
}
}
}
unsigned long LCDDATA=0, i;

/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {

SystemCoreClockUpdate(); // Get Core Clock Frequency


RCC->AHB1ENR |= (1 << 1) ; // Enable GPIOB clock
RCC->AHB1ENR |= (1 << 4) ; // Enable GPIOE clock

GPIOB->MODER = 0x00000005;
GPIOB->OTYPER = 0x00000003;
GPIOB->OSPEEDR = 0xAAAAAAAA;
GPIOB->PUPDR = 0x00000000;

GPIOE->MODER = 0x55555555;
GPIOE->OTYPER = 0x0000FF00;
GPIOE->OSPEEDR = 0xAAAAAAAA;
GPIOE->PUPDR = 0x00000000;
Label:
GPIOB->BSRRH = ((1 << 0) ); // LCD RW -> 0 Read/Write Pin of LCD
lcd_ini(); //LCD inilization function calling
lcd_cmd(0x0); // Command for selecting line 1 for display, command function is called
lcd_data('M'); //data function is called for sending data
lcd_data('S');
lcd_data('I');
lcd_data(' ');
lcd_data('L');
lcd_data('A');
lcd_data('B');
lcd_cmd(0xC0); // // Command for selecting line 2 for display
lcd_data('L');
lcd_data('A');
lcd_data('B');
lcd_data(' ');
lcd_data('9');
lcd_data(' ');
lcd_data('L');
lcd_data('C');
lcd_data('D');
lcd_data(' ');
lcd_data('D');
lcd_data('E');
lcd_data('M');
lcd_data('O');
Delay(20);
for(i=0;i<=15;i++)
{
lcd_cmd(0x1C); //command for "display entire shift right"
Delay(20);
}
goto Label;
while(1){

}
}

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

void lcd_ini(void) //LCD initialization function


{
Delay(10);
lcd_cmd(0x38); //LCD command for function set 8-bit 2 line
lcd_cmd(0x0C); //LCD command for cursor off
lcd_cmd(0x01); //LCD command for clear display
Delay(10);
}

void lcd_cmd(char i) //LCD function for commands


{
unsigned long r=0;
char loop=0;
r |= i; // bitwise OR
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRH = ((1 << 1) ); //for setting the value on RS pin
LCDDATA = r;
GPIOE->ODR &= 0x000000FF; //bitwise and
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) ); //for sending high to low pulse on enable pin
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}
void lcd_data(char j)
{
unsigned long r=0;
char loop=0;
r |= j;
for(loop=0;loop<=7;loop++)
{
r = r << 1;
}
GPIOB->BSRRL = ((1 << 1) );
LCDDATA = r;
GPIOE->ODR &= 0x000000FF;
GPIOE->ODR |= LCDDATA;
GPIOE->BSRRL = ((1 << 7) );
Delay(100);
GPIOE->BSRRH = ((1 << 7) );
}

You might also like