本帖最后由 jinglixixi 于 2025-1-19 13:35 编辑
#申请原创#@21小跑堂
由于APM32M3514开发板引脚资源的稀缺,为了对调试过程能有个直观的了解,就为其配置了一个I2C接口的0.96寸的OLED屏,该显示屏的分辨率为128*64像素点,是一款准双色显示屏,供电电压可达5V。
图1 所用引脚
该显示屏与开发板的连接关系为:
SCLK----PC4
SDIN----PC5
这2个引脚的位置如图2所示,当然也可供UART1来所用。
图2 所用引脚
为输出高低电平,所作的语句定义为:
#define OLED_SCLK_Clr() GPIO_ClearBit(GPIOC, GPIO_PIN_4)
#define OLED_SCLK_Set() GPIO_SetBit(GPIOC, GPIO_PIN_4)
#define OLED_SDIN_Clr() GPIO_ClearBit(GPIOC, GPIO_PIN_5)
#define OLED_SDIN_Set() GPIO_SetBit(GPIOC, GPIO_PIN_5)
采用GPIO口模拟I2C发送字节数据的函数为:
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else
{
OLED_SDIN_Clr();
}
APM_DelayUs(3);
da=da<<1;
OLED_SCLK_Set();
APM_DelayUs(3);
OLED_SCLK_Clr();
APM_DelayUs(3);
}
}
对该显示屏初始化处理的函数为:
void OLED_Init(void)
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x20); //Set Memory Addressing Mode
Write_IIC_Command(0x10);
Write_IIC_Command(0xb0);//Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8);//Set COM Output Scan Direction
Write_IIC_Command(0x00);//---set low column address
Write_IIC_Command(0x10);//---set high column address
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0xdf);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x3F);
Write_IIC_Command(0xa4);
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x20);//0x20,0.77xVcc
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);
Write_IIC_Command(0xaf);//--turn on oled panel
}
实现清屏处理的函数为:
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)
{
APM_MINI_Init();
GPIO_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AMP32M3514",16);
OLED_ShowString(0,2,"OLED TEST",16);
while (1);
}
经程序的编译和下载,其显示效果如图3所示。
图3 显示效果
这样就在一款专用性很强的开发板上,奇葩式地绽放出一朵I2C接口的OLED屏小花!
尽管APM32M3514开发板是一款专用性很强的开发工具,但它也有不可掩藏的片内资源配置,如RTC计时就是一种,将它与OLED屏相配合即可轻松地实现电子时钟的功能。
对RTC配置初始时间值的函数为:
void RTC_ConfigDateTime(void)
{
uint32_t date;
uint32_t time;
RTC_Init();
RTC_Reset();
RTC_Init();
RTC_EnableInit();
RTC_ConfigDateStructInit(&DateStruct);
if (RTC_ReadBackup(RTC_BAKP_DATA4) != RTC_BKP_VALUE)
{
DateStruct.year = 25;
DateStruct.month = 1;
DateStruct.date = 18;
DateStruct.weekday = 6;
RTC_ConfigDate(RTC_FORMAT_BIN, &DateStruct);
APM_DelayMs(10);
TimeStruct.H12 = 12;
TimeStruct.hours = 23;
TimeStruct.minutes = 59;
TimeStruct.seconds = 50;
RTC_ConfigTime(RTC_FORMAT_BIN, &TimeStruct);
RTC_WriteBackup(RTC_BAKP_DATA1, RTC->DATE);
RTC_WriteBackup(RTC_BAKP_DATA0, RTC->TIME);
RTC_WriteBackup(RTC_BAKP_DATA4, RTC_BKP_VALUE);
}
else
{
time = RTC_ReadBackup(RTC_BAKP_DATA0);
TimeStruct.hours = time >> 16;
TimeStruct.minutes = (time >> 8) & 0xff;
TimeStruct.seconds = (time) & 0xff;
RTC_ConfigTime(RTC_FORMAT_BCD, &TimeStruct);
APM_DelayMs(10);
date = RTC_ReadBackup(RTC_BAKP_DATA1);
DateStruct.year = date >> 16;
DateStruct.month = (date >> 8) & 0x0f;
DateStruct.date = date & 0xff;
DateStruct.weekday = (date >> 13) & 0x07;
RTC_ConfigDate(RTC_FORMAT_BCD, &DateStruct);
}
RTC_DisableInit();
}
对OLED屏所用引脚的配置函数为:
void GPIO_Init(void)
{
GPIO_Config_T gpioConfig;
gpioConfig.mode = GPIO_MODE_OUT;
gpioConfig.outtype = GPIO_OUT_TYPE_PP;
gpioConfig.speed = GPIO_SPEED_50MHz;
gpioConfig.pupd = GPIO_PUPD_NO;
RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOC);
gpioConfig.pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_Config(GPIOC, &gpioConfig);
}
为进行数据的显示所配备的数值显示函数为:
void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
{
uint8_t t,temp;
uint8_t enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
}
实现电子时钟功能测试的主程序为:
int main(void)
{
uint8_t prevTime;
char dateStr[11];
char timeStr[9];
APM_MINI_Init();
RTC_ConfigDateTime();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AMP32M3514",16);
OLED_ShowString(0,2,"RTC TEST",16);
OLED_ShowString(0,4," : :",16);
while (1)
{
prevTime = TimeStruct.seconds;
RTC_ReadTime(RTC_FORMAT_BIN, &TimeStruct);
RTC_ReadDate(RTC_FORMAT_BIN, &DateStruct);
if (prevTime != TimeStruct.seconds)
{
OLED_ShowNum(0,4,TimeStruct.hours,2,16);
OLED_ShowNum(24,4,TimeStruct.minutes,2,16);
OLED_ShowNum(48,4,TimeStruct.seconds,2,16);
RTC_WriteBackup(RTC_BAKP_DATA0, RTC->TIME);
if ((TimeStruct.hours == 0) && (TimeStruct.minutes == 0) && (TimeStruct.seconds == 0))
{
RTC_WriteBackup(RTC_BAKP_DATA1, RTC->DATE);
}
}
}
}
经程序的编译和下载,其显示效果如图4和图5所示。
图4 计时效果1
图5 计时效果2
|
|