cancel
Showing results for 
Search instead for 
Did you mean: 

LCD Display

fedaykin86
Associate
Posted on December 28, 2010 at 08:53

Hello,

I'm trying to run WC1602A LCD display but nothing happens, can anyone pinpoint me where i have a mistake in code (the code is compiling)  because i was analising it whole night and couldn't find one

int main(void)

{

  LCD_Init();

  LCD_Clear();

  LCD_GoTo(0,0);

  LCD_SendText(''Hello\0'');

  STM32vldiscovery_LEDInit(LED3);

  STM32vldiscovery_LEDInit(LED4);

  STM32vldiscovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

 }

lcd4bit.h

#include ''stm32f10x.h''

#define LCD_D4           GPIO_Pin_3  // PC3

#define LCD_D5             GPIO_Pin_2  // PC2

#define LCD_D6   GPIO_Pin_1  // PC1

#define LCD_D7             GPIO_Pin_0  // PC0

#define LCD_D_ALL           GPIO_Pin_3 & GPIO_Pin_2 & GPIO_Pin_1 & GPIO_Pin_0 //GPIO_Pin_All  // PC0, PC1, PC2, PC3

#define LCD_RS             GPIO_Pin_12  // PC12

#define LCD_RW             GPIO_Pin_11  // PC11

#define LCD_EN             GPIO_Pin_10  // PC10

void delay_ms(int ms);

void delay_us(int us);

uint8_t LCD_ReadByte(void);

void LCD_SendByte(uint8_t cmd);

void LCD_SendCmd (uint8_t cmd);

void LCD_SendData(uint8_t data);

void LCD_SendText(uint8_t * text);

void LCD_GoTo (uint8_t wers, uint8_t kol);

void LCD_Clear (void);

void LCD_Init (void);

lcd4bit.c

#include ''lcd4bit.h''

//====================================

void delay_ms(int ms)

{

int i, tms;

tms = 9000*ms;

for(i=0;i<tms;i++);

}

//======================================

void delay_us(int us)

{

int i, tus;

tus = 9*us;

for(i=0;i<tus;i++);

}

//=======================================================

uint8_t LCD_ReadByte(void)

{

GPIO_InitTypeDef GPIO_Conf;

uint8_t ReadedData=0;

GPIO_SetBits(GPIOC, LCD_D_ALL); 

// Zmiana konfiguracji wyprowadzen na wejsciach

   GPIO_Conf.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

   GPIO_Conf.GPIO_Mode = GPIO_Mode_IN_FLOATING;

   GPIO_Conf.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOC, &GPIO_Conf);

   GPIO_SetBits(GPIOC, LCD_RW);

GPIO_SetBits(GPIOC, LCD_EN);

if(GPIO_ReadInputDataBit(GPIOC, LCD_D7)) //odczyt starszej polowy

ReadedData |= 0x80;

if(GPIO_ReadInputDataBit(GPIOC, LCD_D6))

ReadedData |= 0x40; 

if(GPIO_ReadInputDataBit(GPIOC, LCD_D5))

ReadedData |= 0x20; 

if(GPIO_ReadInputDataBit(GPIOC, LCD_D4))

ReadedData |= 0x10;  

GPIO_ResetBits(GPIOC, LCD_EN);

delay_us(30);

GPIO_SetBits(GPIOC, LCD_EN);

if(GPIO_ReadInputDataBit(GPIOC, LCD_D7)) //odczyt mlodszej polowy

ReadedData |= 0x08;

if(GPIO_ReadInputDataBit(GPIOC, LCD_D6))

ReadedData |= 0x04; 

if(GPIO_ReadInputDataBit(GPIOC, LCD_D5))

ReadedData |= 0x02; 

if(GPIO_ReadInputDataBit(GPIOC, LCD_D4))

ReadedData |= 0x01;  

GPIO_ResetBits(GPIOC, LCD_EN);

// Powrot do pierwotnych ustawien wyprowadzen

   GPIO_Conf.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

   GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Conf.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOC, &GPIO_Conf);

return ReadedData;

}

//=============================================================================

void LCD_SendByte(uint8_t cmd)

