Reference firmware for PixArt's PMT9123 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.
I2CcommFunctions.h
00001 #define I2C_Slave_ID 0x33 //Slave ID of the PMT9123 sensor. 00002 00003 //========================================================================= 00004 //Communication pinouts for serial COM port, I2C, and interrupts 00005 //========================================================================= 00006 static Serial pc(USBTX, USBRX); //PC comm 00007 static I2C i2c(p26, p27); //SDA, SCL 00008 static DigitalOut NRESET(p25); //Laser diode pin. 00009 00010 //========================================================================= 00011 //Variables and arrays used for communications and data storage 00012 //========================================================================= 00013 int8_t deltaX_low, deltaY_low; //Stores the low-bits of movement data. 00014 int16_t deltaX_high, deltaY_high; //Stores the high-bits of movement data. 00015 int16_t deltaX, deltaY; //Stores the combined value of low and high bits. 00016 int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. 00017 00018 00019 //========================================================================= 00020 //Functions used to communicate with the sensor and grab/print data 00021 //========================================================================= 00022 uint8_t readRegister(uint8_t addr); 00023 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. 00024 00025 void writeRegister(uint8_t addr, uint8_t data); 00026 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. 00027 00028 void startup(void); 00029 //Starts up the sensor to prepare it after receiving power. 00030 00031 void initialize(void); 00032 //Runs the initialization sequence for the sensor. 00033 00034 void load(const uint8_t array[][2], uint8_t arraySize); 00035 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. 00036 00037 void grabData(void); 00038 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. 00039 00040 void printData(void); 00041 //Prints the data out to a serial terminal. 00042 00043 00044 00045 00046 00047 //========================================================================= 00048 //Functions definitions 00049 //========================================================================= 00050 uint8_t readRegister(uint8_t addr) 00051 { 00052 uint8_t data; 00053 00054 i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0); //Send the address to the chip 00055 wait_us(1); 00056 i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0); //Send the memory address where you want to store the read data 00057 return(data); 00058 } 00059 00060 00061 //========================================================================= 00062 void writeRegister(uint8_t addr, uint8_t data) 00063 { 00064 char data_write[2]; //Create an array to store the address/data to pass them at the same time. 00065 00066 data_write[0] = addr; //Store the address in the first byte 00067 data_write[1] = data; //Store the data in the second byte 00068 i2c.write((I2C_Slave_ID << 1), data_write, 2, 0); //Send both over at once 00069 00070 pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); 00071 //Uncomment this line for debugging. Prints every register write operation. 00072 } 00073 00074 00075 //========================================================================= 00076 void startup(void) 00077 { 00078 NRESET = 0; //Drive NRESET low for 20us to reset the device. 00079 wait_us(20); 00080 NRESET = 1; 00081 wait_ms(1.5); 00082 } 00083 00084 00085 //========================================================================= 00086 void initialize(void) 00087 { 00088 writeRegister(0x41, 0xBA); //Disables register write protection. 00089 wait_us(300); //Delay 300us for timing purposes. (necessary) 00090 00091 writeRegister(0x1D, 0x00); //Clears the "what mode are we in" register. 00092 wait_ms(10); //Delay 10ms for timing purposes. (necessary) 00093 00094 readRegister(0x02); //Clears motion bit and motion storage registers. 00095 readRegister(0x03); 00096 readRegister(0x04); 00097 readRegister(0x05); 00098 } 00099 00100 00101 //========================================================================= 00102 void load(const uint8_t array[][2], uint8_t arraySize) 00103 { 00104 for(uint8_t q = 0; q < arraySize; q++) 00105 { 00106 writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. 00107 } 00108 } 00109 00110 00111 //========================================================================= 00112 void grabData(void) 00113 { 00114 deltaX_low = readRegister(0x03); //Grabs data from the proper registers. 00115 deltaY_low = readRegister(0x04); 00116 deltaX_high = (readRegister(0x05)<<4) & 0xF00; //Grabs data and shifts it to make space to be combined with lower bits. 00117 deltaY_high = (readRegister(0x05)<<8) & 0xF00; 00118 00119 if(deltaX_high & 0x800) 00120 deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp) 00121 00122 if(deltaY_high & 0x800) 00123 deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp) 00124 00125 deltaX = deltaX_high | deltaX_low; //Combined the low and high bits. 00126 deltaY = deltaY_high | deltaY_low; 00127 } 00128 00129 00130 //========================================================================= 00131 void printData(void) 00132 { 00133 if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. 00134 { 00135 totalX += deltaX; 00136 totalY += deltaY; 00137 00138 pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. 00139 pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. 00140 } 00141 00142 deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. 00143 deltaY = 0; 00144 }
Generated on Fri Jul 15 2022 11:19:28 by 1.7.2