-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name=ADCTouchSTM | ||
name=ADCTouchSensor | ||
version=0.0.1 | ||
author=Alexander Pruss | ||
maintainer=arpruss <[email protected]> | ||
sentence=Create Touch Sensors with a single analog pin without external Hardware | ||
paragraph=This library uses the internal wiring of STM microcontrollers to measure capacitance sort of as described here <http://tuomasnylund.fi/drupal6/content/capacitive-touch-sensing-avr-and-single-adc-pin> | ||
sentence=Create Touch Sensors with a single analog pin without external hardware | ||
paragraph=This library uses the internal wiring of microcontrollers to measure capacitance much as described here <http://tuomasnylund.fi/drupal6/content/capacitive-touch-sensing-avr-and-single-adc-pin> and is based on <https://github.com/martin2250/ADCTouch> | ||
category=Sensors | ||
url=https://github.com/arpruss/ADCTouchSTM | ||
architectures=STM32F1,STM32F2,STM32F3,STM32F4 | ||
architectures=STM32F1,STM32F2,STM32F3,STM32F4,avr,STM32 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
ADCTouchSTM.cpp - Library for Capacittive touch sensors using only one ADC PIN | ||
Created by Alexander Pruss. MIT license. | ||
*/ | ||
|
||
#include "Arduino.h" | ||
#include "ADCTouchSensor.h" | ||
|
||
ADCTouchSensor::ADCTouchSensor(int pin, int sacrificialPin, unsigned delayTimeMicroseconds) { | ||
touchPin = pin; | ||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) | ||
touchDigitalPin = pgm_read_byte(analog_pin_to_digital_pin + pin); | ||
if ((uint8_t)touchDigitalPin == (uint8_t)NOT_A_PIN) { | ||
valid=false; | ||
return; | ||
} | ||
#else | ||
touchDigitalPin = pin; | ||
#endif | ||
reference = 0; | ||
groundedPin = sacrificialPin; | ||
delayTime = delayTimeMicroseconds; | ||
if (sacrificialPin < 0) { | ||
#if defined(ADCTOUCH_STM32_GROUND_CHANNEL) || defined(ARDUINO_ARCH_AVR) | ||
valid = true; | ||
#else | ||
valid = false; | ||
#endif | ||
} | ||
else { | ||
valid = true; | ||
} | ||
} | ||
|
||
// connect the ADC input and the internal sample and hold capacitor to ground to discharge it | ||
// also, delay to allow for charging of the touch pin | ||
inline void ADCTouchSensor::groundPortable() { | ||
pinMode(groundedPin, OUTPUT); // HARDWARE ISSUE: ensure no short occurs | ||
digitalWrite(groundedPin, 0); | ||
if (delayTime > 0) | ||
delayMicroseconds(delayTime); | ||
analogRead(groundedPin); | ||
} | ||
|
||
#if defined(ADCTOUCH_STM32_GROUND_CHANNEL) | ||
inline void ADCTouchSensor::ground() { | ||
if (delayTime>0) | ||
delayMicroseconds(delayTime); | ||
adc_read(ADCTOUCH_STM32_ADC, ADCTOUCH_STM32_GROUND_CHANNEL); | ||
} | ||
|
||
#elif defined(ARDUINO_ARCH_AVR) | ||
|
||
inline void ADCTouchSensor::ground() { | ||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) | ||
ADMUX = (ADMUX & 0xF0) | 0b1101; | ||
#else | ||
ADMUX |= 0b11111; | ||
#endif | ||
if (delayTime>0) | ||
delayMicroseconds(delayTime); | ||
|
||
ADCSRA |= (1 << ADSC); | ||
// ADSC is cleared when the conversion finishes | ||
while((ADCSRA & (1 << ADSC))) ; | ||
} | ||
#endif | ||
|
||
int ADCTouchSensor::readRaw(unsigned samples) { | ||
if (!valid) | ||
return -10000; | ||
int32_t total = 0; | ||
for (unsigned i=0; i<samples; i++) { | ||
pinMode(touchDigitalPin, INPUT_PULLUP); | ||
if (groundedPin >= 0) { | ||
groundPortable(); | ||
} | ||
else { | ||
ground(); | ||
} | ||
pinMode(touchDigitalPin, INPUT_ANALOG); | ||
total += analogRead(touchPin); | ||
} | ||
return total / ADCTOUCH_DIVIDER / samples; | ||
} | ||
|
||
bool ADCTouchSensor::begin(unsigned samples) { | ||
if (valid) { | ||
reference = readRaw(samples); | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
|
||
int ADCTouchSensor::read(unsigned samples) { | ||
if (valid) | ||
return readRaw(samples) - reference; | ||
else | ||
return -10000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
ADCTouchSensor.h - Library for Capacitive touch sensors using only one ADC PIN, with a second unconnected pin shared between sensors on some devices | ||
Based on code that was created by martin2250, April 23, 2014 and released into the public domain. | ||
*/ | ||
#ifndef ADCTOUCHSENSOR_h | ||
#define ADCTOUCHSENSOR_h | ||
|
||
#include "Arduino.h" | ||
|
||
#if defined(ARDUINO_GENERIC_STM32F103C) | ||
# define ADCTOUCH_STM32_GROUND_CHANNEL 15 | ||
# define ADCTOUCH_STM32_ADC ADC1 | ||
# define ADCTOUCH_INTERNAL_GROUNDING | ||
#endif | ||
|
||
#if defined(ARDUINO_ARCH_AVR) | ||
# define ADCTOUCH_INTERNAL_GROUNDING | ||
# define INPUT_ANALOG INPUT | ||
#endif | ||
|
||
#if defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F2) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) | ||
# define ADCTOUCH_DEFAULT_DELAY 20 | ||
# define ADCTOUCH_DIVIDER 4 | ||
#elif defined(ARDUINO_ARCH_AVR) | ||
# define ADCTOUCH_DEFAULT_DELAY 0 | ||
# define ADCTOUCH_DIVIDER 1 | ||
#endif | ||
|
||
class ADCTouchSensor | ||
{ | ||
private: | ||
int32_t reference; | ||
int groundedPin; | ||
int touchPin; | ||
int touchDigitalPin; | ||
unsigned delayTime; | ||
bool valid; | ||
|
||
private: | ||
void groundPortable(); | ||
#ifdef ADCTOUCH_INTERNAL_GROUNDING | ||
void ground(); | ||
#endif | ||
|
||
public: | ||
ADCTouchSensor(int pin, int sacrificialPin=-1, unsigned delayTimeMicroseconds=ADCTOUCH_DEFAULT_DELAY); | ||
bool begin(unsigned samples=500); | ||
int read(unsigned samples = 5); | ||
int readRaw(unsigned samples = 5); | ||
}; | ||
#endif |