PixArt / Mbed OS 9125_referenceCode
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIcommFunctions.h Source File

SPIcommFunctions.h

00001 #define SPImode                                     //Used to check if the sensor is in I2C mode or SPI mode.
00002 
00003 //=========================================================================
00004 //Communication pinouts for serial COM port, SPI, and interrupts
00005 //=========================================================================
00006 static Serial pc(USBTX, USBRX);                     //PC comm
00007 static SPI spi(p23, p24, p25);                      //mosi, miso, sclk
00008 static DigitalOut cs(p22);                          //chip select
00009 
00010 
00011 //=========================================================================
00012 //Variables and arrays used for communications and data storage
00013 //=========================================================================
00014 int8_t deltaX_low, deltaY_low;                      //Stores the low-bits of movement data.
00015 int16_t deltaX_high, deltaY_high;                   //Stores the high-bits of movement data.
00016 int16_t deltaX, deltaY;                             //Stores the combined value of low and high bits.
00017 int16_t totalX, totalY = 0;                         //Stores the total deltaX and deltaY moved during runtime.
00018 
00019 
00020 //=========================================================================
00021 //Functions used to communicate with the sensor and grab/print data
00022 //=========================================================================
00023 uint8_t readRegister(uint8_t addr);
00024 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
00025 
00026 void writeRegister(uint8_t addr, uint8_t data);
00027 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
00028 
00029 void load(const uint8_t array[][2], uint8_t arraySize);
00030 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
00031 
00032 void grabData(void);
00033 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
00034 
00035 void printData(void);
00036 //Prints the data out to a serial terminal.
00037 
00038 
00039 
00040 
00041 
00042 //=========================================================================
00043 //Functions definitions
00044 //=========================================================================
00045 uint8_t readRegister(uint8_t addr)
00046 {
00047     cs = 0;                                 //Set chip select low/active
00048     addr = addr & 0x7F;                     //Set MSB to 0 to indicate read operation
00049     spi.write(addr);                        //Write the given address
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     //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
00066             //Uncomment this line for debugging. Prints every register write operation.
00067 }
00068 
00069 
00070 //=========================================================================
00071 void load(const uint8_t array[][2], uint8_t arraySize)
00072 {
00073     for(uint8_t q = 0; q < arraySize; q++)
00074     {
00075         writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
00076     }
00077 }
00078 
00079 
00080 //=========================================================================
00081 void grabData(void)
00082 {
00083     deltaX_low = readRegister(0x03);        //Grabs data from the proper registers.
00084     deltaY_low = readRegister(0x04);
00085     deltaX_high = (readRegister(0x12)<<4) & 0xF00;      //Grabs data and shifts it to make space to be combined with lower bits.
00086     deltaY_high = (readRegister(0x12)<<8) & 0xF00;
00087     
00088     if(deltaX_high & 0x800)
00089         deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
00090         
00091     if(deltaY_high & 0x800)
00092         deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
00093         
00094     deltaX = deltaX_high | deltaX_low;      //Combined the low and high bits.
00095     deltaY = deltaY_high | deltaY_low;
00096 }
00097 
00098 
00099 //=========================================================================
00100 void printData(void)
00101 {
00102     if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
00103     {
00104         totalX += deltaX;
00105         totalY += deltaY;
00106         
00107         pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
00108         pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
00109     }
00110     
00111     deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
00112     deltaY = 0;
00113 }