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/SPIcommFunctions.h@1:4ea157704c58, 2018-02-28 (annotated)
- Committer:
- PixArtVY
- Date:
- Wed Feb 28 21:16:23 2018 +0000
- Revision:
- 1:4ea157704c58
- Parent:
- 0:e9cce1154246
- Child:
- 3:979019410df2
Reference firmware for the 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 program and not as a library.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| PixArtVY | 1:4ea157704c58 | 1 | #define SPImode //Used to check if the sensor is in I2C mode or SPI mode. |
| PixArtVY | 0:e9cce1154246 | 2 | |
| PixArtVY | 0:e9cce1154246 | 3 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 4 | //Communication pinouts for serial COM port, SPI, and interrupts |
| PixArtVY | 0:e9cce1154246 | 5 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 6 | static Serial pc(USBTX, USBRX); //PC comm |
| PixArtVY | 0:e9cce1154246 | 7 | static SPI spi(p23, p24, p25); //mosi, miso, sclk |
| PixArtVY | 0:e9cce1154246 | 8 | static DigitalOut cs(p22); //chip select |
| 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 | cs = 0; //Set chip select low/active |
| PixArtVY | 0:e9cce1154246 | 48 | addr = addr & 0x7F; //Set MSB to 0 to indicate read operation |
| PixArtVY | 0:e9cce1154246 | 49 | spi.write(addr); //Write the given address |
| PixArtVY | 0:e9cce1154246 | 50 | wait_us(1); //Add a tiny delay after sending address for some internal cycle timing. |
| PixArtVY | 0:e9cce1154246 | 51 | uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data |
| PixArtVY | 0:e9cce1154246 | 52 | cs = 1; //Set chip select back to high/inactive |
| PixArtVY | 0:e9cce1154246 | 53 | return data_read; //Returns 8-bit data from register |
| PixArtVY | 0:e9cce1154246 | 54 | } |
| PixArtVY | 0:e9cce1154246 | 55 | |
| PixArtVY | 0:e9cce1154246 | 56 | |
| PixArtVY | 0:e9cce1154246 | 57 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 58 | void writeRegister(uint8_t addr, uint8_t data) |
| PixArtVY | 0:e9cce1154246 | 59 | { |
| PixArtVY | 0:e9cce1154246 | 60 | cs = 0; //Set chip select low/active |
| PixArtVY | 0:e9cce1154246 | 61 | addr = addr | 0x80; //Set MSB to 1 to indicate write operation |
| PixArtVY | 0:e9cce1154246 | 62 | spi.write(addr); //Write the given address |
| PixArtVY | 0:e9cce1154246 | 63 | spi.write(data); //Write the given data |
| PixArtVY | 0:e9cce1154246 | 64 | cs = 1; //Set chip select back to high/inactive |
| PixArtVY | 0:e9cce1154246 | 65 | |
| PixArtVY | 0:e9cce1154246 | 66 | //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); |
| PixArtVY | 0:e9cce1154246 | 67 | //Uncomment this line for debugging. Prints every register write operation. |
| PixArtVY | 0:e9cce1154246 | 68 | } |
| PixArtVY | 0:e9cce1154246 | 69 | |
| PixArtVY | 0:e9cce1154246 | 70 | |
| PixArtVY | 0:e9cce1154246 | 71 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 72 | void load(const uint8_t array[][2], uint8_t arraySize) |
| PixArtVY | 0:e9cce1154246 | 73 | { |
| PixArtVY | 0:e9cce1154246 | 74 | for(uint8_t q = 0; q < arraySize; q++) |
| PixArtVY | 0:e9cce1154246 | 75 | { |
| PixArtVY | 0:e9cce1154246 | 76 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
| PixArtVY | 0:e9cce1154246 | 77 | } |
| PixArtVY | 0:e9cce1154246 | 78 | } |
| PixArtVY | 0:e9cce1154246 | 79 | |
| PixArtVY | 0:e9cce1154246 | 80 | |
| PixArtVY | 0:e9cce1154246 | 81 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 82 | void grabData(void) |
| PixArtVY | 0:e9cce1154246 | 83 | { |
| PixArtVY | 0:e9cce1154246 | 84 | deltaX_low = readRegister(0x03); //Grabs data from the proper registers. |
| PixArtVY | 0:e9cce1154246 | 85 | deltaY_low = readRegister(0x04); |
| PixArtVY | 0:e9cce1154246 | 86 | deltaX_high = (readRegister(0x12)<<4) & 0xF00; //Grabs data and shifts it to make space to be combined with lower bits. |
| PixArtVY | 0:e9cce1154246 | 87 | deltaY_high = (readRegister(0x12)<<8) & 0xF00; |
| PixArtVY | 0:e9cce1154246 | 88 | |
| PixArtVY | 0:e9cce1154246 | 89 | if(deltaX_high & 0x800) |
| PixArtVY | 0:e9cce1154246 | 90 | deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp) |
| PixArtVY | 0:e9cce1154246 | 91 | |
| PixArtVY | 0:e9cce1154246 | 92 | if(deltaY_high & 0x800) |
| PixArtVY | 0:e9cce1154246 | 93 | deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp) |
| PixArtVY | 0:e9cce1154246 | 94 | |
| PixArtVY | 0:e9cce1154246 | 95 | deltaX = deltaX_high | deltaX_low; //Combined the low and high bits. |
| PixArtVY | 0:e9cce1154246 | 96 | deltaY = deltaY_high | deltaY_low; |
| PixArtVY | 0:e9cce1154246 | 97 | } |
| PixArtVY | 0:e9cce1154246 | 98 | |
| PixArtVY | 0:e9cce1154246 | 99 | |
| PixArtVY | 0:e9cce1154246 | 100 | //========================================================================= |
| PixArtVY | 0:e9cce1154246 | 101 | void printData(void) |
| PixArtVY | 0:e9cce1154246 | 102 | { |
| PixArtVY | 0:e9cce1154246 | 103 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
| PixArtVY | 0:e9cce1154246 | 104 | { |
| PixArtVY | 0:e9cce1154246 | 105 | totalX += deltaX; |
| PixArtVY | 0:e9cce1154246 | 106 | totalY += deltaY; |
| PixArtVY | 0:e9cce1154246 | 107 | |
| PixArtVY | 0:e9cce1154246 | 108 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
| PixArtVY | 0:e9cce1154246 | 109 | pc.printf("X-axis Counts: %d\t\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
| PixArtVY | 0:e9cce1154246 | 110 | } |
| PixArtVY | 0:e9cce1154246 | 111 | |
| PixArtVY | 0:e9cce1154246 | 112 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
| PixArtVY | 0:e9cce1154246 | 113 | deltaY = 0; |
| PixArtVY | 0:e9cce1154246 | 114 | } |
PAT9125EL | Versatile Low-Energy Surface Tracking Sensor