View Single Post
  #2   Spotlight this post!  
Unread 17-05-2011, 09:22
StevenB StevenB is offline
is having FRC withdrawal symptoms.
AKA: Steven Bell
no team
Team Role: College Student
 
Join Date: May 2005
Rookie Year: 2005
Location: Stanford, CA
Posts: 412
StevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond reputeStevenB has a reputation beyond repute
Re: Help with Arduino and SPI

Here's some code from my optical mouse project. mDataPin and mClockPin are just integers that refer to the specific pins being manipulated.

Code:
/**
 * Writes an 8-bit value to one of the optical mouse's registers.
 *
 * \param address  The register address to write to.  Valid registers for the ADNS-2610 are 0x00
 * through 0x11.
 * \param value  The value to write.
 */
void Mouse::writeRegister(uint8 address, uint8 value)
{
  address = address | 0x80; // Most significant bit is always 1, since we're doing a write (This only applies to the mouse chip)
  pinMode(mDataPin, OUTPUT); // Only necessary if mDataPin is sometimes used as an input

  // We start by writing the most significant bit first
  for(int i = 0; i < 8; i++)
  {
    digitalWrite(mClockPin, 0); // Set the clock pin low
    digitalWrite(mDataPin, address & 0x80); // Set the data to the next bit
    digitalWrite(mClockPin, 1); // Set the clock pin high
    // Now the chip reads the data pin while we go around the loop
    address = address << 1; // Shift to the next bit
  }

  // Now write the data in exactly the same way
  for(int i = 0; i < 8; i++)
  {
    digitalWrite(mClockPin, 0);
    digitalWrite(mDataPin, value & 0x80);
    digitalWrite(mClockPin, 1);
    value = value << 1;
  }

}
__________________
Need a physics refresher? Want to know if that motor is big enough for your arm? A FIRST Encounter with Physics

2005-2007: Student | Team #1519, Mechanical Mayhem | Milford, NH
2008-2011: Mentor | Team #2359, RoboLobos | Edmond, OK
2014-??: Mentor | Looking for a team...