CapSense (QTouchADC)

From SGMK-SSAM-WIKI
Revision as of 15:52, 15 February 2015 by Dusjagr (talk | contribs) (→‎Links)
Jump to navigation Jump to search

Information

This is another CapSense implementation that tries to follow the specifications of Atmels QTouchADC, a version of QTouch that uses the internal Sample & Hold capacitor of the ADC. No external capacitor is needed anymore! Just make some small sensor pads, or even sliders and wheels (see Atmel Design referece).

To make the measurement more stable I used transistors to drive the LEDs (somehow it works much better when there is less current through the chip), and it is recommended to use a stable power source (batteries or add an additional capacitor between 5V and GND).

Not like in the pseudo schematic, there should be a 1k Ohm resistor between the microcontroller an the sensor pad!


{{#widget:Vimeo|id=83185877}}

Links

forked on Github

https://github.com/dusjagr/QTouchADCArduino

More on QTouchADC

discussion on mikrocontroller.net

QTouchADC application note

Atmel QTouch reference

How to make Buttons, Sliders and Wheels

Nice description and implementation on ATMega32u4

https://www.youtube.com/watch?v=Ncu2GZJbluI

QTouch on Attiny and Arduino

Very smoothly working implementation for Arduino ->> can someone translate it to the attiny register-ports? (dusjagr tried and failed...) or even make it into a library?

Library for Arduino for ADC Touch, noisy...

TinyTouch library for attiny (I can't get this to run from the arduino IDE), looks very nice!!!

Programming the attiny85 with arduino IDE

Hardware Cores to program various Attiny 85/84 and others via arduino 1.0 & 1.6 and older, MIT High-Low tech

Hardware Cores to program various ATtiny84 (84/44/24), ATtiny85 (85/45/25), and ATtiny2313 (4313) and others via arduino

Code

/*
 NC      1   8   +5V
 KEY0    2   7   NC
 KEY1    3   6   LAMP PWM2
 GND     4   5   LAMP PWM1
 
 */

#include <avr/io.h>
#include <avr/delay.h>


//------------------------------------------------------------------------------------------------------------

void init(){
    // ADC
    ADMUX = (0<<REFS0); //VCC reference
    ADCSRA = (1<<ADEN)| (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //ADC enable, prescaler 128
 
    // PWM
	DDRB |= (1<<PB0)|(1<<PB1); // PWM-outputs

	TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); // fast PWM
	TCCR0B = (1<<CS02); // counter clock divider 256
	OCR0A = 255;
    OCR0B = 255;
}

// pre: input PB3 (ADC3) and PB4 (ADC2). PB2 (ADC1) is used as reference, PB0 and PB1 for PWM
uint16_t sensePad(uint8_t adcPin){
    int16_t measurement1, measurement2;
    uint8_t portPin;
    if (adcPin == 3) {
        portPin = PB3;
    } else {
        portPin = PB4;
    }

    // first measurement: adcPin low, S/H high
    ADMUX = (0<<REFS0) | 0x01; // set ADC sample+hold condensator to the free PB2 (ADC1) 

    PORTB |= (1<<PB2); //PB2/ADC1 ref/ S/H higt
    DDRB |= (1<<portPin) | (1<<PB2); // both output: adcPin low, S/H (ADC1) high
    _delay_us(32);
    DDRB &= ~((1<<portPin) | (1<<PB2));
    PORTB &= ~((1<<portPin) | (1<<PB2));

    ADMUX = (0<<REFS0) | (adcPin & 0x03); // read extern condensator from adcPin
    ADCSRA |= (1<<ADSC); // start conversion
    while (!(ADCSRA & (1 << ADIF))); // wait for conversion complete
    ADCSRA |= (1 << ADIF); // clear ADIF
    measurement1=ADC;

    // second measurement: adcPin high, S/H low
    ADMUX = (0<<REFS0) | 0x01; // set ADC sample+hold condensator to the free PB2 (ADC1)

    PORTB |= (1<<portPin); // sensePad/adcPin high
    DDRB |= (1<<portPin) | (1<<PB2); // both output: adcPin high, S/H (ADC1) low
    _delay_us(32);
    DDRB &= ~((1<<portPin) | (1<<PB2));
    PORTB &= ~((1<<portPin) | (1<<PB2));

    ADMUX = (0<<REFS0) | (adcPin & 0x03); // read extern condensator from adcPin
    ADCSRA |= (1<<ADSC); // start conversion
    while (!(ADCSRA & (1 << ADIF))); // wait for conversion complete
    ADCSRA |= (1 << ADIF); // clear ADCIF
    measurement2=ADC;


    return (measurement2 - measurement1)+1023;
}

uint8_t getMultiplicator(int16_t value, uint16_t maxValue){
    if (maxValue<64){
        value=value*4;
    } else if (maxValue < 86){
        value= value*3;
    } else if (maxValue < 103) {
        value = (value/2)*5;
    } else if (maxValue<128){
        value=value*2;
    } else if (maxValue < 154){
        value = (value/3)*5;
    } else if (maxValue<171){
        value = (value/2)*3;
    } else if (maxValue<205){
        value = (value/4)*5;
    } else {
        value=(value/5)*4;
    }
    return value;
}

//------------------------------------------------------------------------------------------------------------
int main(void) {
	init();
	int16_t senseValue1, senseValue2;
    int16_t refMin1, refMin2;
    uint16_t refMax1, refMax2;
    refMin1 = sensePad(2);
    refMin1 = sensePad(2);
    refMin2 = sensePad(3);
    refMin2 = sensePad(3);

    for(;;){
        PORTB &= ~(1<<PB3);

        senseValue1 = sensePad(2);
        senseValue1 = senseValue1 - refMin1;
        if (senseValue1<0){
            senseValue1=0;
        }
        if (senseValue1>refMax1) {
            refMax1=senseValue1;
        }
        senseValue1 = getMultiplicator((uint8_t) senseValue1, refMax1);

        senseValue2 = sensePad(3);
        senseValue2 = senseValue2 - refMin2;
        if (senseValue2<0){
            senseValue2=0;
        }
        if (senseValue2>refMax2) {
            refMax2=senseValue2;
        }
        senseValue2 = getMultiplicator((uint8_t) senseValue2, refMax2);

        OCR0B = senseValue1;
        OCR0A = senseValue2;
        
    }
    return 0;   /* never reached */
}