Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
workshop:arduino [2009-06-23 21:51] equinox |
workshop:arduino [2015-09-16 12:41] (current) |
||
---|---|---|---|
Line 13: | Line 13: | ||
* [[http://arduino.cc/en/Guide/HomePage | Getting Started]] | * [[http://arduino.cc/en/Guide/HomePage | Getting Started]] | ||
* [[http://arduino.cc/en/Reference/HomePage| Arduino Language Reference]] | * [[http://arduino.cc/en/Reference/HomePage| Arduino Language Reference]] | ||
- | * [[http://www.atmel.com/dyn/resources/prod_documents/doc8025.pdf| Atmega328 Datasheet]] | + | * [[http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf| Atmega328 Datasheet]] |
* [[http://arduino.cc/en/uploads/Main/arduino-duemilanove-schematic.pdf| Arduino Duemilanove Schematics]] | * [[http://arduino.cc/en/uploads/Main/arduino-duemilanove-schematic.pdf| Arduino Duemilanove Schematics]] | ||
+ | |||
==== Nützliche Scripts ==== | ==== Nützliche Scripts ==== | ||
Line 40: | Line 41: | ||
<code> | <code> | ||
# /etc/init.d/udev reload | # /etc/init.d/udev reload | ||
+ | </code> | ||
+ | |||
+ | === Seriellen Port reseten === | ||
+ | |||
+ | Ein kleines Programm, um einen Arduino zu reseten. Funktionsweise: Auf den Duemilanove und anderen Boards ist die DTR-Leitung über eine Kondenstaor mit dem Reset-Pin verbunden. Wird die Leitung für 100ms auf low gezogen, enstehen ein negativer und eine positiver Spannungspuls, die ausreichen, das Reset durchzführen. Das passiert beim Anstecken, oder aber wenn folgendes Programm ausgeführt wird: | ||
+ | <code> | ||
+ | #include <stdlib.h> | ||
+ | #include <termios.h> | ||
+ | #include <fcntl.h> | ||
+ | #include <sys/ioctl.h> | ||
+ | #include <time.h> | ||
+ | #include <stdio.h> | ||
+ | #include <sys/select.h> | ||
+ | |||
+ | #define STATE_OFF 0 | ||
+ | #define STATE_ON 1 | ||
+ | |||
+ | void setDTRState (int fd, int state) { | ||
+ | int flags; | ||
+ | |||
+ | ioctl(fd, TIOCMGET, &flags); | ||
+ | flags = (state == STATE_ON ? flags | TIOCM_DTR : flags & ~TIOCM_DTR); | ||
+ | ioctl(fd, TIOCMSET, &flags); | ||
+ | } | ||
+ | |||
+ | int | ||
+ | main(int argc, char* argv[]) | ||
+ | { | ||
+ | char* device = argc < 2 ? "/dev/ttyUSB0" : argv[1]; | ||
+ | int fd = open(device, O_RDWR); | ||
+ | if (fd == 0) { | ||
+ | fprintf(stderr, "Could not open %s\n", device); | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | | ||
+ | setDTRState(fd, STATE_ON); | ||
+ | struct timeval sleeptime = {0, 100000}; // 100ms | ||
+ | select(0, NULL, NULL, NULL, &sleeptime); | ||
+ | setDTRState(fd, STATE_OFF); | ||
+ | sleeptime.tv_sec = 0; | ||
+ | sleeptime.tv_usec = 100000; | ||
+ | select(0, NULL, NULL, NULL, &sleeptime); | ||
+ | setDTRState(fd, STATE_ON); | ||
+ | close(fd); | ||
+ | |||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
+ | </code> | ||
+ | Compilieren mit | ||
+ | <code>gcc reset_tty.c -o reset_tty</code> | ||
+ | |||
+ | Damit wird das Flashen des Arduino um einiges leichter: | ||
+ | Statt <code>make upload</code> und wie ein Haftlmacher aufpassen wann der Reset-Button, jetzt neu; | ||
+ | <code>reset_tty; make upload</code> und zurücklehnen | ||
+ | |||
+ | === Besipiel: Lauflicht === | ||
+ | |||
+ | Je eine LED auf Pin 2 bis 7 schalten. | ||
+ | **Achtung: Alle Anoden gemeinsam auf +5V legen!** | ||
+ | |||
+ | Die Anoden der LEDs werden auf +5V gelegt, zum Einschalten der LED muss der entsprechende PIN deshalb auf LOW gesetzt werden. | ||
+ | |||
+ | <code> | ||
+ | #define LEDS_OFF 0xFC | ||
+ | #define LEDS_ON 0x00 | ||
+ | #define LEDS_INIT 0xF8 | ||
+ | #define INTERVALL 50 | ||
+ | |||
+ | byte counter; | ||
+ | byte direction; | ||
+ | |||
+ | void setup(){ | ||
+ | pinMode(13, OUTPUT); | ||
+ | digitalWrite(13, LOW); | ||
+ | |||
+ | counter = 0; | ||
+ | direction = 1; | ||
+ | |||
+ | DDRD = 0xFC; | ||
+ | PORTD = LEDS_INIT; | ||
+ | } | ||
+ | |||
+ | byte led_table(byte led){ | ||
+ | switch(led){ | ||
+ | case 0: return LEDS_INIT; | ||
+ | case 1: return 0xF4; | ||
+ | case 2: return 0xEC; | ||
+ | case 3: return 0xDC; | ||
+ | case 4: return 0xBC; | ||
+ | case 5: return 0x7C; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | byte get_next_state(){ | ||
+ | if(counter == 5) | ||
+ | direction = 0; | ||
+ | else if(counter == 0) | ||
+ | direction = 1; | ||
+ | |||
+ | return (direction)?(led_table(++counter)):(led_table(--counter)); | ||
+ | } | ||
+ | |||
+ | byte get_next_state_oneway(){ | ||
+ | counter++; | ||
+ | counter %= 6; | ||
+ | return led_table(counter); | ||
+ | } | ||
+ | |||
+ | void loop(){ | ||
+ | PORTD = get_next_state(); | ||
+ | //PORTD = get_next_state_oneway(); | ||
+ | delay(INTERVALL); | ||
+ | |||
+ | } | ||
+ | |||
</code> | </code> | ||
---- | ---- | ||
{{tag>workshop vortrag}} | {{tag>workshop vortrag}} |
realraum Graz, Brockmanngasse 15, 8010 Graz, realraum - Verein für Technik in Kultur und Gesellschaft