using wii camera for mbed lpc1768

Dependencies:   mbed

Committer:
exarkun
Date:
Thu Oct 03 13:14:15 2019 +0000
Revision:
0:3cf02af31b20
using wii camera for mbed lpc1768

Who changed what in which revision?

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