I’m trying to use a MiRF v2 with my Arduino NG, and i’m having a bit of trouble receiving data. Here’s my code:
//Pinouts
#define CE 8 //Chip Enable
#define IRQ 9 //Maskable interrupt
#define CSN 10 //Chip Select
#define MOSI 11 //Data Out
#define MISO 12 //Data in
#define SCK 13 //SPI clock
byte initStatus;
byte newStatus;
//Main
void setup()
{
Serial.begin(9600);
pinMode(CE, OUTPUT);
pinMode(CSN, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(IRQ, INPUT);
digitalWrite(CSN, HIGH);
digitalWrite(CSN, LOW);
shiftOut(MOSI, SCK, MSBFIRST, 0x20);
shiftOut(MOSI, SCK, MSBFIRST, 0x0B);
digitalWrite(CSN, HIGH);
digitalWrite(CE, HIGH);
//DEBUG
digitalWrite(CSN, LOW);
shiftOut(MOSI, SCK, MSBFIRST, 0x07);
initStatus = shiftIn(MISO, SCK);
digitalWrite(CSN, HIGH);
Serial.println(initStatus, BIN);
//END DEBUG
}
void loop()
{
digitalWrite(CSN, LOW);
shiftOut(MOSI, SCK, MSBFIRST, 0x07);
newStatus = shiftIn(MISO, SCK);
digitalWrite(CSN, HIGH);
Serial.println(newStatus, BIN);
}
byte shiftIn(int myDataPin, int myClockPin) {
int i;
int temp = 0;
int pinState;
byte myDataIn = 0;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(0.2);
temp = digitalRead(myDataPin);
if (temp) {
pinState = 1;
myDataIn = myDataIn | (1 << i);
}
else {
pinState = 0;
}
digitalWrite(myClockPin, 1);
}
return myDataIn;
}
So void loop() just watches the STATUS register for any changes, just so I can see if i’m receiving anything. The problem is that when I read it in the “DEBUG” section in the setup() function, the STATUS register contains %0000110. But when I read it in the loop() function, the STATUS register contains %01100011. I haven’t changed anything between those two points, and I’m not transmitting anything from my other MiRF v2 (it’s not even powered up).
Could this be due to interference?
This is my first time using these things, so i’m lost.
Thanks!