From 48d96d5b7d2a8c5800887b1ad016228e90b6ca3c Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Mon, 27 Mar 2017 17:13:05 +0200 Subject: [PATCH] More work on PWM and added utility function to map values. --- ard/pins.c | 50 ++++++++++++++++++++++++++++++++++---------------- ard/pins.h | 4 ++-- ard/serial.h | 4 ++-- ard/util.c | 5 +++++ ard/util.h | 6 ++++++ compile | 1 + main.c | 9 ++++++++- 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 ard/util.c create mode 100644 ard/util.h diff --git a/ard/pins.c b/ard/pins.c index 7ca3a0f..c29069b 100644 --- a/ard/pins.c +++ b/ard/pins.c @@ -34,7 +34,7 @@ void pin_mode(unsigned char pin, unsigned char mode){ DDRD |= _BV(pin); } } - else if(pin < 14){ + else if(pin < A0){ // digital pin 8-13 pin -= 8; @@ -47,7 +47,7 @@ void pin_mode(unsigned char pin, unsigned char mode){ } else{ // analog pin 0-7 - pin -= 14; + pin -= A0; if(mode == INPUT){ DDRC &= ~_BV(pin); @@ -59,27 +59,31 @@ void pin_mode(unsigned char pin, unsigned char mode){ } int digital_read(unsigned char pin){ - if(pin > 13){ + if(pin > A7){ return 0; } if(pin < 8){ - // pin 0-7 + // digital pin 0-7 return PIND&_BV(pin) ? 1 : 0; } + else if(pin < A0){ + // digital pin 8-13 + pin -= 8; + return PINB&_BV(pin) ? 1 : 0; + } - // pin 8-13 - pin -= 8; - return PINB&_BV(pin) ? 1 : 0; + // analog pin 0-7 + return analog_read(pin) > 512 ? 1 : 0; } void digital_write(unsigned char pin, unsigned char value){ - if(pin > 13){ + if(pin > A7){ return; } if(pin < 8){ - // pin 0-7 + // digital pin 0-7 if(value == HIGH){ PORTD |= _BV(pin); } @@ -87,8 +91,8 @@ void digital_write(unsigned char pin, unsigned char value){ PORTD &= ~_BV(pin); } } - else{ - // pin 8-13 + else if(pin < A0){ + // digital pin 8-13 pin -= 8; if(value == HIGH){ @@ -98,6 +102,17 @@ void digital_write(unsigned char pin, unsigned char value){ PORTB &= ~_BV(pin); } } + else{ + // analog pin 0-7 + pin -= A0; + + if(value == HIGH){ + PORTC |= _BV(pin); + } + else{ + PORTC &= ~_BV(pin); + } + } } unsigned int analog_read(unsigned char pin){ @@ -121,19 +136,22 @@ unsigned int analog_read(unsigned char pin){ return ADC; } +// TODO: write PWM to digital pins which allow it void analog_write(unsigned char pin, unsigned int value){ - pin = map_analog_pin(pin); - - if(pin > 7){ + if(pin > 11){ return; } - if(value == HIGH){ + if(value > 255){ + value = 255; + } + + /*if(value == HIGH){ PORTC |= _BV(pin); } else{ PORTC &= ~_BV(pin); - } + }*/ } // maps A0-A7 to 0-7 diff --git a/ard/pins.h b/ard/pins.h index a70fa85..150a93c 100644 --- a/ard/pins.h +++ b/ard/pins.h @@ -1,5 +1,5 @@ -#ifndef PINS_H_ -#define PINS_H_ +#ifndef ARD_PINS_H_ +#define ARD_PINS_H_ extern const unsigned char LOW; extern const unsigned char HIGH; diff --git a/ard/serial.h b/ard/serial.h index ccbe788..08cff37 100644 --- a/ard/serial.h +++ b/ard/serial.h @@ -1,5 +1,5 @@ -#ifndef SERIAL_H_ -#define SERIAL_H_ +#ifndef ARD_SERIAL_H_ +#define ARD_SERIAL_H_ void serial_init(unsigned int); void serial_write(char*, unsigned int); diff --git a/ard/util.c b/ard/util.c new file mode 100644 index 0000000..75cf876 --- /dev/null +++ b/ard/util.c @@ -0,0 +1,5 @@ +#include "util.h" + +float map(float value, float fromLow, float fromHigh, float toLow, float toHigh){ + return fromLow+((toHigh-toLow)/(fromHigh-fromLow))*(value-fromLow); +} diff --git a/ard/util.h b/ard/util.h new file mode 100644 index 0000000..9c8d412 --- /dev/null +++ b/ard/util.h @@ -0,0 +1,6 @@ +#ifndef ARD_UTIL_H_ +#define ARD_UTIL_H_ + +float map(float, float, float, float, float); + +#endif diff --git a/compile b/compile index dd1150a..4ab5a2e 100755 --- a/compile +++ b/compile @@ -1,6 +1,7 @@ #!/bin/bash mkdir build +avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/util.o -Wall ard/util.c avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/serial.o -Wall ard/serial.c avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/pins.o -Wall ard/pins.c avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/main.o -Wall main.c diff --git a/main.c b/main.c index fef1763..37fb760 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include #include "ard/serial.h" #include "ard/pins.h" +#include "ard/util.h" void prepare(); void loop(); @@ -31,6 +32,7 @@ void prepare(){ pin_mode(A2, INPUT);*/ // PWM example + pin_mode(A1, INPUT); pin_mode(3, OUTPUT); } @@ -51,7 +53,12 @@ void loop(){ serial_write(out, 15);*/ // PWM example - digital_write(3, HIGH); + int analog = map(analog_read(A1), 0, 1023, 0, 255); + analog_write(3, analog); + + char out[15]; + sprintf(out, "%d", analog); + serial_write(out, 15); _delay_ms(25); }