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

« Back to documentation index

Show/hide line numbers I2CcommFunctions.h Source File

I2CcommFunctions.h

00001 #define I2Cmode                                     //Used to check if the sensor is in I2C mode or SPI mode.
00002 #define I2C_Slave_ID 0x75                           //ONLY WHEN ID PIN IS TIED TO GROUND! ID changes when tied to VDD (0x73) or when left floating (0x79).
00003 
00004 //=========================================================================
00005 //Communication pinouts for serial COM port, I2C, and interrupts
00006 //=========================================================================
00007 static Serial pc(USBTX, USBRX);                     //PC comm
00008 static I2C i2c(p26, p27);                           //SDA, SCL
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     uint8_t data;
00048     i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0);   //Send the address to the chip
00049     wait_us(1);
00050     i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0);          //Send the memory address where you want to store the read data
00051     return(data);
00052 }
00053 
00054 
00055 //=========================================================================
00056 void writeRegister(uint8_t addr, uint8_t data)
00057 {
00058     char data_write[2];             //Create an array to store the address/data to pass them at the same time.
00059     data_write[0] = addr;           //Store the address in the first byte
00060     data_write[1] = data;           //Store the data in the second byte
00061     i2c.write((I2C_Slave_ID << 1), data_write, 2, 0);   //Send both over at once
00062     
00063     pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
00064             //Uncomment this line for debugging. Prints every register write operation.
00065 }
00066 
00067 
00068 //=========================================================================
00069 void load(const uint8_t array[][2], uint8_t arraySize)
00070 {
00071     for(uint8_t q = 0; q < arraySize; q++)
00072     {
00073         writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
00074     }
00075 }
00076 
00077 
00078 //=========================================================================
00079 void grabData(void)
00080 {
00081     deltaX_low = readRegister(0x03);        //Grabs data from the proper registers.
00082     deltaY_low = readRegister(0x04);
00083     deltaX_high = (readRegister(0x12)<<4) & 0xF00;      //Grabs data and shifts it to make space to be combined with lower bits.
00084     deltaY_high = (readRegister(0x12)<<8) & 0xF00;
00085     
00086     if(deltaX_high & 0x800)
00087         deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
00088         
00089     if(deltaY_high & 0x800)
00090         deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
00091         
00092     deltaX = deltaX_high | deltaX_low;      //Combined the low and high bits.
00093     deltaY = deltaY_high | deltaY_low;
00094 }
00095 
00096 
00097 //=========================================================================
00098 void printData(void)
00099 {
00100     if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
00101     {
00102         totalX += deltaX;
00103         totalY += deltaY;
00104         
00105         pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
00106         pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
00107     }
00108     
00109     deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
00110     deltaY = 0;
00111 }