void I2C_Color_init(void) { unsigned char txCBuf[2] = { 0x0, 0x89 }; //I2C_1_Start(); I2C_1_MasterWriteBuf(0x2A,(uint8 *)&txCBuf,2,I2C_1_MODE_COMPLETE_XFER); while(0u==(I2C_1_MasterStatus() & I2C_1_MSTAT_WR_CMPLT)); I2C_1_MasterClearStatus(); txCBuf[1] = 0x09; I2C_1_MasterWriteBuf(0x2A,(uint8 *)&txCBuf,2,I2C_1_MODE_COMPLETE_XFER); while(0u==(I2C_1_MasterStatus() & I2C_1_MSTAT_WR_CMPLT)); I2C_1_MasterClearStatus(); }
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 }
//Configure the strain gauge amplifier //6-ch, 2 per digital pot. void strain_config(uint8 ch, uint8 offs) { uint8 i2c_init_buf[2]; uint8 addr = 0; //Assign addresses switch(ch) { case 0: i2c_init_buf[0] = STRAIN_OFFSET_A; addr = I2C_POT_ADDR_CH1; break; case 1: i2c_init_buf[0] = STRAIN_OFFSET_B; addr = I2C_POT_ADDR_CH1; break; case 2: i2c_init_buf[0] = STRAIN_OFFSET_A; addr = I2C_POT_ADDR_CH2; break; case 3: i2c_init_buf[0] = STRAIN_OFFSET_B; addr = I2C_POT_ADDR_CH2; break; case 4: i2c_init_buf[0] = STRAIN_OFFSET_A; addr = I2C_POT_ADDR_CH3; break; case 5: i2c_init_buf[0] = STRAIN_OFFSET_B; addr = I2C_POT_ADDR_CH3; break; default: return; break; } //Offset: i2c_init_buf[1] = offs; //Offset I2C_1_MasterWriteBuf(addr, (uint8 *) i2c_init_buf, 2, I2C_1_MODE_COMPLETE_XFER); CyDelayUs(100); }
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; }