0
【目的】OLED显示,由于我有GD32F103的使用经验,所以干货立马上架,希望友友们喜欢。
【步骤】
1、初始化I2C
- #include "my_i2c.h"
- #include "gd32vf103.h"
- /*!
- brief enable the peripheral clock
- param[in] none
- param[out] none
- retval none
- */
- static void rcu_config(void)
- {
- /* enable GPIOB clock */
- rcu_periph_clock_enable(RCU_GPIOB);
- /* enable I2C0 clock */
- rcu_periph_clock_enable(RCU_I2C0);
- }
- /*!
- brief configure the GPIO ports
- param[in] none
- param[out] none
- retval none
- */
- static void gpio_config(void)
- {
- gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
- }
- /*!
- brief configure the I2C0 and I2C1 inter faces
- param[in] none
- param[out] none
- retval none
- */
- static void i2c_config(void)
- {
- /* configure I2C clock */
- i2c_clock_config(I2C0,400000,I2C_DTCY_2);
- /* configure I2C address */
- i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C0_SLAVE_ADDRESS7);
- /* enable I2C0 */
- i2c_enable(I2C0);
- /* enable acknowledge */
- i2c_ack_config(I2C0,I2C_ACK_ENABLE);
- }
- /*!
- brief 初始化i20
- param[in] none
- param[out] none
- retval none
- */
- void my_i2c0_init(void)
- {
- rcu_config();
- gpio_config();
- i2c_config();
- }
复制代码
2、I2C发送数据:
- void ssd1306_write_cmd(uint8_t var)
- {
- /* wait until I2C bus is idle */
- while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
- /* send a start condition to I2C0 bus */
- i2c_start_on_bus(I2C0);
- /* wait until SBSEND bit is set */
- while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
- /* send slave address to I2C bus */
- i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_TRANSMITTER);
- /* wait until ADDSEND bit is set */
- while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
- /* clear ADDSEND bit */
- i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
- /* wait until the transmit data buffer is empty */
- i2c_data_transmit(I2C0, 0x00);
- while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
- /* data transmission */
- i2c_data_transmit(I2C0, var);
- /* wait until the TBE bit is set */
-
- while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
-
-
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2C0);
- /* wait until stop condition generate */
- while(I2C_CTL0(I2C0) & 0x0200);
-
- }
- void ssd1306_write_date(uint8_t var)
- {
- /* wait until I2C bus is idle */
- while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
- /* send a start condition to I2C0 bus */
- i2c_start_on_bus(I2C0);
- /* wait until SBSEND bit is set */
- while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
- /* send slave address to I2C bus */
- i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_TRANSMITTER);
- /* wait until ADDSEND bit is set */
- while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
- /* clear ADDSEND bit */
- i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
- /* wait until the transmit data buffer is empty */
- i2c_data_transmit(I2C0, 0x40);
- while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
- /* data transmission */
- i2c_data_transmit(I2C0, var);
- /* wait until the TBE bit is set */
- while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2C0);
- /* wait until stop condition generate */
- while(I2C_CTL0(I2C0) & 0x0200);
-
- }
复制代码
3、其他的就直接用ssd1306的通用代码就行啦,我将附件提交上来,大家直接用就行啦。。。
4、显示测试程序:咱一定给电子发烧友做广告哒。。。
- //显示测试
- void oled_display_test(void)
- {
- oled_init();
- oled_clear();
- OLED_ShowStr(2,0,"GD32VF103R",24);
- OLED_ShowStr(12,3,"TEST By lugl",16);
- OLED_ShowStr(28,5,"HELLO WORLD!",12);
- OLED_ShowStr(12,7,"www.elecfans.com",6);
- }
复制代码
5、主程序:
- int main(void) {
- led_init();
- key_init();
- my_i2c0_init();
- gd_eval_com_init();
- printf("GD32VF103rn");
- oled_display_test();
- // LCD_Init();//LCD初始化
- // LCD_Fill(0,0,LCD_W,LCD_H,BLUE);
- // LCD_ShowString(12,0,"GD32F310G",WHITE,BLUE,32,0);
- // LCD_ShowString(18,36,"EEWORDL",WHITE,BLUE,32,0);
- // LCD_ShowString(16,72,"2022-5-7",BLACK,BLUE,32,0);
- // LCD_ShowString(12,100,"GD32f310g_adc_demo",RED,BLUE,16,0);
- while (1) {
- sta_flash(key_sta);
- }
- }
复制代码
【总结】做为GD32VF103,很多风格跟GD32F103都有相似之处,移标植相当方便。
|
|