cancel
Showing results for 
Search instead for 
Did you mean: 

how to display float on 7segment 8digit led MAX7219? ex (0.0001)

parth kothiya
Senior
void write_max (uint8_t address, uint8_t data)
{
	HAL_GPIO_WritePin (cs_GPIO_Port, cs_Pin, GPIO_PIN_RESET);  // pull the CS pin LOW
	write_byte (address);
	write_byte (data);
	HAL_GPIO_WritePin (cs_GPIO_Port, cs_Pin, GPIO_PIN_SET);  // pull the CS pin HIGH
}
 
void display_value(uint8_t address, uint32_t number, uint8_t dp_at){
	uint8_t k = 0;
	dp_at++;
	if (number > 0) {
		for (int x = address; number != 0; ++x) {
			k = number % 10;
			k = dp_at==x ?(k|0b10000000):k;
			write_max(x,k);
			number = number/10;
		}
	} else {
		number = dp_at>1 ? number|0b10000000 : number;
		write_max(address,number);
	}
}
 
void display_floatvalue(float Value,uint8_t precision){
	uint32_t prmultiplayer=0,partInt=0,fractional=0;
	uint8_t ndigit = 1,k=0;
	prmultiplayer = pow(10,precision);
	partInt = floorf(Value);
	fractional = (Value-partInt)*prmultiplayer;
	if(fractional>0){
		while(fractional != 0){
			k = fractional % 10;
			write_max(ndigit,k);
			fractional = fractional/10;
			ndigit++;
		}
	}else {
		write_max(1,0);
	}
	k = ndigit-1;
	display_value(ndigit,partInt,k);
}
 
int main(void){
while(0){
led_desplay_float(0.001);
}
}

above code not work properly

led_desplay_float(0.001);

but it shows 0.1

so how can i display exact value 0.001

4 REPLIES 4
KnarfB
Principal III

use sprintf?

I have tried this function but not work.

above code works fine ex 12.345 , 2 it shows 12.34

if i input ex 0.01 , 2 it shows 0.1 wrong value not add zero before 1.

sprintf and friends should work with float if you supply "-u _printf_float" as a linker option. There is a check box in the STM32CubeIDE C Build Tool Settings.

Apart from the need to have a libc version with floating point print support ...

You are using a wild mixture of float and double all around in your code and your posts.

Are you aware of the differences ?

sprintf (buf,"%8.3f", (double) fvar);

POSIX supports only double arguments for the e,E,f,F,g,G conversions. The Cube-supported lib might be different.