sprintf float übergeben geht nicht. Wieso?

OP #4513617
Lesenswert?

Guten Tag,
Ich hatte da mal ne Frage zu ein kleinen Problem, und zwar versuche ich 
grade mir am LCD die Spannung anzuzeigen zu lassen. Denn Wert dafür 
bekomme ich vom ein Poti denn ich über dem ADC auslese. Ich habe mir das 
Arduino Tutorial "ReadAnalogVoltage" angesehen und habe dieses in Atmel 
Studio übernommen.

In Arduino:
1
  
2
float voltage = sensorValue * (5.0 / 1023.0);

sensorValue ist in dem Fall analogRead(A0).

In Atmel Studio:
1
 adc_row = readADC(5);
2
 float voltage = adc_row * (5.0 / 1023.0);
3
 snprintf(volt, 100, "Voltage: %f V", voltage);
4
 lcd_string(volt);

volt ist ein char mit [100].

Auf dem LCD wird nur "Voltage: ? V" angezeigt.

Habt ihr da ne Idee was da falsch ist?

Mfg Jan Hampel
Gast #4513655
Lesenswert?

Einfach in Makefile reinschreiben. Ich hänge mal als Beispiel ein 
komplettes Makefile an.
1
# Makefile for this AVR project
2

3
# make code       Compiles the source code
4
# make fuses      Program fuses
5
# make program    Program flash and eeprom
6

7
# make list       Create generated code listing
8
# make clean      Delete all generated files
9

10
# Programmer hardware settings for avrdude
11
# AVRDUDE_HW = -c avr910 -P /dev/ttyUSB0 -b 115200
12
# AVRDUDE_HW = -c arduino -P COM3 -b 57600
13
AVRDUDE_HW = -c avrispmkII -P usb -B16
14

15
# Name of the program without extension
16
PRG = HelloWorld
17

18
# Microcontroller type and clock frequency
19
MCU = attiny2313
20
F_CPU = 1000000
21

22
# Fuse settings
23
# Comment out if you are using a bootloader (e.g. in case of Arduino)
24
LFUSE = 0x64
25
HFUSE = 0xDF
26
EFUSE = 0xFF
27

28
# Source files, separated by space.
29
SRC = main.c
30

31

32
###################################################################
33
# You possibly do not need to change settings below this marker
34
###################################################################
35

36
# Binaries to be used
37
# You may add the path to them if they are not in the PATH variable.
38
CC       = avr-gcc
39
OBJCOPY  = avr-objcopy
40
OBJDUMP  = avr-objdump
41
AVRDUDE  = avrdude
42
AVR_SIZE = avr-size
43

44
# Do we need to write Eeprom? (yes/no)
45
EEPROM = no
46

47
# Libraries
48
#LIBS = -L path/to/libraries -llibrary1 -llibrary2
49

50
# Includes
51
#INCLUDES = -Ipath/to/include/files
52

53
# Compiler options for all c source files
54
CFLAGS = -std=c99 -Wall -O1 -mmcu=$(MCU) -DF_CPU=$(F_CPU) $(INCLUDES)
55

56
# Enable map file
57
# LDFLAGS += -Wl,-Map,$(PRG).map
58

59
# Enable floating-point support in printf
60
#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm
61

62
# Enable automatic removal of unused functions
63
CFLAGS += -ffunction-sections -fdata-sections
64
LDFLAGS += -Wl,--gc-sections
65

66
# Collect fuse operations for avrdude
67
ifdef FUSE
68
  FUSES += -U fuse:w:$(FUSE):m
69
endif
70
ifdef LFUSE
71
  FUSES += -U lfuse:w:$(LFUSE):m
72
endif
73
ifdef HFUSE
74
  FUSES += -U hfuse:w:$(HFUSE):m
75
endif
76
ifdef EFUSE
77
  FUSES += -U efuse:w:$(EFUSE):m
78
endif
79
ifdef FUSE0
80
  FUSES += -U fuse0:w:$(FUSE0):m
81
endif
82
ifdef FUSE1
83
  FUSES += -U fuse1:w:$(FUSE1):m
