Analog Digital Umwandlung, einstiegs Code.

Gast #4444386
Lesenswert?

Hi,


habe hier ein ADC Beispiel das einfach nicht funktionieren will.
1
#include <avr/io.h> //Defines pins, ports, etc.
2
#include <util/delay.h>
3

4
static inline void initADC0(void) {
5
  ADMUX |= (1 << REFS0); //reference voltage on AVCC
6
  //ADCSRA |= (1 << ADPS1) | (1 << ADPS0); //ADC clock prescaler /8
7
    ADCSRA |= (1 << ADPS1) | (1 << ADPS2); //ADC clock prescaler /64
8

9
  ADCSRA |= (1 << ADEN); //enables the ADC
10
}
11

12
int main(void) {
13
  uint16_t potentiometerValue;
14
  uint16_t threshold_level;
15
  threshold_level= 0b10000000;
16
  DDRD |= (1 << PD7); //Data Direction Register B: writing a 1 to the bit enables output
17

18
  initADC0();
19

20
  while (1) {
21
    ADCSRA |= (1 << ADSC); //start ADC conversion
22
    loop_until_bit_is_clear(ADCSRA, ADSC); //wait until ADC conversion is done
23
    potentiometerValue= ADC; //read ADC value in
24
    if (potentiometerValue > threshold_level) {
25
      PORTD= 0b10000000; //turn on LED attached to port PB0
26
    }
27
    else {
28
      PORTD= 0b00000000; //turn off LED attached to port PB0
29
    }
30
      return (0); //this line is never actually reached
31
    }
32
}
Quelle: 
http://www.learningaboutelectronics.com/Articles/AVR-potentiometer-circuit.php
Leicht abgewandelt.

Verkabelung angepasst an Atmega32, ADC0 = PA0, led out = PD7


Laufen soll es auf einem Atmega32 mit 8Mhz und einem prescalar von 64 
für 125.000hz Abtastrate.
Eine LED ist an PD7 angeschlossen, soll an sein wenn der ADC0 über 128 
steht und aus darunter.
Momentan ist die LED immer an.


Was läuft hier falsch?



Zum kompilieren verwende ich diese Makefile:
1
# Hey Emacs, this is a -*- makefile -*-
2
#
3
# WinAVR makefile written by Eric B. Weddington, J�rg Wunsch, et al.
4
# Released to the Public Domain
5
# Please read the make user manual!
6
#
7
# Additional material for this makefile was submitted by:
8
#  Tim Henigan
9
#  Peter Fleury
10
#  Reiner Patommel
11
#  Sander Pool
12
#  Frederik Rouleau
13
#  Markus Pfaff
14
#
15
# On command line:
16
#
17
# make all = Make software.
18
#
19
# make clean = Clean out built project files.
20
#
21
# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).
22
#
23
# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio
24
#                4.07 or greater).
25
#
26
# make program = Download the hex file to the device, using avrdude.  Please
27
#                customize the avrdude settings below first!
28
#
29
# make filename.s = Just compile filename.c into the assembler code only
30
#
31
# To rebuild project do "make clean" then "make all".
32
#
33

34
# mth 2004/09 
35
# Differences from WinAVR 20040720 sample:
36
# - DEPFLAGS according to Eric Weddingtion's fix (avrfreaks/gcc-forum)
37
# - F_OSC Define in CFLAGS and AFLAGS
38

39

40
# MCU name
41
#MCU = atmega168
42
DEVICE = atmega32
43
MCU = $(DEVICE)
44

45
# Main Oscillator Frequency
46
# This is only used to define F_OSC in all assembler and c-sources.
47
F_OSC = 8000000
48
F_CPU = $(F_OSC)
49
CFLAGS = -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
50

51
# Output format. (can be srec, ihex, binary)
52
FORMAT = ihex
53

54
# Target file name (without extension).
55
TARGET = poti
56

57

58
# List C source files here. (C dependencies are automatically generated.)
59
SRC = $(TARGET).c
60

61

62
# List Assembler source files here.
63
# Make them always end in a capital .S.  Files ending in a lowercase .s
64
# will not be considered source files but generated files (assembler
65
# output from the compiler), and will be deleted upon "make clean"!
66
# Even though the DOS/Win* filesystem matches both .s and .S the same,
67
# it will preserve the spelling of the filenames, and gcc itself does
68
# care about how the name is spelled on its command-line.
69
ASRC = 
70

