Reference firmware for PixArt's PAT9130EW sensor and evaluation board. "Hello World" and "Library" contain the exact same files. Please import just one of the two into your mBed compiler as a new program and not as a library.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIcommFunctions.h Source File

SPIcommFunctions.h

00001 //=========================================================================
00002 //Communication pinouts for serial COM port, SPI, and interrupts
00003 //=========================================================================
00004 static Serial pc(USBTX, USBRX);                     //PC comm
00005 static SPI spi(p23, p24, p25);                      //mosi, miso, sclk
00006 static DigitalOut cs(p22);                          //chip select
00007 
00008 
00009 //=========================================================================
00010 //Variables and arrays used for communications and data storage
00011 //=========================================================================
00012 int8_t deltaX_low, deltaY_low;                      //Stores the low-bits of movement data.
00013 int16_t deltaX_high, deltaY_high;                   //Stores the high-bits of movement data.
00014 int16_t deltaX, deltaY;                             //Stores the combined value of low and high bits.
00015 int16_t totalX, totalY = 0;                         //Stores the total deltaX and deltaY moved during runtime.
00016 int8_t currentBank;                                 //Stores the current register bank (for tracking/debugging).
00017 
00018 
00019 //=========================================================================
00020 //Functions used to communicate with the sensor and grab/print data
00021 //=========================================================================
00022 uint8_t readRegister(uint8_t addr);
00023 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
00024 
00025 void writeRegister(uint8_t addr, uint8_t data);
00026 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
00027 
00028 void load(const uint8_t array[][2], uint8_t arraySize);
00029 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
00030 
00031 void grabData(void);
00032 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
00033 
00034 void printData(void);
00035 //Prints the data out to a serial terminal.
00036 
00037 
00038 
00039 
00040 
00041 //=========================================================================
00042 //Functions definitions
00043 //=========================================================================
00044 uint8_t readRegister(uint8_t addr)
00045 {
00046     cs = 0;                                 //Set chip select low/active
00047     addr = addr & 0x7F;                     //Set MSB to 0 to indicate read operation
00048     spi.write(addr);                        //Write the given address
00049     wait_us(1);                             //Add a tiny delay after sending address for some internal cycle timing.
00050     uint8_t data_read = spi.write(0x00);    //Throw dummy byte after sending address to receieve data
00051     cs = 1;                                 //Set chip select back to high/inactive
00052     return data_read;                       //Returns 8-bit data from register
00053 }
00054 
00055 
00056 //=========================================================================
00057 void writeRegister(uint8_t addr, uint8_t data)
00058 {
00059     cs = 0;                         //Set chip select low/active
00060     addr = addr | 0x80;             //Set MSB to 1 to indicate write operation
00061     spi.write(addr);                //Write the given address
00062     spi.write(data);                //Write the given data
00063     cs = 1;                         //Set chip select back to high/inactive
00064 
00065     if(addr == 0x7F)                //You can't read register 0x7F for the current bank so we store the value before writing it
00066     {
00067         currentBank = data;         //Store the current bank for printing later.
00068         //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, addr, currentBank);
00069             //Uncomment this line and the other print line below for debugging. Prints every register bank change.
00070     }
00071     
00072     else
00073     {
00074         //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, addr, readRegister(addr));
00075             //Uncomment this line and the other print line above for debugging. Prints every register write operation.
00076     }
00077 }
00078 
00079 
00080 //=========================================================================
00081 void load(const uint8_t array[][2], uint8_t arraySize)
00082 {
00083     for(uint8_t q = 0; q < arraySize; q++)
00084     {
00085         writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
00086     }
00087 }
00088 
00089 
00090 //=========================================================================
00091 void grabData(void)
00092 {
00093     deltaX_low = readRegister(0x03);        //Grabs data from the proper registers.
00094     deltaY_low = readRegister(0x04);
00095     deltaX_high = (readRegister(0x11)<<8) & 0xFF00;      //Grabs data and shifts it to make space to be combined with lower bits.
00096     deltaY_high = (readRegister(0x12)<<8) & 0xFF00;
00097     
00098     if(deltaX_high & 0x8000)
00099         deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
00100         
00101     if(deltaY_high & 0x8000)
00102         deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
00103         
00104     deltaX = deltaX_high | deltaX_low;      //Combined the low and high bits.
00105     deltaY = deltaY_high | deltaY_low;
00106 }
00107 
00108 
00109 //=========================================================================
00110 void printData(void)
00111 {
00112     if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
00113     {
00114         totalX += deltaX;
00115         totalY += deltaY;
00116         
00117         pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
00118         pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
00119     }
00120     
00121     deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
00122     deltaY = 0;
00123 }