开发平台:MDK5和rasc
开发板RA4M2
芯片:R7FA4M2AD3CFP
RA4开发板上刚好有一个IIC接口其实可以直接接上OLED屏,可惜我的屏电源和地脚刚好相反,不能直接接在上面。
下面我来IIC来驱动OLED屏显示。
功能如下:
- IIC驱动在RASC上配置;
- OLED驱动程序编写;
- 驱动OLED字符。
硬件连接:
框图:
配置:
配置属性
核心代码,代码抄的。
显示字条:
OLED_ShowString(12, 0, (const uint8_t*)"R7FA4M2AD3CFP", 16, 1);
OLED_ShowString(24, 25, (const uint8_t*)"--IIC OLED--", 16, 1);
OLED_ShowString(54, 45, (const uint8_t*)"--YAO--", 16, 1);
IIC回调内家:
i2c_event = I2C_MASTER_EVENT_ABORTED;
if (NULL != p_args)
{
i2c_event = p_args->event;
}
IIC初始:
err = R_SCI_I2C_Open(&g_sci3_i2c_ctrl, &g_sci3_i2c_cfg);
assert(FSP_SUCCESS == err);
OLED初始:
OLED_WrCmd(0xAE); //关闭显示
OLED_WrCmd(0xD5); //设置时钟分频因子,震荡频率
OLED_WrCmd(80); //[3:0],分频因子;[7:4],震荡频率
OLED_WrCmd(0xA8); //设置驱动路数
OLED_WrCmd(0X3F); //默认0X3F(1/64)
OLED_WrCmd(0xD3); //设置显示偏移
OLED_WrCmd(0X00); //默认为0
OLED_WrCmd(0x40);
OLED_WrCmd(0x8D);
OLED_WrCmd(0x14);
OLED_WrCmd(0x20);
OLED_WrCmd(0x02);
OLED_WrCmd(0xA1);
OLED_WrCmd(0xC0);
OLED_WrCmd(0xDA);
OLED_WrCmd(0x12);
OLED_WrCmd(0x81);
OLED_WrCmd(0xEF);
OLED_WrCmd(0xD9);
OLED_WrCmd(0xf1);
OLED_WrCmd(0xDB);
OLED_WrCmd(0x30);
OLED_WrCmd(0xA4);
OLED_WrCmd(0xA6);
OLED_WrCmd(0xAF);
OLED_Clear();
写:
fsp_err_t err;
uint32_t timeout_ms = 100;
uint8_t i2c_transmitter[2]={0x40, IIC_Data};
err = R_SCI_I2C_Write(&g_sci3_i2c_ctrl, i2c_transmitter, 0x02, true);
assert(FSP_SUCCESS == err);
while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms>0)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MICROSECONDS);
timeout_ms--;
}
if (I2C_MASTER_EVENT_ABORTED == i2c_event)
{
__BKPT(0);
}
i2c_event = I2C_MASTER_EVENT_ABORTED;
显示效果: