# STM32F0 Capacitive Touch Not Detecting

I am trying to implement a simple capacitive touch sensor using the STM32F0's TSC peripheral. I currently have the following setup. (PA1 and PA2 are switched in the schematic, accidentally)

simulate this circuit – Schematic created using CircuitLab

I use the following function to set up the TSC peripheral.

void setupTSC() {
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
GPIOA->MODER |= (0x2 << (2*1)) + (0x2 << (2*2)); //enable AF for PA1 and PA2
GPIOA->AFR[0] |= (0x3 << (4*1)) + (0x3 << (4*2)); //enable AF3 for PA1 and PA2
RCC->AHBENR |= RCC_AHBENR_TSEN;
TSC->IER |= 0x01; //enable end of acquisition interrupt
TSC->IOSCR |= 0x04; //enable G1_IO3 (PA2) as sampling capacitor
TSC->IOCCR |= 0x02; //enable G1_IO2 (PA1) as channel
TSC->IOGCSR |= 0x01; //enable G1 analog group
TSC->IOHCR &= ~(0x06); //disable hysteresis on PA1 and PA2
//TSC->IOASCR |= 0x04; //do I need this?
NVIC->ISER[0] |= 1 << TS_IRQn; //enable TSC interrupt
TSC->CR |= 0x01 + 0x00C0 + 0x02; //enable TSC and start acquisition
}


And then use the following interrupt handler for when acquisition is completed.

void TS_IRQHandler() {
char s[16];
itoa(TSC->IOGXCR[0], s, 10);
TSC->ICR |= 0x03; //clear interrupts
writeLine(1, s); //prints s to LCD so I can verify delay
TSC->CR |= 0x02; //restart acquisition
}


However, no matter what I do, the TSC always counts up to the maximum value (16383 with my configuration). I have tried using larger and smaller electrodes, replacing the electrode with a capacitor to ground (to test), but nothing works, which makes me think my setup is incorrect. What's going wrong here?

My oscilloscope shows that both the sampling capacitor and electrode are charging, but the count register never returns anything other than the maximum value.

• Writing directly against the registers is challenging as there is so much that can go wrong. See if you can find a canned HAL-based example for a Discovery or Nucleo F0 and try that, then once it works start comparing how that manipulates the registers to what you are doing until you figure out the difference. – Chris Stratton Mar 6 '19 at 22:52
• @ChrisStratton I've tried finding examples, and this was about the closest I came, and yes, it uses the HAL. However, after comparing, it seems as though my initialization is identical, so I'm still at a loss. – Chris Loonam Mar 6 '19 at 22:57
• Find the source mentioned in AN4735 and actually build and try it. – Chris Stratton Mar 6 '19 at 23:01