Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Learning STM32 + LCD Interfacing code not working

Status
Not open for further replies.

thannara123

Advanced Member level 5
Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Activity points
10,615
Hello
I am trying to interface 4 bit LCD (16x2 ) with STM32f103 controller its not working whats the wrong with me .The main.c code is attached here with


LCD.C file
C:
#include "lcd.h"
#include "main.h"



extern void Delay_Us (uint16_t us);
void LCD_Strobe(void)
{

  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_SET);// LCD Enable PIN to High
  HAL_Delay(1);
  //Delay_Us(100);
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_RESET);// LCD Enable High to Low
  //Delay_Us(100);
  HAL_Delay(1);
 }


void LCD_init(void)
{

  HAL_Delay(40);
  LCD_cmd(0x28);
  HAL_Delay(1);
  LCD_cmd(0x28);
  HAL_Delay(1);
  //Delay_Us(100);
  LCD_cmd(0x28);
  LCD_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
  LCD_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
//  LCD_cmd(0x0c);            // Make cursorinvisible
//    LCD_Clear();            // Clear screen
 //  LCD_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

void LCD_cmd(__uint16_t cmd)
{       HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_RESET);
        cmd = (cmd << 8) ;
        GPIOB->ODR =  (cmd & 0xf000);
        LCD_Strobe();
        cmd = (cmd << 4) ;
     // printf("GPIOB->ODR Value %d \n\r",cmd);
        GPIOB->ODR =  (cmd & 0xf000);
        LCD_Strobe();
}


void LCD_data(__uint16_t LCD_Data)
{
     HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_SET);
     LCD_Data = (LCD_Data << 8) ;
     GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (LCD_Data & 0xf000);
    // LCD_Data =GPIOB->ODR;
    // printf("GPIOB->ODR Value %d \n\r",LCD_Data);
     LCD_Strobe();
     LCD_Data = (LCD_Data << 4) ;
     GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (LCD_Data & 0xf000);
     LCD_Strobe();

}


void LCD_Clear(void)
{
    LCD_cmd(0x01);
    HAL_Delay(5);
}

void LCD_String(const char *ptr)  // ptr pointing the address of displaying string
{
    while (*ptr)             // check weather the endo of file then exit the loop
    {
        LCD_data(*ptr++);    // Sending data to LCD_data functions
    }
}

lcd.png





Whener i check the GPIOB->ODR value it shows some grabage value how to clear it ? is that the problem
 

Attachments

  • Main.c.txt
    7.5 KB · Views: 127
Last edited:

Solved .
The problem Dont try STM32 on Proteus ,It not properly work ,
work on real Hardware .

But I learned a lot of . Thanks great foroum members ☺️☺️☺️☺️
 

Hai I changed the code as follows

C:
void lcd_put(__uint16_t  lcd_data ,char rs)
{
    if(rs==0) // check weather it is command
    { HAL_GPIO_WritePin(GPIOB,RS_Pin, RESET);}// command}
    if(rs==1)
    {HAL_GPIO_WritePin(GPIOB,RS_Pin, SET); }// for data}

    lcd_data = (lcd_data << 8) ;
    GPIOB->ODR |=  (lcd_data & 0xf000); // send the higher nibbile to the GPIOB P15 is D7 ---GPIOB P12 is D4
        HAL_GPIO_WritePin(GPIOB,EN_Pin, SET);// LCD Enable PIN to High
        HAL_Delay(1);
        HAL_GPIO_WritePin(GPIOB,EN_Pin, RESET);       // LCD Enable High to Low

    lcd_data = (lcd_data << 4) ;  //send the lower nibbile to the GPIOB P15 is D7 ---GPIOB P12 is D4
    GPIOB->ODR |=  (lcd_data & 0xf000);
        HAL_GPIO_WritePin(GPIOB,EN_Pin, SET);// LCD Enable PIN to High
        HAL_Delay(1);
        HAL_GPIO_WritePin(GPIOB,EN_Pin, RESET);       // LCD Enable High to Low

}

is it make any optimization or any sense ?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top