本帖最后由 jinglixixi 于 2019-12-16 22:18 编辑
PIC16F18446开发板是一块外设简单的最小系统板,在熟悉GPIO口的使用方法后,可以采用模拟I2C接口的方式来驱动OLED屏进行显示,其显示效果如图1和图2所示。
图1 6*8点阵字模显示
图2 8*16点阵字模显示
在编写程序前,用MCC可对使用的引脚进行设置,见图3所示。
图3 设置OLED屏引脚
为识读的方便,可对使用的引脚加以重新命名,如PB5为SCL,PB7为SDA。
OLED屏的初始化函数为:
void OLED_Init(void)
{
__delay_ms(50);
OLED_WrCmd(0xae);//--turn off oled panel
OLED_WrCmd(0x00);//---set low column address
OLED_WrCmd(0x10);//---set high column address
OLED_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WrCmd(0x81);//--set contrast control register
OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
OLED_WrCmd(0xa1);//--Set SEG/Column Mapping 0xa0×óÓÒ·´ÖÃ 0xa1Õý³£
OLED_WrCmd(0xc8);//Set COM/Row Scan Direction 0xc0ÉÏÏ·´Öà 0xc8Õý³£
OLED_WrCmd(0xa6);//--set normal display
OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
OLED_WrCmd(0x3f);//--1/64 duty
OLED_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WrCmd(0x00);//-not offset
OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WrCmd(0xd9);//--set pre-charge period
OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WrCmd(0xda);//--set com pins hardware configuration
OLED_WrCmd(0x12);
OLED_WrCmd(0xdb);//--set vcomh
OLED_WrCmd(0x40);//Set VCOM Deselect Level
OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WrCmd(0x02);//
OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
OLED_WrCmd(0x14);//--set(0x10) disable
OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
OLED_WrCmd(0xaf);//--turn on oled panel
OLED_Fill(0x00);
OLED_Set_Pos(0,0);
}
相应的字符串显示函数为:
void OLED_P8x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!='\0')
{
c =ch[j]-32;
if(x>120){x=0;y++;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WrDat(F8X16[c*16+i]);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WrDat(F8X16[c*16+i+8]);
x+=8;
j++;
}
}
图示效果的主程序为:
void main(void)
{
SYSTEM_Initialize();
OLED_Init();
__delay_ms(20);
OLED_CLS();
OLED_P8x16Str(0,0,"PIC16F18446");
OLED_P8x16Str(0,2,"OLED Display");
OLED_P8x16Str(0,4,"jinglixixi");
OLED_P8x16Str(0,6,"2019.12.16");
while (1)
{
led0_Toggle(); // LED ON
__delay_ms(200); // 1 Second Delay
}
}
图4 程序界面
经编译处理,将生成的HEX文件复制到虚拟U盘即可见到显示效果。
|