lpcware

Has anyone encountered similar situation with LCD.

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by black_ghost on Thu Oct 13 23:29:12 MST 2011
first of i think i have a bad lcd. when i power it on it doesnt display the solid square boxes. And when i tried to adjust the 10k tripot the lcd remains solid green. Before i seen flashes of solid squares and could adjust the contrast. I dont think my code is wrong cause i used a logic analyzer to to determine my pin output and could see when the signals were low and high. I have tried compiling my code but no success. As anyone encountered similar situation if so pls what do i do and below is my code to verify also.


#define NVIC_ST_CTRL_R          (*((volatile unsigned long *)0xE000E010))

#define NVIC_ST_RELOAD_R        (*((volatile unsigned long *)0xE000E014))

#define NVIC_ST_CURRENT_R       (*((volatile unsigned long *)0xE000E018))

#define NVIC_ST_CTRL_COUNT      0x00010000  // Count flag

#define NVIC_ST_CTRL_CLK_SRC    0x00000004  // Clock Source

#define NVIC_ST_CTRL_INTEN      0x00000002  // Interrupt enable

#define NVIC_ST_CTRL_ENABLE     0x00000001  // Counter mode

#define NVIC_ST_RELOAD_M        0x00FFFFFF  // Counter load value



void lcd_init(); //initialization of LCD.
void lcd_initwrite(uint8_t value);  //writes into 2 nibbles
void command(uint8_t value);  //command functions
void lcd_line1();
void lcd_string(char *a);
void print (char cmd);   //counter for 1ms SysTicks
// read current tick counter
//  SysTick_Handler - just increment SysTick counter
void SysTick_init(void);
void SysTick_Wait(unsigned long delay);
void SysTick_Wait10ms(unsigned long delay);

//initialization of LCD
void lcd_init()
{

    SysTick_Wait10ms(20); // sets delay off 20ms before powering up.
    lcd_initwrite(0x30);  // initialize commands for LCD
    SysTick_Wait10ms(10); //delays for more than 5ms.
    lcd_initwrite(0x30); 
    SysTick_Wait(10000); //delays for about 140micro seconds
    lcd_initwrite(0x30);
    SysTick_Wait(10000);
    lcd_initwrite(0x20); //initialization command for a 4 bit
    SysTick_Wait(10000);
    command(0x28);
    SysTick_Wait(120000);//set to 4 bit and 2 lines
    command(0x01);
    SysTick_Wait(120000);//clears the display
    command(0x06);
    SysTick_Wait(12000);//Entry mode for the LCD
    command(0x0E);  //Turn on the display
    SysTick_Wait(12000);
}
//sends the initialization commands
void lcd_initwrite(uint8_t value)
{
    LPC_GPIO0->FIOCLR |= (1<<9); // clears the RS signal
    LPC_GPIO0->FIOCLR |= (1<<8); // clears the RW signal

    //upper nibble and stores it in the data line d4-d7
    uint8_t bit[4];
    bit[0]=(value & 0x10)>>4;
    bit[1]=(value & 0x20)>>5;
    bit[2]=(value & 0x40)>>6;
    bit[3]=(value & 0x80)>>7;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) :(LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7); //sets the enable pin
    SysTick_Wait(10000);                 //lms second delay
    LPC_GPIO0->FIOCLR |= (1<<7); //clears the enable pin
}

void command(uint8_t value)
{
    uint8_t bit[4];

    LPC_GPIO0->FIOCLR |= (1<<9); //RW is low
    LPC_GPIO0->FIOCLR |= (1<<8); //RS is set low
    //upper nibble and sending the data to the ports
    bit[0] = (value & 0x10) >> 4;
    bit[1] = (value & 0x20) >> 5;
    bit[2] = (value & 0x40) >> 6;
    bit[3] = (value & 0x80) >> 7;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6))  : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0))  : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1))  : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) : (LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7);
    SysTick_Wait(10000);
    LPC_GPIO0->FIOCLR |= (1<<7);

  //lower nibble and stores the value in the data port
    bit[0]=(value & 0x01)>>0;
    bit[1]=(value & 0x02)>>1;
    bit[2]=(value & 0x04)>>2;
    bit[3]=(value & 0x08)>>3;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)): (LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7);
    SysTick_Wait(10000);
    LPC_GPIO0->FIOCLR |= (1<<7);
    SysTick_Wait(10000);
}

