C++ GPIO for STM32F0 LCD

Stuck with a problem in your code? Seek help here.

C++ GPIO for STM32F0 LCD

Postby brbeardm » October 23rd, 2016, 9:10 pm

I'm trying to learn embedded dev in C++ using CoIDE. I have a STM32F0 chip on a break out board. i've done some LED tutorials etc. I'm stuck on this code that is supposed to write some simple text strings to the LCD. The tutorial I'm following is at eeherald tutorial i've adapted the code to my STM32F0 chip. I think I'm close, but the LCD just stays in initialization mode with all the cells lit up. the screen never clears to write the text. here's my code... any help to point me in the right direction would be APPRECIATED!

I "think" the problem may be in the initGPIO() for the data direction code, but I'm not sure... i've tried so many different things with no luck.

Code: Select all
#include "stm32f0xx_hal.h"

#define EN  12 // EN Enable on PortB chip pin#53
#define RW  11 // RW Read Write on PortB chip pin#52
#define RS  10 // RS Register Select on PortB chip pin#51

void initGPIO(void);
void s_init(void);
void s_data(void);
void s_latch(void);

//------------------------------------------------------------------------------
// Function Name : Init GPIO
// Description : pins ,port clock & mode initialization.
//------------------------------------------------------------------------------
void initGPIO()
{
    // Define GPIO Structures
    GPIO_InitTypeDef GPIO_InitStructure;

    // Reset and clock control - Advanced high-performance bus - Enabling GPIO Port B and Port C
    __GPIOB_CLK_ENABLE();
    __GPIOC_CLK_ENABLE();

    // Set Data Direction for Port C
    GPIO_InitStructure.Pin = RS | RW | EN;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);

    //Set Data Direction for Port A
    GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//------------------------------------------------------------------------------
// Function Name : s_init
// Description : Send Instruction Function (RS=0 & RW=0)
//------------------------------------------------------------------------------

void s_init()
{
    GPIOC->BRR=RS;
    GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_data
// Description : Send Data Select routine(RS=1 & RW=0)
//------------------------------------------------------------------------------

void s_data()
{
    GPIOC->BSRR=RS;
    GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_latch
// Description : Latch Data/Instruction on LCD Databus.
//------------------------------------------------------------------------------

void s_latch()
{
    GPIOC->BSRR=EN;
    HAL_Delay(10);
    GPIOC->BRR=EN;
    HAL_Delay(10);
}

/*******************************************************************************
* Function Name : main
* Description : Main program.
*******************************************************************************/
int main(void) //Main function
{

    initGPIO();

    int k=0;
    char a[]="WWW.EEHERALD.COM";
    char b[]="EMBEDDED SYSTEMS";

    GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction Send
    GPIOC->BRR=RW; // Select RW=0 to write Instruction/data on LCD
    GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial condition)

    HAL_Delay(10);

    s_init(); //Call Instruction Select routine
    GPIOB->ODR=0x0001; // Clear Display, Cursor to Home
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0038; // Display Function (2 rows for 8-bit data; small)
    s_latch(); //Latch this above instruction 4 times
    s_latch();
    s_latch();
    s_latch();
    GPIOB->ODR=0x000E; // Display and Cursor on, Cursor Blink off
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0010; // Cursor shift left
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0006; // Cursor Increment, Shift off
    s_data(); //Change the input type to Data.(before it was instruction input)
    s_latch(); //Latch the above instruction

    for(k=0;a[k];k++)
    {
        GPIOB->ODR=a[k]; //It will send a[0]='P' as = '0x0050' on Port B.
        s_latch(); //Latch the above instruction only once. Or it will clone each character twice if you latch twice.
    }
    GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction Send
    GPIOC->BRR=RW; // Select RW=0 to write Instruction/data on LCD
    GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial condition)


    GPIOB->ODR=0x00C0; // Move cursor to beginning of second row
    s_latch(); //Latch the above instruction
    s_data(); //Change the input type to Data.(before it was instruction input)
    for(k=0;b[k];k++)
    {
        GPIOB->ODR=b[k]; //It will send b[0]='E' as = '0x0044' on Port B.
        s_latch();//Latch the above instruction only once. Or it will clone each character twice if you latch twice.
    }
    s_init();
}
brbeardm
 
Posts: 1
Joined: October 23rd, 2016, 1:03 pm

Re: C++ GPIO for STM32F0 LCD

Postby st2000 » November 5th, 2016, 8:24 am

That's a lot of code. Maybe someone will look through it. Here's my suggestion(s):
Go over to st.com and find some GPIO sample code for this processor and (better yet) for this board. This is an ARM M0 core and st.com may have put all kinds of bells and whistles around it. For all you know getting the directions of the processor's pins is just the start. You may be dealing with pins that can be programmed to be GPIO, or ADC inputs, or PWM outputs, or Interrupt inputs... And since these type of processors are embedded and could be running off batteries, most have the ability to turn bits and pieces of them selfs off. So you may have to pay attention to power-management features and make sure the GPIO feature for the pins of interest is up and running. Like I said, finding a working example should help a lot.
st2000
 
Posts: 1453
Joined: February 3rd, 2011, 6:10 pm

Re: C++ GPIO for STM32F0 LCD

Postby k-ww » November 5th, 2016, 3:06 pm

Silly question:

Are you sure the problem is with your code?

Most LCD's have a contrast adjustment pot, and if the pot is cranked too far in one direction,
all the pixels will be 'lit' and in the other direction, they will all be off regardless of the data
actually in the LCD's controller chip.

What is the model # of the LCD display you are using?
User avatar
k-ww
 
Posts: 1265
Joined: February 17th, 2011, 10:47 am


Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron