OLED屏其本质也是LED,所以并不比点亮LED难多少,这次就来点个OELD吧
鉴于OELD的像素相对于LED非常非常多,因此需要专用的屏驱IC来批量控制LED,也称之为屏驱IC,实际上OELD内部的LED需要点亮的电压高达十几伏特,一般常见的是SSD、SH系列的IC,兼容IIC、SPI、8080接口,我用的是4线SPI接口的。
第一步:测试SPI驱动是否正常
仿照demo例程crc_transfer_polling修改SPI的管脚及工作模式等。这里用的是SPI2,由于不需要CRC,直接关掉即可spi_crc_enable(SPI2, FALSE);
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
/* slave sck pin */
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_12;
gpio_init(GPIOB, &gpio_initstructure);
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_4;
gpio_init(GPIOB, &gpio_initstructure);
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_4;
gpio_init(GPIOC, &gpio_initstructure);
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
/* slave sck pin */
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
gpio_initstructure.gpio_pins = GPIO_PINS_13;
gpio_init(GPIOB, &gpio_initstructure);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE13, GPIO_MUX_5);
/* slave miso pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_2;
gpio_init(GPIOC, &gpio_initstructure);
gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE2, GPIO_MUX_5);
/* slave mosi pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_3;
gpio_init(GPIOC, &gpio_initstructure);
gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE3, GPIO_MUX_5);
spi_init_type spi_init_struct;
crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
spi_default_para_init(&spi_init_struct);
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(SPI2, &spi_init_struct);
spi_crc_polynomial_set(SPI2, 7);
spi_crc_enable(SPI2, FALSE);
spi_enable(SPI2, TRUE);
第二步:编写SPI测试函数,查看管脚波形,调用此函数随便发送个数据测试一下,测试正常
uint8_t spi_tx_rx(uint8_t para)
{
uint8_t rx;
while(spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
spi_i2s_data_transmit(SPI2, para);
while(spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET);
rx = spi_i2s_data_receive(SPI2);
return rx;
}
第三步:移植OLED驱动程序,这一步可以说是轻车熟路了,CtrlC、CtrlV走起
OLED_CS_Set();
OLED_DC_Set();
OLED_RES_Set();
DelayMs(10);
OLED_RES_Clr();
DelayMs(120);
OLED_RES_Set();
OLED_WR_Byte(0xAE, OLED_CMD); //关闭显示
OLED_WR_Byte(0xD5, OLED_CMD); //设置时钟分频因子,震荡频率
OLED_WR_Byte(80, OLED_CMD); //[3:0],分频因子;[7:4],震荡频率
OLED_WR_Byte(0xA8, OLED_CMD); //设置驱动路数
OLED_WR_Byte(0X3F, OLED_CMD); //默认0X3F(1/64)
OLED_WR_Byte(0xD3, OLED_CMD); //设置显示偏移
OLED_WR_Byte(0X00, OLED_CMD); //默认为0
OLED_WR_Byte(0x40, OLED_CMD); //设置显示开始行 [5:0],行数.
OLED_WR_Byte(0x8D, OLED_CMD); //电荷泵设置
OLED_WR_Byte(0x14, OLED_CMD); //bit2,开启/关闭
OLED_WR_Byte(0x20, OLED_CMD); //设置内存地址模式
OLED_WR_Byte(0x02, OLED_CMD); //[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;
OLED_WR_Byte(0xA1, OLED_CMD); //段重定义设置,bit0:0,0->0;1,0->127;
OLED_WR_Byte(0xC8, OLED_CMD); //设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数
OLED_WR_Byte(0xDA, OLED_CMD); //设置COM硬件引脚配置
OLED_WR_Byte(0x12, OLED_CMD); //[5:4]配置
OLED_WR_Byte(0x81, OLED_CMD); //对比度设置
OLED_WR_Byte(0xEF, OLED_CMD); //1~255;默认0X7F (亮度设置,越大越亮)
OLED_WR_Byte(0xD9, OLED_CMD); //设置预充电周期
OLED_WR_Byte(0xf1, OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;
OLED_WR_Byte(0xDB, OLED_CMD); //设置VCOMH 电压倍率
OLED_WR_Byte(0x30, OLED_CMD); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
OLED_WR_Byte(0xA4, OLED_CMD); //全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
OLED_WR_Byte(0xA6, OLED_CMD); //设置显示方式;bit0:1,反相显示;0,正常显示
OLED_WR_Byte(0xAF, OLED_CMD); //开启显示
OLED_Clear(0);
第四步:写OLED测试函数,下载到开发板,就随便画仨框吧
int main(void)
{
uint8_t i;
system_clock_config();
at32_board_init();
OLED_Init();
OLED_SetDir(3);
OLED_Clear(0);
OLED_DrawRectangle(5,5,122,59,1);
OLED_DrawRectangle(15,15,112,49,1);
OLED_DrawRectangle(25,25,102,39,1);
OLED_Refresh_Gram();
while(1)
{
}
}
第五步:接线再复位重启
另一张图
|
共1人点赞
|