thanks to Zoltan Hudak publish the way to use stm32f103c8t6 on mbed. now you can use it with MPC4725 DAC
Dependencies: mbed-STM32F103C8T6 mbed
Fork of Wii_IRCam_Test by
Diff: main.cpp
- Revision:
- 2:2b68d1d14aca
- Parent:
- 1:150525e9c21f
- Child:
- 3:37fb1e2aacf3
--- a/main.cpp Fri Dec 31 09:33:30 2010 +0000 +++ b/main.cpp Tue May 23 16:52:22 2017 +0000 @@ -1,4 +1,6 @@ +#include "stm32f103c8t6.h" #include "mbed.h" +#include "mcp4725.h" // Adapted from kako's source code: http://www.kako.com/neta/2008-009/2008-009.html // i2c protocol details from - http://blog.makezine.com/archive/2008/11/hacking_the_wiimote_ir_ca.html @@ -11,103 +13,53 @@ // Interfacing details here: http://www.bot-thoughts.com/2010/12/connecting-mbed-to-wiimote-ir-camera.html // -DigitalOut myled(LED1); -PwmOut servo(p21); -Serial pc(USBTX, USBRX); // tx, rx -I2C i2c(p9, p10); // sda, scl -const int addr = 0xB0; // define the I2C Address of camera +MCP4725 mcp4725_interface(PB_9, PB_8, MCP4725::Standard100kHz, 0);//sda,scl,bus_frequency,device_address_bit +// modes : Standard100kHz Fast400kHz HighSpeed3_4Mhz +DigitalOut F_R(PC_14); +//PwmOut servo(PA_0); -void i2c_write2(int addr, char a, char b) -{ - char cmd[2]; - - cmd[0] = a; - cmd[1] = b; - i2c.write(addr, cmd, 2); - wait(0.07); // delay 70ms -} +//I2C i2c(PB_7, PB_6); // sda, scl +//I2C i2c(PB_9, PB_8); // sda, scl +//const int addr = 0xB0; // define the I2C Address of camera +//int c = 0; -void clock_init() + + + +int main() { - // set up ~20-25MHz clock on p21 - LPC_PWM1->TCR = (1 << 1); // Reset counter, disable PWM - LPC_SC->PCLKSEL0 &= ~(0x3 << 12); - LPC_SC->PCLKSEL0 |= (1 << 12); // Set peripheral clock divider to /1, i.e. system clock - LPC_PWM1->MR0 = 4; // Match Register 0 is shared period counter for all PWM1 - LPC_PWM1->MR6 = 2; // Pin 21 is PWM output 6, so Match Register 6 - LPC_PWM1->LER |= 1; // Start updating at next period start - LPC_PWM1->TCR = (1 << 0) || (1 << 3); // Enable counter and PWM -} - -void cam_init() -{ - // Init IR Camera sensor - i2c_write2(addr, 0x30, 0x01); - i2c_write2(addr, 0x30, 0x08); - i2c_write2(addr, 0x06, 0x90); - i2c_write2(addr, 0x08, 0xC0); - i2c_write2(addr, 0x1A, 0x40); - i2c_write2(addr, 0x33, 0x33); - wait(0.1); -} - -int main() { - char cmd[8]; - char buf[16]; - int Ix1,Iy1,Ix2,Iy2; - int Ix3,Iy3,Ix4,Iy4; - int s; - - clock_init(); - - // PC serial output + Serial pc(PA_2, PA_3); + confSysClock(); + // PC serial output pc.baud(115200); //pc.printf("Initializing camera..."); - - cam_init(); - - //pc.printf("complete\n"); + int dac_value = (int) (0xFFF * (0/3.3) ); + mcp4725_interface.write(MCP4725::Normal, dac_value, false); + //modes: Normal PowerDown1k PowerDown100k PowerDown500k // read I2C stuff while(1) { - myled = 1; - // IR Sensor read - cmd[0] = 0x36; - i2c.write(addr, cmd, 1); - i2c.read(addr, buf, 16); // read the 16-byte result - - myled = 0; - - Ix1 = buf[1]; - Iy1 = buf[2]; - s = buf[3]; - Ix1 += (s & 0x30) <<4; - Iy1 += (s & 0xC0) <<2; - - Ix2 = buf[4]; - Iy2 = buf[5]; - s = buf[6]; - Ix2 += (s & 0x30) <<4; - Iy2 += (s & 0xC0) <<2; - - Ix3 = buf[7]; - Iy3 = buf[8]; - s = buf[9]; - Ix3 += (s & 0x30) <<4; - Iy3 += (s & 0xC0) <<2; - - Ix4 = buf[10]; - Iy4 = buf[11]; - s = buf[12]; - Ix4 += (s & 0x30) <<4; - Iy4 += (s & 0xC0) <<2; - - // print the coordinate data - //pc.printf("Ix1: %4d, Iy1: %4d\n", Ix1, Iy1 ); - //pc.printf("Ix2: %4d, Iy2: %4d\n", Ix2, Iy2 ); - pc.printf("%d,%d,%d,%d,%d,%d,%d,%d\r\n", Ix1, Iy1, Ix2, Iy2, Ix3, Iy3, Ix4, Iy4); - - wait(0.050); + if(pc.readable()) { + char c = pc.getc();//-128; + //c = c*3; + //pc.putc(c); + + if (c>=0x80) { + F_R = 1; + c = c - 127; + //pc.putc(c); + mcp4725_interface.write(MCP4725::Normal,uint16_t(c*20),false); + } else { + F_R = 0; + c = 127 - c; + //pc.putc(c); + mcp4725_interface.write(MCP4725::Normal,uint16_t(c*20),false); + } + } + /*for(int i = 0; i<0xFFF; i++){ + mcp4725_interface.write(MCP4725::Normal, i, false); + wait(0.01); + }*/ } }