More work on PWM and added utility function to map values.

This commit is contained in:
Marvin Blum
2017-03-27 17:13:05 +02:00
parent bc2c6da436
commit 48d96d5b7d
7 changed files with 58 additions and 21 deletions

View File

@@ -34,7 +34,7 @@ void pin_mode(unsigned char pin, unsigned char mode){
DDRD |= _BV(pin); DDRD |= _BV(pin);
} }
} }
else if(pin < 14){ else if(pin < A0){
// digital pin 8-13 // digital pin 8-13
pin -= 8; pin -= 8;
@@ -47,7 +47,7 @@ void pin_mode(unsigned char pin, unsigned char mode){
} }
else{ else{
// analog pin 0-7 // analog pin 0-7
pin -= 14; pin -= A0;
if(mode == INPUT){ if(mode == INPUT){
DDRC &= ~_BV(pin); DDRC &= ~_BV(pin);
@@ -59,27 +59,31 @@ void pin_mode(unsigned char pin, unsigned char mode){
} }
int digital_read(unsigned char pin){ int digital_read(unsigned char pin){
if(pin > 13){ if(pin > A7){
return 0; return 0;
} }
if(pin < 8){ if(pin < 8){
// pin 0-7 // digital pin 0-7
return PIND&_BV(pin) ? 1 : 0; return PIND&_BV(pin) ? 1 : 0;
} }
else if(pin < A0){
// pin 8-13 // digital pin 8-13
pin -= 8; pin -= 8;
return PINB&_BV(pin) ? 1 : 0; 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){ void digital_write(unsigned char pin, unsigned char value){
if(pin > 13){ if(pin > A7){
return; return;
} }
if(pin < 8){ if(pin < 8){
// pin 0-7 // digital pin 0-7
if(value == HIGH){ if(value == HIGH){
PORTD |= _BV(pin); PORTD |= _BV(pin);
} }
@@ -87,8 +91,8 @@ void digital_write(unsigned char pin, unsigned char value){
PORTD &= ~_BV(pin); PORTD &= ~_BV(pin);
} }
} }
else{ else if(pin < A0){
// pin 8-13 // digital pin 8-13
pin -= 8; pin -= 8;
if(value == HIGH){ if(value == HIGH){
@@ -98,6 +102,17 @@ void digital_write(unsigned char pin, unsigned char value){
PORTB &= ~_BV(pin); 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){ unsigned int analog_read(unsigned char pin){
@@ -121,19 +136,22 @@ unsigned int analog_read(unsigned char pin){
return ADC; return ADC;
} }
// TODO: write PWM to digital pins which allow it
void analog_write(unsigned char pin, unsigned int value){ void analog_write(unsigned char pin, unsigned int value){
pin = map_analog_pin(pin); if(pin > 11){
if(pin > 7){
return; return;
} }
if(value == HIGH){ if(value > 255){
value = 255;
}
/*if(value == HIGH){
PORTC |= _BV(pin); PORTC |= _BV(pin);
} }
else{ else{
PORTC &= ~_BV(pin); PORTC &= ~_BV(pin);
} }*/
} }
// maps A0-A7 to 0-7 // maps A0-A7 to 0-7

View File

@@ -1,5 +1,5 @@
#ifndef PINS_H_ #ifndef ARD_PINS_H_
#define PINS_H_ #define ARD_PINS_H_
extern const unsigned char LOW; extern const unsigned char LOW;
extern const unsigned char HIGH; extern const unsigned char HIGH;

View File

@@ -1,5 +1,5 @@
#ifndef SERIAL_H_ #ifndef ARD_SERIAL_H_
#define SERIAL_H_ #define ARD_SERIAL_H_
void serial_init(unsigned int); void serial_init(unsigned int);
void serial_write(char*, unsigned int); void serial_write(char*, unsigned int);

5
ard/util.c Normal file
View File

@@ -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);
}

6
ard/util.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef ARD_UTIL_H_
#define ARD_UTIL_H_
float map(float, float, float, float, float);
#endif

View File

@@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
mkdir build 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/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/pins.o -Wall ard/pins.c
avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/main.o -Wall main.c avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o build/main.o -Wall main.c

9
main.c
View File

@@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include "ard/serial.h" #include "ard/serial.h"
#include "ard/pins.h" #include "ard/pins.h"
#include "ard/util.h"
void prepare(); void prepare();
void loop(); void loop();
@@ -31,6 +32,7 @@ void prepare(){
pin_mode(A2, INPUT);*/ pin_mode(A2, INPUT);*/
// PWM example // PWM example
pin_mode(A1, INPUT);
pin_mode(3, OUTPUT); pin_mode(3, OUTPUT);
} }
@@ -51,7 +53,12 @@ void loop(){
serial_write(out, 15);*/ serial_write(out, 15);*/
// PWM example // 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); _delay_ms(25);
} }