71

72

73
# Optimization level, can be [0, 1, 2, 3, s]. 
74
# 0 = turn off optimization. s = optimize for size.
75
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
76
OPT = s
77

78
# Debugging format.
79
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
80
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
81
#DEBUG = stabs
82
DEBUG = dwarf-2
83

84
# List any extra directories to look for include files here.
85
#     Each directory must be seperated by a space.
86
EXTRAINCDIRS = 
87

88

89
# Compiler flag to set the C Standard level.
90
# c89   - "ANSI" C
91
# gnu89 - c89 plus GCC extensions
92
# c99   - ISO C99 standard (not yet fully implemented)
93
# gnu99 - c99 plus GCC extensions
94
CSTANDARD = -std=gnu99
95

96
# Place -D or -U options here
97
CDEFS =
98

99
# Place -I options here
100
CINCS =
101

102

103
# Compiler flags.
104
#  -g*:          generate debugging information
105
#  -O*:          optimization level
106
#  -f...:        tuning, see GCC manual and avr-libc documentation
107
#  -Wall...:     warning level
108
#  -Wa,...:      tell GCC to pass this to the assembler.
109
#    -adhlns...: create assembler listing
110
CFLAGS += -g$(DEBUG)
111
CFLAGS += $(CDEFS) $(CINCS)
112
CFLAGS += -O$(OPT)
113
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
114
CFLAGS += -Wall -Wextra -Wstrict-prototypes
115
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
116
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
117
CFLAGS += $(CSTANDARD)
118
#CFLAGS += -DF_OSC=$(F_OSC)
119

120

121

122
# Assembler flags.
123
#  -Wa,...:   tell GCC to pass this to the assembler.
124
#  -ahlms:    create listing
125
#  -gstabs:   have the assembler create line number information; note that
126
#             for use in COFF files, additional information about filenames
127
#             and function names needs to be present in the assembler source
128
#             files -- see avr-libc docs [FIXME: not yet described there]
129
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
130
ASFLAGS += -DF_OSC=$(F_OSC)
131

132

133
#Additional libraries.
134

135
# Minimalistic printf version
136
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
137

138
# Floating point printf version (requires MATH_LIB = -lm below)
139
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
140

141
PRINTF_LIB = 
142

143
# Minimalistic scanf version
144
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
145

146
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
147
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
148

149
SCANF_LIB = 
150

151
MATH_LIB = -lm
152

153
# External memory options
154

155
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
156
# used for variables (.data/.bss) and heap (malloc()).
157
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
158

159
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
160
# only used for heap (malloc()).
161
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
162

163
EXTMEMOPTS =
164

165
# Linker flags.
166
#  -Wl,...:     tell GCC to pass this to linker.
167
#    -Map:      create map file
168
#    --cref:    add cross reference to  map file
169
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
170
LDFLAGS += $(EXTMEMOPTS)
171
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
172

173

174

175

176
# Programming support using avrdude. Settings and variables.
177

178
# Programming hardware: alf avr910 avrisp bascom bsd 
179
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
180
#
181
# Type: avrdude -c ?
182
# to get a full listing.
183
#
184
AVRDUDE_PROGRAMMER = stk500
185

186
# com1 = serial port. Use lpt1 to connect to parallel port.
187
AVRDUDE_PORT = com1    # programmer connected to serial device
188

189
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
190
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
191

192

193
# Uncomment the following if you want avrdude's erase cycle counter.
194
# Note that this counter needs to be initialized first using -Yn,
195
# see avrdude manual.
196
#AVRDUDE_ERASE_COUNTER = -y
197

198
# Uncomment the following if you do /not/ wish a verification to be
199
# performed after programming the device.
200
#AVRDUDE_NO_VERIFY = -V
201

202
# Increase verbosity level.  Please use this when submitting bug
203
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> 
204
# to submit bug reports.
205
#AVRDUDE_VERBOSE = -v -v
206

207
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
208
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
209
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
210
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
211

212

213

214
# ---------------------------------------------------------------------------
215

