Go Down

Topic: ATTiny84 Falling edge interrupt (Read 2121 times) previous topic - next topic

Abek42

Hi,

Background:
I am trying to set up an Arduino Mega (master) to ATTiny84 (slave) using SPI.
Since there will be more than one ATTiny84 slaves, I need to use the SS to tell the ATTiny to start listening to the incoming transmission.

While I plan to use some code from (http://forum.arduino.cc/index.php?topic=71975.0 and  http://forum.arduino.cc/index.php/topic,126771.15.html) but neither talks about the SS interrupt.

My plan is to use Pin12 (PCINT1) to receive a falling edge interrupt to set a flag. If flag is set, USI_OVF_vect subroutine processes the received byte else discards it.

Question:
What code allows me to detect the interrupt on a falling edge on the specified pin?
Here's my existing code which doesn't work.

I would like to turn on LED2 when I pull CS low but currently this code only causes LED1 and LED0 to flicker (they should not as the analog inputs are set to make them stay on all the time)

Code: [Select]

#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define LED2_PIN  1 //Pin3 on chip
#define LED1_PIN  5 //Pin8 on chip
#define LED0_PIN  6 //Pin9 on chip
#define ADC_IN0  A2 //Pin11 on chip will read a pot value in future
#define ADC_IN1  A3 //Pin10 on chip
#define CS        9 //Pin12 on chip

int cnt=0;
int p0=LOW;
int p1=LOW;
int p2=LOW;


// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(LED0_PIN, OUTPUT);     
  pinMode(LED1_PIN, OUTPUT);   
  pinMode(LED2_PIN, OUTPUT); 
  pinMode(CS      ,  INPUT);

 
  digitalWrite(LED0_PIN,  LOW); //the LED is on
  digitalWrite(LED1_PIN,  LOW); //the LED is on
  digitalWrite(LED2_PIN,  LOW); //the LED is on
 
  sei();
  sbi(GIMSK,PCIE0); // Turn on Pin Change interrupt <=Not sure this is correct
  sbi(PCMSK0,PCINT1); // Which pins are affected by the interrupt <=Not sure this is correct
}

ISR(PCINT1_vect){ <=Not sure this is correct
  p2=(p2==HIGH)?LOW:HIGH;
}

// the loop routine runs over and over again forever:
void loop() {
  delay(200);
  p0 = (analogRead(ADC_IN0)>512)?HIGH:LOW;
  p1 = (analogRead(ADC_IN1)<512)?HIGH:LOW; 
  digitalWrite(LED0_PIN, p0);
  digitalWrite(LED1_PIN, p1);
  digitalWrite(LED2_PIN, p2);

}


Additional info:
I am using core files for ATTiny84 from here: http://arduino-tiny.googlecode.com/files/arduino-tiny-0100-0017.zip
which seems a little wonky. When I used the 8MHz chip option my delay(1000) took 8 seconds to execute. So I am using the 1MHz option in board type.

Thanks.

Regards,
Abe

Coding Badly

What code allows me to detect the interrupt on a falling edge on the specified pin?


http://code.google.com/p/arduino-tiny/downloads/list
PinChangeInterrupt*.zip

Coding Badly

I am using core files for ATTiny84 from here: http://arduino-tiny.googlecode.com/files/arduino-tiny-0100-0017.zip
which seems a little wonky. When I used the 8MHz chip option my delay(1000) took 8 seconds to execute. So I am using the 1MHz option in board type.


A lack of knowledge on your part does not create wonky-ness on my part.

Abek42

Thanks Coding Badly.

That worked perfectly. This is my first day with an ATTiny84 and I believe it is not as commonly used as the Mega family.

My observation on the wonkiness was not intended to thumb my nose at the author. I just put it there in case that was in some way related to the problem I encountered.

Not to mention my manual pin assignment is also a result of similar observation how the pin assignments were working.

Out of curiosity, what exactly is the deal with the 1MHz and 8MHz? I mean, how do I know what chip to use with the 1MHz and which one to use with 8MHz? From my day's worth of search I think I am definitely going to run into some clock frequency problems for setting up a 3-wire SPI (4 if you count the chip-select wire too).

Thanks.

Regards,
Abe

fungus


Out of curiosity, what exactly is the deal with the 1MHz and 8MHz? I mean, how do I know what chip to use with the 1MHz and which one to use with 8MHz?


ATtiny84s have various options for clocking. You can choose the one you want.

8MHz needs more electricity than 1MHz so some people might not want that (eg. running on batteries).

You can even change clock speed on the fly - run at low speed most of the time and boost it when you need it. The datasheet will tell you all about it.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Coding Badly

Out of curiosity, what exactly is the deal with the 1MHz and 8MHz?


From the factory, the processor is configured to run from the internal oscillator divided by 8.  The "divided by 8" can be changed to "divided by 1" by changing the "fuses".  Conveniently the Arduino IDE will do all the work.  Select the board (e.g. ATtiny84 @ 8 MHz) then click Tools / Burn Bootloader.

Quote
I mean, how do I know what chip to use with the 1MHz and which one to use with 8MHz?


Start with the target running at 8 MHz.

Abek42

Thanks folks. The ATTiny84 now purrs at 8MHz. (At least the delay(1000) is 1s long)

I have resolved the interrupt issues.
But the pin assignment is driving me nuts.
I originally assumed the assignments that are here (http://www.pighixxx.com/pgdev/Temp/attiny.pdf)
So to set Pin2 as output I tried  pinMode(10, OUTPUT); which didn't work.
I had to use pinMode(0, OUTPUT); to make it work.

Where can I find out the correct pin assignments for the ATTiny84?

Thanks.

Regards,
Abe

Coding Badly

I originally assumed the assignments that are here (http://www.pighixxx.com/pgdev/Temp/attiny.pdf)


...is the pin numbering for the High-Low Tech variant.

Where can I find out the correct pin assignments for the ATTiny84?


http://code.google.com/p/arduino-tiny/source/browse/cores/tiny/pins_arduino.c#161

Abek42

Thanks again.

I scanned through the .h file but not the .c file. Next time, will do :-)

I am still having problems with SPI, but I will be putting those on another thread.

Thanks.

Regards,
Abe

Go Up