在AT-START-L021开发板上配有ArduinoTM Uno R3扩展接口,合理地所用它可起到事半功倍的效果。这里以一款I2C接口的OLED屏显示驱动为例来进行介绍,为了借用图1所示的扩展接口从而免去接线的问题,故OLED屏与开发板的连接关系为: SCL---PA5 SDA---PA6
图1 所用接口 使这2个引脚输出高低电平的语句定义为: #define OLED_SCLK_Clr() gpio_bits_write(GPIOA,GPIO_PINS_5,FALSE) #define OLED_SCLK_Set() gpio_bits_write(GPIOA,GPIO_PINS_5,TRUE) #define OLED_SDIN_Clr() gpio_bits_write(GPIOA,GPIO_PINS_6,FALSE) #define OLED_SDIN_Set() gpio_bits_write(GPIOA,GPIO_PINS_6,TRUE) 配置这2个引脚为输出功能的函数为: void oled_CONFIG(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
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 = GPIO_PINS_5|GPIO_PINS_6;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
}
该显示屏的初始化函数为: void OLED_Init(void)
{
Write_IIC_Command(0xAE);
Write_IIC_Command(0x20);
Write_IIC_Command(0x10);
Write_IIC_Command(0xb0);
Write_IIC_Command(0xc8);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
Write_IIC_Command(0x40);
Write_IIC_Command(0x81);
Write_IIC_Command(0xdf);
Write_IIC_Command(0xa1);
Write_IIC_Command(0xa6);
Write_IIC_Command(0xa8);
Write_IIC_Command(0x3F);
Write_IIC_Command(0xa4);
Write_IIC_Command(0xd3);
Write_IIC_Command(0x00);
Write_IIC_Command(0xd5);
Write_IIC_Command(0xf0);
Write_IIC_Command(0xd9);
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);
Write_IIC_Command(0x20);
Write_IIC_Command(0x8d);
Write_IIC_Command(0x14);
Write_IIC_Command(0xaf);
}
实现清屏处理的函数为: void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD);
OLED_WR_Byte (0x00,OLED_CMD);
OLED_WR_Byte (0x10,OLED_CMD);
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}
实现字符串显示功能的函数为: void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
while (chr[j]!='\0')
{
OLED_ShowChar(x,y,chr[j],Char_Size);
x+=8;
if(x>120) {x=0;y+=2;}
j++;
}
}
显示显示功能测试的主程序为: int main(void)
{
system_clock_config();
at32_board_init();
oled_CONFIG();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AT32L021 TEST",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
OLED_ShowString(0,4,"jinglixixi",16);
OLED_ShowString(0,6,"2024.11.28",16);
while(1)
{
at32_led_toggle(LED2);
delay_ms(200);
at32_led_toggle(LED3);
delay_ms(200);
at32_led_toggle(LED4);
delay_ms(200);
}
}
经程序的编译与下载,其显示效果如图2所示。 图2 显示效果
|