Printing an adc value on an lcd screen
Hello!
code at the end (the functions)
I am trying to get the value of a potar (to start) and to print it on a 4 bit lcd.
The lcd works great .To make it work, I made functions.
Ex to print:
sendString("Hello world!"); // print Hello world! to the lcd
To get the adc value I use
int adcVal;
adcVal = HAL_ADC_GetValue(&hadc1);
(the return value(retval) of this funtionc is "@retval Converted value" according to the comments on top of the function declaration)
If i do
sendString("adcVal");
it will just print adcVal
If i do
sendString(adcVal);
It doesn't do anything.
I also tried to put adcVal as a char;
I get it, I am trying to send an int on a function that works with hexa value of ascii code.
I am thinking of 2 solutions:
- Making a function that take an int (one number only) and change it to it's ascii value by doing 10 "if()" to checks every possibility. This seems long and not optimised.
- Putting each numbers of the value into an int tab and then trying to print the tab
(sendString take as only parameter a pointer to an int aka, an int tab)
I tried the last one by doing
adcVal = HAL_ADC_GetValue(&hadc1);
adcTab[0]=adcVal>>3;
adcTab[1]=adcVal>>2&9;
adcTab[2]=adcVal>>1&9;
adcTab[3]=adcVal&9;
adcTab[4]=0;
sendString(adcTab);
But nothing happened
the functions I use:
void numberToGpio (char number)// change a 4 bit number into gpio outputs
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_7, 0); //reset pins
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_6, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_5, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_4, 0);
if(number>=8)
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_7, 1);
number=number-8;
}
if(number>=4)
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_6, 1);
number=number-4;
}
if(number>=2)
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_5, 1);
number=number-2;
}
if(number>=1)
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_4, 1);
}
}
void send4bitCommand (char number) // send a 4 bit command
{
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_0, 0); //rs=0 ==> cmd
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_1, 0); //R/W=O ==> write
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 1); //En=1
HAL\_Delay(1);
numberToGpio (number);
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 0); //En=0
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_7, 0); //reset pins
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_6, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_5, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_4, 0);
}
void sendCommand (char number)
{
char lower=number & 0x0F;
char higher=(number>>4) & 0x0F;
send4bitCommand(higher);
send4bitCommand(lower);
}
void sendData (char number)
{
char lower=number & 0x0F;
char higher=(number>>4) & 0x0F;
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_0, 1); //rs=1 ==> data
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_1, 0); //R/W=O ==> write
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 1); //En=1
HAL\_Delay(1);
numberToGpio (higher);
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 0); //En=0
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_7, 0); //reset pins
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_6, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_5, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_4, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_0, 1); //rs=1 ==> data
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_1, 0); //R/W=O ==> write
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 1); //En=1
HAL\_Delay(1);
numberToGpio (lower);
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_2, 0); //En=0
HAL\_Delay(1);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_7, 0); //reset pins
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_6, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_5, 0);
HAL\_GPIO\_WritePin(GPIOD, GPIO\_PIN\_4, 0);
}
void initLCD()
{
//init sequence
HAL\_Delay(100);
send4bitCommand(0x3);
HAL\_Delay(5);
send4bitCommand(0x3);// no need to add delay, since i already added 1sec delay in the sendCommadn function
send4bitCommand(0x3);
send4bitCommand(0x2); //function set 4 bit
sendCommand(0x28);//function set 4 bit 2 lines, 5\*8
sendCommand(0x08);//forced to put 0x08 for the initialization ==> display off
sendCommand(0x01);//clear
HAL\_Delay(3);
sendCommand(0x06);//entry mode set incremente et shift cursor
//display control function to put the display on
sendCommand(0x0F);//the "real" cursor or display shift: Display, cursor and blink on
}
void sendString (char *str)
{
while (\*str) sendData (\*str++);
}
Posted Jul 9, 2019, 4:25 PM UTC · edited