//instruction command for setting up line 1.
void lcd_line1()
{
    command(0x80);
}
//displays characters on LCD
void print(char cmd)
{
        SysTick_Wait(4000);
        uint8_t bit[4]; //array of 4bits
        LPC_GPIO0->FIOCLR |= (1<<8); //RW is 0
        LPC_GPIO0->FIOSET |= (1<<9); //sets RS = 1;

        //upper nibble
        bit[0] = (cmd & 0x10) >> 4; //
        bit[1] = (cmd & 0x20) >> 5;
        bit[2] = (cmd & 0x40) >> 6;
        bit[3] = (cmd & 0x80) >> 7;
        bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR =  (1<<6));
        bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR =  (1<<0));
        bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR =  (1<<1));
        bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)): (LPC_GPIO0->FIOCLR =  (1<<18));
        LPC_GPIO0->FIOSET |= (1<<7); //sets Enable pin
        SysTick_Wait(10000);
        LPC_GPIO0->FIOCLR |= (1<<7); //clear enable pin
        SysTick_Wait(10000);


        //lower nibble
        bit[0]=(cmd & 0x01)>>0;
        bit[1]=(cmd & 0x02)>>1;
        bit[2]=(cmd & 0x04)>>2;
        bit[3]=(cmd & 0x08)>>3;
        bit[0] ? (LPC_GPIO0->FIOSET = (1<<6))  : (LPC_GPIO0->FIOCLR = (1<<6));
        bit[1] ? (LPC_GPIO0->FIOSET = (1<<0))  : (LPC_GPIO0->FIOCLR = (1<<0));
        bit[2] ? (LPC_GPIO0->FIOSET = (1<<1))  : (LPC_GPIO0->FIOCLR = (1<<1));
        bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) : (LPC_GPIO0->FIOCLR = (1<<18));
        LPC_GPIO0->FIOSET |= (1<<7); //sets enable to high
        SysTick_Wait(10000);
        LPC_GPIO0->FIOCLR |= (1<<7); //sets enable pin to low
        SysTick_Wait(10000);
}
void lcd_string(char *a)
{

while (*a != '\0')
  {
    print(*a);
    a++;
  }
}


    void SysTick_Init(void)
{

      NVIC_ST_CTRL_R = 0;                   // disable SysTick during setup

      NVIC_ST_RELOAD_R = NVIC_ST_RELOAD_M;  // maximum reload value

      NVIC_ST_CURRENT_R = 0;
      // enable SysTick with core clock

        NVIC_ST_CTRL_R = NVIC_ST_CTRL_ENABLE+NVIC_ST_CTRL_CLK_SRC;

  }

    // Time delay using busy wait.


    void SysTick_Wait(unsigned long delay)
    {

      volatile unsigned long elapsedTime;

      unsigned long startTime = NVIC_ST_CURRENT_R;

      do{

        elapsedTime = (startTime-NVIC_ST_CURRENT_R)&0x00FFFFFF;

      }

      while(elapsedTime <= delay);

    }

    // Time delay using busy wait.

    // 10000us equals 10ms

    void SysTick_Wait10ms(unsigned long delay)
    {

      unsigned long i;

      for(i=0; i<delay; i++){

        SysTick_Wait(60000);  // wait 10ms (assumes 6 MHz clock)

      }

    }


int main(void)
{
    //selecting my data line pins on port 1
    LPC_PINCON->PINSEL0 = 0x00000000;
    LPC_GPIO0->FIODIR = 0x000403C3;      //selecting to outputs on port 0.
    LPC_GPIO0->FIOCLR = 0x000403C3;    //clears the port 0.

    SysTick_Init();
    lcd_init();
    while(1)
    {
        lcd_line1();
        lcd_string("Hello.");
        SysTick_Wait(10000);

    }

    return 0 ;
}

Outcomes