STM32F4 DISCOVERY to LCD
Here is my first working program for STM32F4 microprocessor
It was adopted from MSP430 library, but stm32f4 is just to fast do drive it.So i added some delay and soon i was able to write string and integer on lcd. it can do integer to 100.000.
here is code:
#include “stm32f4xx.h”
#define LCM_OUT GPIOB->ODR
#define LCM_PIN_RS GPIO_Pin_0
#define LCM_PIN_EN GPIO_Pin_1
#define LCM_PIN_D7 GPIO_Pin_7
#define LCM_PIN_D6 GPIO_Pin_6
#define LCM_PIN_D5 GPIO_Pin_5
#define LCM_PIN_D4 GPIO_Pin_4
GPIO_InitTypeDef GPIO_InitStructure;
#define LCM_PIN_MASK ((LCM_PIN_RS | LCM_PIN_EN | LCM_PIN_D7 | LCM_PIN_D6 | LCM_PIN_D5 | LCM_PIN_D4))
#define FALSE 0
#define TRUE 1
void __delay_cycles(int a)
{
int i = 0;
int f = 0;
while(f<a)
{
while(i<60)
{i++;}
f++;
}
}
void PulseLcm()
{
LCM_OUT &= ~LCM_PIN_EN;
__delay_cycles(250);
LCM_OUT |= LCM_PIN_EN;
__delay_cycles(250);
LCM_OUT &= (~LCM_PIN_EN);
__delay_cycles(250);
}
void SendByte(char ByteToSend, int IsData)
{
LCM_OUT &= (~LCM_PIN_MASK);
LCM_OUT |= (ByteToSend & 0xF0);
if (IsData == TRUE)
{
LCM_OUT |= LCM_PIN_RS;
}
else
{
LCM_OUT &= ~LCM_PIN_RS;
}
PulseLcm();
LCM_OUT &= (~LCM_PIN_MASK);
LCM_OUT |= ((ByteToSend & 0x0F) << 4);
if (IsData == TRUE)
{
LCM_OUT |= LCM_PIN_RS;
}
else
{
LCM_OUT &= ~LCM_PIN_RS;
}
PulseLcm();
}
void Cursor(char Row, char Col)
{
char address;
if (Row == 0)
{
address = 0;
}
else
{
address = 0x40;
}
address |= Col;
SendByte(0x80 | address, FALSE);
}
void ClearLcmScreen()
{
SendByte(0x01, FALSE);
SendByte(0x02, FALSE);
}
void InitializeLcm(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
LCM_OUT &= ~(LCM_PIN_MASK);
__delay_cycles(32000);
LCM_OUT &= ~LCM_PIN_RS;
LCM_OUT &= ~LCM_PIN_EN;
LCM_OUT = 0x20;
PulseLcm();
SendByte(0x28, FALSE);
SendByte(0x0E, FALSE);
SendByte(0x06, FALSE);
}
void PrintStr(char *Text)
{
char *c;
c = Text;
while ((c != 0) && (*c != 0))
{
SendByte(*c, TRUE);
c++;
}
}
void skaicius(int sk)
{
switch(sk)
{
case 0 : PrintStr(“0”);
break;
case 1 : PrintStr(“1”);
break;
case 2 : PrintStr(“2”);
break;
case 3 : PrintStr(“3”);
break;
case 4 : PrintStr(“4”);
break;
case 5 : PrintStr(“5”);
break;
case 6 : PrintStr(“6”);
break;
case 7 : PrintStr(“7”);
break;
case 8 : PrintStr(“8”);
break;
case 9 : PrintStr(“9″);
default: break;
}
}
void PrintInt(int sk)
{
__delay_cycles(999);
if(sk<10)
{
skaicius(sk);
}
else
{if(sk<100)
{
skaicius(sk/10);
skaicius(sk%10);
}
else{
if(sk<1000)
{
skaicius(sk/100);
skaicius((sk/10)% 10);
skaicius(sk % 10);
}
else{
if(sk<10000)
{
skaicius(sk/1000);
skaicius((sk/100)% 10);
skaicius((sk/10)%10);
skaicius(sk % 10);
}
else{
if(sk<100000)
{
skaicius(sk/10000);
skaicius((sk/1000)% 10);
skaicius((sk/100)%10);
skaicius((sk/10)%10);
skaicius(sk%10);
}
}
}
}
}
}
int main(void)
{
int a = 0;
__delay_cycles(32000);
InitializeLcm();
__delay_cycles(32000);
ClearLcmScreen();
__delay_cycles(32000);
PrintStr(” STM32F407VGT “);
Cursor(1,0);
PrintStr(“Hello World !!”);
a=0;
ClearLcmScreen();
while(a<100)
{
__delay_cycles(32000);
a++;
}
while (1)
{
if((GPIOA->IDR & 0x01) != 0)
{
PrintStr(“Not Pressed”);
Cursor(0,0);
}
else
{
PrintStr(“Pressed”);
Cursor(0,0);
}
}
}
//*****************************************
you can print by calling PrintInt(int a) function
