cancel
Showing results for 
Search instead for 
Did you mean: 

Some questions about using float, snprintf, sprtinf

ughr1
Associate II

I would like to advise you, I create software for the STM32L151C8 microcontroller that will control the LCD segment display via the I2C PCF85176 driver

Design:

- battery powered

- configurable by the attached bluetooth module and keeping configuration values ​​in EEPROM / FLASH

- pulse ounter on the interrupt and holding the value in the flash, the device does not reset after inserting the battery

- displaying on LCD pulse values ​​after simple mathematical operations (addition, multiplication)

My questions:

  • using the snprintf / sprintf function to convert floats to char array - it uses a lot of memory?
  • using float (this MCU don't have FPU i should using uint and only add dot on LCD?)
  • I choose good MCU for this application?
void LCD_Update(void) {
	uint8_t msg[2+20];
	uint8_t *display_mem=msg+2;
	msg[0]=CMD_OPCODE_DEVICE_SELECT | 0 | CMD_CONTINUE;
	msg[1]=CMD_OPCODE_LOAD_DATA_POINTER | 0;
	memset(display_mem, 0, 20);
 
	int i,j;
	for(i=0;i<8;i++) {
		for(j=0;j<4;j++) {
			uint8_t nibble=(display_digits[i]>>(4*j))&0xf;
			uint8_t nibbleaddr=digit_addrs[i][j];
			uint8_t byteaddr=nibbleaddr>>1;
			nibbleaddr&=1;
			display_mem[byteaddr]|=nibble<<(4*nibbleaddr);
		}
	}
	HAL_I2C_Master_Transmit(&hi2c1, PCF8576_ADDR, msg, sizeof(msg), 100);
}
 
void LCD_Clear(void) {
	for(int i=0;i<=8;i++) {
		display_digits[i]=0;
	}
	LCD_Update();
}
 
void LCD_Print(char* str) {
	int idx=0;
	for(int i=0;i<=8;i++) {
		char c=str[i];
		if(c>='A' && c<='Z') {
			display_digits[idx]=alpha[c-'A'];
		}
		else if (c=='.') {
			display_digits[idx-1]=numsDot[str[i-1]-'0'];
			idx--;
		}
		else if(c>='0' && c<='9') {
			display_digits[idx]=nums[c-'0'];
		}
		idx++;
	}
	LCD_Update();
}
 
void LCD_PrintInt(int value) {
	char str[8];
	sprintf(str, "%d", value);
	LCD_Print(str);
}
 
void LCD_PrintFloat(float value, uint8_t length) {
	char str[length];
	snprintf(str, length + 1, "%f", value);
	LCD_Print(str);
}

Above is fragment of my program to control LCD

1 REPLY 1
Ozone
Lead

> I choose good MCU for this application?

That depends on your application.

If the MCU can fulfill the timing requirements, and your application fits in the Flash space, you are fine.

> using the snprintf / sprintf function to convert floats to char array - it uses a lot of memory?

> using float (this MCU don't have FPU i should using uint and only add dot on LCD?)

The toolchain will add emulation code if necessary, increasing code size.

A FPU does help very little for printf-like functions.

Most CPU-intensive calculation routines can be implemented with integer, which is usually faster than FPU-based calculations.