216
# Define directories, if needed.
217
DIRAVR = c:/winavr
218
DIRAVRBIN = $(DIRAVR)/bin
219
DIRAVRUTILS = $(DIRAVR)/utils/bin
220
DIRINC = .
221
DIRLIB = $(DIRAVR)/avr/lib
222

223

224
# Define programs and commands.
225
SHELL = sh
226
CC = avr-gcc
227
OBJCOPY = avr-objcopy
228
OBJDUMP = avr-objdump
229
SIZE = avr-size
230
NM = avr-nm
231
AVRDUDE = avrdude
232
REMOVE = rm -f
233
COPY = cp
234

235

236

237

238
# Define Messages
239
# English
240
MSG_ERRORS_NONE = Errors: none
241
MSG_BEGIN = -------- begin --------
242
MSG_END = --------  end  --------
243
MSG_SIZE_BEFORE = Size before: 
244
MSG_SIZE_AFTER = Size after:
245
MSG_COFF = Converting to AVR COFF:
246
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
247
MSG_FLASH = Creating load file for Flash:
248
MSG_EEPROM = Creating load file for EEPROM:
249
MSG_EXTENDED_LISTING = Creating Extended Listing:
250
MSG_SYMBOL_TABLE = Creating Symbol Table:
251
MSG_LINKING = Linking:
252
MSG_COMPILING = Compiling:
253
MSG_ASSEMBLING = Assembling:
254
MSG_CLEANING = Cleaning project:
255

256

257

258

259
# Define all object files.
260
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 
261

262
# Define all listing files.
263
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
264

265

266
# Compiler flags to generate dependency files.
267
### GENDEPFLAGS = -Wp,-M,-MP,-MT,$(*F).o,-MF,.dep/$(@F).d
268
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
269

270
# Combine all necessary flags and optional flags.
271
# Add target processor to flags.
272
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
273
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
274

275

276

277

278

279
# Default target.
280
all: begin gccversion sizebefore build sizeafter finished end
281

282
build: elf hex eep lss sym
283

284
elf: $(TARGET).elf
285
hex: $(TARGET).hex
286
eep: $(TARGET).eep
287
lss: $(TARGET).lss 
288
sym: $(TARGET).sym
289

290

291

292
# Eye candy.
293
# AVR Studio 3.x does not check make's exit code but relies on
294
# the following magic strings to be generated by the compile job.
295
begin:
296
  @echo
297
  @echo $(MSG_BEGIN)
298

299
finished:
300
  @echo $(MSG_ERRORS_NONE)
301

302
end:
303
  @echo $(MSG_END)
304
  @echo
305

306

307
# Display size of file.
308
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
309
ELFSIZE = $(SIZE) -A $(TARGET).elf
310
sizebefore:
311
  @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
312

313
sizeafter:
314
  @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
315

316

317

318
# Display compiler version information.
319
gccversion : 
320
  @$(CC) --version
321

322

323

324
# Program the device.  
325
program: $(TARGET).hex $(TARGET).eep
326
  $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
327

328

329

330

331
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
332
COFFCONVERT=$(OBJCOPY) --debugging \
333
--change-section-address .data-0x800000 \
334
--change-section-address .bss-0x800000 \
335
--change-section-address .noinit-0x800000 \
336
--change-section-address .eeprom-0x810000 
337

338

339
coff: $(TARGET).elf
340
  @echo
341
  @echo $(MSG_COFF) $(TARGET).cof
342
  $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
343

344

345
extcoff: $(TARGET).elf
346
  @echo
347
  @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
348
  $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
349

350

351

352
# Create final output files (.hex, .eep) from ELF output file.
353
%.hex: %.elf
354
  @echo
355
  @echo $(MSG_FLASH) $@
356
  $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
357

358
%.eep: %.elf
359
  @echo
360
  @echo $(MSG_EEPROM) $@
361
  -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
362
  --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
363

364
# Create extended listing file from ELF output file.
365
%.lss: %.elf
366
  @echo
367
  @echo $(MSG_EXTENDED_LISTING) $@
368
  $(OBJDUMP) -h -S $< > $@
369

370
# Create a symbol table from ELF output file.
371
%.sym: %.elf
372
  @echo
373
  @echo $(MSG_SYMBOL_TABLE) $@
