From c358ee6a05a1963b39944cbb4ccc58293228cad6 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Thu, 9 Mar 2017 23:30:15 +0100 Subject: [PATCH] Initial commit with basic C code to write to pins. --- compile | 9 +++++++++ main.c | 12 ++++++++++++ pins.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pins.h | 10 ++++++++++ 4 files changed, 84 insertions(+) create mode 100755 compile create mode 100644 main.c create mode 100644 pins.c create mode 100644 pins.h diff --git a/compile b/compile new file mode 100755 index 0000000..557efb9 --- /dev/null +++ b/compile @@ -0,0 +1,9 @@ +#!/bin/bash + +mkdir build +avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/pins.o -Wall pins.c +avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/main.o -Wall main.c +avr-gcc -mmcu=atmega328p build/main.o build/pins.o -o build/main +avr-objcopy -O ihex -R .eeprom build/main build/main.hex +avrdude -F -V -c arduino -p ATMEGA328P -P $1 -b 57600 -U flash:w:build/main.hex +rm -r build diff --git a/main.c b/main.c new file mode 100644 index 0000000..77784b3 --- /dev/null +++ b/main.c @@ -0,0 +1,12 @@ +#include +#include +#include "pins.h" + +int main(){ + while(1){ + digitalWrite(5, HIGH); + _delay_ms(1000); + digitalWrite(5, LOW); + _delay_ms(1000); + } +} diff --git a/pins.c b/pins.c new file mode 100644 index 0000000..8ef18af --- /dev/null +++ b/pins.c @@ -0,0 +1,53 @@ +#include "pins.h" +#include + +const unsigned char LOW = 0x00; +const unsigned char HIGH = 0x01; + +void digitalWrite(unsigned char pin, unsigned char value){ + if(pin > 13){ + return; + } + + // TODO turn off pwm + + if(pin < 8){ + // pin 0-7 + DDRD |= _BV(DDD0+pin); + + if(value == HIGH){ + PORTD |= _BV(DDD0+pin); + } + else{ + PORTD &= ~_BV(DDD0+pin); + } + } + else{ + // pin 8-13 + DDRB |= _BV(DDB0+pin); + + if(value == HIGH){ + PORTB |= _BV(DDB0+pin); + } + else{ + PORTB &= ~_BV(DDB0+pin); + } + } +} + +void analogWrite(unsigned char pin, unsigned char value){ + if(pin > 7){ + return; + } + + // TODO turn off pwm + + DDRC |= _BV(DDC0+pin); + + if(value == HIGH){ + PORTC |= _BV(DDC0+pin); + } + else{ + PORTC &= ~_BV(DDC0+pin); + } +} \ No newline at end of file diff --git a/pins.h b/pins.h new file mode 100644 index 0000000..4af9988 --- /dev/null +++ b/pins.h @@ -0,0 +1,10 @@ +#ifndef PINS_H_ +#define PINS_H_ + +extern const unsigned char LOW; +extern const unsigned char HIGH; + +void digitalWrite(unsigned char, unsigned char); +void analogWrite(unsigned char, unsigned char); + +#endif