84
endif
85
ifdef FUSE2
86
  FUSES += -U fuse2:w:$(FUSE2):m
87
endif
88
ifdef FUSE3
89
  FUSES += -U fuse3:w:$(FUSE3):m
90
endif
91
ifdef FUSE4
92
  FUSES += -U fuse4:w:$(FUSE4):m
93
endif
94
ifdef FUSE5
95
  FUSES += -U fuse5:w:$(FUSE5):m
96
endif
97
ifdef FUSE6
98
  FUSES += -U fuse6:w:$(FUSE6):m
99
endif
100
ifdef FUSE7
101
  FUSES += -U fuse7:w:$(FUSE7):m
102
endif
103

104
# Default sections
105
ifeq ($(EEPROM),yes)
106
all: code eeprom
107
else
108
all: code
109
endif
110

111
# Program code
112
code: $(PRG).hex
113

114
# Eeprom content
115
eeprom: $(PRG)_eeprom.hex
116

117
# Generated code listing
118
list: $(PRG).lst
119

120
# Remove all generated files
121
clean:
122
  rm -rf *.o driver/*.o $(PRG).hex $(PRG).elf $(PRG).lst $(PRG).map $(PRG)_eeprom.hex
123

124
# Program flash memory with or without eeprom
125
ifeq ($(EEPROM),yes)
126
program: code eeprom
127
  $(AVRDUDE) -p $(MCU) $(AVRDUDE_HW) -U flash:w:$(PRG).hex:i -U eeprom:w:$(PRG)_eeprom.hex:i
128
else
129
program: code 
130
  $(AVRDUDE) -p $(MCU) $(AVRDUDE_HW) -U flash:w:$(PRG).hex:i
131
endif
132

133
# Program fuses
134
fuses:
135
  $(AVRDUDE) -p $(MCU) $(AVRDUDE_HW) $(FUSES)
136

137
$(PRG).elf: $(SRC:.c=.o)
138
  $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
139
  $(AVR_SIZE) $(PRG).elf
140

141
%.lst: %.elf
142
  $(OBJDUMP) -h -S $< > $@
143

144
%.hex: %.elf
145
  $(OBJCOPY) -j .text -j .data -O ihex $< $@
146

147
%_eeprom.hex: %.elf
148
  $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
OP #4513659
Lesenswert?

Stefan U. schrieb:
> Einfach in Makefile reinschreiben. Ich hänge mal als Beispiel ein
> komplettes Makefile an.

Hallo nochmal,
Ich habe grade das gefunden: 
https://www.mikrocontroller.net/articles/FAQ#Aktivieren_der_Floating_Point_Version_von_sprintf_bei_WinAVR_bzw_AVR-Studio

Flashen ist von 1940 kb auf 4220 kb angestiegen. macht habe nix viel 
mehr kommt dort aber auch nicht drauf.

Nun gibt er mit z.b 3.010000 aus. Ich habe es mit

float new_volt = (float)((int)(voltage * 100)) / 100;

Auf 2 Nachkommastellen gerundet. Würde aber gerne jetzt noch die letzen 
4 nullen weg haben. Das da dann z.b 4.10 V, 5.00 V, 1.82 V, etc... nur 
geht.

Nur weis nicht wie ich das umsetzten soll.
Persönliche Seite #4513690
Lesenswert?

%f kann (wie andere Formatspezifizierer für printf & Co. auch) mit 
zusätzlichen Angaben erweitert werden. Gewünschte Feldbreite und Anzahl 
der Nachkommastellen.

Ist die für die Ausgabe benötigte Anzahl von Zeichen kleiner als die 
Feldbreite, werden Leerzeichen vor der Zahl ausgegeben.

Ein Beispiel:
1
printf("%8.2f", 12.345);
2

3
 12345678
4
"   12.35"

Die Feldbreite kann auch weggelassen werden:
1
printf("%.2f", 12.345);
2

3
 12345678
4
"12.35"

Das alles steht in der Dokumentation von printf & Co. drin. Lies sie!

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren