Esempio n. 1
0
uint8 find_i2c_device(uint8 address)
{
	uint8 stat;
	uint8 dat;
	for(;address<100;address++)
	{
		stat=I2C_1_MasterClearStatus();
		dat=0; //read reg at byte 1.
		I2C_1_MasterWriteBuf(address,&dat,1,I2C_1_MODE_COMPLETE_XFER); //send write reg request at address
		
not_done:
		stat = I2C_1_MasterStatus();
		if(stat & I2C_1_MSTAT_XFER_INP)
			goto not_done;
		if(stat & I2C_1_MSTAT_ERR_XFER)
			continue;
		if(stat & I2C_1_MSTAT_ERR_ADDR_NAK)
			continue;
			
		stat=I2C_1_MasterReadBuf(address,&dat,1,I2C_1_MODE_COMPLETE_XFER); //send write reg request at address
		return address;
	}
	return 0; //not found
	
}
void Color_Sensor(Let *let, Color *color)
{
    uint8 r=0,g=0,b=0,x=0;
    unsigned char txReadStatus = 0x03;
    unsigned char rxBuf[8] = {1,0,0,0,0,0,0,0};
    char value[40];
    Sensor_LED_Write(1);
    I2C_1_MasterWriteBuf(0x2A,(uint8*)&txReadStatus,1,I2C_1_MODE_COMPLETE_XFER);
    while(0u==(I2C_1_MasterStatus() & I2C_1_MSTAT_WR_CMPLT));
    I2C_1_MasterClearStatus();
    I2C_1_MasterReadBuf(0x2A,(uint8 *)&rxBuf,8,I2C_1_MODE_COMPLETE_XFER);
    while(0u==(I2C_1_MasterStatus() & I2C_1_MSTAT_RD_CMPLT));
    I2C_1_MasterClearStatus();
    r = rxBuf[0]<< 8|rxBuf[1];
    g = rxBuf[2]<< 8|rxBuf[3];
    b = rxBuf[4]<< 8|rxBuf[5];
    if((r < 10) || (g < 10) || (b < 10))
    {
        return;
    }
    if(g > color->g_max)
    {
        color->g_max = g;
    }
    if(g < color->g_min)
    {
        color->g_min = g;
    }
    I2C_LCD_1_Clear();
    //色判断して構造体に格納
    if((r > b) && (g > b))
    {
        if((r > 90) && (g > 100))
        {
            let->color = YELLO;
            I2C_LCD_Position(1u,7u);
            I2C_LCD_1_PrintString("YELLO");
        }
        else //if(r > g)
        {
            let->color = RED;
            I2C_LCD_Position(1u,7u);
            I2C_LCD_1_PrintString("RED");
        }
    }
    else if((r < b) && (r < g))
    {
        let->color = BLUE;
        I2C_LCD_Position(1u,7u);
        I2C_LCD_1_PrintString("BLUE");
    }
    if((color->g_max - color->g_min) > 30)
    {
        let->color = MISS;
        I2C_LCD_Position(1u,7u);
        I2C_LCD_1_PrintString("MISS");
    }

    sprintf(value, "r=%3d g=%3d",r,g);
    I2C_LCD_Position(0u,0u);
    I2C_LCD_1_PrintString(value);
    sprintf(value, "b=%3d",b);
    I2C_LCD_Position(1u,0u);
    I2C_LCD_1_PrintString(value);
    /*
    sprintf(value, "r=%3d g=%3d b=%3d\n",r,g,b);
    UART_Line_Sensor_PutString(value);
    */
    return;
}