├── README.md ├── mcs-12085.cpp └── mcs-12085.h /README.md: -------------------------------------------------------------------------------- 1 | Arduino interface to MCS-12085/12086 Optical Mouse Sensor 2 | 3 | Connect SDIO/SCLK to pins defined in mcs-12085.cpp and then call 4 | mcs12085_dx() and mcs12085_dy() to read distance mouse has moved since 5 | last call. -------------------------------------------------------------------------------- /mcs-12085.cpp: -------------------------------------------------------------------------------- 1 | // mcs-12085.cpp 2 | // 3 | // Copyright (c) 2012 John Graham-Cumming 4 | // 5 | // Code to interface to MCS-12085 Optical Mouse Chip 6 | 7 | #include "mcs-12085.h" 8 | 9 | // The digital pins that are connected to SCLK and SDIO on the sensor 10 | 11 | int clk_pin = 4; 12 | int dat_pin = 7; 13 | 14 | // The time of a clock pulse. This will be used twice to make the clock 15 | // signal: cycle us low and then cycle us high 16 | int cycle = 25; 17 | 18 | // mcs12085_init: set up the pins for the clock and data 19 | void mcs12085_init() 20 | { 21 | // When not being clocked the clock pin needs to be high 22 | 23 | pinMode(clk_pin, OUTPUT); 24 | digitalWrite(clk_pin, HIGH); 25 | 26 | pinMode(dat_pin, OUTPUT); 27 | digitalWrite(dat_pin, LOW); 28 | } 29 | 30 | // mcs12085_tick: perform a single clock tick of 25us low 31 | void mcs12085_tick() 32 | { 33 | digitalWrite(clk_pin, LOW); 34 | delayMicroseconds(cycle); 35 | digitalWrite(clk_pin, HIGH); 36 | } 37 | 38 | // mcs12085_tock: finish the clock pulse by waiting during the high period 39 | void mcs12085_tock() 40 | { 41 | delayMicroseconds(cycle); 42 | } 43 | 44 | // mcs12085_read_bit: read a single bit from the chip by creating a clock 45 | // pulse and reading the value returned 46 | int mcs12085_read_bit() 47 | { 48 | mcs12085_tick(); 49 | 50 | int r = (digitalRead(dat_pin) == HIGH); 51 | 52 | mcs12085_tock(); 53 | return r; 54 | } 55 | 56 | // mcs12085_read_byte: Reads 8 bits from the sensor MSB first 57 | byte mcs12085_read_byte() 58 | { 59 | int bits = 8; 60 | byte value = 0x80; 61 | byte b = 0; 62 | 63 | while (bits > 0) { 64 | if ( mcs12085_read_bit() ) { 65 | b |= value; 66 | } 67 | 68 | value >>= 1; 69 | --bits; 70 | } 71 | 72 | pinMode(dat_pin, OUTPUT); 73 | digitalWrite(dat_pin, LOW); 74 | 75 | return b; 76 | } 77 | 78 | // mcs12085_write_bit: write a single bit to the chip by creating a clock 79 | // pulse and writing the value 80 | void mcs12085_write_bit(byte b) // 1 or 0 81 | { 82 | // Set the data pin value ready for the write and then clock 83 | 84 | if ( b ) { 85 | digitalWrite(dat_pin, HIGH); 86 | } else { 87 | digitalWrite(dat_pin, LOW); 88 | } 89 | 90 | mcs12085_tick(); 91 | mcs12085_tock(); 92 | digitalWrite(dat_pin, LOW); 93 | } 94 | 95 | // mcs12086_write_byte: write a byte to the sensor MSB first 96 | void mcs12085_write_byte(byte w) // Number to get the bits from 97 | { 98 | int bits = 8; 99 | 100 | while ( bits > 0 ) { 101 | mcs12085_write_bit(w & 0x80); 102 | w <<= 1; 103 | --bits; 104 | } 105 | 106 | pinMode(dat_pin, INPUT); 107 | } 108 | 109 | // mcs12085_wr_pause: pause between a write and a read to the sensor 110 | void mcs12085_wr_pause() 111 | { 112 | delayMicroseconds(100); 113 | } 114 | 115 | // mcs12085_rw_pause: pause between a read and a write to the sensor 116 | void mcs12085_rw_pause() 117 | { 118 | delayMicroseconds(250); 119 | } 120 | 121 | // mcs12085_convert: converts a byte into a signed 8-bit int 122 | int mcs12085_convert(byte b) 123 | { 124 | if ( b < 128 ) { 125 | return int(b); 126 | } else { 127 | return -(int(b ^ 0xFF) + 1); 128 | } 129 | } 130 | 131 | // mcs12085_dx: read the change in X position since last read 132 | int mcs12085_dx() 133 | { 134 | mcs12085_write_byte(0x02); // 0x02 = Read DX Register 135 | mcs12085_wr_pause(); 136 | int i = mcs12085_convert(mcs12085_read_byte()); 137 | mcs12085_rw_pause(); 138 | return i; 139 | } 140 | 141 | // mcs12085_dy: read the change in Y position since last read 142 | int mcs12085_dy() 143 | { 144 | mcs12085_write_byte(0x03); // 0x03 = Read DY Register 145 | mcs12085_wr_pause(); 146 | int i = mcs12085_convert(mcs12085_read_byte()); 147 | mcs12085_rw_pause(); 148 | return i; 149 | } 150 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /mcs-12085.h: -------------------------------------------------------------------------------- 1 | // Turtley - Logo Turtle controller 2 | // 3 | // Copyright (c) 2012 John Graham-Cumming 4 | // 5 | // Code to interface to MCS-12085 Optical Mouse Chip 6 | 7 | #ifndef INCLUDED_MCS12085 8 | #define INCLUDED_MCS12085 1 9 | 10 | #include 11 | 12 | void mcs12085_init(); 13 | int mcs12085_dx(); 14 | int mcs12085_dy(); 15 | 16 | #endif // INCLUDED_MCS12085 17 | 18 | 19 | 20 | 21 | 22 | --------------------------------------------------------------------------------