Laborboard: Unterschied zwischen den Versionen
Aus LaborWiki
 |
(+Demo-Code) |
||
Zeile 8: | Zeile 8: | ||
===Demo-Code=== | |||
Den folgenden C-Code einfach unter '''demo.c''' speichern und die folgende Makefile einfach unter '''Makefile'''. Und dann könnt ihr mit '''make''' das demo-Programm für den AVR erzeugen. Falls '''make''' mit einer Fehlermeldung ala ''***missing seperator (did you mean TAB instaed of 8 spaces?)'' abbricht, könnt ihr mit '''<pre>sed s/"    "/\\t/g Makefile > makefile</pre>''' das Problem beheben (zwischen den Anführungszeichen müssen entsprechend der Fehlermedung viele Leerzeichen sein). | |||
====C-Code==== | |||
<pre class="code"> | |||
//demo.c | |||
//This is a simple Demo for the Labor Board. | |||
//It displays 2 different light patterns on the LEDs, | |||
//and allows the pattern to be selected, and the speed to be controled | |||
//with the 4 keys. | |||
#include <inttypes.h> | |||
#include <avr/io.h> | |||
int main (void) | |||
{ | |||
DDRB = 0; //Port B all inputs | |||
PORTB = 0x0F; //Internal Pullups on on key inputs | |||
DDRC = 0xFF; //Port C all outputs | |||
PORTC = 0x01;Â //1 LED on to start of Patterns | |||
int x, delay=10000; char mode=0, ud=0; | |||
for ( ;; ){ //for ever | |||
//this is a time delay loop | |||
for(x=0; x< delay; x++){ | |||
//The volatile qualifier tells the compiler not to optimize | |||
//it away. | |||
volatile int y = 0; | |||
}; | |||
if(!mode){ | |||
//knight rider mode | |||
switch (ud){ | |||
case 0: //shift light left | |||
if( (PORTC<<=1) == 0x80 ){ //until bit7 is reached | |||
ud = 1; | |||
}; | |||
break; | |||
case 1: //shift bit right | |||
if( (PORTC>>=1) == 0x01 ){ //until bit0 is reached | |||
ud = 0; | |||
}; | |||
break; | |||
}; | |||
}else{ | |||
//scroll mode | |||
switch (ud){ | |||
case 0: | |||
PORTC|=(PORTC<<1);//shift additional ones in from left | |||
if(PORTC & 0x80)Â //until bit7 is reached | |||
ud = 1; | |||
break; | |||
case 1: | |||
PORTC<<=1; //shift zeros in | |||
if (PORTC == 0){//until all zeros | |||
ud = 0; | |||
PORTC = 0x01;//start over | |||
} | |||
break; | |||
}; | |||
} | |||
switch(PINB&0x0f){ | |||
case 0x0E: //Button 1 pressed | |||
if (delay<30000){ | |||
delay += 100; | |||
} | |||
break; | |||
case 0x0D: //Button 2 pressed | |||
if (delay>1000){ | |||
delay -= 100; | |||
} | |||
break; | |||
case 0x0B: //Button 3 pressed | |||
mode = 0; | |||
break; | |||
case 0x07: //Button 4 pressed | |||
mode = 1; | |||
break; | |||
}; | |||
}; | |||
} | |||
</pre> | |||
====Makefile==== | |||
<pre class="code"> | |||
PRGÂ Â Â Â Â Â = demo | |||
OBJÂ Â Â Â Â Â = demo.o | |||
MCU_TARGETÂ Â = atmega32 | |||
OPTIMIZEÂ Â Â = -Os | |||
DEFSÂ Â Â Â Â = | |||
LIBSÂ Â Â Â Â = | |||
# You should not have to change anything below here. | |||
CCÂ Â Â Â Â Â = avr-gcc | |||
# Override is only needed by avr-lib build system. | |||
override CFLAGSÂ Â Â Â = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) | |||
$(DEFS) | |||
override LDFLAGSÂ Â Â = -Wl,-Map,$(PRG).map | |||
OBJCOPYÂ Â Â Â = avr-objcopy | |||
OBJDUMPÂ Â Â Â = avr-objdump | |||
all: $(PRG).elf lst text eeprom | |||
$(PRG).elf: $(OBJ) | |||
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) | |||
clean: | |||
rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak | |||
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) | |||
lst:Â $(PRG).lst | |||
%.lst: %.elf | |||
$(OBJDUMP) -h -S $< > $@ | |||
# Rules for building the .text rom images | |||
text: hex bin srec | |||
hex:Â $(PRG).hex | |||
bin:Â $(PRG).bin | |||
srec: $(PRG).srec | |||
%.hex: %.elf | |||
$(OBJCOPY) -j .text -j .data -O ihex $< $@ | |||
%.srec: %.elf | |||
$(OBJCOPY) -j .text -j .data -O srec $< $@ | |||
%.bin: %.elf | |||
$(OBJCOPY) -j .text -j .data -O binary $< $@ | |||
# Rules for building the .eeprom rom images | |||
eeprom: ehex ebin esrec | |||
ehex:Â $(PRG)_eeprom.hex | |||
ebin:Â $(PRG)_eeprom.bin | |||
esrec: $(PRG)_eeprom.srec | |||
%_eeprom.hex: %.elf | |||
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ | |||
%_eeprom.srec: %.elf | |||
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ | |||
%_eeprom.bin: %.elf | |||
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ | |||
# Every thing below here is used by avr-libc's build system and can be ignored | |||
# by the casual user. | |||
FIG2DEVÂ Â Â Â Â Â Â Â = fig2dev | |||
EXTRA_CLEAN_FILESÂ Â Â = *.hex *.bin *.srec | |||
dox: eps png pdf | |||
eps: $(PRG).eps | |||
png: $(PRG).png | |||
pdf: $(PRG).pdf | |||
%.eps: %.fig | |||
$(FIG2DEV) -L eps $< $@ | |||
%.pdf: %.fig | |||
$(FIG2DEV) -L pdf $< $@ | |||
%.png: %.fig | |||
$(FIG2DEV) -L png $< $@ | |||
</pre> | |||