From: <Enregistré par Windows Internet Explorer 7>
Date: Tue, 1 Feb 2011 18:31:33 +0100

#include "HWconfig.h"
#include "wii_cam.h"
#include <math.h>
#include "libpic30.h"
#include "iomapping.h"
#include "uart.h"
#include "g_i2c.h"
#include "delay.h"
#include "timer.h"

#define _WCAM_DEBUG_

void system_init(void);

#define USE_AND_OR /* To enable AND_OR mask setting */
#include<i2c.h>

//******************** Interrupt sevice rotuine for I2C2 Master ***************************
void __attribute__((interrupt,no_auto_psv)) _MI2C2Interrupt(void)
{
MI2C2_Clear_Intr_Status_Bit; //Clear Interrupt status of I2C2
}

int main(void)
{

CLKDIVbits.RCDIV = 0;
BLOB_T dot;

system_init();

init_wiicam();

DLED1_TRIS = 0;

//Get data from Camera:
while(1){

get_wiicam_data(&dot);
idle_delay(18000, PRSCL_1_8); //10 ms between each reading
DLED1 = !DLED1;
}

}

/********************************************************
Function: init_wiicam

Initializes the i2c module and sends the
initialization equence to the wii camera

Parameters:

None

Returns:

None

Notes:

Uses the Microchip I2C peripheral
libraries are used for I2C functionality

I2C Settings:

* I2C2 enabled
* continue I2C module in Idle mode
* IPMI mode not enabled
* I2CADD is 7-bit address
* Disable Slew Rate Control for 100KHz
* Enable SM bus specification
* Disable General call address
* Baud @ 7.3728MHz = 40 into I2CxBRG
*********************************************************/
void init_wiicam(void){

unsigned int config1 = 0;
unsigned int config2 = 0;
unsigned int write_result;

/* Turn off I2C modules */
CloseI2C2();

ConfigIntI2C2(MI2C_INT_OFF); //Disable I2C interrupt

config1 = (I2C_ON | I2C_7BIT_ADD );
config2 = 40;
OpenI2C2(config1,config2); //configure I2C2

IdleI2C2(); //wait for the I2C to be Idle
StopI2C2(); //Terminate communication protocol with stop signal
RestartI2C2(); //Restart signal
while(I2C2CONbits.RSEN ); //Wait till Restart sequence is completed

//First Sequence
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x30);
write_result = i2c1_mtx_data(0x01);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms

//Second Sequence
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x90);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms

//Third Sequence
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x07);
write_result = i2c1_mtx_data(0x00);
write_result = i2c1_mtx_data(0x41);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms

//Fourth Sequence
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x1A);
write_result = i2c1_mtx_data(0x40);
write_result = i2c1_mtx_data(0x00);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms

//Fifth
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x33);
write_result = i2c1_mtx_data(0x03);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms

//Sixth
write_result = i2c1_mtx_addr(0xB0, 5);
write_result = i2c1_mtx_data(0x030);
write_result = i2c1_mtx_data(0x08);
StopI2C2();
idle_delay(23040, PRSCL_1_64); //Delay 100ms
}

/********************************************************
Function: get_wiicam_data

Grab the wii camera data

Parameters:

None

Returns:

None

Notes:

This function requests data from the wii camera
using the i2c module. Microchips I2C peripheral
libraries are used for I2C functionality
*********************************************************/
int get_wiicam_data(BLOB_T *blob){

int temp;
unsigned char buffer[12];

i2c1_mtx_addr(0xB0, 5);
i2c1_mtx_data(0x037);
idle_delay(370, PRSCL_1_1); //Delay 25 usec

IdleI2C2(); //wait for the I2C to be Idle
StopI2C2(); //Terminate communication protocol with stop signal
StartI2C2();
i2c1_mtx_addr(0xB1, 5);
MastergetsI2C2(8,buffer,1000);
idle_delay(5603, PRSCL_1_1); //Delay 380 usec
i2c1_mtx_addr(0xB1, 5);
MastergetsI2C2(4,&buffer[8],1000);

//Blob 1
blob->X[0] = buffer[0];
blob->Y[0] = buffer[1];
temp = buffer[2] & 0x30;
temp = (temp << 4);
blob->X[0] = blob->X[0] | temp ;
temp = (buffer[2] & 0xc0);
temp = (temp << 2);
blob->Y[0] |= temp;
blob->Size[0] = buffer[2] & 0x0F;

//Blob 2
blob->X[1] = buffer[3];
blob->Y[1] = buffer[4];
temp = buffer[5] & 0x30;
temp = (temp << 4);
blob->X[1] |= temp ;
temp = (buffer[5] & 0xc0);
temp = temp << 2;
blob->Y[1] |= temp;
blob->Size[1] = buffer[5] & 0x0F;

//Blob 3
blob->X[2] = buffer[6];
blob->Y[2] = buffer[7];
temp = buffer[8] & 0x30;
temp = (temp << 4);
blob->X[2] |= temp ;
temp = (buffer[8] & 0xc0);
temp = temp << 2;
blob->Y[2] |= temp;
blob->Size[2] = buffer[8] & 0x0F;

//Blob 4
blob->X[3] = buffer[9];
blob->Y[3] = buffer[10];
temp = buffer[11] & 0x30;
temp = (temp << 4);
blob->X[3] |= temp ;
temp = (buffer[11] & 0xc0);
temp = temp << 2;
blob->Y[3] |= temp;
blob->Size[3] = buffer[11] & 0x0F;

#ifdef _WCAM_DEBUG_
int i;
for(i = 0; i < 4; i++){
if((blob->X[i] != 1023)){
printf("Blob%d - X: %-5i Y: %-5i\n\r",i, blob->X[i], blob->Y[i]);
}
}
#endif

return 0;
}

void system_init(void){

uart_ioMap();
uart_init();
init_timer4();
}