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 23:23] trapicki |
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 46: | Line 47: | ||
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: | 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> | <code> | ||
+ | #include <stdlib.h> | ||
#include <termios.h> | #include <termios.h> | ||
#include <fcntl.h> | #include <fcntl.h> | ||
Line 51: | Line 53: | ||
#include <time.h> | #include <time.h> | ||
#include <stdio.h> | #include <stdio.h> | ||
+ | #include <sys/select.h> | ||
#define STATE_OFF 0 | #define STATE_OFF 0 | ||
Line 64: | Line 67: | ||
int | int | ||
- | main(void) | + | main(int argc, char* argv[]) |
{ | { | ||
- | const char device[] = "/dev/ttyUSB0"; | + | char* device = argc < 2 ? "/dev/ttyUSB0" : argv[1]; |
- | int fd; | + | int fd = open(device, O_RDWR); |
- | const struct timespec sleeptime = {0, 100000000}; // 100ms | + | |
- | + | ||
- | fd = open(device, O_RDWR); | + | |
if (fd == 0) { | if (fd == 0) { | ||
- | printf("Could not open %s\n", device); | + | fprintf(stderr, "Could not open %s\n", device); |
- | return 1; | + | return EXIT_FAILURE; |
} | } | ||
| | ||
- | printf ("Resetting %s\n", device); | ||
setDTRState(fd, STATE_ON); | setDTRState(fd, STATE_ON); | ||
- | nanosleep(&sleeptime, NULL); | + | struct timeval sleeptime = {0, 100000}; // 100ms |
+ | select(0, NULL, NULL, NULL, &sleeptime); | ||
setDTRState(fd, STATE_OFF); | setDTRState(fd, STATE_OFF); | ||
- | nanosleep(&sleeptime, NULL); | + | sleeptime.tv_sec = 0; |
+ | sleeptime.tv_usec = 100000; | ||
+ | select(0, NULL, NULL, NULL, &sleeptime); | ||
setDTRState(fd, STATE_ON); | setDTRState(fd, STATE_ON); | ||
close(fd); | close(fd); | ||
+ | |||
+ | return EXIT_SUCCESS; | ||
} | } | ||
</code> | </code> | ||
Line 92: | Line 96: | ||
<code>reset_tty; make upload</code> und zurücklehnen | <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> | ||
---- | ---- | ||
{{tag>workshop vortrag}} | {{tag>workshop vortrag}} |
realraum Graz, Brockmanngasse 15, 8010 Graz, realraum - Verein für Technik in Kultur und Gesellschaft