cancel
Showing results for 
Search instead for 
Did you mean: 

how would i turn this from 8 bit to 4 bit??

j g
Associate II
Posted on December 22, 2017 at 16:13

&sharpinclude '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++);

}

}

void WaitLcdBusy(void)

{

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: 2 Line, 8-bit, 5x7 dots

cmdLCD(0x0c); //Display on, Cursor blinking command

cmdLCD(0x01); //Clear LCD

cmdLCD(0x06); //Entry mode, auto increment with no shift

}

&sharpifndef LCD_H

&sharpdefine LCD_H

&sharpdefine LCD_PORT GPIOD

&sharpdefine LCD_RS_pin 11

&sharpdefine LCD_RW_pin 12

&sharpdefine LCD_E_pin 13

&sharpdefine LCD_D0_pin 0

&sharpdefine LCD_LINE1 0x80

&sharpdefine LCD_LINE2 0xc0

&sharpdefine set_LCD_RS() LCD_PORT->BSRR=(1u<<LCD_RS_pin)

&sharpdefine clr_LCD_RS() LCD_PORT->BSRR=(1u<<(LCD_RS_pin+16))

&sharpdefine set_LCD_RW() LCD_PORT->BSRR=(1u<<LCD_RW_pin)

&sharpdefine clr_LCD_RW() LCD_PORT->BSRR=(1u<<(LCD_RW_pin+16))

&sharpdefine set_LCD_E() LCD_PORT->BSRR=(1u<<LCD_E_pin)

&sharpdefine clr_LCD_E() LCD_PORT->BSRR=(1u<<(LCD_E_pin+16))

&sharpdefine LCD_CLR() cmdLCD(0x01)

&sharpdefine set_LCD_bus_input() LCD_PORT->MODER&=~(0xffff<<(2*LCD_D0_pin))

&sharpdefine set_LCD_bus_output() LCD_PORT->MODER|=(0x5555<<(2*LCD_D0_pin))

&sharpdefine Masking() LCD_PORT->IDR &=(1u<<7)

&sharpinclude <stm32f4xx.h>

void lcd_delayus(unsigned int us);

void WaitLcdBusy(void);

void set_LCD_data(unsigned char d);

void LCD_strobe(void);

void cmdLCD(unsigned char cmd);

void putLCD(unsigned char put);

void initLCD(void);

void Display_Voltage(float fVolt);

void float_to_string(float fVolt);

void Init4bit();

&sharpendif?

#endif
1 REPLY 1
john doe
Lead
Posted on December 22, 2017 at 17:45

put the lcd driver in 4 bit mode [read the data sheet for the command, i think its 0x03 but im not at home] then when you want to send something, set your command/data pin, then set the BRR for the high nibble, flutter the enable pin, set the BRR for the low nibble, flutter the enable pin