mirror of
https://github.com/Kugelschieber/arduino-c.git
synced 2026-01-18 11:00:26 +00:00
More work on PWM and added utility function to map values.
This commit is contained in:
50
ard/pins.c
50
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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
5
ard/util.c
Normal file
5
ard/util.c
Normal 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
6
ard/util.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef ARD_UTIL_H_
|
||||
#define ARD_UTIL_H_
|
||||
|
||||
float map(float, float, float, float, float);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user