Hello,
I have been given the task to modify my code to run in 4 bit rather than the origional 8 bit. I have tried changing the function command to reflect this but all that happens is the LCD displays random letters, im really stuck as to what else could need changing. The LCD is your bog standard 2 row 16 character one. I will attach my code below. Any help would be amazing. (Note that lcd.c is below main)
Code:
#include "lcd.h"
int main(void)
{
initLCD();
cmdLCD(LCD_LINE1);
putLCD('H');
putLCD('e');
putLCD('l');
putLCD('l');
putLCD('o');
putLCD(' ');
putLCD('W');
putLCD('o');
putLCD('r');
putLCD('l');
putLCD('d');
putLCD('!');
putLCD(' ');
putLCD(' ');
while(1);
}
insert
Code:
#include "lcd.h"
void lcd_delayus(unsigned int us) //blocking delay for LCD, argument is approximate number of micro-seconds to delay
{
unsigned char i;
while(us--)
{
for(i=0; i<SystemCoreClock/4000000; i++){__NOP();}
}
}
void WaitLcdBusy(void)
{
set_LCD_bus_input();
set_LCD_RW();
clr_LCD_E();
clr_LCD_RS();
int port;
do {
set_LCD_E();
lcd_delayus(10);
port= LCD_PORT->IDR&(1u << 7);
clr_LCD_E();
}
while(port>0);
set_LCD_bus_output();
}
//lcd_delayus(3000); //3ms blocking delay
void set_LCD_data(unsigned char d)
{
LCD_PORT->BSRR=(0xff<<(LCD_D0_pin+16)); //clear data lines
LCD_PORT->BSRR=(d<<LCD_D0_pin); //put data on lines
}
void LCD_strobe(void) //10us high pulse on LCD enable line
{
lcd_delayus(10);
set_LCD_E();
lcd_delayus(10);
clr_LCD_E();
}
void cmdLCD(unsigned char cmd) //sends a byte to the LCD control register
{
WaitLcdBusy(); //wait for LCD to be not busy
clr_LCD_RS(); //control command
clr_LCD_RW(); //write command
set_LCD_data(cmd); //set data on bus
LCD_strobe(); //apply command
}
void putLCD(unsigned char put) //sends a char to the LCD display
{
WaitLcdBusy(); //wait for LCD to be not busy
set_LCD_RS(); //text command
clr_LCD_RW(); //write command
set_LCD_data(put); //set data on bus
LCD_strobe(); //apply command
}
void initLCD(void)
{
SystemCoreClockUpdate();
RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN; //enable LCD port clock
//CONFIG LCD GPIO PINS
LCD_PORT->MODER&=~( //clear pin direction settings
(3u<<(2*LCD_RS_pin))
|(3u<<(2*LCD_RW_pin))
|(3u<<(2*LCD_E_pin))
|(0xffff<<(2*LCD_D0_pin))
);
LCD_PORT->MODER|=( //reset pin direction settings to digital outputs
(1u<<(2*LCD_RS_pin))
|(1u<<(2*LCD_RW_pin))
|(1u<<(2*LCD_E_pin))
|(0x5555<<(2*LCD_D0_pin))
);
//LCD INIT COMMANDS
clr_LCD_RS(); //all lines default low
clr_LCD_RW();
clr_LCD_E();
lcd_delayus(25000); //25ms startup delay
cmdLCD(0x38); //Function set=38: 2 Line, 8-bit, 5x7 dots or 28 for 4 bit mode
cmdLCD(0x0c); //Display on, Cursor blinking command
cmdLCD(0x01); //Clear LCD
cmdLCD(0x06); //Entry mode, auto increment with no shift
}