1、复制一个示例i2c的示例工程,并重命名为LP_MSPM0G3507_oled
2、开启i2c1,并设置速率为400K
3、保存到工程,然后添加oled的驱动库
4、在驱动库中,主要需要修改的为write_byte函数,内容如下:
int I2C_WriteByte(uint8_t addr, uint8_t data) { uint8_t buff[2] = {0}; buff[0] = addr; buff[1] = data; /* Wait for I2C to be Idle */ while (!(DL_I2C_getControllerStatus(I2C_OLED_INST) & DL_I2C_CONTROLLER_STATUS_IDLE)) ; /* Send the packet to the controller. * This function will send Start + Stop automatically. */ DL_I2C_startControllerTransfer(I2C_OLED_INST, OLED_ADDR, DL_I2C_CONTROLLER_DIRECTION_TX, 2); DL_I2C_fillControllerTXFIFO(I2C_OLED_INST, &buff[0], 2); /* Poll until the Controller writes all bytes */ while (DL_I2C_getControllerStatus(I2C_OLED_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) ; /* Trap if there was an error */ if (DL_I2C_getControllerStatus(I2C_OLED_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) { /* LED will remain high if there is an error */ __BKPT(0); } return 0; }
这样我们的代码就移植完毕。
4、添加测试代码:
#include "ti_msp_dl_config.h" #include "\oled\oled.h" int main(void) { SYSCFG_DL_init(); /* Set LED to indicate start of transfer */ OLED_Init(); OLED_CLS(); OLED_ShowString(3, 1,"LP-MSPM0G3507", 2); OLED_ShowString(5, 1,"eepw Ti", 2); /* If write and read were successful, toggle LED */ while (1) { } }
5、测试结果:
到此移植OLED就完毕。
【移植要点】
1、要准备已经做好的OLED的驱动。
2、配置好I2C。
3、改写i2cwritbyte函数。