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

Committer:
PixArtVY
Date:
Mon Jul 23 22:15:42 2018 +0000
Revision:
5:577976dae20d
Parent:
4:8f7d8ff001f8
Comments.

Who changed what in which revision?

UserRevisionLine numberNew 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 uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data
PixArtVY 0:e9cce1154246 51 cs = 1; //Set chip select back to high/inactive
PixArtVY 0:e9cce1154246 52 return data_read; //Returns 8-bit data from register
PixArtVY 0:e9cce1154246 53 }
PixArtVY 0:e9cce1154246 54
PixArtVY 0:e9cce1154246 55
PixArtVY 0:e9cce1154246 56 //=========================================================================
PixArtVY 0:e9cce1154246 57 void writeRegister(uint8_t addr, uint8_t data)
PixArtVY 0:e9cce1154246 58 {
PixArtVY 0:e9cce1154246 59 cs = 0; //Set chip select low/active
PixArtVY 0:e9cce1154246 60 addr = addr | 0x80; //Set MSB to 1 to indicate write operation
PixArtVY 0:e9cce1154246 61 spi.write(addr); //Write the given address
PixArtVY 0:e9cce1154246 62 spi.write(data); //Write the given data
PixArtVY 0:e9cce1154246 63 cs = 1; //Set chip select back to high/inactive
PixArtVY 0:e9cce1154246 64
PixArtVY 0:e9cce1154246 65 //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
PixArtVY 0:e9cce1154246 66 //Uncomment this line for debugging. Prints every register write operation.
PixArtVY 0:e9cce1154246 67 }
PixArtVY 0:e9cce1154246 68
PixArtVY 0:e9cce1154246 69
PixArtVY 0:e9cce1154246 70 //=========================================================================
PixArtVY 0:e9cce1154246 71 void load(const uint8_t array[][2], uint8_t arraySize)
PixArtVY 0:e9cce1154246 72 {
PixArtVY 0:e9cce1154246 73 for(uint8_t q = 0; q < arraySize; q++)
PixArtVY 0:e9cce1154246 74 {
PixArtVY 0:e9cce1154246 75 writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data.
PixArtVY 0:e9cce1154246 76 }
PixArtVY 0:e9cce1154246 77 }
PixArtVY 0:e9cce1154246 78
PixArtVY 0:e9cce1154246 79
PixArtVY 0:e9cce1154246 80 //=========================================================================
PixArtVY 0:e9cce1154246 81 void grabData(void)
PixArtVY 0:e9cce1154246 82 {
PixArtVY 0:e9cce1154246 83 deltaX_low = readRegister(0x03); //Grabs data from the proper registers.
PixArtVY 0:e9cce1154246 84 deltaY_low = readRegister(0x04);
PixArtVY 0:e9cce1154246 85 deltaX_high = (readRegister(0x12)<<4) & 0xF00; //Grabs data and shifts it to make space to be combined with lower bits.
PixArtVY 0:e9cce1154246 86 deltaY_high = (readRegister(0x12)<<8) & 0xF00;
PixArtVY 0:e9cce1154246 87
PixArtVY 0:e9cce1154246 88 if(deltaX_high & 0x800)
PixArtVY 0:e9cce1154246 89 deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
PixArtVY 0:e9cce1154246 90
PixArtVY 0:e9cce1154246 91 if(deltaY_high & 0x800)
PixArtVY 0:e9cce1154246 92 deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
PixArtVY 0:e9cce1154246 93
PixArtVY 0:e9cce1154246 94 deltaX = deltaX_high | deltaX_low; //Combined the low and high bits.
PixArtVY 0:e9cce1154246 95 deltaY = deltaY_high | deltaY_low;
PixArtVY 0:e9cce1154246 96 }
PixArtVY 0:e9cce1154246 97
PixArtVY 0:e9cce1154246 98
PixArtVY 0:e9cce1154246 99 //=========================================================================
PixArtVY 0:e9cce1154246 100 void printData(void)
PixArtVY 0:e9cce1154246 101 {
PixArtVY 0:e9cce1154246 102 if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data.
PixArtVY 0:e9cce1154246 103 {
PixArtVY 0:e9cce1154246 104 totalX += deltaX;
PixArtVY 0:e9cce1154246 105 totalY += deltaY;
PixArtVY 0:e9cce1154246 106
PixArtVY 0:e9cce1154246 107 pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY.
PixArtVY 3:979019410df2 108 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 109 }
PixArtVY 0:e9cce1154246 110
PixArtVY 0:e9cce1154246 111 deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
PixArtVY 0:e9cce1154246 112 deltaY = 0;
PixArtVY 0:e9cce1154246 113 }