我们通过Datasheet了解到GD32F307 I2C0使用的IO为PB6/7
因此软件我们只需要使能I2C0即可
rcu_periph_clock_enable(RCU_I2C0);
接下来就是OLED的数据传输
OLED DRIVER DATASHEET 描述The D/C# bit determines the next data byte is acted as a command or a data. If the D/C# bit is
set to logic “0”, it defines the following data byte as a command. If the D/C# bit is set to
logic “1”, it defines the following data byte as a data which will be stored at the GDDRAM.
The GDDRAM column address pointer will be increased by one automatically after each
data write.
接下来该写command or data控制函数
void oled_byte_write(uint8_t write_address)
{
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
i2c_start_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C0, oled_address, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, 0x00);//COMMAND
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_data_transmit(I2C0, write_address);
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_stop_on_bus(I2C0);
while(I2C_CTL0(I2C0)&0x0200);
}
void oled_dat_write(uint8_t write_dat)
{
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
i2c_start_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C0, oled_address, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, 0x40);//DATA
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_data_transmit(I2C0, write_dat);
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_stop_on_bus(I2C0);
while(I2C_CTL0(I2C0)&0x0200);
}
|