AnsweredAssumed Answered

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

Question asked by j g on Dec 22, 2017
Latest reply on Dec 22, 2017 by john doe

#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++);
}
}

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
}

 

 

 

 

 

 

 

 

 

 

#ifndef LCD_H
#define LCD_H
#define LCD_PORT GPIOD
#define LCD_RS_pin 11
#define LCD_RW_pin 12
#define LCD_E_pin 13

#define LCD_D0_pin 0


#define LCD_LINE1 0x80
#define LCD_LINE2 0xc0

#define set_LCD_RS() LCD_PORT->BSRR=(1u<<LCD_RS_pin)
#define clr_LCD_RS() LCD_PORT->BSRR=(1u<<(LCD_RS_pin+16))
#define set_LCD_RW() LCD_PORT->BSRR=(1u<<LCD_RW_pin)
#define clr_LCD_RW() LCD_PORT->BSRR=(1u<<(LCD_RW_pin+16))
#define set_LCD_E() LCD_PORT->BSRR=(1u<<LCD_E_pin)
#define clr_LCD_E() LCD_PORT->BSRR=(1u<<(LCD_E_pin+16))

#define LCD_CLR() cmdLCD(0x01)

#define set_LCD_bus_input() LCD_PORT->MODER&=~(0xffff<<(2*LCD_D0_pin))
#define set_LCD_bus_output() LCD_PORT->MODER|=(0x5555<<(2*LCD_D0_pin))
#define Masking() LCD_PORT->IDR &=(1u<<7)

#include <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();

#endif

Outcomes