cancel
Showing results for 
Search instead for 
Did you mean: 

Need simple code on i2c for stm32f051

SKuma.4
Associate
 
2 REPLIES 2

Some pretty simple stuff here that should work on an F051 based board

STM32Cube_FW_F0_V1.9.0\Drivers\BSP\STM32F072B-Discovery\stm32f072b_discovery.c

STM32Cube_FW_F0_V1.9.0\Drivers\BSP\STM32F072B-Discovery\stm32f072b_discovery_eeprom.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead

super simple, do you use the cube ?

I had to write code for a 4port DAC.

Everyone knows IIC can be problematic, however, this worked first time without any initialisation other than enabled in the cube.

//#define MCP4728_DacAddress 0x60 << 1
void write4Dacs(void) {
 
    struct DacIIC MCP4728IIC;   
    HAL_StatusTypeDef IIC_Response;
    
    //Channel 0
    MCP4728IIC.DacTxBuffer[0] =  (Dac0_Value & 0x0f00) >> 8; // Dac0Value adjusted in another process..
    MCP4728IIC.DacTxBuffer[1] =   Dac0_Value & 0x00ff;
 
    //Channel 1
    Dac1_Value  = Dac0_Value + (4096 * 1) / 5;      // 1V   // test DAC2
    MCP4728IIC.DacTxBuffer[2] =  (Dac1_Value & 0x0f00) >> 8;
    MCP4728IIC.DacTxBuffer[3] =   Dac1_Value & 0x00ff;
 
    //Channel 2
    Dac2_Value  = Dac1_Value + (4096 * 1) / 5;      // 2V
    MCP4728IIC.DacTxBuffer[4] =  (Dac2_Value & 0x0f00) >> 8;
    MCP4728IIC.DacTxBuffer[5] =   Dac2_Value & 0x00ff;
 
    //Channel 3
    Dac3_Value  = Dac2_Value + (4096 * 1) / 5;      // 3V
    MCP4728IIC.DacTxBuffer[6] =  (Dac3_Value & 0x0f00) >> 8;
    MCP4728IIC.DacTxBuffer[7] =   Dac3_Value & 0x00ff;
 
 
    //DAC_IIC.write(DAC_ADDR + (Dac_Address << 1), buf, 8);   //MBED write
    IIC_Response = HAL_I2C_Master_Transmit(&hi2c1, MCP4728_DacAddress, &MCP4728IIC.DacTxBuffer[0], 8, 10); 
}