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.
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)
#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.
Abek42:
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.
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).
Abek42:
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.
Abek42:
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.
I mean, how do I know what chip to use with the 1MHz and which one to use with 8MHz?
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?