Displaying Characters to LCD
LCDFunctions.h
#ifndef LCDFunctionsHeader
#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 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 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();
}
#endif
#ifndef LCDFunctionsHeader
#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 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 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();
}
#endif
Main Program
#include "stm32f1xx.h"
#include "LCDFunctions.h"
int main(void)
{
InitializePortsForLCD();
/*
LCDsendACommand(0b00111000); //8 bit data line for initialization
LCDsendACommand(0b00001100); //Display ON, Cursor OFF
LCDsendACommand(0b00000110); //Increment Cursor Position
LCDsendACommand(0b00000001); //Clear display and return cursor
*/
LCDsendACommand(0x38); //8 bit data line for initialization
LCDsendACommand(0x0F); //Display ON, Cursor OFF
LCDsendACommand(0x06); //Increment Cursor Position
LCDsendACommand(0b01); //Clear display and return cursor
LCDsendACharacter('M');
LCDsendACharacter('Y');
LCDsendACharacter(' ');
LCDsendACharacter('L');
LCDsendACharacter('C');
LCDsendACharacter('D');
LCDsendACharacter(' ');
LCDsendACharacter('I');
LCDsendACharacter('S');
LCDsendACharacter(' ');
LCDsendACharacter('G');
LCDsendACharacter('L');
LCDsendACharacter('O');
LCDsendACharacter('W');
LCDsendACharacter('I');
LCDsendACharacter('N');
LCDsendACommand(0xC0); //Next Line
LCDsendACharacter('G');
LCDsendACharacter(' ');
LCDsendACharacter('C');
LCDsendACharacter('O');
LCDsendACharacter('R');
LCDsendACharacter('R');
LCDsendACharacter('E');
LCDsendACharacter('C');
LCDsendACharacter('T');
LCDsendACharacter('L');
LCDsendACharacter('Y');
while(1)
{
}
}
Comments
Post a Comment