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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // PAA5101 Optical Tracking Minuature Chip reference code.
00002 // Version: 1.11
00003 // Latest Revision Date: 18 July 2018
00004 // By PixArt Imaging Inc.
00005 // Primary Engineer: Vincent Yeh (PixArt USA)
00006 
00007 // Copyright [2018] [Vincent Yeh]
00008 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
00009 // http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 
00012 /*
00013 //=======================
00014 //Revision History
00015 //=======================
00016 Version 1.11 -- 18 July 2018
00017 Added apache license notice.
00018 
00019 Version 1.1
00020 Fixed SPI frequency from 10MHz to 2MHz.
00021 
00022 Version 1.0
00023 First release.
00024 */
00025 
00026 #include "mbed.h"
00027 #include "registerArrays.h"
00028 #include "SPIcommFunctions.h"
00029 
00030 int main()
00031 {
00032     pc.baud(115200);                    // Set baud rate to 115200. Remember to sync serial terminal baud rate to the same value.
00033 
00034     spi.format(8,3);                    // Set SPI to 8 bits with inverted polarity and phase-shifted to second edge.
00035     spi.frequency(2000000);            // Set frequency for SPI communication.
00036     cs = 1;                             // Initialize chip-select pin to be HIGH (inactive).
00037     
00038     pc.printf("Program START\n\r");
00039     
00040     writeRegister(0x7F, 0x00);          //Reset to bank 0 before initializing again
00041     
00042     pc.printf("ID Check: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly.
00043     if(readRegister(0x00) != 0x31)
00044     {
00045         pc.printf("Communication protocol error! Terminating program.\n\r");
00046         return 0;
00047     }
00048     
00049     writeRegister(0x06, 0x80);          //Resets the chip now that we have verified that the communication works.
00050     wait_ms(1);                         //Wait at least 1 millisecond after resetting for timing.
00051     
00052     pc.printf("ID Check #2: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly after the reset.
00053     if(readRegister(0x00) != 0x31)
00054     {
00055         pc.printf("Communication protocol error! Terminating program.\n\r");
00056         return 0;
00057     }
00058     
00059     writeRegister(0x09, 0x5A);          //Disables write-protect
00060     writeRegister(0x51, 0x06);          //Sets laser diode power. Value should be <= 6
00061     
00062     load(initialize, initialize_size);  //Loads initial register settings
00063     
00064     writeRegister(0x5D, 0x3E);          //Unlisted register. Internal recommendation...
00065     wait_ms(10);
00066     writeRegister(0x5D, 0x3F);          //Unlisted register. Internal recommendation...
00067 
00068     load(modeLaser, modeLaser_size);    //Goes into laser mode which is the default mode (not LED)
00069     mode = 0;                           //This variable tracks if we are in laser mode or LED mode. 0 = laser, 1 = LED
00070     LDP = 0;                            //Change GPIO pin to the laser diode to be LOW to activate the laser.
00071     
00072     writeRegister(0x09, 0x00);          //Enables write-protect
00073     
00074     
00075     
00076     while(1)                            //After all setup/initialization is done, we can loop for interrupts and mode detection.
00077     {   
00078         checkMode();                    //Checks image quality and switches to laser or LED mode accordingly.
00079         
00080         if(readRegister(0x02) & 0x80)   //If motion bit (bit 7 of register 0x02) is 1, movement has been detected.
00081         {
00082             grabData();                 //Grabs data into variables deltaX and deltaY.
00083             printData();                //Prints deltaX and deltaY, but only if they have changed from their previous values.
00084         }
00085     }
00086 }