{

uint8_t tcmd = 0;

GPIO_ResetBits(GPIOC, LCD_RW);

GPIO_ResetBits(GPIOC, LCD_D_ALL);

GPIO_SetBits(GPIOC, LCD_EN); 

tcmd = cmd >> 4; // 4x w prawo

if( tcmd & 0x01 )

GPIO_SetBits(GPIOC, LCD_D4); //zapis starszej polowy

if( tcmd & 0x02 )

GPIO_SetBits(GPIOC, LCD_D5);

if( tcmd & 0x04 )

GPIO_SetBits(GPIOC, LCD_D6);

if( tcmd & 0x08 )

GPIO_SetBits(GPIOC, LCD_D7);

GPIO_ResetBits(GPIOC, LCD_EN); 

delay_us(30);

GPIO_SetBits(GPIOC, LCD_EN); 

GPIO_ResetBits(GPIOC, LCD_D_ALL);

cmd &= 0x0F;   // maskowanie czterech mlodszych bitow

if( cmd & 0x01 )

GPIO_SetBits(GPIOC, LCD_D4); //zapis mlodszej polowy

if( cmd & 0x02 )

GPIO_SetBits(GPIOC, LCD_D5);

if( cmd & 0x04 )

GPIO_SetBits(GPIOC, LCD_D6);

if( cmd & 0x08 )

GPIO_SetBits(GPIOC, LCD_D7);

GPIO_ResetBits(GPIOC, LCD_EN); 

GPIO_ResetBits(GPIOC, LCD_D_ALL); 

GPIO_ResetBits(GPIOC, LCD_RS);

while(LCD_ReadByte() & 0x80); // czekaj na zakonczenie operacji

}

//============================================================================

void LCD_SendCmd(uint8_t cmd)

{

GPIO_ResetBits(GPIOC, LCD_RS);

LCD_SendByte(cmd);

}

//=============================================================================

void LCD_SendData(uint8_t data)

{

GPIO_SetBits(GPIOC, LCD_RS);

LCD_SendByte(data);

}

//===============================================================================

void LCD_SendText(uint8_t * text)

{

while(*text)

{

LCD_SendData(*text);

text++;

}

}

//=======================================================================================

void LCD_Clear(void)

{

LCD_SendCmd(0x01);

}

//=====================================================================================

void LCD_GoTo(uint8_t wers, uint8_t kol)

{

uint8_t pos = 0;

pos = (wers * 0x40) + kol;

pos += 0x80;

LCD_SendCmd(pos); 

}

//===========================================================================

void LCD_Init(void)

{

uint8_t i;

GPIO_InitTypeDef GPIO_Conf;

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

   GPIO_Conf.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

   GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;

   GPIO_Conf.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOC, &GPIO_Conf);

delay_ms(30); //me

GPIO_ResetBits(GPIOC, LCD_RS);

GPIO_ResetBits(GPIOC, LCD_EN);

for(i = 0; i<3; i++)

{

GPIO_SetBits(GPIOC, LCD_EN); 

GPIO_SetBits(GPIOC, LCD_D4 | LCD_D5);

GPIO_ResetBits(GPIOC, LCD_D6 | LCD_D7);

GPIO_ResetBits(GPIOC, LCD_EN);

delay_ms(5);

}

GPIO_SetBits(GPIOC, LCD_EN); 

GPIO_SetBits(GPIOC, LCD_D5);

GPIO_ResetBits(GPIOC,LCD_D4 | LCD_D6 | LCD_D7);

GPIO_ResetBits(GPIOC, LCD_EN);

LCD_SendCmd(0x28);

LCD_SendCmd(0x08);

LCD_SendCmd(0x01);

LCD_SendCmd(0x06);

LCD_SendCmd(0x0C);

delay_ms(5); //me

}

4 REPLIES 4
STM32 hobbyist
Associate III
Posted on December 28, 2010 at 18:01

Hi bartek;

I think your mistake comes from the following line:

#define LCD_D_ALL    GPIO_Pin_3 & GPIO_Pin_2 & GPIO_Pin_1 & GPIO_Pin_0 //GPIO_Pin_All  // PC0, PC1, PC2, PC3

it should be:

#define LCD_D_ALL    (GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0)

B.R.

STM32

fedaykin86
Associate
Posted on December 29, 2010 at 13:27

I already did that but that wasn't a solution, still nothing happend with LCD 

Posted on December 29, 2010 at 21:26

I already did that but that wasn't a solution, still nothing happend with LCD 

May be you got it wired wrong. Without knowing what you've wired where it's hard to say. Break out the scope and look at the signals, and debug it. Start with some simple interactions with the LCD and work from there.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
marcinha
Associate
Posted on December 30, 2010 at 11:37

Hi Bartek,

try it:

main:

  LCD_Init();

  LCD_Clear();

  LCD_GoTo(0,0);

  //LCD_SendCmd(0x02);  //home

  delay_ms(5);

  LCD_SendText(''Hello'');

LCD:

#define LCD_D4           GPIO_Pin_3  // PC3

#define LCD_D5             GPIO_Pin_2  // PC2

#define LCD_D6   GPIO_Pin_1  // PC1

#define LCD_D7             GPIO_Pin_0  // PC0

#define LCD_D_ALL    (GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0) //GPIO_Pin_All  // PC0, PC1, PC2, PC3

#define LCD_RS             GPIO_Pin_12  // PC12

//#define LCD_RW             GPIO_Pin_11  // PC11

#define LCD_EN             GPIO_Pin_11  // PC11

void delay_ms(int ms);

void delay_us(int us);

//uint8_t LCD_ReadByte(void);

