上一篇测评【【AT-START-F405测评】-1- I2C扫描设备】使用硬件I2C检测到了总线上的OLED设备,本次使用硬件I2C来驱动这个常见的显示设备。
OLED移植的关键在于下面函数的移植:
- void OLED_WR_Byte(uint8_t dat,uint8_t mode)
- {
- i2c_status_type i2c_status;
- uint8_t buff[2] = {0};
- if(mode){
- buff[0] = 0x40;
- buff[1] = dat;
- i2c_status = i2c_master_transmit(&hi2cx, 0x78, buff, 2, 0xFFFFFFF);
- if(i2c_status != I2C_OK){
- printf("\r\nerror send %d", i2c_status);
- }
- }else{
- buff[0] = 0x00;
- buff[1] = dat;
- i2c_status = i2c_master_transmit(&hi2cx, 0x78, buff, 2, 0xFFFFFFF);
- if(i2c_status != I2C_OK){
- printf("\r\nerror send %d", i2c_status);
- }
- }
- }
其中i2c_master_transmit()是雅特力重新封装的一个便于使用的I2C发送函数,更简洁有好。
主函数中先初始化I2C与OLED,然后扫描I2C设备,最后把扫描结果显示在OLED屏幕上。
- int main(void)
- {
-
- /* initial system clock */
- system_clock_config();
- /* config nvic priority group */
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- /* at board initial */
- at32_board_init();
- uart_print_init(115200);
- hi2cx.i2cx = I2Cx_PORT;
- /* i2c config */
- i2c_config(&hi2cx);
- printf("i2c scan example: aim to find the i2c device on the bus\r\n");
-
- main_i2c_scan(I2C_DEVICE_General_ADDRESS_MIN, I2C_DEVICE_General_ADDRESS_MAX);
- OLED_Init();
- OLED_Clear();
- while(1)
- {
- static uint32_t i = 0;
- OLED_ShowString(0,0," AT-START-F405",12, 1);
- OLED_ShowString(0,12,"Scaning I2C Device...",12, 1);
- OLED_ShowString(0,24,"I2C Slave 8 bit Addr: ",12, 1);
- OLED_ShowString(0,36,i2cScanResult,12, 1);
- OLED_Refresh();
- printf("\r\n inside while loop %d\r\n", i++);
- delay_ms(1000);
- }
-
-
- }
实物结果:
|