手上有个SPI接口OLED屏,尺寸128x64,正好给AT32F437评估板加个显示器。OLED没太多好讲的,主要就是代码实现:
连接如下:
OLED MCU
D/C PB10
CS PA8
RST PA10
CLK PB3
DAT PB5
底层主要是SPI接口调试,相对比较简单,实现初始化和写函数即可:
初始化:
- void ext_spi_init(void)
- {
- spi_init_type cfg;
- intx_alloc();
- // 开启时钟
- intx_disable();
- crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
- crm_periph_reset(CRM_SPI1_PERIPH_RESET, TRUE);
- crm_periph_reset(CRM_SPI1_PERIPH_RESET, FALSE);
- intx_enable();
- // SPI 配置
- cfg.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
- cfg.mclk_freq_division = SPI_MCLK_DIV_32;
- cfg.master_slave_mode = SPI_MODE_MASTER;
- cfg.frame_bit_num = SPI_FRAME_8BIT;
- cfg.first_bit_transmission = SPI_FIRST_BIT_MSB;
- cfg.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
- cfg.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
- cfg.clock_phase = SPI_CLOCK_PHASE_2EDGE;
- spi_init(SPIn, &cfg);
- SPIn->ctrl1_bit.spien = 1;
- }
写函数
- void ext_spi_wb(uint8_t dataW)
- {
- volatile uint8_t rb;
- spi_rx_reset();
- //send data
- spi_send(dataW);
- spi_read_delay();
- //wait for trans complete
- while (is_spi_busy())
- ;
- //read data
- rb = spi_read();
- }
oled 数据/命令操作:
- void oled1_write_cmd(uu8 Data)
- {
- oled_cs.dis();
- oled_dc.dis();
-
- oled_spi_wb(Data);
- oled_cs.en();
- }
- void oled1_write_data(uu8 Data)
- {
- oled_cs.dis();
- oled_dc.en();
-
- oled_spi_wb(Data);
- oled_cs.en();
- }
oled模块初始化:
- void ssd1306_m1_init(void)
- {
- //internal setting
- oled1_write_cmd(0xae); //--turn off oled panel
- oled1_write_cmd(0x00); //--set low column address
- oled1_write_cmd(0x10); //--set high column address
- oled1_write_cmd(0x40); //--set start line address
- oled1_write_cmd(0x81); //--set contrast control register
- oled1_write_cmd(0xf0);
- oled1_write_cmd(0xa1); //--ÁÐɨÃè˳Ðò£º´Ó×óµ½ÓÒa1 //×óÓҷתa0
- oled1_write_cmd(0xc8); //--ÐÐɨÃè˳Ðò£º´ÓÉϵ½ÏÂc8 //ÉÏϵߵ¹c0
- oled1_write_cmd(0xa6); //--set normal display
- oled1_write_cmd(0xa8); //--set multiples ratio(1to64)
- oled1_write_cmd(0x3f); //--1/64 duty
- oled1_write_cmd(0xd3); //--set display offset
- oled1_write_cmd(0x00); //--not offset
- oled1_write_cmd(0xd5); //--set display clock divide ratio/oscillator frequency
- oled1_write_cmd(0x80); //--set divide ratio
- oled1_write_cmd(0xd9); //--set pre-charge period
- oled1_write_cmd(0xf1);
- oled1_write_cmd(0xda); //--set com pins hardware configuration
- oled1_write_cmd(0x12);
- oled1_write_cmd(0xdb); //--set vcomh
- oled1_write_cmd(0x30);
- oled1_write_cmd(0x8d); //--set chare pump enable/disable
- oled1_write_cmd(0x14); //--set(0x10) disable
- clr_display();
- oled1_write_cmd(0xaf);
- //oled1_write_cmd(0xa7); //--set normal display
- oled1_write_cmd(0xa6); //--set normal display
- }
显示效果如下图所示:
代码托管在GITEE,后面评测会一直用这个仓库更新。
https://gitee.com/aple_sun/atf437-start
|