This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

lcd project at stm32

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.

Parents
  • 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));
    }
    
    

Reply
  • 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));
    }
    
    

Children
  • 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");
    
              }
    
    }