SOLVED: Atmega328 Cap touch on XTAL pins

Stuck with a problem in your code? Seek help here.

SOLVED: Atmega328 Cap touch on XTAL pins

Postby TerribleCircus » February 17th, 2014, 7:24 pm

Hello fellow hack-a-day brethren,

I have an atmega 328 running on an internal oscillator and want to take advantage of the extra pins as per:
http://www.spikenzielabs.com/blog/?p=434

But I want to use them as one pin capacitive touch as per:
http://playground.arduino.cc/Code/CapacitiveSensor


I'm using arduino, and have some familiarity addressing ports directly... but clearly not enough. I've taken a few stabs at resolving this section to work with the XTAL pins but have not yet had any success:

Code: Select all
  port = portOutputRegister(digitalPinToPort(pinToMeasure));
  ddr = portModeRegister(digitalPinToPort(pinToMeasure));
  bitmask = digitalPinToBitMask(pinToMeasure);
  pin = portInputRegister(digitalPinToPort(pinToMeasure));



I could use help getting those variables resolved with the correct values to address XTAL1 and XTAL2 which I guess is the same as PB6 and PB7.

Many thanks!
Last edited by TerribleCircus on February 20th, 2014, 10:22 pm, edited 1 time in total.
TerribleCircus
 
Posts: 7
Joined: March 27th, 2013, 4:23 pm

Re: Atmega328 Cap touch on XTAL pins

Postby TerribleCircus » February 17th, 2014, 11:41 pm

My best guess was:

Code: Select all
  port = PORTB;
  ddr = DDB6;
  bitmask = (1<<PORTB6);
  pin = PINB6;


But this did not work :(
TerribleCircus
 
Posts: 7
Joined: March 27th, 2013, 4:23 pm

Re: Atmega328 Cap touch on XTAL pins

Postby TerribleCircus » February 20th, 2014, 10:21 pm

Hard coded it into this function! Solved!

Code: Select all

uint8_t readCapXtal(int pinToMeasure) {

  byte bitmask;
 
  if(pinToMeasure == 2){
   bitmask = B10000000;
  }
  else {
   bitmask = B01000000;
  }
 
 
 
PORTB &= ~(bitmask);  // SET LOW
DDRB  |= bitmask;  // OUTPUT
  delay(1);
  // Prevent the timer IRQ from disturbing our measurement
  noInterrupts();
  // Make the pin an input with the internal pull-up on
  DDRB &= ~(bitmask);  // INPUT
  PORTB |= bitmask;


  uint8_t cycles = 17;
       if (PINB & bitmask) { cycles =  0;}
  else if (PINB & bitmask) { cycles =  1;}
  else if (PINB & bitmask) { cycles =  2;}
  else if (PINB & bitmask) { cycles =  3;}
  else if (PINB & bitmask) { cycles =  4;}
  else if (PINB & bitmask) { cycles =  5;}
  else if (PINB & bitmask) { cycles =  6;}
  else if (PINB & bitmask) { cycles =  7;}
  else if (PINB & bitmask) { cycles =  8;}
  else if (PINB & bitmask) { cycles =  9;}
  else if (PINB & bitmask) { cycles = 10;}
  else if (PINB & bitmask) { cycles = 11;}
  else if (PINB & bitmask) { cycles = 12;}
  else if (PINB & bitmask) { cycles = 13;}
  else if (PINB & bitmask) { cycles = 14;}
  else if (PINB & bitmask) { cycles = 15;}
  else if (PINB & bitmask) { cycles = 16;}

  // End of timing-critical section
  interrupts();

  // Discharge the pin again by setting it low and output
  PORTB &= ~(bitmask);
  DDRB  |= bitmask;

  return cycles;
}
TerribleCircus
 
Posts: 7
Joined: March 27th, 2013, 4:23 pm

Re: SOLVED: Atmega328 Cap touch on XTAL pins

Postby lastchancename » March 20th, 2014, 2:12 am

I'd be worried if the code reaches 16 !
lastchancename
 
Posts: 33
Joined: March 13th, 2014, 4:57 am


Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 1 guest