void LCD_SendByte(uint8_t cmd);

void LCD_SendCmd (uint8_t cmd);

void LCD_SendData(uint8_t data);

void LCD_SendText(uint8_t * text);

void LCD_GoTo (uint8_t wers, uint8_t kol);

void LCD_Clear (void);

void LCD_Init (void);

//====================================

void delay_ms(int ms)

{

    int i, tms;

    tms = 9000*ms;

    for(i=0;i<tms;i++);

}

//======================================

void delay_us(int us)

{

    int i, tus;

    tus = 9*us;

    for(i=0;i<tus;i++);

}

//=============================================================================

void LCD_SendByte(uint8_t cmd)

{

uint8_t tcmd = 0;

      GPIO_ResetBits(GPIOC, LCD_D_ALL);

   

    tcmd = cmd >> 4; // 4x w prawo

    if( tcmd & 0x01 )

    GPIO_SetBits(GPIOC, LCD_D4); //zapis starszej polowy

    if( tcmd & 0x02 )

    GPIO_SetBits(GPIOC, LCD_D5);

    if( tcmd & 0x04 )

    GPIO_SetBits(GPIOC, LCD_D6);

    if( tcmd & 0x08 )

    GPIO_SetBits(GPIOC, LCD_D7);

    GPIO_SetBits(GPIOC, LCD_EN);     //E pulse

    delay_us(30);

    GPIO_ResetBits(GPIOC, LCD_EN);

    GPIO_ResetBits(GPIOC, LCD_D_ALL);

    cmd &= 0x0F;   // maskowanie czterech mlodszych bitow

    if( cmd & 0x01 )

    GPIO_SetBits(GPIOC, LCD_D4); //zapis mlodszej polowy

    if( cmd & 0x02 )

    GPIO_SetBits(GPIOC, LCD_D5);

    if( cmd & 0x04 )

    GPIO_SetBits(GPIOC, LCD_D6);

    if( cmd & 0x08 )

    GPIO_SetBits(GPIOC, LCD_D7);

    GPIO_SetBits(GPIOC, LCD_EN);     //E pulse

    delay_us(30);

    GPIO_ResetBits(GPIOC, LCD_EN);

    GPIO_ResetBits(GPIOC, LCD_D_ALL);

    GPIO_ResetBits(GPIOC, LCD_RS);

    delay_us(200);

    //while(LCD_ReadByte() & 0x80); // czekaj na zakonczenie operacji

}

//============================================================================

void LCD_SendCmd(uint8_t cmd)

{

    GPIO_ResetBits(GPIOC, LCD_RS);

    LCD_SendByte(cmd);

}

//=============================================================================

void LCD_SendData(uint8_t data)

{

    GPIO_SetBits(GPIOC, LCD_RS);

    LCD_SendByte(data);

}

//===============================================================================

void LCD_SendText(uint8_t * text)

{

    while(*text)

    {

        LCD_SendData(*text);

        text++;

    }

}

//=======================================================================================

void LCD_Clear(void)

{

    LCD_SendCmd(0x01);

    delay_ms(5);

}

//=====================================================================================

void LCD_GoTo(uint8_t wers, uint8_t kol)

{

    uint8_t pos = 0;

    pos = (wers * 0x40) + kol;

    pos += 0x80;

    LCD_SendCmd(pos);

}

//===========================================================================

void LCD_Init(void)

{

    uint8_t i;

    GPIO_InitTypeDef GPIO_Conf;

   

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

   

       GPIO_Conf.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

       GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;

       GPIO_Conf.GPIO_Speed = GPIO_Speed_50MHz;

       GPIO_Init(GPIOC, &GPIO_Conf);

   

    delay_ms(30); //me

    GPIO_ResetBits(GPIOC, LCD_RS);

    GPIO_ResetBits(GPIOC, LCD_EN);

      for(i = 0; i<3; i++)

        {

        GPIO_SetBits(GPIOC, LCD_EN);

        GPIO_SetBits(GPIOC, LCD_D4 | LCD_D5);

        GPIO_ResetBits(GPIOC, LCD_D6 | LCD_D7);

        GPIO_ResetBits(GPIOC, LCD_EN);

        delay_ms(5);

        }

    GPIO_SetBits(GPIOC, LCD_EN);

    GPIO_SetBits(GPIOC, LCD_D5);

    GPIO_ResetBits(GPIOC,LCD_D4 | LCD_D6 | LCD_D7);

    GPIO_ResetBits(GPIOC, LCD_EN);

    LCD_SendCmd(0x28);

    LCD_SendCmd(0x08);

    LCD_SendCmd(0x01);

    delay_ms(5);

    LCD_SendCmd(0x06);

    LCD_SendCmd(0x0C);

    delay_ms(5); //me

}

You haven't to wait for reply from LCD at end of sending data - delay is enough.

Also I improve delays and E-pulses.

It work on my board.

Regards,

MH