【AT-START-F425 Review】Use of GPIO port and OLED screen display driver
[Copy link]
This post was last edited by jinglixixi on 2022-3-30 09:27
On the AT-START-F425 development board, there are 3 LEDs and 1 user key, and its schematic diagram is shown in Figure 1.
Figure 1 LED and KEY circuit
Through the routine led_toggle, it can be used as the output function of GPIO. So how to use the input function of the GPIO port to realize KEY control of LED?
The procedure is as follows:
int main(void)
{
system_clock_config();
at32_board_init();
at32_button_init();
while(1)
{
if(at32_button_state())
at32_led_off(LED2);
else
at32_led_on(LED2);
delay_ms(200);
}
}
That is to say, the function at32_button_init() can be used to set the pin connected to the button as an input function, and the function at32_button_state() can be used to read the state of the button, thereby controlling the on and off of the LED.
The functions at32_button_init() and at32_button_state() used have the following contents:
void at32_button_init(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(USER_BUTTON_CRM_CLK, 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_INPUT;
gpio_init_struct.gpio_pins = USER_BUTTON_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
gpio_init(USER_BUTTON_PORT, &gpio_init_struct);
}
uint8_t at32_button_state(void)
{
return gpio_input_data_bit_read(USER_BUTTON_PORT, USER_BUTTON_PIN);
}
In this way, the purpose of controlling the LED with KEY is achieved, and the input and output functions and usage of the GPIO port are mastered.
On this basis, it is a very simple matter to use GPIO to simulate I2C to drive the OLED screen.
All you need to do is to determine the pins to which the OLED screen is connected. For ease of use, select PA5 and PA6 of the Arduino interface.
PA5 is connected to SCL and PA6 is connected to SDA.
Figure 2 Arduino interface
Next is the definition of setting the output high and low levels, which are as follows:
#define OLED_SCLK_Clr() GPIOA->clr = GPIO_PINS_5
#define OLED_SCLK_Set() GPIOA->scr = GPIO_PINS_5
#define OLED_SDIN_Clr() GPIOA->clr = GPIO_PINS_6
#define OLED_SDIN_Set() GPIOA->scr = GPIO_PINS_6
Then the function settings of the relevant pins are as follows:
void app_oled_init(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);
}
The initialization function of the OLED screen is:
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);
}
The main program to achieve the effect shown in Figure 3 is:
int main(void)
{
system_clock_config();
at32_board_init();
app_oled_init();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AT32F425",16);
OLED_ShowString(0,2,"OLED TEST",16);
OLED_ShowString(8,4,"jinglixixi",16);
while(1)
{
at32_led_toggle(LED2);
delay_ms(200);
at32_led_toggle(LED3);
delay_ms(200);
at32_led_toggle(LED4);
delay_ms(200);
}
}
Figure 3 shows the effect
For your convenience, the project files are as follows:
|