#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
}
}
void LCD_cmd(__uint16_t cmd)
{ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, 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_10, 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();
}
Sir,The 16 bit first left sfiht 8 bit to get the higher Nibbile and the again next 4 left shift to write the lower nibble.At first sight seems a bit confusing all the cumulative 4 and 8 bit shifts on the variable LCD_Data inside the LCD functions.
#define LCD_RS GPIO_PIN_10
#define LCD_E GPIO_PIN_11
#define LCD_CTRL GPIOB
HAL_GPIO_WritePin(LCD_CTRL , LCD_RS, GPIO_PIN_RESET);
I used the same initilization earlier on PIC ,,AVR micrcontroller ,that worked one i used here as followsTo understand why your code isn't working, perform single stepping in debugger. Check all pin levels against expected values.
--- Updated ---
Apparently we need to check any single line of your code.
I wondered what else could be wrong and found that the first init instructions are already wrong. Please review example code, e.g. Hitachi hd44780 data sheet. First instruction(s) are 4-bit rather than 8-bit writes. I suggest to perform complete "initialization by instruction" that doesn't depend on successful LCD hardware reset, see below:
The Initilization I wrote the above is not correct i changed it before posting Sorry for the mistake ,
void LCD_init()
{
__delay_ms(50);
LCD_cmd(0x28);
LCD_cmd(0x28);
LCD_cmd(0x0C);
LCD_cmd(0x06);
LCD_cmd(0x01);
}
Not true.Earlier forum moderatores told that there is no require to say thanks or some like comment that i avoided that
/*
* lcd.c
*/
#include "stm32f1xx_hal.h"
#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,EN_Pin, RESET);// LCD Enable High to Low
//Delay_Us(100);PIO_PIN_11
HAL_Delay(1);
}
void LCD_init(void)
{
HAL_Delay(20);
lcd_cmd(0x30);
HAL_Delay(20);
lcd_cmd(0x30);
HAL_Delay(20);
//Delay_Us(100);
lcd_cmd(0x28);
HAL_Delay(20);
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)
{ GPIOB->ODR =0;
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_RESET);
cmd = (cmd << 8) ;
GPIOB->ODR = (cmd & 0xf000);
lcd_Strobe();
cmd = (cmd << 4) ;
GPIOB->ODR = (cmd & 0xf000);
lcd_Strobe();
GPIOB->ODR =0;
}
void lcd_data(__uint16_t lcd_data)
{ GPIOB->ODR =0x0000;
// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET);
lcd_data = (lcd_data << 8) ;
GPIOB->ODR = (lcd_data& 0xf000)|0x400;
lcd_Strobe();
GPIOB->ODR =0;
lcd_data = (lcd_data << 4) ;
GPIOB->ODR = (lcd_data & 0xf000)|0x400;
lcd_Strobe();
GPIOB->ODR =0;
}
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
}
}
Init procedure still not correct.
void lcd_init(void)
{
HAL_Delay(50);
lcd_cmd(0x30);
HAL_Delay(20);
lcd_cmd(0x30);
HAL_Delay(20); // for 4bit
//Delay_Us(100);
HAL_Delay(10);
lcd_cmd(0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
HAL_Delay(1);
lcd_cmd(0x0c); // Make cursorinvisible
lcd_Clear(); // Clear screen
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}
Thanks ,I am doing this project as learning purpose , .Now I tired with this oneYou may get help from this video.
void lcd_init(void)
{
HAL_Delay(100);
lcd_cmd(0x03);
HAL_Delay(1);
lcd_cmd(0x03);
HAL_Delay(1);
lcd_cmd(0x03);
HAL_Delay(1);
lcd_cmd(0x02); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
HAL_Delay(1);
lcd_cmd(0x02);
lcd_cmd(0x08);
HAL_Delay(1);
lcd_cmd(0x00);
lcd_cmd(0x01);
HAL_Delay(3);
lcd_cmd(0x00);
lcd_cmd(0x0c); // Make cursorinvisible
lcd_Clear(); // Clear screen
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}
By the by the project is not worked yet
You should not do a post containing only "Thank you" and nothing else..
But I have learned much more from experts
whats wrong ?
lcd_cmd(0x28);
HAL_Delay(20);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
Hello!
Not sure if this was a reply to my question. Which project?
LCD or LED blinking?
Could you reply _clearly_ to the questions?
-> Does your earlier led blinker work, yes or no?
Exactly. And even more. The purpose of this kind of discussion is to
also help other. Otherwise we will endless reply to LCD not working
posts. So what you could consider replying is:
I found what's wrong. Bit xx of register yy wasn't set. I corrected
with the following instruction [some code] and now the LED is blinking.
Thanks for your help.
By the way; what does the LCD do when you turn the contrast potentiometer?
-> If it's not tuned, it will not display anything even if the init code is right.
I'm not an expert, but you haven't learned enough:
- You haven't commented the code or written self-explanatory code as adviced earlier
Without looking at the documentation, can you tell me what (for instance)
LCD_cmd(0x30); does? No? Me neither.
- Commenting the code is one method. Self-commented code is better.
Example:
Instead of: lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
View attachment 185262
Why not
lcd_cmd(LCD_4BITS | LCD_2LINES | LCD_5X8); // You don't need comments here, they are in the code.
Of course you have to #define these codes.
Either you are using a very old LCD, almost deaf, so that you have to tell it 3 times
the same thing, or there is an issue. Do you have a technical reason for sending 3 times the same code?
Now some more advice: You want help? Then help us by applying our advices. If you don't,
you may consider using an Arduino with its predefined libraries.
Best regards,
Dora.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?