I'm trying to port GreatScott's capacitive touch code to avr. From my understanding the timer is constantly generating a pulse via the TIMER1_COMPA interrupt and the circuit works when I test it through Arduino IDE but I've hit a roadblock. When trying to port the code the circuit doesn't work at all and if it does work it's very unreliable. I've managed to pinpoint the issue being in the ISR(TIMER1_COMPA_vect) method. Here's the main body code which is pretty much identical in both implementations
unsigned long value;
uint8_t outputstate = 0;
void setup()
{
Serial.begin(9600);
TCCR1A = 0;
TCCR1B = (1 << ICES1) | (1 << WGM12) | (1 << CS10);
OCR1A = 8000;
TIMSK1 = (1 << OCIE1A);
ACSR = (1 << ACI) | (1 << ACIE) | (1 << ACIS1) | (1 << ACIS0);
DDRB |= (1 << DDB4);
}
void loop()
{
if (value > 1000)
outputstate = -1;
else
outputstate = 0;
if (outputstate)
PORTB |= (1 << DDB4);
else
PORTB &= ~(1 << DDB4);
Serial.println(value);
_delay_ms(100);
}
ISR(ANALOG_COMP_vect)
{
value = TCNT1;
}
The only real differences exist in the following method.
//arduino
ISR(TIMER1_COMPA_vect)
{
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
pinMode(8, INPUT);
}
//avr
ISR(TIMER1_COMPA_vect)
{
cli();
DDRB |= 1;
PORTB |= 1;
DDRB &= ~1;
PORTB &= ~1;
sei();
}
Now, I cannot figure out for the life of me why my implementation gives me a different result than the arduino code. Any help would be appreciated. Website