Skip to content

Instantly share code, notes, and snippets.

@six519
Created April 22, 2018 07:30
Show Gist options
  • Save six519/0ee66e0b3b241343bee504ee6663371b to your computer and use it in GitHub Desktop.
Save six519/0ee66e0b3b241343bee504ee6663371b to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment