Reference firmware for PixArt's PAT9125EL 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.
Welcome to the code repository for PixArt's PAT9125EL sensor and evaluation board.
For general information about this product, please visit this product's components page here:
https://os.mbed.com/components/PAT9125EL-Evaluation-Board/
For guides and tips on how to setup and evaluate the PAT9125EL sensor with the Nordic nRF52-DK microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/9125_referenceCode/wiki/Guide-for-nRF52-DK-Platform
For guides and tips on how to setup and evaluate the PAT9125EL sensor with any microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/9125_referenceCode/wiki/Guide-for-Any-Platform
commHeaders/I2CcommFunctions.h@5:577976dae20d, 2018-07-23 (annotated)
- Committer:
- PixArtVY
- Date:
- Mon Jul 23 22:15:42 2018 +0000
- Revision:
- 5:577976dae20d
- Parent:
- 3:979019410df2
Comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtVY | 1:4ea157704c58 | 1 | #define I2Cmode //Used to check if the sensor is in I2C mode or SPI mode. |
PixArtVY | 0:e9cce1154246 | 2 | #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). |
PixArtVY | 0:e9cce1154246 | 3 | |
PixArtVY | 0:e9cce1154246 | 4 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 5 | //Communication pinouts for serial COM port, I2C, and interrupts |
PixArtVY | 0:e9cce1154246 | 6 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 7 | static Serial pc(USBTX, USBRX); //PC comm |
PixArtVY | 0:e9cce1154246 | 8 | static I2C i2c(p26, p27); //SDA, SCL |
PixArtVY | 0:e9cce1154246 | 9 | |
PixArtVY | 0:e9cce1154246 | 10 | |
PixArtVY | 0:e9cce1154246 | 11 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 12 | //Variables and arrays used for communications and data storage |
PixArtVY | 0:e9cce1154246 | 13 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 14 | int8_t deltaX_low, deltaY_low; //Stores the low-bits of movement data. |
PixArtVY | 0:e9cce1154246 | 15 | int16_t deltaX_high, deltaY_high; //Stores the high-bits of movement data. |
PixArtVY | 0:e9cce1154246 | 16 | int16_t deltaX, deltaY; //Stores the combined value of low and high bits. |
PixArtVY | 0:e9cce1154246 | 17 | int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. |
PixArtVY | 0:e9cce1154246 | 18 | |
PixArtVY | 0:e9cce1154246 | 19 | |
PixArtVY | 0:e9cce1154246 | 20 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 21 | //Functions used to communicate with the sensor and grab/print data |
PixArtVY | 0:e9cce1154246 | 22 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 23 | uint8_t readRegister(uint8_t addr); |
PixArtVY | 0:e9cce1154246 | 24 | //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. |
PixArtVY | 0:e9cce1154246 | 25 | |
PixArtVY | 0:e9cce1154246 | 26 | void writeRegister(uint8_t addr, uint8_t data); |
PixArtVY | 0:e9cce1154246 | 27 | //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. |
PixArtVY | 0:e9cce1154246 | 28 | |
PixArtVY | 0:e9cce1154246 | 29 | void load(const uint8_t array[][2], uint8_t arraySize); |
PixArtVY | 0:e9cce1154246 | 30 | //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. |
PixArtVY | 0:e9cce1154246 | 31 | |
PixArtVY | 0:e9cce1154246 | 32 | void grabData(void); |
PixArtVY | 0:e9cce1154246 | 33 | //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. |
PixArtVY | 0:e9cce1154246 | 34 | |
PixArtVY | 0:e9cce1154246 | 35 | void printData(void); |
PixArtVY | 0:e9cce1154246 | 36 | //Prints the data out to a serial terminal. |
PixArtVY | 0:e9cce1154246 | 37 | |
PixArtVY | 0:e9cce1154246 | 38 | |
PixArtVY | 0:e9cce1154246 | 39 | |
PixArtVY | 0:e9cce1154246 | 40 | |
PixArtVY | 0:e9cce1154246 | 41 | |
PixArtVY | 0:e9cce1154246 | 42 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 43 | //Functions definitions |
PixArtVY | 0:e9cce1154246 | 44 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 45 | uint8_t readRegister(uint8_t addr) |
PixArtVY | 0:e9cce1154246 | 46 | { |
PixArtVY | 0:e9cce1154246 | 47 | uint8_t data; |
PixArtVY | 0:e9cce1154246 | 48 | i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0); //Send the address to the chip |
PixArtVY | 0:e9cce1154246 | 49 | wait_us(1); |
PixArtVY | 0:e9cce1154246 | 50 | i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0); //Send the memory address where you want to store the read data |
PixArtVY | 0:e9cce1154246 | 51 | return(data); |
PixArtVY | 0:e9cce1154246 | 52 | } |
PixArtVY | 0:e9cce1154246 | 53 | |
PixArtVY | 0:e9cce1154246 | 54 | |
PixArtVY | 0:e9cce1154246 | 55 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 56 | void writeRegister(uint8_t addr, uint8_t data) |
PixArtVY | 0:e9cce1154246 | 57 | { |
PixArtVY | 0:e9cce1154246 | 58 | char data_write[2]; //Create an array to store the address/data to pass them at the same time. |
PixArtVY | 0:e9cce1154246 | 59 | data_write[0] = addr; //Store the address in the first byte |
PixArtVY | 0:e9cce1154246 | 60 | data_write[1] = data; //Store the data in the second byte |
PixArtVY | 0:e9cce1154246 | 61 | i2c.write((I2C_Slave_ID << 1), data_write, 2, 0); //Send both over at once |
PixArtVY | 0:e9cce1154246 | 62 | |
PixArtVY | 0:e9cce1154246 | 63 | pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); |
PixArtVY | 0:e9cce1154246 | 64 | //Uncomment this line for debugging. Prints every register write operation. |
PixArtVY | 0:e9cce1154246 | 65 | } |
PixArtVY | 0:e9cce1154246 | 66 | |
PixArtVY | 0:e9cce1154246 | 67 | |
PixArtVY | 0:e9cce1154246 | 68 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 69 | void load(const uint8_t array[][2], uint8_t arraySize) |
PixArtVY | 0:e9cce1154246 | 70 | { |
PixArtVY | 0:e9cce1154246 | 71 | for(uint8_t q = 0; q < arraySize; q++) |
PixArtVY | 0:e9cce1154246 | 72 | { |
PixArtVY | 0:e9cce1154246 | 73 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
PixArtVY | 0:e9cce1154246 | 74 | } |
PixArtVY | 0:e9cce1154246 | 75 | } |
PixArtVY | 0:e9cce1154246 | 76 | |
PixArtVY | 0:e9cce1154246 | 77 | |
PixArtVY | 0:e9cce1154246 | 78 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 79 | void grabData(void) |
PixArtVY | 0:e9cce1154246 | 80 | { |
PixArtVY | 0:e9cce1154246 | 81 | deltaX_low = readRegister(0x03); //Grabs data from the proper registers. |
PixArtVY | 0:e9cce1154246 | 82 | deltaY_low = readRegister(0x04); |
PixArtVY | 0:e9cce1154246 | 83 | deltaX_high = (readRegister(0x12)<<4) & 0xF00; //Grabs data and shifts it to make space to be combined with lower bits. |
PixArtVY | 0:e9cce1154246 | 84 | deltaY_high = (readRegister(0x12)<<8) & 0xF00; |
PixArtVY | 0:e9cce1154246 | 85 | |
PixArtVY | 0:e9cce1154246 | 86 | if(deltaX_high & 0x800) |
PixArtVY | 0:e9cce1154246 | 87 | deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp) |
PixArtVY | 0:e9cce1154246 | 88 | |
PixArtVY | 0:e9cce1154246 | 89 | if(deltaY_high & 0x800) |
PixArtVY | 0:e9cce1154246 | 90 | deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp) |
PixArtVY | 0:e9cce1154246 | 91 | |
PixArtVY | 0:e9cce1154246 | 92 | deltaX = deltaX_high | deltaX_low; //Combined the low and high bits. |
PixArtVY | 0:e9cce1154246 | 93 | deltaY = deltaY_high | deltaY_low; |
PixArtVY | 0:e9cce1154246 | 94 | } |
PixArtVY | 0:e9cce1154246 | 95 | |
PixArtVY | 0:e9cce1154246 | 96 | |
PixArtVY | 0:e9cce1154246 | 97 | //========================================================================= |
PixArtVY | 0:e9cce1154246 | 98 | void printData(void) |
PixArtVY | 0:e9cce1154246 | 99 | { |
PixArtVY | 0:e9cce1154246 | 100 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtVY | 0:e9cce1154246 | 101 | { |
PixArtVY | 0:e9cce1154246 | 102 | totalX += deltaX; |
PixArtVY | 0:e9cce1154246 | 103 | totalY += deltaY; |
PixArtVY | 0:e9cce1154246 | 104 | |
PixArtVY | 0:e9cce1154246 | 105 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
PixArtVY | 3:979019410df2 | 106 | pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
PixArtVY | 0:e9cce1154246 | 107 | } |
PixArtVY | 0:e9cce1154246 | 108 | |
PixArtVY | 0:e9cce1154246 | 109 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtVY | 0:e9cce1154246 | 110 | deltaY = 0; |
PixArtVY | 0:e9cce1154246 | 111 | } |