Reference firmware for PixArt's PAA5101 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 PAA5101 sensor and evaluation board.
For general information about this product, please visit this product's components page here:
https://os.mbed.com/components/PAA5101-Floor-Tracking-Sensor-with-Wide-/
For guides and tips on how to setup and evaluate the PAA5101 sensor with the Nordic nRF52-DK microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/5101_referenceCode/wiki/Guide-for-nRF52-DK-Platform
For guides and tips on how to setup and evaluate the PAA5101 sensor with any microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/5101_referenceCode/wiki/Guide-for-Any-Platform
commHeaders/SPIcommFunctions.h@2:045cc9f995ae, 2018-06-01 (annotated)
- Committer:
- PixArtVY
- Date:
- Fri Jun 01 21:37:39 2018 +0000
- Revision:
- 2:045cc9f995ae
- Parent:
- 1:469063631a05
- Child:
- 3:1d5c2956f415
Removed some unnecessary variables.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtVY | 0:2c144b6813d1 | 1 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 2 | //Communication pinouts for serial COM port, SPI, and interrupts |
PixArtVY | 0:2c144b6813d1 | 3 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 4 | static Serial pc(USBTX, USBRX); //PC serial communication. |
PixArtVY | 0:2c144b6813d1 | 5 | static SPI spi(p23, p24, p25); //MOSI, MISO, SCLK (because this is 3-Wire SPI, we tie MOSI and MISO together on the SDIO pin, but separate them when we get to the MCU). |
PixArtVY | 0:2c144b6813d1 | 6 | static DigitalOut cs(p22); //Chip select pin. |
PixArtVY | 0:2c144b6813d1 | 7 | static DigitalOut LDP(p20); //Laser diode pin. |
PixArtVY | 0:2c144b6813d1 | 8 | |
PixArtVY | 0:2c144b6813d1 | 9 | |
PixArtVY | 0:2c144b6813d1 | 10 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 11 | //Variables and arrays used for communications and data storage |
PixArtVY | 0:2c144b6813d1 | 12 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 13 | int16_t deltaX = 0; //Stores X-axis output data. |
PixArtVY | 0:2c144b6813d1 | 14 | int16_t deltaY = 0; //Stores Y-axis output data. |
PixArtVY | 1:469063631a05 | 15 | int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. |
PixArtVY | 0:2c144b6813d1 | 16 | uint8_t frameCounter = 0; //Looping variable to track when we have grabbed 8 frames of data. |
PixArtVY | 0:2c144b6813d1 | 17 | uint16_t imageQuality[8]; //Stores 8 frames of image data to determine when quality changes to a point where we need to switch modes (LED/laser). |
PixArtVY | 0:2c144b6813d1 | 18 | uint16_t imageQuality_total = 0; //Stores the sum of all raw values from the 8 frames from imageQuality[8]. |
PixArtVY | 0:2c144b6813d1 | 19 | uint8_t mode = 0; //Modes: 0 = Laser, 1 = LED. Laser is default. |
PixArtVY | 0:2c144b6813d1 | 20 | #define laser2LED_threshold 0x700 //Stores threshold level for when we need to swap to LED mode. |
PixArtVY | 0:2c144b6813d1 | 21 | #define LED2laser_threshold 0x500 //Stores threshold level for when we need to swap to laser mode. |
PixArtVY | 0:2c144b6813d1 | 22 | |
PixArtVY | 0:2c144b6813d1 | 23 | uint8_t currentBank; //Stores current register bank. |
PixArtVY | 0:2c144b6813d1 | 24 | |
PixArtVY | 0:2c144b6813d1 | 25 | |
PixArtVY | 0:2c144b6813d1 | 26 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 27 | //Functions used to communicate with the sensor and grab/print data |
PixArtVY | 0:2c144b6813d1 | 28 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 29 | uint8_t readRegister(uint8_t addr); |
PixArtVY | 0:2c144b6813d1 | 30 | //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. |
PixArtVY | 0:2c144b6813d1 | 31 | |
PixArtVY | 0:2c144b6813d1 | 32 | void writeRegister(uint8_t addr, uint8_t data); |
PixArtVY | 0:2c144b6813d1 | 33 | //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. |
PixArtVY | 0:2c144b6813d1 | 34 | |
PixArtVY | 0:2c144b6813d1 | 35 | void load(const uint8_t array[][2], uint8_t arraySize); |
PixArtVY | 0:2c144b6813d1 | 36 | //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. |
PixArtVY | 0:2c144b6813d1 | 37 | |
PixArtVY | 0:2c144b6813d1 | 38 | void checkMode(void); |
PixArtVY | 0:2c144b6813d1 | 39 | //This function checks the image quality of each frame and switches between LED and laser modes when necessary. |
PixArtVY | 0:2c144b6813d1 | 40 | |
PixArtVY | 0:2c144b6813d1 | 41 | void grabData(void); |
PixArtVY | 0:2c144b6813d1 | 42 | //Takes data from the sensor and stores it into variables deltaX and deltaY. |
PixArtVY | 0:2c144b6813d1 | 43 | |
PixArtVY | 0:2c144b6813d1 | 44 | void printData(void); |
PixArtVY | 0:2c144b6813d1 | 45 | //Prints the deltaX and deltaY values to a serial terminal whenever the values change. |
PixArtVY | 0:2c144b6813d1 | 46 | |
PixArtVY | 0:2c144b6813d1 | 47 | |
PixArtVY | 0:2c144b6813d1 | 48 | |
PixArtVY | 0:2c144b6813d1 | 49 | |
PixArtVY | 0:2c144b6813d1 | 50 | |
PixArtVY | 0:2c144b6813d1 | 51 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 52 | //Functions definitions |
PixArtVY | 0:2c144b6813d1 | 53 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 54 | uint8_t readRegister(uint8_t addr) |
PixArtVY | 0:2c144b6813d1 | 55 | { |
PixArtVY | 0:2c144b6813d1 | 56 | cs = 0; //Set chip select low/active |
PixArtVY | 0:2c144b6813d1 | 57 | addr = addr & 0x7F; //Set MSB to 0 to indicate read operation |
PixArtVY | 0:2c144b6813d1 | 58 | spi.write(addr); //Write the given address |
PixArtVY | 0:2c144b6813d1 | 59 | wait_ms(1); |
PixArtVY | 0:2c144b6813d1 | 60 | uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data |
PixArtVY | 0:2c144b6813d1 | 61 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:2c144b6813d1 | 62 | return data_read; //Returns 8-bit data from register |
PixArtVY | 0:2c144b6813d1 | 63 | } |
PixArtVY | 0:2c144b6813d1 | 64 | |
PixArtVY | 0:2c144b6813d1 | 65 | |
PixArtVY | 0:2c144b6813d1 | 66 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 67 | void writeRegister(uint8_t addr, uint8_t data) |
PixArtVY | 0:2c144b6813d1 | 68 | { |
PixArtVY | 0:2c144b6813d1 | 69 | cs = 0; //Set chip select low/active |
PixArtVY | 0:2c144b6813d1 | 70 | addr = addr | 0x80; //Set MSB to 1 to indicate write operation |
PixArtVY | 0:2c144b6813d1 | 71 | spi.write(addr); //Write the given address |
PixArtVY | 0:2c144b6813d1 | 72 | spi.write(data); //Write the given data |
PixArtVY | 0:2c144b6813d1 | 73 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:2c144b6813d1 | 74 | |
PixArtVY | 0:2c144b6813d1 | 75 | if(addr == 0x7F) //You can't read register 0x7F for the current bank so we store the value before writing it |
PixArtVY | 0:2c144b6813d1 | 76 | { |
PixArtVY | 0:2c144b6813d1 | 77 | currentBank = data; //Store the current bank for printing later. |
PixArtVY | 0:2c144b6813d1 | 78 | //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), currentBank); |
PixArtVY | 0:2c144b6813d1 | 79 | //Uncomment this line and the other print line below for debugging. Prints every register bank change. |
PixArtVY | 0:2c144b6813d1 | 80 | } |
PixArtVY | 0:2c144b6813d1 | 81 | |
PixArtVY | 0:2c144b6813d1 | 82 | else |
PixArtVY | 0:2c144b6813d1 | 83 | { |
PixArtVY | 0:2c144b6813d1 | 84 | //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), readRegister(addr)); |
PixArtVY | 0:2c144b6813d1 | 85 | //Uncomment this line and the other print line above for debugging. Prints every register write operation. |
PixArtVY | 0:2c144b6813d1 | 86 | } |
PixArtVY | 0:2c144b6813d1 | 87 | |
PixArtVY | 0:2c144b6813d1 | 88 | wait_ms(2); |
PixArtVY | 0:2c144b6813d1 | 89 | } |
PixArtVY | 0:2c144b6813d1 | 90 | |
PixArtVY | 0:2c144b6813d1 | 91 | |
PixArtVY | 0:2c144b6813d1 | 92 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 93 | void load(const uint8_t array[][2], uint8_t arraySize) |
PixArtVY | 0:2c144b6813d1 | 94 | { |
PixArtVY | 0:2c144b6813d1 | 95 | for(uint8_t q = 0; q < arraySize; q++) |
PixArtVY | 0:2c144b6813d1 | 96 | { |
PixArtVY | 0:2c144b6813d1 | 97 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
PixArtVY | 0:2c144b6813d1 | 98 | } |
PixArtVY | 0:2c144b6813d1 | 99 | } |
PixArtVY | 0:2c144b6813d1 | 100 | |
PixArtVY | 0:2c144b6813d1 | 101 | |
PixArtVY | 0:2c144b6813d1 | 102 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 103 | void checkMode(void) |
PixArtVY | 0:2c144b6813d1 | 104 | { |
PixArtVY | 0:2c144b6813d1 | 105 | uint16_t data_msb, data_lsb = 0; //These variables store the high and low bits of image quality data. |
PixArtVY | 0:2c144b6813d1 | 106 | imageQuality_total = 0; //This variable holds the sum of 8 frames of data. |
PixArtVY | 0:2c144b6813d1 | 107 | |
PixArtVY | 0:2c144b6813d1 | 108 | data_msb = readRegister(0x75); //Reads upper 8 bits of image quality. |
PixArtVY | 0:2c144b6813d1 | 109 | data_lsb = readRegister(0x76); //Reads lower 8 bits of image quality. |
PixArtVY | 0:2c144b6813d1 | 110 | imageQuality[frameCounter] = data_msb*256 + data_lsb; //Combines the low/high bits to be a single element in this array. |
PixArtVY | 0:2c144b6813d1 | 111 | |
PixArtVY | 0:2c144b6813d1 | 112 | if(frameCounter == 7) //We check image quality every EIGHT samples before determining if a mode-switch is necessary. |
PixArtVY | 0:2c144b6813d1 | 113 | { |
PixArtVY | 0:2c144b6813d1 | 114 | for(int i=0; i<8; i++) //Sums up 8 frames of image quality data into 1 variable. |
PixArtVY | 0:2c144b6813d1 | 115 | { |
PixArtVY | 0:2c144b6813d1 | 116 | imageQuality_total = imageQuality_total + imageQuality[i]; |
PixArtVY | 0:2c144b6813d1 | 117 | } |
PixArtVY | 0:2c144b6813d1 | 118 | |
PixArtVY | 1:469063631a05 | 119 | if(mode == 1 && imageQuality_total < LED2laser_threshold) //Check the condition for when we need to change to laser. |
PixArtVY | 0:2c144b6813d1 | 120 | { |
PixArtVY | 1:469063631a05 | 121 | load(modeLaser, modeLaser_size); //Loads the register array that has the settings for laser mode. |
PixArtVY | 1:469063631a05 | 122 | mode = 0; //Sets the mode-tracker to be 0 to reflect laser mode. |
PixArtVY | 1:469063631a05 | 123 | LDP = 0; //Sets the laser diode GPIO pin to be LOW (active because of PMOS pull-up). |
PixArtVY | 1:469063631a05 | 124 | wait_ms(40); //Delay for timing. |
PixArtVY | 1:469063631a05 | 125 | writeRegister(0x03, 0x00); //Unlisted... |
PixArtVY | 0:2c144b6813d1 | 126 | |
PixArtVY | 1:469063631a05 | 127 | //pc.printf("Image Quality: %4X\n\r", imageQuality_total); |
PixArtVY | 1:469063631a05 | 128 | //pc.printf("Now in laser mode. Value of 'mode': %1X\n\r", mode); |
PixArtVY | 1:469063631a05 | 129 | //Uncomment these lines and the other print lines below for debugging. Prints image quality and tracks mode-switches. |
PixArtVY | 0:2c144b6813d1 | 130 | } |
PixArtVY | 0:2c144b6813d1 | 131 | |
PixArtVY | 1:469063631a05 | 132 | if(mode == 0 && imageQuality_total < laser2LED_threshold) //Check the condition for when we need to change to LED. |
PixArtVY | 0:2c144b6813d1 | 133 | { |
PixArtVY | 1:469063631a05 | 134 | load(modeLED, modeLED_size); //Loads the register array that has the settings for LED mode. |
PixArtVY | 1:469063631a05 | 135 | mode = 1; //Sets the mode-tracker to be 1 to reflect LED mode. |
PixArtVY | 1:469063631a05 | 136 | LDP = 1; //Sets the laser diode GPIO pin to be HIGH (inactive because of PMOS pull-up). |
PixArtVY | 1:469063631a05 | 137 | wait_ms(40); //Delay for timing. |
PixArtVY | 1:469063631a05 | 138 | writeRegister(0x03, 0x00); //Unlisted... |
PixArtVY | 0:2c144b6813d1 | 139 | |
PixArtVY | 1:469063631a05 | 140 | //pc.printf("Image Quality: %4X\n\r", imageQuality_total); |
PixArtVY | 1:469063631a05 | 141 | //pc.printf("Now in LED mode. Value of 'mode': %1X\n\r", mode); |
PixArtVY | 1:469063631a05 | 142 | //Uncomment these lines and the other print lines above for debugging. Prints image quality and tracks mode-switches. |
PixArtVY | 0:2c144b6813d1 | 143 | } |
PixArtVY | 0:2c144b6813d1 | 144 | } |
PixArtVY | 0:2c144b6813d1 | 145 | |
PixArtVY | 0:2c144b6813d1 | 146 | frameCounter = (frameCounter + 1) & 0x07; //This variable loops from zero to 7 without using a looper or variable. Every time this function is called, it moves up 1 towards 7 or resets to zero. |
PixArtVY | 0:2c144b6813d1 | 147 | } |
PixArtVY | 0:2c144b6813d1 | 148 | |
PixArtVY | 0:2c144b6813d1 | 149 | |
PixArtVY | 0:2c144b6813d1 | 150 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 151 | void grabData(void) |
PixArtVY | 0:2c144b6813d1 | 152 | { |
PixArtVY | 0:2c144b6813d1 | 153 | int16_t deltaX_high, deltaX_low = 0; //These four variables store the low/high bits of the deltaX and deltaY data. |
PixArtVY | 0:2c144b6813d1 | 154 | int16_t deltaY_high, deltaY_low = 0; |
PixArtVY | 0:2c144b6813d1 | 155 | |
PixArtVY | 0:2c144b6813d1 | 156 | deltaX_low = (int16_t)readRegister(0x03); //Grabbing the data from registers. |
PixArtVY | 0:2c144b6813d1 | 157 | deltaY_low = (int16_t)readRegister(0x04); |
PixArtVY | 0:2c144b6813d1 | 158 | deltaX_high = ((int16_t)readRegister(0x11))<<8; //Shifts high bits left by 8 so that we can combine high and low bytes together. |
PixArtVY | 0:2c144b6813d1 | 159 | deltaY_high = ((int16_t)readRegister(0x12))<<8; |
PixArtVY | 0:2c144b6813d1 | 160 | |
PixArtVY | 0:2c144b6813d1 | 161 | deltaX = deltaX_high | deltaX_low; //Combines the low/high bits of X and Y to be one variable. |
PixArtVY | 0:2c144b6813d1 | 162 | deltaY = deltaY_high | deltaY_low; |
PixArtVY | 0:2c144b6813d1 | 163 | } |
PixArtVY | 0:2c144b6813d1 | 164 | |
PixArtVY | 0:2c144b6813d1 | 165 | |
PixArtVY | 0:2c144b6813d1 | 166 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 167 | void printData(void) |
PixArtVY | 0:2c144b6813d1 | 168 | { |
PixArtVY | 0:2c144b6813d1 | 169 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtVY | 0:2c144b6813d1 | 170 | { |
PixArtVY | 1:469063631a05 | 171 | totalX += deltaX; |
PixArtVY | 1:469063631a05 | 172 | totalY += deltaY; |
PixArtVY | 1:469063631a05 | 173 | |
PixArtVY | 1:469063631a05 | 174 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
PixArtVY | 1:469063631a05 | 175 | pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
PixArtVY | 0:2c144b6813d1 | 176 | } |
PixArtVY | 0:2c144b6813d1 | 177 | |
PixArtVY | 0:2c144b6813d1 | 178 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtVY | 0:2c144b6813d1 | 179 | deltaY = 0; |
PixArtVY | 0:2c144b6813d1 | 180 | } |