- void i2c_lowlevel_init(i2c_handle_type* hi2c)
- {
- gpio_init_type gpio_init_structure;
- if(hi2c->i2cx == I2Cx_PORT)
- {
- /* i2c periph clock enable */
- crm_periph_clock_enable(I2Cx_CLK, TRUE);
- crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
- crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);
- /* gpio configuration */
- gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);
- gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);
- /* configure i2c pins: scl */
- gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_structure.gpio_mode = GPIO_MODE_MUX;
- gpio_init_structure.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
- gpio_init_structure.gpio_pull = GPIO_PULL_NONE;
- gpio_init_structure.gpio_pins = I2Cx_SCL_GPIO_PIN;
- gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);
- /* configure i2c pins: sda */
- gpio_init_structure.gpio_pins = I2Cx_SDA_GPIO_PIN;
- gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);
- /* config i2c */
- i2c_init(hi2c->i2cx, 0x0F, I2Cx_CLKCTRL);
- i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
- }
- }
OLED 显示部分:
- OLED_Init();
- OLED_Fill(0x00);
- OLED_ShowCN(0,0,34);
- OLED_ShowCN(16,0,34);
- OLED_ShowCN(32,0,37);
- OLED_ShowCN(48,0,37);
- OLED_ShowCN(64,0,32);
- OLED_ShowCN(80,0,33);
- OLED_ShowCN(96,0,35);
- OLED_ShowCN(112,0,36);
- OLED_ShowStr(0,2,"I2C OLED TEST",2);
- OLED_ShowStr(0,4,"AT32L021 IIC ",2);
- OLED_ShowStr(0,6,"2024- 11 - 13",2);
OLED驱动部分:
- void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
- {
- unsigned char c = 0,i = 0,j = 0;
- switch(TextSize)
- {
- case 1:
- {
- while(ch[j] != '\0')
- {
- c = ch[j] - 32;
- if(x > 126)
- {
- x = 0;
- y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<6;i++)
- WriteDat(F6x8[c][i]);
- x += 6;
- j++;
- }
- }break;
- case 2:
- {
- while(ch[j] != '\0')
- {
- c = ch[j] - 32;
- if(x > 120)
- {
- x = 0;
- y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<8;i++)
- WriteDat(F8X16[c*16+i]);
- OLED_SetPos(x,y+1);
- for(i=0;i<8;i++)
- WriteDat(F8X16[c*16+i+8]);
- x += 8;
- j++;
- }
- }break;
- }
- }
- void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
- {
- unsigned char wm=0;
- unsigned int adder=32*N;
- OLED_SetPos(x , y);
- for(wm = 0;wm < 16;wm++)
- {
- WriteDat(F16x16[adder]);
- adder += 1;
- }
- OLED_SetPos(x,y + 1);
- for(wm = 0;wm < 16;wm++)
- {
- WriteDat(F16x16[adder]);
- adder += 1;
- }
- }
自己制作的字库显示:
- 0x40,0x40,0x42,0xCC,0x00,0x40,0x20,0x90,0x0C,0x03,0x0C,0x90,0x20,0x40,0x40,0x00,
- 0x00,0x00,0x00,0x7F,0x20,0x10,0x00,0x3F,0x44,0x42,0x41,0x40,0x40,0x70,0x00,0x00,/*"论"32*/
- /* (16 X 16 , 宋体 )*/
- 0x20,0x20,0x20,0xFF,0x20,0x20,0x40,0x44,0x44,0x44,0xC4,0x44,0x44,0x44,0x40,0x00,
- 0x10,0x30,0x10,0x0F,0x08,0x08,0x20,0x70,0x28,0x26,0x21,0x20,0x28,0x30,0x60,0x00,/*"坛",33*/
- /* (16 X 16 , 宋体 )*/
- 0x02,0xFE,0x92,0x92,0xFE,0x02,0x00,0xF8,0x89,0x8E,0x88,0x8C,0x8B,0xF8,0x00,0x00,
- 0x10,0x1F,0x08,0x08,0xFF,0x04,0x40,0x38,0x00,0x3C,0x41,0x46,0x60,0x04,0x38,0x00,/*"聪",34*/
- /* (16 X 16 , 宋体 )*/
- 0x10,0x60,0x02,0x8C,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0xFF,0x00,0x00,
- 0x04,0x04,0x7E,0x01,0x80,0x47,0x30,0x0F,0x10,0x27,0x00,0x47,0x80,0x7F,0x00,0x00,/*"测",35*/
- /* (16 X 16 , 宋体 )*/
- 0x40,0x40,0x42,0xCC,0x00,0x00,0x12,0x62,0x02,0xFE,0x02,0x42,0x32,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x3F,0x10,0x09,0x01,0x01,0x01,0xFF,0x01,0x01,0x01,0x01,0x01,0x00,/*"评",36*/
- 0x00,0x02,0x02,0x7A,0x4A,0x4A,0x4A,0x4A,0x7A,0x02,0x02,0x7E,0x02,0x02,0x00,0x00,
- 0x01,0x01,0x01,0x3D,0x25,0x25,0x25,0x25,0x3D,0x41,0x81,0x7F,0x01,0x01,0x01,0x00,/*"哥",37*/
实物演示:
后记:在调试硬件IIC时候,也是遇到了问题,在这里和大家分享一下过程:基本上参考了21论坛上面其他的大佬的作品,和驱动方法和过程,在调试AT硬件IIC的时候,底层驱动都是使用的官方的驱动函数,只是在运用时候使用的驱动OLED函数罢了,当时出现的问题是:屏幕乱码,但是可以正常点亮了,开始以为是AT32的驱动能力问题,这里我使用的是1.8V给开发板供电,开始认为是TTL信号问题,后来才发现是制作的字库有问题,于是有重新对制作的字库的过程学习了一下,这里也将制作过程分享一下!!!