G’day all,
has anyone had any issues using the sprintf() function when compiling with WinAVR. I try to compile with a float in the data format and WinAVR gives me a warning saying that the compiler was expecting a double instead it has been presented with a float, however it does compile. When i run the prog it doesn’t give any value.
I can verify that the sprintf() function does work, cos when you change the appropriate data type values for an int, it works like a charm.
The program is for a simple voltmeter for an Atmega8. it uses ADC0 and the LCD is connected to PORTD.
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include "lcd.h"
#include <util/delay.h>
#define F_CPU 16000000UL // 16 MHz
void delay_ms(uint16_t x); //General purpose delay
int main(void)
{
float adc_data = 0,
volts = 0;
char lcd_buff[20];
int j =0;
DDRD = 0x00; //makes all outputs
PORTD = 0x00;
/* initialize display, cursor off */
lcd_init(LCD_DISP_ON);
/* clear display and home cursor */
lcd_clrscr();
delay_ms(3000);
ADCSRA = 0b11100111;
ADMUX = 0b00000000; //0b 00000000
while(1==1)
{
ADCSRA=0b11100111; //0b 10001110
ADMUX=0b0000000;
delay_ms(2000);
adc_data = ADC;
//j = ADC;
//adc_data = adc_data*5/1023; //convert into volts
//adc_data = adc_data - 1.650; //shift midpoint
lcd_clrscr();
lcd_gotoxy(4,1);
sprintf(lcd_buff,"%1.4f",adc_data);
//sprintf(lcd_buff,"%4d",j);
lcd_puts(lcd_buff);
lcd_gotoxy(8,1);
lcd_puts("mV");
}
}
void delay_ms(uint16_t x)
{
uint8_t y, z;
for ( ; x > 0 ; x--){
for ( y = 0 ; y < 90 ; y++){
for ( z = 0 ; z < 6 ; z++){
asm volatile ("nop");
}
}
}
}
Any suggestions or info from people who have used sprintf() successfully with floats would be most appreciated.
Cheers,
Timmy