thanks to Zoltan Hudak publish the way to use stm32f103c8t6 on mbed. now you can use it with Wii IRcam
Dependencies: mbed-STM32F103C8T6 mbed
Fork of Wii_IRCam_Test by
main.cpp@1:150525e9c21f, 2010-12-31 (annotated)
- Committer:
- shimniok
- Date:
- Fri Dec 31 09:33:30 2010 +0000
- Revision:
- 1:150525e9c21f
- Parent:
- 0:cf1bc7c313b4
- Child:
- 2:43bff54a5f67
Updated text output and connect speed to work with Java GUI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:cf1bc7c313b4 | 1 | #include "mbed.h" |
shimniok | 0:cf1bc7c313b4 | 2 | |
shimniok | 0:cf1bc7c313b4 | 3 | // Adapted from kako's source code: http://www.kako.com/neta/2008-009/2008-009.html |
shimniok | 0:cf1bc7c313b4 | 4 | // i2c protocol details from - http://blog.makezine.com/archive/2008/11/hacking_the_wiimote_ir_ca.html |
shimniok | 0:cf1bc7c313b4 | 5 | // wiring from - http://translate.google.com/translate?u=http://www.kako.com/neta/2007-001/2007-001.html&hl=en&ie=UTF-8&sl=ja&tl=en |
shimniok | 0:cf1bc7c313b4 | 6 | // obviously mbed is 3.3v so no level translation is needed |
shimniok | 0:cf1bc7c313b4 | 7 | // using built in i2c on pins 9/10 |
shimniok | 0:cf1bc7c313b4 | 8 | // |
shimniok | 1:150525e9c21f | 9 | // PC GUI client here: http://code.google.com/p/wii-cam-blobtrack/ |
shimniok | 1:150525e9c21f | 10 | // |
shimniok | 1:150525e9c21f | 11 | // Interfacing details here: http://www.bot-thoughts.com/2010/12/connecting-mbed-to-wiimote-ir-camera.html |
shimniok | 1:150525e9c21f | 12 | // |
shimniok | 0:cf1bc7c313b4 | 13 | |
shimniok | 0:cf1bc7c313b4 | 14 | DigitalOut myled(LED1); |
shimniok | 0:cf1bc7c313b4 | 15 | PwmOut servo(p21); |
shimniok | 0:cf1bc7c313b4 | 16 | Serial pc(USBTX, USBRX); // tx, rx |
shimniok | 0:cf1bc7c313b4 | 17 | I2C i2c(p9, p10); // sda, scl |
shimniok | 0:cf1bc7c313b4 | 18 | const int addr = 0xB0; // define the I2C Address of camera |
shimniok | 0:cf1bc7c313b4 | 19 | |
shimniok | 0:cf1bc7c313b4 | 20 | void i2c_write2(int addr, char a, char b) |
shimniok | 0:cf1bc7c313b4 | 21 | { |
shimniok | 0:cf1bc7c313b4 | 22 | char cmd[2]; |
shimniok | 0:cf1bc7c313b4 | 23 | |
shimniok | 0:cf1bc7c313b4 | 24 | cmd[0] = a; |
shimniok | 0:cf1bc7c313b4 | 25 | cmd[1] = b; |
shimniok | 0:cf1bc7c313b4 | 26 | i2c.write(addr, cmd, 2); |
shimniok | 0:cf1bc7c313b4 | 27 | wait(0.07); // delay 70ms |
shimniok | 0:cf1bc7c313b4 | 28 | } |
shimniok | 0:cf1bc7c313b4 | 29 | |
shimniok | 0:cf1bc7c313b4 | 30 | void clock_init() |
shimniok | 0:cf1bc7c313b4 | 31 | { |
shimniok | 0:cf1bc7c313b4 | 32 | // set up ~20-25MHz clock on p21 |
shimniok | 0:cf1bc7c313b4 | 33 | LPC_PWM1->TCR = (1 << 1); // Reset counter, disable PWM |
shimniok | 0:cf1bc7c313b4 | 34 | LPC_SC->PCLKSEL0 &= ~(0x3 << 12); |
shimniok | 0:cf1bc7c313b4 | 35 | LPC_SC->PCLKSEL0 |= (1 << 12); // Set peripheral clock divider to /1, i.e. system clock |
shimniok | 0:cf1bc7c313b4 | 36 | LPC_PWM1->MR0 = 4; // Match Register 0 is shared period counter for all PWM1 |
shimniok | 0:cf1bc7c313b4 | 37 | LPC_PWM1->MR6 = 2; // Pin 21 is PWM output 6, so Match Register 6 |
shimniok | 0:cf1bc7c313b4 | 38 | LPC_PWM1->LER |= 1; // Start updating at next period start |
shimniok | 0:cf1bc7c313b4 | 39 | LPC_PWM1->TCR = (1 << 0) || (1 << 3); // Enable counter and PWM |
shimniok | 0:cf1bc7c313b4 | 40 | } |
shimniok | 0:cf1bc7c313b4 | 41 | |
shimniok | 0:cf1bc7c313b4 | 42 | void cam_init() |
shimniok | 0:cf1bc7c313b4 | 43 | { |
shimniok | 0:cf1bc7c313b4 | 44 | // Init IR Camera sensor |
shimniok | 0:cf1bc7c313b4 | 45 | i2c_write2(addr, 0x30, 0x01); |
shimniok | 0:cf1bc7c313b4 | 46 | i2c_write2(addr, 0x30, 0x08); |
shimniok | 0:cf1bc7c313b4 | 47 | i2c_write2(addr, 0x06, 0x90); |
shimniok | 0:cf1bc7c313b4 | 48 | i2c_write2(addr, 0x08, 0xC0); |
shimniok | 0:cf1bc7c313b4 | 49 | i2c_write2(addr, 0x1A, 0x40); |
shimniok | 0:cf1bc7c313b4 | 50 | i2c_write2(addr, 0x33, 0x33); |
shimniok | 0:cf1bc7c313b4 | 51 | wait(0.1); |
shimniok | 0:cf1bc7c313b4 | 52 | } |
shimniok | 0:cf1bc7c313b4 | 53 | |
shimniok | 0:cf1bc7c313b4 | 54 | int main() { |
shimniok | 0:cf1bc7c313b4 | 55 | char cmd[8]; |
shimniok | 0:cf1bc7c313b4 | 56 | char buf[16]; |
shimniok | 0:cf1bc7c313b4 | 57 | int Ix1,Iy1,Ix2,Iy2; |
shimniok | 0:cf1bc7c313b4 | 58 | int Ix3,Iy3,Ix4,Iy4; |
shimniok | 0:cf1bc7c313b4 | 59 | int s; |
shimniok | 0:cf1bc7c313b4 | 60 | |
shimniok | 0:cf1bc7c313b4 | 61 | clock_init(); |
shimniok | 0:cf1bc7c313b4 | 62 | |
shimniok | 0:cf1bc7c313b4 | 63 | // PC serial output |
shimniok | 0:cf1bc7c313b4 | 64 | pc.baud(115200); |
shimniok | 1:150525e9c21f | 65 | //pc.printf("Initializing camera..."); |
shimniok | 0:cf1bc7c313b4 | 66 | |
shimniok | 0:cf1bc7c313b4 | 67 | cam_init(); |
shimniok | 0:cf1bc7c313b4 | 68 | |
shimniok | 1:150525e9c21f | 69 | //pc.printf("complete\n"); |
shimniok | 0:cf1bc7c313b4 | 70 | |
shimniok | 0:cf1bc7c313b4 | 71 | // read I2C stuff |
shimniok | 0:cf1bc7c313b4 | 72 | while(1) { |
shimniok | 0:cf1bc7c313b4 | 73 | myled = 1; |
shimniok | 0:cf1bc7c313b4 | 74 | // IR Sensor read |
shimniok | 0:cf1bc7c313b4 | 75 | cmd[0] = 0x36; |
shimniok | 0:cf1bc7c313b4 | 76 | i2c.write(addr, cmd, 1); |
shimniok | 0:cf1bc7c313b4 | 77 | i2c.read(addr, buf, 16); // read the 16-byte result |
shimniok | 0:cf1bc7c313b4 | 78 | |
shimniok | 0:cf1bc7c313b4 | 79 | myled = 0; |
shimniok | 0:cf1bc7c313b4 | 80 | |
shimniok | 0:cf1bc7c313b4 | 81 | Ix1 = buf[1]; |
shimniok | 0:cf1bc7c313b4 | 82 | Iy1 = buf[2]; |
shimniok | 0:cf1bc7c313b4 | 83 | s = buf[3]; |
shimniok | 0:cf1bc7c313b4 | 84 | Ix1 += (s & 0x30) <<4; |
shimniok | 0:cf1bc7c313b4 | 85 | Iy1 += (s & 0xC0) <<2; |
shimniok | 0:cf1bc7c313b4 | 86 | |
shimniok | 0:cf1bc7c313b4 | 87 | Ix2 = buf[4]; |
shimniok | 0:cf1bc7c313b4 | 88 | Iy2 = buf[5]; |
shimniok | 0:cf1bc7c313b4 | 89 | s = buf[6]; |
shimniok | 0:cf1bc7c313b4 | 90 | Ix2 += (s & 0x30) <<4; |
shimniok | 0:cf1bc7c313b4 | 91 | Iy2 += (s & 0xC0) <<2; |
shimniok | 0:cf1bc7c313b4 | 92 | |
shimniok | 0:cf1bc7c313b4 | 93 | Ix3 = buf[7]; |
shimniok | 0:cf1bc7c313b4 | 94 | Iy3 = buf[8]; |
shimniok | 0:cf1bc7c313b4 | 95 | s = buf[9]; |
shimniok | 0:cf1bc7c313b4 | 96 | Ix3 += (s & 0x30) <<4; |
shimniok | 0:cf1bc7c313b4 | 97 | Iy3 += (s & 0xC0) <<2; |
shimniok | 0:cf1bc7c313b4 | 98 | |
shimniok | 0:cf1bc7c313b4 | 99 | Ix4 = buf[10]; |
shimniok | 0:cf1bc7c313b4 | 100 | Iy4 = buf[11]; |
shimniok | 0:cf1bc7c313b4 | 101 | s = buf[12]; |
shimniok | 0:cf1bc7c313b4 | 102 | Ix4 += (s & 0x30) <<4; |
shimniok | 0:cf1bc7c313b4 | 103 | Iy4 += (s & 0xC0) <<2; |
shimniok | 0:cf1bc7c313b4 | 104 | |
shimniok | 0:cf1bc7c313b4 | 105 | // print the coordinate data |
shimniok | 1:150525e9c21f | 106 | //pc.printf("Ix1: %4d, Iy1: %4d\n", Ix1, Iy1 ); |
shimniok | 1:150525e9c21f | 107 | //pc.printf("Ix2: %4d, Iy2: %4d\n", Ix2, Iy2 ); |
shimniok | 1:150525e9c21f | 108 | pc.printf("%d,%d,%d,%d,%d,%d,%d,%d\r\n", Ix1, Iy1, Ix2, Iy2, Ix3, Iy3, Ix4, Iy4); |
shimniok | 0:cf1bc7c313b4 | 109 | |
shimniok | 1:150525e9c21f | 110 | wait(0.050); |
shimniok | 0:cf1bc7c313b4 | 111 | } |
shimniok | 0:cf1bc7c313b4 | 112 | |
shimniok | 0:cf1bc7c313b4 | 113 | } |