Arduino/Arduino2AVR: Unterschied zwischen den Versionen
aus Metalab Wiki, dem offenen Zentrum für meta-disziplinäre Magier und technisch-kreative Enthusiasten.
Zur Navigation springenZur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
(2 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
Zeile 63: | Zeile 63: | ||
| pinMode(pin, INPUT) || DDRx &= ~_BV(bit); || | // For I/O pins, convert pin to port+bit | | pinMode(pin, INPUT) || DDRx &= ~_BV(bit); || | // For I/O pins, convert pin to port+bit | ||
|- | |- | ||
| pinMode(pin, OUTPUT) || DDRx | | pinMode(pin, OUTPUT) || DDRx |= _BV(bit); | ||
|- | |- | ||
| digitalWrite(pin, LOW) || PORTx &= ~_BV(bit); | | digitalWrite(pin, LOW) || PORTx &= ~_BV(bit); | ||
|- | |- | ||
| digitalWrite(pin, HIGH) || PORTx | | digitalWrite(pin, HIGH) || PORTx |= _BV(bit); | ||
|- | |- | ||
| digitalRead(pin) || (PINx & _BV(bit)) >> bit; | | digitalRead(pin) || (PINx & _BV(bit)) >> bit; | ||
Zeile 137: | Zeile 137: | ||
[[Kategorie:Projekte]] | [[Kategorie:Projekte]] | ||
[[Kategorie:Arduino]] | [[Kategorie:Arduino]] |
Aktuelle Version vom 21. Januar 2013, 22:23 Uhr
Arduino -> AVR HOWTO
This is an (incomplete) guide to how to map from Arduino source code to avr-gcc source code. It's been written to map this this direction, but can be used to understand how to go the other direction as well.
Step 1: Setup project
- Copy Makefile from some other project
- NB! Make sure DEVICE and SERIAL are correctly set
- Copy the <MyProject.pde> file to main.cpp
Step 2: Edit main.cpp
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <avr/interrupt.h> #include <avr/io.h> #include <util/delay.h> <insert .pde code here> int main(void) { cli(); // set a2d reference to AVCC (5 volts) ADMUX &= ~(1<<REFS1); ADMUX |= (1<<REFS0); // set a2d prescale factor to 128 // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. // FIXME: this will not work properly for other clock speeds, and // this code should use F_CPU to determine the prescale factor. ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)| // enable a2d conversions (1<<ADEN); setup(); sei(); while (1) { loop(); } return 0; }
Step 3: Port code away from Arduino
Arduino | AVR | Notes |
---|---|---|
byte | uint8_t | |
Serial.begin(<baud>) | uart_init(<baud>) | |
Serial.print*() | printf() | |
Serial.available() | getchar() | NB! blocking |
pinMode(pin, INPUT) | DDRx &= ~_BV(bit); | // For I/O pins, convert pin to port+bit |
pinMode(pin, OUTPUT) | DDRx |= _BV(bit); | |
digitalWrite(pin, LOW) | PORTx &= ~_BV(bit); | |
digitalWrite(pin, HIGH) | PORTx |= _BV(bit); | |
digitalRead(pin) | (PINx & _BV(bit)) >> bit; | |
delay(ms) | _delay_ms(ms) | max delay is 262.14 ms / F_CPU in MHz. (16Mhz -> 16 ms) |
delayMicroseconds(us) | _delay_us(us) | max delay is 768 us / F_CPU in MHz. (16Mhz -> 48 us) |
millis() | Must be rewritten or implemented using a timer |
Misc. Notes
Address of I/O register: _SFR_IO_ADDR(PORTB)
(e.g. #define _SFR_IO_ADDR(sfr) (sfr - 0x20))
Read contents of I/O register: _SFR_IO8(register) // register = 00-3f
(e.g. #define PORTB _SFR_IO8(0x18)) #define _SFR_IO8(io_addr) (io_addr + 0x20)
Arduino pins to AVR port+bit
digital | |
0 | PORTD,0 |
1 | PORTD,1 |
2 | PORTD,2 |
3 | PORTD,3 |
4 | PORTD,4 |
5 | PORTD,5 |
6 | PORTD,6 |
7 | PORTD,7 |
8 | PORTB,0 |
9 | PORTB,1 |
10 | PORTB,2 |
11 | PORTB,3 |
12 | PORTB,4 |
13 | PORTB,5 |
analog | |
0 | PORTC,0 |
1 | PORTC,1 |
2 | PORTC,2 |
3 | PORTC,3 |
4 | PORTC,4 |
5 | PORTC,5 |