My Daily Gist | Ferdinand Silva


Pure C Language In Arduino Uno (Sample Code 3) [Digital Read]

 
/*
Compiling and uploading to Arduino Uno (Sample Code 3) [Digital Read]
=====================================================================
avr-gcc -Os -mmcu=atmega328p -c test.c
avr-gcc -mmcu=atmega328p -o test.elf test.o
avr-objcopy -O ihex -R .eeprom test.elf test.hex
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/cu.usbmodem1421 -b 115200 -U flash:w:test.hex
*/
#ifndef F_CPU
#define F_CPU 16000000UL //set frequency
#endif
#include <avr/io.h>
#include <util/delay.h>
#define MS_DELAY 500 //500 milliseconds
int main() {
DDRB |= _BV(DDB0); //set pin 8 to output
DDRB &= ~_BV(DDB1); //set pin 9 to input
for(;;) {
int val = PINB & _BV(PB1);
if(val == 0) {
PORTB |= _BV(PORTB0); //set pin 8 to high
} else {
PORTB &= ~_BV(PORTB0); //set pin 8 to low
}
_delay_ms(MS_DELAY); //delay
}
return 0;
}
view raw test.c hosted with ❤ by GitHub