[National Technology N32G457 Review] OLED screen display driver and RTC electronic clock
[Copy link]
This post was last edited by jinglixixi on 2022-2-10 09:57
There are many types of OLED screens. According to the interface mode, they can be divided into I2C interface and SPI interface mode. According to the color, they can be divided into single color, dual color and full color. In addition, according to the geometric size, there are specifications such as 0.91 inches and 0.96 inches.
Here we introduce a 0.96-inch dual-color display screen that drives the I2C interface. Since there are two ways to implement it, hardware and software, only the software method is used here.
The connection relationship between the display screen and the development board is:
CLK---PE2
DIN---PE4
The statements defined to achieve high and low level output are:
#define OLED_SCLK_Clr() GPIOE->PBC = GPIO_PIN_2
#define OLED_SCLK_Set() GPIOE->PBSC = GPIO_PIN_2
#define OLED_SDIN_Clr() GPIOE->PBC = GPIO_PIN_4
#define OLED_SDIN_Set() GPIOE->PBSC = GPIO_PIN_4
The function to set the relevant pin as output function is:
void OLEDInit(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_2|GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
}
The function to implement I2C start and stop is:
void IIC_Start()
{
OLED_SCLK_Set();
Delayus(2);
OLED_SDIN_Set();
Delayus(2);
OLED_SDIN_Clr();
Delayus(2);
OLED_SCLK_Clr();
Delayus(2);
}
void IIC_Stop()
{
OLED_SCLK_Set();
Delayus(2);
OLED_SDIN_Clr();
Delayus(2);
OLED_SDIN_Set();
Delayus(2);
}
The initialization function of the display is:
void OLED_Init(void)
{
OLED_SCLK_Set();
OLED_SDIN_Set();
Delayms(800);
OLED_WR_Byte(0xAE,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xff,OLED_CMD);
OLED_WR_Byte(0xA1,OLED_CMD);
OLED_WR_Byte(0xC8,OLED_CMD);
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_WR_Byte(0xA8,OLED_CMD);
OLED_WR_Byte(0x3f,OLED_CMD);
OLED_WR_Byte(0xD3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd5,OLED_CMD);
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD);
OLED_WR_Byte(0xF1,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD);
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x20,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xA4,OLED_CMD)
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD);
}
The function that implements string display is:
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++;
}
}
The main program to achieve the graphic effect is:
int main(void)
{
LedInit(PORT_GROUP2, LED3_PIN);
OLEDInit();
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"N32G457 ",16);
OLED_ShowString(20,2,"OLED TEST",16);
while (1)
{
LedBlink(PORT_GROUP2, LED3_PIN);
Delay(0x28FFFF);
}
}
After compilation and downloading, the display effect is shown in Figure 1.
Figure 1 OLED screen display effect
Based on this display function, the electronic clock function can be realized by combining it with the RTC timer.
The function to realize date display is:
void RTC_DateShowP(void)
{
RTC_DateType RTC_DateStructure;
RTC_GetDate(RTC_FORMAT_BIN, &RTC_DateStructure);
OLED_ShowNum(36,4,RTC_DateStructure.Year,2,16);
OLED_ShowNum(60,4,RTC_DateStructure.Month,2,16);
OLED_ShowNum(84,4,RTC_DateStructure.Date,2,16);
}
The function that realizes the design display is:
void RTC_TimeShowP(void)
{
RTC_TimeType RTC_TimeStructure;
RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
OLED_ShowNum(20,6,RTC_TimeStructure.Hours,2,16);
OLED_ShowNum(44,6,RTC_TimeStructure.Minutes,2,16);
OLED_ShowNum(68,6,RTC_TimeStructure.Seconds,2,16);
(void)RTC->DATE;
}
The main program to implement the RTC electronic clock is:
int main(void)
{
LEDInit(LED1_PORT,LED1_PIN);
LEDOff(LED1_PORT,LED1_PIN);
OLEDInit();
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"N32G457 ",16);
OLED_ShowString(20,2,"OLED & RTC",16);
OLED_ShowString(20,4,"20 - -",16);
OLED_ShowString(20,6," : :",16);
RTC_DateAndTimeDefaultVale();
RTC_CLKSourceConfig(RTC_CLK_SRC_TYPE_LSE, true, true);
RTC_PrescalerConfig();
RTC_DateRegulate();
RTC_TimeRegulate();
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ);
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
RTC_EnableCalibOutput(ENABLE);
EXTI_PB8_TimeStamp_Configuration();
EXTI20_TimeStampIRQn_Configuration(EXTI_Trigger_Falling);
RTC_ClrFlag(RTC_FLAG_TISF);
RTC_ClrFlag(RTC_FLAG_TISOVF);
RTC_EnableTimeStamp(RTC_TIMESTAMP_EDGE_FALLING, ENABLE);
while (1)
{
if (RTC_GetFlagStatus(RTC_FLAG_TISF) != RESET)
{
RTC_DateShowP();
RTC_TimeShowP();
RTC_ClrFlag(RTC_FLAG_TISF);
RTC_ClrFlag(RTC_FLAG_TISOVF);
}
LEDBlink(LED1_PORT,LED1_PIN);
SysTick_Delay_Ms(1000);
}
}
After compilation and downloading, when PC13 and PB8 are connected, the display effect of the RTC electronic clock is shown in Figure 2 and Figure 3.
Figure 2 Electronic clock timing effect 1
Figure 3 Electronic clock timing effect 2
|