本帖最后由 dirty123 于 2024-5-15 23:06 编辑
本篇讲述AT-START-F405开发板点亮OLED.
一.硬件准备
这里准备了128*64分辨率ssd1306 OLED屏。引脚连接如下
OLED 开发板
VCC VDD
GND GND
SCL PB8
SDA PB7
二.代码准备
1.引脚初始化
void oled_gpio_init(void)
{
gpio_init_type gpio_init_struct;
/* enable the led clock */
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
/* set default parameter */
gpio_default_para_init(&gpio_init_struct);
/* configure the led gpio */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = OLED_SCLK_Pin|OLED_SDIN_Pin;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(OLED_SDIN_GPIO_Port, &gpio_init_struct);
}
2.I2C写数据/命令
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(uint8_t IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
IIC_Wait_Ack();
Write_IIC_Byte(0x00); //write command
IIC_Wait_Ack();
Write_IIC_Byte(IIC_Command);
IIC_Wait_Ack();
IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(uint8_t IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78); //D/C#=0; R/W#=0
IIC_Wait_Ack();
Write_IIC_Byte(0x40); //write data
IIC_Wait_Ack();
Write_IIC_Byte(IIC_Data);
IIC_Wait_Ack();
IIC_Stop();
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
Write_IIC_Data(dat);
}
else
{
Write_IIC_Command(dat);
}
}
3.寄存器初始化
//初始化SSD1306
void OLED_Init(void)
{
oled_gpio_init();
delay_sec(1);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap //--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC0,OLED_CMD);//Com scan direction//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
4.设计UI界面显示
int main(void)
{
system_clock_config();
at32_board_init();
uart_print_init(115200);
/* output a message on hyperterminal using printf function */
printf("usart printf example: retarget the c library printf function to the usart\r\n");
OLED_Init();
OLED_Clear();
OLED_ShowStringCN(0,0,"雅特力 & 21ic",1);
OLED_ShowStringCN(0,16,"AT-START-F405",1);
OLED_ShowStringCN(0,32,"bbs.21ic.com/",1);
OLED_Refresh();
while(1)
{
printf("usart printf counter: %u\r\n",time_cnt++);
delay_sec(1);
}
}
三.测验
编译烧录后,按复位键,可以看到OLED按设计显示如下。
至此实现AT-START-F405驱动oled显示。
|