0

I have MCU ATMega16 and i doing digital voltmeter. I want to display decimal number on LCD display. This is my code:

#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"
#include <stdlib.h>

    signed int temp = 0;
    double voltage = 0;
    char buffer[10];

int main(void)
{
    DDRD = 0b11111111;
    PORTD = 0b00000000;
    DDRB = 0b00001111;
    PORTB = 0b00000000;

    ADMUX = (0<<REFS1)|(1<<REFS0);
    ADCSRA |= (1<<ADEN)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0);  

    lcd_init(LCD_DISP_ON_CURSOR);
    _delay_ms(100);
    lcd_clrscr();
    lcd_home();
    lcd_puts("Voltage= ");

    while (1) 
    {   
        ADCSRA |= (1<<ADSC);
        while(ADCSRA & (1<<ADSC));
        ADCSRA |= (1<<ADIF);
        temp = ADCW;

        if(temp <= 0)
        {
            lcd_gotoxy(8, 0);
            lcd_puts("Null");
            _delay_ms(100);
        }
        else
        {
            voltage = (5.0 * temp * 5) / 1023;

            lcd_gotoxy(8, 0);
            sprintf(buffer, "%f", voltage);
            lcd_puts(buffer);
            _delay_ms(100);
        }
}
}

..and it works. if i measure the battery with 12.3 volts, then the programm will display on LCD value equals 12.31671. But, the programm displays decimal number because i changed project settings to works function "sprintf". How do i manually display decimal number on LCD display? Help please. I can't understand how to make it. If i return project settings to default and use "itoa" instead "sprintf", then programm will display a number 12. I need it, because i want to display value on 7 segment display in future.

Yeoha I
  • 71
  • 1
  • 3
  • Use int as voltage instead of double or cast it to int if you want just decimal numbers. – koper89 Dec 10 '16 at 20:35
  • Take a look at that answer to **["Arduino sprintf float not formatting"](http://stackoverflow.com/a/27652012/6945651)**. The useful function `dtostrf()` should solve your problem. – J. Piquard Dec 10 '16 at 20:42

1 Answers1

0

declare voltage as integer and your problem will be solved.