Shiftin MSBPOST Argument Not an option

I'm trying to copy some pBasic code from a Parallax Mouse Sensor Kit into something that will work on my Arduino. I think I've got everything nailed down except for one piece: Shiftin

The pBasic code as an "MSBPOST" option which the Arduino does not appear to have.

Anyone know how to implement this functionality on an Arduino?

I don't know that pBasic stuff but have you tried LSBFIRST? What does the MSBPOST option mean? That the MSB is the last bit? If yes, than LSBFIRST is your choice.

Here's what the pBasic documentation says:

MSBPRE: Data is MSB-first; sample bits before clock pulse

LSBPRE: Data is lsb-first; sample bits before clock pulse

MSBPOST: Data is MSB-first; sample bits after clock pulse

LSBPOST: Data is LSB-first; sample bits after clock pulse

See the attachment for a picture that was also in the documentation.

Shiftin Timing.PNG

Out of curiosity, how do you sample before a clock pulse?

No idea. This is way out of my level of expertise. The info above is from the documentation from Parallax's IDE for the basic stamp.

I was making a bit of a joke there. After all, if you said you had to do something "before Christmas", well that could be today, right?

SPI might help you here. That handles clock and data, with sampling on the leading or trailing edge.

I took a look at your page. It shows the SPI library using 4 pins whereas the shiftin command only uses 2 (which is how my hardware is set up).

What I'm trying to do is:

byte incoming = shiftIn(dataPin, clockPin, MSBPOST)

I don't see how I would implement this functionality with the SPI library - my integrated circuit has one datapin and I'd somehow need to replace the datapin with a MOSI, MISO, and maybe even a slave select (although it looks like the last is optional if there's only one ic).

mbackus:
I took a look at your page. It shows the SPI library using 4 pins whereas the shiftin command only uses 2 (which is how my hardware is set up).

What I'm trying to do is:

byte incoming = shiftIn(dataPin, clockPin, MSBPOST)

I don't see how I would implement this functionality with the SPI library - my integrated circuit has one datapin and I'd somehow need to replace the datapin with a MOSI, MISO, and maybe even a slave select (although it looks like the last is optional if there's only one ic).

You don't need MISO, becase the slave isn't outputting and the master isn't receiving. MISO is what you need if you want to use shiftIn.

You don't need slave select because you only have 1 slave.

Quite right. I use SPI to shift out to a 595 shift register. It gives you clock and data. You can configure the clock to be on the leading or trailing edge and normally high or normally low.

Well, I tried the SPI approach, but didn't have any success. I hooked the clock up to pin 13 and the data line to pin 11 (I actually tried pin 12 too to no avail). If it helps at all, I'm trying to read a change in x and y position from the optical sensor in Parallax's Mouse Sensor Kit: https://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/677/Default.aspx?txtSearch=mouse+sensor+kit

Here's the code I tried:

#include <SPI.h>

//Mouse sensor register addresses
#define CONF 0x1B
#define LORES 0x80
#define CHNG  0x80
#define OFLOW 0x18
#define NEG 0x80

byte stat;
byte dx;
byte dy;
byte quality;
byte ovfl;

int x;
int y;

void WriteAddr(byte address, byte data)
{
  SPI.transfer(address|0x80);
  SPI.transfer(data);
}

int ReadAddr(byte address)
{
  byte data;
  data = SPI.transfer(address);
  return data;
}

void setup()
{
  delay(2000);
  Serial.begin(9600);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3); 
  delay(100);
  WriteAddr(CONF,LORES);
  
}

void loop()
{
  stat = ReadAddr(0x16);
  dx = ReadAddr(0x03);
  dy = ReadAddr(0x02);
  quality = ReadAddr(0x04);  
  Serial.println(dx,BIN);
}

Now that I look at the documentation (and it might have saved time to put the link in your first post) it doesn't seem that SPI will help here, because the data line is bi-directional.

It looks like you will have to choose a pin for the data and clock (any pins) and configure the clock as output. Then configure the data as output and do a ShiftOut to send the address (one byte) then change the data pin to an input, and use ShiftIn to get the results back.

http://arduino.cc/en/Reference/ShiftOut

http://arduino.cc/en/Reference/ShiftIn

It looks like you have the general idea in your sketch, so changing it to do that shouldn't be too hard.

I finally got it working, and it's working really well. I was forgetting to toggle the dataline's pinmode between input and output before the shift commands. Thanks so much.

So, to help out other people, could you post the working code?

Go to http://arduino.cc/forum/index.php/topic,124808.0.html and scroll to the bottom.