Displaying String and Integers on LCD
First add "Retarget_printf" in the project
LCDFunctions.h
#ifndef LCDFunctionsHeader
#include <stdio.h>
#define LCDFunctionsHeader
#define LCDD0Pin 12
#define LCDD0Port GPIOB
#define LCDD1Pin 13
#define LCDD1Port GPIOB
#define LCDD2Pin 14
#define LCDD2Port GPIOB
#define LCDD3Pin 15
#define LCDD3Port GPIOB
#define LCDD4Pin 8
#define LCDD4Port GPIOA
#define LCDD5Pin 9
#define LCDD5Port GPIOA
#define LCDD6Pin 10
#define LCDD6Port GPIOA
#define LCDD7Pin 11
#define LCDD7Port GPIOA
#define LCDRegisterSelectPin 7
#define LCDRegisterSelectPort GPIOB
#define LCDReadWritePin 8
#define LCDReadWritePort GPIOB
#define LCDEnablePin 9
#define LCDEnablePort GPIOB
#define LCD_8_bit_dataline 0x38
#define LCD_DisplayON_CursorON 0x0F
#define LCD_Increment_Cursor_Position 0x06
#define LCD_Clear_Display 0x01
#define LCD_Next_Line_Command 0xC0
#define delayBeforeEnable 400
#define delayAfterEnable 800
void notExactTimedelay(int delayTime)
{
volatile int i;
for (i = 0; i < delayTime; ++i)
{
}
}
void SetupBitsAndPorts(GPIO_TypeDef *port, int pinNumber)
{
//Setup Reset and Clock Control Register
if(port == GPIOA)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //Enable clock to Port A
}
if(port == GPIOB)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; //Enable clock to Port B
}
if(port == GPIOC)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; //Enable clock to Port C
}
if(port == GPIOD)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN; //Enable clock to Port D
}
if(port == GPIOE)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPEEN; //Enable clock to Port E
}
if(pinNumber>7)//If pinNumber>7 setup CRH else set CRL
{
//Setting Port as Push-Pull Configuration by setting CNF bits
port->CRH &= ~((1<<(4*(pinNumber-8)+3))|(1<<(4*(pinNumber-8)+2)));
//Setting Port as Output and at Max. Speed by setting MODE bits
port->CRH |= (1<<(4*(pinNumber-8)+1))|(1<<((4*pinNumber)));
}
else
{
//Setting Port as Push-Pull Configuration by setting CNF bits
port->CRL &= ~((1<<(4*(pinNumber)+3))|(1<<(4*(pinNumber)+2)));
//Setting Port as Output and at Max. Speed by setting MODE bits
port->CRL |= (1<<(4*(pinNumber)+1))|(1<<((4*pinNumber)));
}
}
void InitializePortsForLCD()
{
SetupBitsAndPorts(LCDD0Port, LCDD0Pin);
SetupBitsAndPorts(LCDD1Port, LCDD1Pin);
SetupBitsAndPorts(LCDD2Port, LCDD2Pin);
SetupBitsAndPorts(LCDD3Port, LCDD3Pin);
SetupBitsAndPorts(LCDD4Port, LCDD4Pin);
SetupBitsAndPorts(LCDD5Port, LCDD5Pin);
SetupBitsAndPorts(LCDD6Port, LCDD6Pin);
SetupBitsAndPorts(LCDD7Port, LCDD7Pin);
SetupBitsAndPorts(LCDRegisterSelectPort, LCDRegisterSelectPin);
SetupBitsAndPorts(LCDReadWritePort, LCDReadWritePin);
SetupBitsAndPorts(LCDEnablePort, LCDEnablePin);
}
void LCDDisplaySetup()
{
LCDsendACommand(LCD_8_bit_dataline); //8 bit data line for initialization
LCDsendACommand(LCD_DisplayON_CursorON); //Display ON, Cursor ON
LCDsendACommand(LCD_Increment_Cursor_Position); //Increment Cursor Position
LCDsendACommand(LCD_Clear_Display); //Clear display and return cursor
}
void LCDEnable()
{
sendBitToPortAndPin(LCDEnablePort, LCDEnablePin, 1);
}
void LCDDisable()
{
notExactTimedelay(delayAfterEnable);
sendBitToPortAndPin(LCDEnablePort, LCDEnablePin, 0);
}
void LCDReadEnable()
{
sendBitToPortAndPin(LCDReadWritePort, LCDReadWritePin, 1);
notExactTimedelay(delayBeforeEnable);
}
void LCDWriteEnable()
{
sendBitToPortAndPin(LCDReadWritePort, LCDReadWritePin, 0);
notExactTimedelay(delayBeforeEnable);
}
void LCDcommandMode()
{
sendBitToPortAndPin(LCDRegisterSelectPort, LCDRegisterSelectPin, 0); //RS=0
}
void LCDcharacterMode()
{
sendBitToPortAndPin(LCDRegisterSelectPort, LCDRegisterSelectPin, 1); //RS=1
}
void sendAByteToLCDDataPins(char character)
{
//Test bit 0 of character and send it to PB12
sendBitToPortAndPin(LCDD0Port, LCDD0Pin, character & 0b00000001);
//Test bit 1 of character and send it to PB13
sendBitToPortAndPin(LCDD1Port, LCDD1Pin, character & 0b00000010);
//Test bit 2 of character and send it to PB14
sendBitToPortAndPin(LCDD2Port, LCDD2Pin, character & 0b00000100);
//Test bit 3 of character and send it to PB15
sendBitToPortAndPin(LCDD3Port, LCDD3Pin, character & 0b00001000);
//Test bit 4 of character and send it to PA8
sendBitToPortAndPin(LCDD4Port, LCDD4Pin, character & 0b00010000);
//Test bit 5 of character and send it to PA9
sendBitToPortAndPin(LCDD5Port, LCDD5Pin, character & 0b00100000);
//Test bit 6 of character and send it to PA10
sendBitToPortAndPin(LCDD6Port, LCDD6Pin, character & 0b01000000);
//Test bit 7 of character and send it to PA11
sendBitToPortAndPin(LCDD7Port, LCDD7Pin, character & 0b10000000);
}
void sendBitToPortAndPin(GPIO_TypeDef *port, int pinNumber, uint8_t bitState)
{
if(bitState) {
port->BSRR |= (1 << pinNumber);
} else {
port->BRR |= (1 << pinNumber);
}
}
void LCDsendACommand(char command)
{
LCDcommandMode();
LCDWriteEnable();
LCDEnable();
sendAByteToLCDDataPins(command);
LCDDisable();
}
void LCDsendACharacter(char character)
{
LCDcharacterMode();
LCDWriteEnable();
LCDEnable();
sendAByteToLCDDataPins(character);
LCDDisable();
}
void LCDsendAString(char* StringOfCharacters)
{
while(*StringOfCharacters)
{
LCDsendACharacter(*StringOfCharacters++);
}
}
void LCDsendAnInteger(int number,uint8_t digitLength)
{
char StringInt[digitLength];
sprintf(StringInt,"%d",number);
LCDsendAString(StringInt);
}
void LCDSendStringsIn2Lines(char* String1, char* String2)
{
LCDsendAString(String1);
LCDsendACommand(LCD_Next_Line_Command);
LCDsendAString(String2);
}
#endif
main.c
#include "stm32f1xx.h"
#include "LCDFunctions.h"
int main(void)
{
InitializePortsForLCD();
LCDDisplaySetup();
LCDsendAnInteger(4096,4);
while(1)
{
}
}
LCDFunctions.h
#ifndef LCDFunctionsHeader
#include <stdio.h>
#define LCDFunctionsHeader
#define LCDD0Pin 12
#define LCDD0Port GPIOB
#define LCDD1Pin 13
#define LCDD1Port GPIOB
#define LCDD2Pin 14
#define LCDD2Port GPIOB
#define LCDD3Pin 15
#define LCDD3Port GPIOB
#define LCDD4Pin 8
#define LCDD4Port GPIOA
#define LCDD5Pin 9
#define LCDD5Port GPIOA
#define LCDD6Pin 10
#define LCDD6Port GPIOA
#define LCDD7Pin 11
#define LCDD7Port GPIOA
#define LCDRegisterSelectPin 7
#define LCDRegisterSelectPort GPIOB
#define LCDReadWritePin 8
#define LCDReadWritePort GPIOB
#define LCDEnablePin 9
#define LCDEnablePort GPIOB
#define LCD_8_bit_dataline 0x38
#define LCD_DisplayON_CursorON 0x0F
#define LCD_Increment_Cursor_Position 0x06
#define LCD_Clear_Display 0x01
#define LCD_Next_Line_Command 0xC0
#define delayBeforeEnable 400
#define delayAfterEnable 800
void notExactTimedelay(int delayTime)
{
volatile int i;
for (i = 0; i < delayTime; ++i)
{
}
}
void SetupBitsAndPorts(GPIO_TypeDef *port, int pinNumber)
{
//Setup Reset and Clock Control Register
if(port == GPIOA)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //Enable clock to Port A
}
if(port == GPIOB)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; //Enable clock to Port B
}
if(port == GPIOC)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; //Enable clock to Port C
}
if(port == GPIOD)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN; //Enable clock to Port D
}
if(port == GPIOE)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPEEN; //Enable clock to Port E
}
if(pinNumber>7)//If pinNumber>7 setup CRH else set CRL
{
//Setting Port as Push-Pull Configuration by setting CNF bits
port->CRH &= ~((1<<(4*(pinNumber-8)+3))|(1<<(4*(pinNumber-8)+2)));
//Setting Port as Output and at Max. Speed by setting MODE bits
port->CRH |= (1<<(4*(pinNumber-8)+1))|(1<<((4*pinNumber)));
}
else
{
//Setting Port as Push-Pull Configuration by setting CNF bits
port->CRL &= ~((1<<(4*(pinNumber)+3))|(1<<(4*(pinNumber)+2)));
//Setting Port as Output and at Max. Speed by setting MODE bits
port->CRL |= (1<<(4*(pinNumber)+1))|(1<<((4*pinNumber)));
}
}
void InitializePortsForLCD()
{
SetupBitsAndPorts(LCDD0Port, LCDD0Pin);
SetupBitsAndPorts(LCDD1Port, LCDD1Pin);
SetupBitsAndPorts(LCDD2Port, LCDD2Pin);
SetupBitsAndPorts(LCDD3Port, LCDD3Pin);
SetupBitsAndPorts(LCDD4Port, LCDD4Pin);
SetupBitsAndPorts(LCDD5Port, LCDD5Pin);
SetupBitsAndPorts(LCDD6Port, LCDD6Pin);
SetupBitsAndPorts(LCDD7Port, LCDD7Pin);
SetupBitsAndPorts(LCDRegisterSelectPort, LCDRegisterSelectPin);
SetupBitsAndPorts(LCDReadWritePort, LCDReadWritePin);
SetupBitsAndPorts(LCDEnablePort, LCDEnablePin);
}
void LCDDisplaySetup()
{
LCDsendACommand(LCD_8_bit_dataline); //8 bit data line for initialization
LCDsendACommand(LCD_DisplayON_CursorON); //Display ON, Cursor ON
LCDsendACommand(LCD_Increment_Cursor_Position); //Increment Cursor Position
LCDsendACommand(LCD_Clear_Display); //Clear display and return cursor
}
void LCDEnable()
{
sendBitToPortAndPin(LCDEnablePort, LCDEnablePin, 1);
}
void LCDDisable()
{
notExactTimedelay(delayAfterEnable);
sendBitToPortAndPin(LCDEnablePort, LCDEnablePin, 0);
}
void LCDReadEnable()
{
sendBitToPortAndPin(LCDReadWritePort, LCDReadWritePin, 1);
notExactTimedelay(delayBeforeEnable);
}
void LCDWriteEnable()
{
sendBitToPortAndPin(LCDReadWritePort, LCDReadWritePin, 0);
notExactTimedelay(delayBeforeEnable);
}
void LCDcommandMode()
{
sendBitToPortAndPin(LCDRegisterSelectPort, LCDRegisterSelectPin, 0); //RS=0
}
void LCDcharacterMode()
{
sendBitToPortAndPin(LCDRegisterSelectPort, LCDRegisterSelectPin, 1); //RS=1
}
void sendAByteToLCDDataPins(char character)
{
//Test bit 0 of character and send it to PB12
sendBitToPortAndPin(LCDD0Port, LCDD0Pin, character & 0b00000001);
//Test bit 1 of character and send it to PB13
sendBitToPortAndPin(LCDD1Port, LCDD1Pin, character & 0b00000010);
//Test bit 2 of character and send it to PB14
sendBitToPortAndPin(LCDD2Port, LCDD2Pin, character & 0b00000100);
//Test bit 3 of character and send it to PB15
sendBitToPortAndPin(LCDD3Port, LCDD3Pin, character & 0b00001000);
//Test bit 4 of character and send it to PA8
sendBitToPortAndPin(LCDD4Port, LCDD4Pin, character & 0b00010000);
//Test bit 5 of character and send it to PA9
sendBitToPortAndPin(LCDD5Port, LCDD5Pin, character & 0b00100000);
//Test bit 6 of character and send it to PA10
sendBitToPortAndPin(LCDD6Port, LCDD6Pin, character & 0b01000000);
//Test bit 7 of character and send it to PA11
sendBitToPortAndPin(LCDD7Port, LCDD7Pin, character & 0b10000000);
}
void sendBitToPortAndPin(GPIO_TypeDef *port, int pinNumber, uint8_t bitState)
{
if(bitState) {
port->BSRR |= (1 << pinNumber);
} else {
port->BRR |= (1 << pinNumber);
}
}
void LCDsendACommand(char command)
{
LCDcommandMode();
LCDWriteEnable();
LCDEnable();
sendAByteToLCDDataPins(command);
LCDDisable();
}
void LCDsendACharacter(char character)
{
LCDcharacterMode();
LCDWriteEnable();
LCDEnable();
sendAByteToLCDDataPins(character);
LCDDisable();
}
void LCDsendAString(char* StringOfCharacters)
{
while(*StringOfCharacters)
{
LCDsendACharacter(*StringOfCharacters++);
}
}
void LCDsendAnInteger(int number,uint8_t digitLength)
{
char StringInt[digitLength];
sprintf(StringInt,"%d",number);
LCDsendAString(StringInt);
}
void LCDSendStringsIn2Lines(char* String1, char* String2)
{
LCDsendAString(String1);
LCDsendACommand(LCD_Next_Line_Command);
LCDsendAString(String2);
}
#endif
main.c
#include "stm32f1xx.h"
#include "LCDFunctions.h"
int main(void)
{
InitializePortsForLCD();
LCDDisplaySetup();
LCDsendAnInteger(4096,4);
while(1)
{
}
}
Comments
Post a Comment