PixArt / Mbed OS 3901_referenceFirmware
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // PMW3901MB: Optical Motion Tracking Chip reference code.
00002 // Version: 1.1
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.1 -- 18 July 2018
00017 Added apache license notice.
00018 
00019 Version 1.0 -- 13 Mar. 2018
00020 First release.
00021 */
00022 
00023 #include "mbed.h"
00024 #include "SPIcommFunctions.h"
00025 
00026 int main()
00027 {
00028     pc.baud(115200);                    // Set baud rate to 115200. Remember to sync serial terminal baud rate to the same value.
00029 
00030     spi.format(8,3);                    // Set SPI to 8 bits with inverted polarity and phase-shifted to second edge.
00031     spi.frequency(1000000);             // Set frequency for SPI communication.
00032     cs = 1;                             // Initialize chip select as inactive.
00033     
00034     pc.printf("Program START\n\r");
00035     
00036     pc.printf("ID Check: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly.
00037     if(readRegister(0x00) != 0x49)
00038     {
00039         pc.printf("Communication protocol error! Terminating program.\n\r");
00040         return 0;
00041     }
00042     
00043     initializeSensor();
00044     
00045     while(1)
00046     {
00047         //pc.printf("MOTION bit: %2X\n\r", (readRegister(0x02) & 0x80) >> 7);   //Prints motion bit for debugging. 1 = motion detected. 0 = no motion detected.
00048         
00049         if(readRegister(0x02) & 0x80)
00050         {
00051             grabData();
00052             printData();
00053         }
00054     }
00055 }