Thursday, March 29, 2018

Programming Arduino in C

Code Example
#include "avr/io.h"
#include "util/delay.h"

#define BLINK_DELAY_MS 100

int main (void)
{
 /* set pin 5 of PORTB for output*/
 DDRB |= _BV(DDB5);

 while(1) {
  /* set pin 5 high to turn led on */
  PORTB |= _BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);

  /* set pin 5 low to turn led off */
  PORTB &= ~_BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);
 }
}



How to compile
avr-gcc -I./arduino-1.8.5/hardware/tools/avr/avr/include/ -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c
avr-gcc -L./arduino-1.8.5/hardware/tools/avr/avr/lib crtatmega328p.o led.o -o led
avr-objcopy -O ihex -R .eeprom led led.hex
ArduinoSketchUploader.exe --file=led.hex --port=COM18 --model=UnoR3

Build Process
Arduino Uno in C
Arduino Uno with Code Block

No comments:

Post a Comment