Hi, I'm new in embedded system,I've stm32f100rb and I want to display a string at an lcd named jhd162a 16x2, but when I test it, the lcd always display a black line,I test it with an empty board and it's the same problem, one black line. anyone have an idea please??? and thank you.
u dont do the display init properly. do it properly.
this is my init function:
void lcd_init(void) { volatile unsigned char i = 0; volatile unsigned int delayCnt = 0; RCC_APB2PeriphClockCmd(LCD_CLK_LINE, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_RW|LCD_EN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); GPIO_ResetBits(LCD_GPIO, LCD_RS | LCD_EN | LCD_RW); for(delayCnt = 0; delayCnt < 300000; delayCnt++); for(i = 0; i < 3; i++) { lcd_writenibble(0x03); for(delayCnt = 0; delayCnt < 30000; delayCnt++); } lcd_writenibble(0x02); for(delayCnt = 0; delayCnt < 6000; delayCnt++); lcd_writecommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_4_BIT); lcd_writecommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); lcd_writecommand(HD44780_CLEAR); lcd_writecommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT); lcd_writecommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); }
I set the GPIO_Speed to 50 Mhz and it the same problem :/ this is all of the code project:
lcd_hd44780.h:
//****************************************************************************** // THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. // AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT // OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING // FROM USE OF THIS SOFTWARE. //****************************************************************************** //############################################################################## // lcd_hd44780.h //############################################################################## //****************************************************************************// #include "STM32vldiscovery.h" #define LCD_GPIO GPIOB #define LCD_CLK_LINE RCC_APB2Periph_GPIOB #define LCD_D4 GPIO_Pin_6 #define LCD_D5 GPIO_Pin_5 #define LCD_D6 GPIO_Pin_4 #define LCD_D7 GPIO_Pin_3 #define LCD_RS GPIO_Pin_9 #define LCD_RW GPIO_Pin_8 #define LCD_EN GPIO_Pin_7 //******************************************************************************// #define HD44780_CLEAR 0x01 #define HD44780_HOME 0x02 #define HD44780_ENTRY_MODE 0x04 #define HD44780_EM_SHIFT_CURSOR 0 #define HD44780_EM_SHIFT_DISPLAY 1 #define HD44780_EM_DECREMENT 0 #define HD44780_EM_INCREMENT 2 #define HD44780_DISPLAY_ONOFF 0x08 #define HD44780_DISPLAY_OFF 0 #define HD44780_DISPLAY_ON 4 #define HD44780_CURSOR_OFF 0 #define HD44780_CURSOR_ON 2 #define HD44780_CURSOR_NOBLINK 0 #define HD44780_CURSOR_BLINK 1 #define HD44780_DISPLAY_CURSOR_SHIFT 0x10 #define HD44780_SHIFT_CURSOR 0 #define HD44780_SHIFT_DISPLAY 8 #define HD44780_SHIFT_LEFT 0 #define HD44780_SHIFT_RIGHT 4 #define HD44780_FUNCTION_SET 0x20 #define HD44780_FONT5x7 0 #define HD44780_FONT5x10 4 #define HD44780_ONE_LINE 0 #define HD44780_TWO_LINE 8 #define HD44780_4_BIT 0 #define HD44780_8_BIT 16 #define HD44780_CGRAM_SET 0x40 #define HD44780_DDRAM_SET 0x80 //############################################################## void lcd_init(void); void lcd_cls(void); void lcd_str(unsigned char * text); void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y); void lcd_locate(unsigned char x, unsigned char y); void lcd_int(int n); void lcd_intxy(int n, unsigned char x, unsigned char y); //############################################################### void lcd_writedata(unsigned char dataToWrite); void lcd_writecommand(unsigned char commandToWrite); void lcd_writebinary(unsigned int var, unsigned char bitCount); void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p);
lcd_hd44780.c:
#include "lcd_hd44780_lib.h" #include "stm32f10x_gpio.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include "STM32vldiscovery.h" GPIO_InitTypeDef GPIO_InitStructure; //----------------------------------------------------------------------------- void lcd_writenibble(unsigned char nibbleToWrite) { GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET); GPIO_WriteBit(LCD_GPIO, LCD_D4,(BitAction) (nibbleToWrite & 0x01)); GPIO_WriteBit(LCD_GPIO, LCD_D5,(BitAction)(nibbleToWrite & 0x02)); GPIO_WriteBit(LCD_GPIO, LCD_D6,(BitAction)(nibbleToWrite & 0x04)); GPIO_WriteBit(LCD_GPIO, LCD_D7,(BitAction)(nibbleToWrite & 0x08)); GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET); } //----------------------------------------------------------------------------- unsigned char LCD_ReadNibble(void) { unsigned char tmp = 0; GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET); tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D4) << 0); tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D5) << 1); tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D6) << 2); tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D7) << 3); GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET); return tmp; } //----------------------------------------------------------------------------- unsigned char LCD_ReadStatus(void) { unsigned char status = 0; GPIO_InitStructure.GPIO_Pin = LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_SET); GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); status |= (LCD_ReadNibble() << 4); status |= LCD_ReadNibble(); GPIO_InitStructure.GPIO_Pin = LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); return status; } //----------------------------------------------------------------------------- void lcd_writedata(unsigned char dataToWrite) { GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_RESET); GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_SET); lcd_writenibble(dataToWrite >> 4); lcd_writenibble(dataToWrite & 0x0F); while(LCD_ReadStatus() & 0x80); } //----------------------------------------------------------------------------- void lcd_writecommand(unsigned char commandToWrite) { GPIO_WriteBit(LCD_GPIO, LCD_RW | LCD_RS, Bit_RESET); lcd_writenibble(commandToWrite >> 4); lcd_writenibble(commandToWrite & 0x0F); while(LCD_ReadStatus() & 0x80); } //----------------------------------------------------------------------------- void lcd_str(unsigned char * text) { while(*text) lcd_writedata(*text++); } //----------------------------------------------------------------------------- void lcd_locate(unsigned char x, unsigned char y) { lcd_writecommand(HD44780_DDRAM_SET | (x + (0x40 * y))); } //----------------------------------------------------------------------------- void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y) { lcd_locate(x,y); while(*text) lcd_writedata(*text++); } //----------------------------------------------------------------------------- void lcd_writebinary(unsigned int var, unsigned char bitCount) { signed char i; for(i = (bitCount - 1); i >= 0; i--) { lcd_writedata((var & (1 << i))?'1':'0'); } } //----------------------------------------------------------------------------- void LCD_ShiftLeft(void) { lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_LEFT | HD44780_SHIFT_DISPLAY); } //----------------------------------------------------------------------------- void LCD_ShiftRight(void) { lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_RIGHT | HD44780_SHIFT_DISPLAY); } //----------------------------------------------------------------------------- void lcd_init(void) { volatile unsigned char i = 0; volatile unsigned int delayCnt = 0; RCC_APB2PeriphClockCmd(LCD_CLK_LINE, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_RW|LCD_EN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); GPIO_ResetBits(LCD_GPIO, LCD_RS | LCD_EN | LCD_RW); for(delayCnt = 0; delayCnt < 300000; delayCnt++); for(i = 0; i < 3; i++) { lcd_writenibble(0x03); for(delayCnt = 0; delayCnt < 30000; delayCnt++); } lcd_writenibble(0x02); for(delayCnt = 0; delayCnt < 6000; delayCnt++); lcd_writecommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_4_BIT); lcd_writecommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); lcd_writecommand(HD44780_CLEAR); lcd_writecommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT); lcd_writecommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); } //----------------------------------------------------------------------------- void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p) { lcd_writecommand(HD44780_CGRAM_SET | chrNum * 8); n *= 8; do lcd_writedata(*p++); while (--n); } //----------------------------------------------------------------------------- void lcd_cls(void){ lcd_writecommand(HD44780_CLEAR); } unsigned char* intToStr(int n) { int i = 0; char *tmp = (char*)malloc(sizeof(char)); unsigned char *ret = (unsigned char*)malloc(12); if(n < 0) { *ret = '-'; i++; n = -n; } do { *tmp = n % 10 + 48; n -= n % 10; if(n > 9) *tmp++; } while(n /= 10); while(ret[i++] = *tmp--); return ret; } void lcd_int(int n){ lcd_str(intToStr(n)); } void lcd_intxy(int n, unsigned char x, unsigned char y){ lcd_locate(x,y); lcd_str(intToStr(n)); }
main.c
#include <stddef.h> #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "lcd_hd44780_lib.h" #include "STM32vldiscovery.h" int main(void) { RCC_PLLConfig (0x00010000, RCC_PLLMul_2); lcd_init(); while (1) { lcd_locate(1,0); lcd_str("ENSI"); lcd_locate(7,0); lcd_str("STM32F1"); lcd_locate(0,1); lcd_str("electronics"); } }
"I set the GPIO_Speed to 50 Mhz and it the same problem"
That has nothing to do with busy loops!
Did you follow the links?
sorry, but I can't understand what I have to do ??
thank you very much, but I ask if you have some simple projects about lcd to more understand it :)
What you have shown is pretty simple as LCD projects go!
That's the point - to be able to understand even a "simple" LCD project, you need to have worked through the basics of handling GPIO, registers, timing, etc.
You need to lay the foundations first before you start trying to put in doors & windows!