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.

Committer:
PixArtVY
Date:
Wed Jul 18 18:36:29 2018 +0000
Revision:
1:1d99f5c9581f
Parent:
0:71ff24e8c21e
Added Apache License notice.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PixArtVY 0:71ff24e8c21e 1 #define I2C_Slave_ID 0x33 //Slave ID of the PMT9123 sensor.
PixArtVY 0:71ff24e8c21e 2
PixArtVY 0:71ff24e8c21e 3 //=========================================================================
PixArtVY 0:71ff24e8c21e 4 //Communication pinouts for serial COM port, I2C, and interrupts
PixArtVY 0:71ff24e8c21e 5 //=========================================================================
PixArtVY 0:71ff24e8c21e 6 static Serial pc(USBTX, USBRX); //PC comm
PixArtVY 0:71ff24e8c21e 7 static I2C i2c(p26, p27); //SDA, SCL
PixArtVY 0:71ff24e8c21e 8 static DigitalOut NRESET(p25); //Laser diode pin.
PixArtVY 0:71ff24e8c21e 9
PixArtVY 0:71ff24e8c21e 10 //=========================================================================
PixArtVY 0:71ff24e8c21e 11 //Variables and arrays used for communications and data storage
PixArtVY 0:71ff24e8c21e 12 //=========================================================================
PixArtVY 0:71ff24e8c21e 13 int8_t deltaX_low, deltaY_low; //Stores the low-bits of movement data.
PixArtVY 0:71ff24e8c21e 14 int16_t deltaX_high, deltaY_high; //Stores the high-bits of movement data.
PixArtVY 0:71ff24e8c21e 15 int16_t deltaX, deltaY; //Stores the combined value of low and high bits.
PixArtVY 0:71ff24e8c21e 16 int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime.
PixArtVY 0:71ff24e8c21e 17
PixArtVY 0:71ff24e8c21e 18
PixArtVY 0:71ff24e8c21e 19 //=========================================================================
PixArtVY 0:71ff24e8c21e 20 //Functions used to communicate with the sensor and grab/print data
PixArtVY 0:71ff24e8c21e 21 //=========================================================================
PixArtVY 0:71ff24e8c21e 22 uint8_t readRegister(uint8_t addr);
PixArtVY 0:71ff24e8c21e 23 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
PixArtVY 0:71ff24e8c21e 24
PixArtVY 0:71ff24e8c21e 25 void writeRegister(uint8_t addr, uint8_t data);
PixArtVY 0:71ff24e8c21e 26 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
PixArtVY 0:71ff24e8c21e 27
PixArtVY 0:71ff24e8c21e 28 void startup(void);
PixArtVY 0:71ff24e8c21e 29 //Starts up the sensor to prepare it after receiving power.
PixArtVY 0:71ff24e8c21e 30
PixArtVY 0:71ff24e8c21e 31 void initialize(void);
PixArtVY 0:71ff24e8c21e 32 //Runs the initialization sequence for the sensor.
PixArtVY 0:71ff24e8c21e 33
PixArtVY 0:71ff24e8c21e 34 void load(const uint8_t array[][2], uint8_t arraySize);
PixArtVY 0:71ff24e8c21e 35 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
PixArtVY 0:71ff24e8c21e 36
PixArtVY 0:71ff24e8c21e 37 void grabData(void);
PixArtVY 0:71ff24e8c21e 38 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
PixArtVY 0:71ff24e8c21e 39
PixArtVY 0:71ff24e8c21e 40 void printData(void);
PixArtVY 0:71ff24e8c21e 41 //Prints the data out to a serial terminal.
PixArtVY 0:71ff24e8c21e 42
PixArtVY 0:71ff24e8c21e 43
PixArtVY 0:71ff24e8c21e 44
PixArtVY 0:71ff24e8c21e 45
PixArtVY 0:71ff24e8c21e 46
PixArtVY 0:71ff24e8c21e 47 //=========================================================================
PixArtVY 0:71ff24e8c21e 48 //Functions definitions
PixArtVY 0:71ff24e8c21e 49 //=========================================================================
PixArtVY 0:71ff24e8c21e 50 uint8_t readRegister(uint8_t addr)
PixArtVY 0:71ff24e8c21e 51 {
PixArtVY 0:71ff24e8c21e 52 uint8_t data;
PixArtVY 0:71ff24e8c21e 53
PixArtVY 0:71ff24e8c21e 54 i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0); //Send the address to the chip
PixArtVY 0:71ff24e8c21e 55 wait_us(1);
PixArtVY 0:71ff24e8c21e 56 i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0); //Send the memory address where you want to store the read data
PixArtVY 0:71ff24e8c21e 57 return(data);
PixArtVY 0:71ff24e8c21e 58 }
PixArtVY 0:71ff24e8c21e 59
PixArtVY 0:71ff24e8c21e 60
PixArtVY 0:71ff24e8c21e 61 //=========================================================================
PixArtVY 0:71ff24e8c21e 62 void writeRegister(uint8_t addr, uint8_t data)
PixArtVY 0:71ff24e8c21e 63 {
PixArtVY 0:71ff24e8c21e 64 char data_write[2]; //Create an array to store the address/data to pass them at the same time.
PixArtVY 0:71ff24e8c21e 65
PixArtVY 0:71ff24e8c21e 66 data_write[0] = addr; //Store the address in the first byte
PixArtVY 0:71ff24e8c21e 67 data_write[1] = data; //Store the data in the second byte
PixArtVY 0:71ff24e8c21e 68 i2c.write((I2C_Slave_ID << 1), data_write, 2, 0); //Send both over at once
PixArtVY 0:71ff24e8c21e 69
PixArtVY 0:71ff24e8c21e 70 pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
PixArtVY 0:71ff24e8c21e 71 //Uncomment this line for debugging. Prints every register write operation.
PixArtVY 0:71ff24e8c21e 72 }
PixArtVY 0:71ff24e8c21e 73
PixArtVY 0:71ff24e8c21e 74
PixArtVY 0:71ff24e8c21e 75 //=========================================================================
PixArtVY 0:71ff24e8c21e 76 void startup(void)
PixArtVY 0:71ff24e8c21e 77 {
PixArtVY 0:71ff24e8c21e 78 NRESET = 0; //Drive NRESET low for 20us to reset the device.
PixArtVY 0:71ff24e8c21e 79 wait_us(20);
PixArtVY 0:71ff24e8c21e 80 NRESET = 1;
PixArtVY 0:71ff24e8c21e 81 wait_ms(1.5);
PixArtVY 0:71ff24e8c21e 82 }
PixArtVY 0:71ff24e8c21e 83
PixArtVY 0:71ff24e8c21e 84
PixArtVY 0:71ff24e8c21e 85 //=========================================================================
PixArtVY 0:71ff24e8c21e 86 void initialize(void)
PixArtVY 0:71ff24e8c21e 87 {
PixArtVY 0:71ff24e8c21e 88 writeRegister(0x41, 0xBA); //Disables register write protection.
PixArtVY 0:71ff24e8c21e 89 wait_us(300); //Delay 300us for timing purposes. (necessary)
PixArtVY 0:71ff24e8c21e 90
PixArtVY 0:71ff24e8c21e 91 writeRegister(0x1D, 0x00); //Clears the "what mode are we in" register.
PixArtVY 0:71ff24e8c21e 92 wait_ms(10); //Delay 10ms for timing purposes. (necessary)
PixArtVY 0:71ff24e8c21e 93
PixArtVY 0:71ff24e8c21e 94 readRegister(0x02); //Clears motion bit and motion storage registers.
PixArtVY 0:71ff24e8c21e 95 readRegister(0x03);
PixArtVY 0:71ff24e8c21e 96 readRegister(0x04);
PixArtVY 0:71ff24e8c21e 97 readRegister(0x05);
PixArtVY 0:71ff24e8c21e 98 }
PixArtVY 0:71ff24e8c21e 99
PixArtVY 0:71ff24e8c21e 100
PixArtVY 0:71ff24e8c21e 101 //=========================================================================
PixArtVY 0:71ff24e8c21e 102 void load(const uint8_t array[][2], uint8_t arraySize)
PixArtVY 0:71ff24e8c21e 103 {
PixArtVY 0:71ff24e8c21e 104 for(uint8_t q = 0; q < arraySize; q++)
PixArtVY 0:71ff24e8c21e 105 {
PixArtVY 0:71ff24e8c21e 106 writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data.
PixArtVY 0:71ff24e8c21e 107 }
PixArtVY 0:71ff24e8c21e 108 }
PixArtVY 0:71ff24e8c21e 109
PixArtVY 0:71ff24e8c21e 110
PixArtVY 0:71ff24e8c21e 111 //=========================================================================
PixArtVY 0:71ff24e8c21e 112 void grabData(void)
PixArtVY 0:71ff24e8c21e 113 {
PixArtVY 0:71ff24e8c21e 114 deltaX_low = readRegister(0x03); //Grabs data from the proper registers.
PixArtVY 0:71ff24e8c21e 115 deltaY_low = readRegister(0x04);
PixArtVY 0:71ff24e8c21e 116 deltaX_high = (readRegister(0x05)<<4) & 0xF00; //Grabs data and shifts it to make space to be combined with lower bits.
PixArtVY 0:71ff24e8c21e 117 deltaY_high = (readRegister(0x05)<<8) & 0xF00;
PixArtVY 0:71ff24e8c21e 118
PixArtVY 0:71ff24e8c21e 119 if(deltaX_high & 0x800)
PixArtVY 0:71ff24e8c21e 120 deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
PixArtVY 0:71ff24e8c21e 121
PixArtVY 0:71ff24e8c21e 122 if(deltaY_high & 0x800)
PixArtVY 0:71ff24e8c21e 123 deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
PixArtVY 0:71ff24e8c21e 124
PixArtVY 0:71ff24e8c21e 125 deltaX = deltaX_high | deltaX_low; //Combined the low and high bits.
PixArtVY 0:71ff24e8c21e 126 deltaY = deltaY_high | deltaY_low;
PixArtVY 0:71ff24e8c21e 127 }
PixArtVY 0:71ff24e8c21e 128
PixArtVY 0:71ff24e8c21e 129
PixArtVY 0:71ff24e8c21e 130 //=========================================================================
PixArtVY 0:71ff24e8c21e 131 void printData(void)
PixArtVY 0:71ff24e8c21e 132 {
PixArtVY 0:71ff24e8c21e 133 if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data.
PixArtVY 0:71ff24e8c21e 134 {
PixArtVY 0:71ff24e8c21e 135 totalX += deltaX;
PixArtVY 0:71ff24e8c21e 136 totalY += deltaY;
PixArtVY 0:71ff24e8c21e 137
PixArtVY 0:71ff24e8c21e 138 pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY.
PixArtVY 0:71ff24e8c21e 139 pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime.
PixArtVY 0:71ff24e8c21e 140 }
PixArtVY 0:71ff24e8c21e 141
PixArtVY 0:71ff24e8c21e 142 deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
PixArtVY 0:71ff24e8c21e 143 deltaY = 0;
PixArtVY 0:71ff24e8c21e 144 }