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 }
/******************************************************************************* * Function Name: I2C_1_Init ******************************************************************************** * * Summary: * Initializes I2C registers with initial values provided from customizer. * * Parameters: * None * * Return: * None * * Global variables: * None * * Reentrant: * No * *******************************************************************************/ void I2C_1_Init(void) { #if(I2C_1_FF_IMPLEMENTED) I2C_1_CFG_REG = I2C_1_DEFAULT_CFG; I2C_1_XCFG_REG = I2C_1_DEFAULT_XCFG; #if(CY_PSOC5A) I2C_1_CLKDIV_REG = LO8(I2C_1_DEFAULT_DIVIDE_FACTOR); #else I2C_1_CLKDIV1_REG = LO8(I2C_1_DEFAULT_DIVIDE_FACTOR); I2C_1_CLKDIV2_REG = HI8(I2C_1_DEFAULT_DIVIDE_FACTOR); #endif /* (CY_PSOC5A) */ #else uint8 enableInterrupts; I2C_1_CFG_REG = I2C_1_DEFAULT_CFG; /* control */ I2C_1_INT_MASK_REG = I2C_1_DEFAULT_INT_MASK; /* int_mask */ /* Enable interrupts from block */ enableInterrupts = CyEnterCriticalSection(); I2C_1_INT_ENABLE_REG |= I2C_1_INTR_ENABLE; /* aux_ctl */ CyExitCriticalSection(enableInterrupts); #if(I2C_1_MODE_MASTER_ENABLED) I2C_1_MCLK_PRD_REG = I2C_1_DEFAULT_MCLK_PRD; I2C_1_MCLK_CMP_REG = I2C_1_DEFAULT_MCLK_CMP; #endif /* (I2C_1_MODE_MASTER_ENABLED) */ #if(I2C_1_MODE_SLAVE_ENABLED) I2C_1_PERIOD_REG = I2C_1_DEFAULT_PERIOD; #endif /* (I2C_1_MODE_SLAVE_ENABLED) */ #endif /* (I2C_1_FF_IMPLEMENTED) */ #if(I2C_1_TIMEOUT_ENABLED) I2C_1_TimeoutInit(); #endif /* (I2C_1_TIMEOUT_ENABLED) */ /* Disable Interrupt and set vector and priority */ CyIntDisable (I2C_1_ISR_NUMBER); CyIntSetPriority(I2C_1_ISR_NUMBER, I2C_1_ISR_PRIORITY); #if(I2C_1_INTERN_I2C_INTR_HANDLER) (void) CyIntSetVector(I2C_1_ISR_NUMBER, &I2C_1_ISR); #endif /* (I2C_1_INTERN_I2C_INTR_HANDLER) */ /* Put state machine in idle state */ I2C_1_state = I2C_1_SM_IDLE; #if(I2C_1_MODE_SLAVE_ENABLED) /* Reset status and buffers index */ I2C_1_SlaveClearReadBuf(); I2C_1_SlaveClearWriteBuf(); I2C_1_slStatus = 0u; /* Reset slave status */ /* Set default address */ I2C_1_SlaveSetAddress(I2C_1_DEFAULT_ADDR); #endif /* (I2C_1_MODE_SLAVE_ENABLED) */ #if(I2C_1_MODE_MASTER_ENABLED) /* Reset status and buffers index */ I2C_1_MasterClearReadBuf(); I2C_1_MasterClearWriteBuf(); (void) I2C_1_MasterClearStatus(); #endif /* (I2C_1_MODE_MASTER_ENABLED) */ }
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; }
void main() { uint8 ButtonPressFlag = 0; int32 temp=0; uint32 lowest=24,i; uint32 increment=2; uint32 battery_volts; volatile uint32 counter=0; CYGlobalIntDisable; /* Intitalize hardware */ LEDControlReg_Write(0xff); /* Turn off the LEDs on PORT2(pin 0-3) and PORT4 pin(0-3) */ PSU_Enable_Write(3); // AMux_1_Start(); /* Enable THe analog mux input to the ADC */ // AcclADC_Start(); /* Start ADC */ // VDAC8_1_Start(); /* Start and configure the VDAC used to measure the Thermistor */ // VDAC8_1_SetRange(VDAC8_1_RANGE_1V); // VDAC8_1_SetValue(200 ); PWM_0_Start(); PWM_1_Start(); PWM_2_Start(); PWM_3_Start(); PWM_4_Start(); PWM_5_Start(); PWM_6_Start(); PWM_7_Start(); // VBATT_ADC_Start(); //VBATT_ADC_StartConvert(); // VBATT_ADC_Stop(); //debugging // UART_1_Start(); I2C_1_Start(); I2C_1_EnableInt(); Button_ClearInterrupt(); ALERT2_ClearPending(); ALERT1_ClearPending(); ALERT1_StartEx(ALERT1_ISR); Button_Pressed_StartEx(Button_Press_ISR); ALERT2_StartEx(ALERT2_ISR); CYGlobalIntEnable; /* Enable global interrupt */ I2C_1_MasterClearStatus(); while(set_ina226(CH1)!=CYRET_SUCCESS); while(set_tmp100(CH1)!=CYRET_SUCCESS); while(1) { if(0) //if(Status_Reg_1_Read()&1) { read_tmp100(CH1); read_ina226(CH1); } LEDControlReg_Write(((uint8)~(PSU_Enable_Read()))); for(i=0;i<65000;i++); LEDControlReg_Write(((uint8)~(PSU_Enable_Read())) & ~(1<<7)); for(i=0;i<65000;i++); // CyPmSaveClocks(); // CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_PICU); // CyPmRestoreClocks(); // battery_volts = VBATT_ADC_GetResult32(); if(reset==1) { for(i=0;i<65000;i++); //delay for half a second. PSU_Enable_Write(3); //Both PSU ON I2C_1_Start(); I2C_1_EnableInt(); while(set_ina226(CH1)!=CYRET_SUCCESS); while(set_tmp100(CH1)!=CYRET_SUCCESS); reset=0; } } while(1) { /* Calculate the current board temperature */ temp = Thermistor_TemperatureCompute() / 10; //we get 24.1 as 241. We drop fractionals. uint32 barrels_above= (temp-lowest)/increment; if(temp<=lowest) barrels_above=0; //negative temperatures are too low! if(barrels_above>8) barrels_above=8; LEDControlReg_Write(1<<barrels_above); } }