Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

U20EC090

Experiment-8

Aim: Write code to display data on LCD using STM32 discovery board.
Apparatus: STM32 discovery board
Software: Keil micro vision, STlink
Code:
/*LCD 16x2 DISPLAYS THE MESSAGE "STM32F4XX" ON FIRST LINE */

#include "stm32f4xx.h"

void command(uint8_t);
void ready(void);
void display(uint8_t);

void delay(void);

GPIO_InitTypeDef GPIO_InitStructure;
uint32_t ii;
uint8_t msg[16]= {" WELCOME "};
uint8_t msg1[16]={" TO "};
uint8_t msg2[16]={" SVNIT "};

#define RS_Port GPIOB


#define RS_Pin GPIO_Pin_11
#define RWB_Port GPIOB
#define RWB_Pin GPIO_Pin_10
#define EN_Port GPIOB
#define EN_Pin GPIO_Pin_2
#define DATA_Port GPIOE
#define DATA_Pin GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7

Embedded Systems Laboratory, ECED, SVNIT, Surat 1


U20EC090

#define DATA_Pin_0 GPIO_Pin_0

int main(void)
{
/* GPIO Periph clock enable */
SystemCoreClockUpdate();
/LCD configuration/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = DATA_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(DATA_Port, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = RS_Pin|EN_Pin;
GPIO_Init(RS_Port, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = RWB_Pin;
GPIO_Init(RWB_Port, &GPIO_InitStructure);

EN_Port->BSRRH=(uint16_t)EN_Pin;

/* GPIOG Periph clock enable */


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

Embedded Systems Laboratory, ECED, SVNIT, Surat 2


U20EC090

/* Configure PG6 and PG8 in output pushpull mode */


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 |
GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 |
GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
for(ii=0;ii<=200;ii++)
delay();

command(0x38);
command(0x01);
command(0x06);
command(0x0E);
command(0x80);

for(ii=0;ii<16;ii++)
display(msg[ii]);
for(ii=0;ii<=50000000;ii++);
while (1)
{
/* Set PG6 and PG8 */
command(0xc0);
for(ii=0;ii<16;ii++)
display(msg1[ii]);

//GPIOD->BSRRL = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_10 |


GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
GPIO_Pin_15;

Embedded Systems Laboratory, ECED, SVNIT, Surat 3


U20EC090

for(ii=0;ii<=50000000;ii++);

/* Reset PG6 and PG8 */


command(0xc0);
for(ii=0;ii<16;ii++)
display(msg2[ii]);
for(ii=0;ii<=50000000;ii++);
}
}

void command(uint8_t temp)


{
ready();
DATA_Port->ODR&=~((uint32_t)DATA_Pin);
DATA_Port->ODR|=temp<<(DATA_Pin_0-1);
RS_Port->BSRRH=(uint16_t)RS_Pin;
delay();
RWB_Port->BSRRH=(uint16_t)RWB_Pin;
delay();
EN_Port->BSRRL=(uint16_t)EN_Pin;
delay();
EN_Port->BSRRH=(uint16_t)EN_Pin;
}

void display(uint8_t temp)


{
ready();
DATA_Port->ODR&=~((uint32_t)DATA_Pin);
DATA_Port->ODR|=temp<<(DATA_Pin_0-1);
RS_Port->BSRRL=(uint16_t)RS_Pin;
delay();

Embedded Systems Laboratory, ECED, SVNIT, Surat 4


U20EC090

RWB_Port->BSRRH=(uint16_t)RWB_Pin;
delay();
EN_Port->BSRRL=(uint16_t)EN_Pin;
delay();
EN_Port->BSRRH=(uint16_t)EN_Pin;
}

void ready(void)
{
uint8_t i;
for(i=0;i<=50;i++)
delay();
}

void delay(void)
{
uint16_t x;
for(x=0;x<=1000;x++);
}

Embedded Systems Laboratory, ECED, SVNIT, Surat 5

You might also like