374
  $(NM) -n $< > $@
375

376

377

378
# Link: create ELF output file from object files.
379
.SECONDARY : $(TARGET).elf
380
.PRECIOUS : $(OBJ)
381
%.elf: $(OBJ)
382
  @echo
383
  @echo $(MSG_LINKING) $@
384
  $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
385

386

387
# Compile: create object files from C source files.
388
%.o : %.c
389
  @echo
390
  @echo $(MSG_COMPILING) $<
391
  $(CC) -c $(ALL_CFLAGS) $< -o $@ 
392

393

394
# Compile: create assembler files from C source files.
395
%.s : %.c
396
  $(CC) -S $(ALL_CFLAGS) $< -o $@
397

398

399
# Assemble: create object files from assembler source files.
400
%.o : %.S
401
  @echo
402
  @echo $(MSG_ASSEMBLING) $<
403
  $(CC) -c $(ALL_ASFLAGS) $< -o $@
404

405

406

407
# Target: clean project.
408
clean: begin clean_list finished end
409

410
clean_list :
411
  @echo
412
  @echo $(MSG_CLEANING)
413
  $(REMOVE) $(TARGET).hex
414
  $(REMOVE) $(TARGET).eep
415
  $(REMOVE) $(TARGET).obj
416
  $(REMOVE) $(TARGET).cof
417
  $(REMOVE) $(TARGET).elf
418
  $(REMOVE) $(TARGET).map
419
  $(REMOVE) $(TARGET).obj
420
  $(REMOVE) $(TARGET).a90
421
  $(REMOVE) $(TARGET).sym
422
  $(REMOVE) $(TARGET).lnk
423
  $(REMOVE) $(TARGET).lss
424
  $(REMOVE) $(OBJ)
425
  $(REMOVE) $(LST)
426
  $(REMOVE) $(SRC:.c=.s)
427
  $(REMOVE) $(SRC:.c=.d)
428
  $(REMOVE) .dep/*
429

430

431

432
# Include the dependency files.
433
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
434

435

436
# Listing of phony targets.
437
.PHONY : all begin finish end sizebefore sizeafter gccversion \
438
build elf hex eep lss sym coff extcoff \
439
clean clean_list program
Gast #4444393
Lesenswert?

Ok, der Code war jetzt nicht schön formatiert. An schlechten Code hatte 
ich nicht gleich dabei gedacht.

mit dem return(0) ausdokumentiert funktioniert der Code, halbwegs.
Der Bereich in dem die LED aus ist ist relativ klein, vielleicht ein 
viertel der Poti Drehung.
Gast #4444424
Lesenswert?

julius schrieb:
> Was läuft hier falsch?
>
> Zum kompilieren verwende ich diese Makefile:
> ...

Für Make-Files in der Länge gibt es die Funktion Anhang. Wer soll denn 
beim Lesen durch den ganzen Kram durchscrollen.

Erst Nachdenken ...

Viel wichtiger wäre zu wissen, ob z.B. loop_until_bit_is_clear() tut, 
was es soll und ob dein µC tatsächlich mit 8MHz läuft.
Gast #4444910
Lesenswert?

julius schrieb:
> An schlechten Code hatte
> ich nicht gleich dabei gedacht.

Wer ernsthaft mit Quellcode arbeitet, würde nie unformatierten Quellcode 
irgendwohin klatschen, als Lehrbeispiel schon gar nicht. Mit 
formatiertem Code  sieht man mit minimaler Erfahrung das falsche return 
innerhalb der Endlos-Main-Schleife sofort.

Immerhin hast Du aus dem Beispiel gelernt, dass Kommentare völlig 
irreführend sein können ;)


julius schrieb:
> Was ist verkehrt am LM741?

Zu seiner Zeit nichts. Aber seit vielen vielen Jahren ist das einzige 
Problem, dessen Lösung 741 heißt, dieses: "Ich habe noch einen Haufen 
741 in der Bastelkiste und will die verbasteln." In allen anderen Fällen 
gibt es geeignetere OPVs. Wenn der 741 in einer Lehr-Seite auftaucht, 
heißt das mit großer Wahrscheinlichkeit, dass selbige mehr oder minder 
kompetent von irgendwo zusammenkopiert ist.

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