四线SPI 128x64OLED的连接方式
-
操作方式
u8 OLED_GRAM[132][8];
void OLED_Refresh_Gram(void)
{
u8 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<131;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
void OLED_WR_Byte(u8 dat,u8 cmd)
{
u8 i;
OLED_RS=cmd; //дļ®
OLED_CS=0;
for(i=0;i<8;i++)
{
OLED_SCLK=0;
if(dat&0x80)OLED_SDIN=1;
else OLED_SDIN=0;
OLED_SCLK=1;
dat<<=1;
}
OLED_CS=1;
OLED_RS=1;
}
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCļ®
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCļ®
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
//for(n=0;n<128;n++)
for(n=0;n<131;n++)
{
OLED_GRAM[n][i]=0X00;
}
}
OLED_Refresh_Gram();//ټтДʾ
}
//ۭ֣
//x:0~127
//y:0~63
//t:1 ͮԤ 0,ȥࠕ
void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
u8 pos,bx,temp=0;
if(x>131||y>63)return;//ӬԶΧ�.
pos=7-y/8;
bx=y%8;
temp=1<<(7-bx);
if(t)OLED_GRAM[x][pos]|=temp;
else OLED_GRAM[x][pos]&=~temp;
}
//x1,y1,x2,y2 ͮԤȸԲքהއظҪ
//ȷѣx1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63
//dot:0,ȥࠕ;1,ͮԤ
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot)
{
u8 x,y;
for(x=x1;x<=x2;x++)
{
for(y=y1;y<=y2;y++)OLED_DrawPoint(x,y,dot);
}
OLED_Refresh_Gram();//ټтДʾ
}
//՚ָ֨λ׃Дʾһٶؖػ,Ѽ(ҿؖؖػ
//x:0~127
//y:0~63
//mode:0,״їДʾ;1,ֽӣДʾ
//size:ѡձؖͥ 16/12
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode)
{
u8 temp,t,t1;
u8 y0=y;
u8 csize=(size/8+((size%8)?1:0))*(size/2); //փսؖͥһٶؖػהӦֳ֣ܯ̹ռքؖޚ˽
chr=chr-' ';//փսƫӆ۳քֵ
for(t=0;t=' '))//Ɛˇһˇ؇רؖػ!
{
if(x>(128-(size/2))){x=0;y+=size;}
if(y>(64-size)){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,size,1);
x+=size/2;
p++;
}
}
-
初始化方式
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //ʹŜPA,PDࠚʱד
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //LED0-->PA.8 ࠚƤ׃
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ΆάˤԶ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IOࠚ̙Ϊ50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //ٹߝʨ֨ӎ˽ԵʼۯGPIOA.8
GPIO_SetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11); //PA.8 ˤԶٟ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //LED1-->PD.2 ࠚƤ׃, ΆάˤԶ
GPIO_Init(GPIOB, &GPIO_InitStructure); //ΆάˤԶ ìIOࠚ̙Ϊ50MHz
GPIO_SetBits(GPIOB,GPIO_Pin_2); //PD.2 ˤԶٟ
OLED_WR_Byte(0xAE,OLED_CMD); //ژҕДʾ
OLED_WR_Byte(0xD5,OLED_CMD); //ʨ׃ʱדؖƵӲؓ,ְմƵÊ
OLED_WR_Byte(80,OLED_CMD); //[3:0],ؖƵӲؓ;[7:4],ְմƵÊ
OLED_WR_Byte(0xA8,OLED_CMD); //ʨ׃Ƚ֯·˽
OLED_WR_Byte(0X3F,OLED_CMD); //Ĭɏ0X3F(1/64)
OLED_WR_Byte(0xD3,OLED_CMD); //ʨ׃Дʾƫӆ
OLED_WR_Byte(0X00,OLED_CMD); //ĬɏΪ0
OLED_WR_Byte(0x40,OLED_CMD); //ʨ׃Дʾߪʼѐ [5:0],ѐ˽.
OLED_WR_Byte(0x8D,OLED_CMD); //֧ۉ҃ʨ׃
OLED_WR_Byte(0x14,OLED_CMD); //bit2ìߪǴ/ژҕ
OLED_WR_Byte(0x20,OLED_CMD); //ʨ׃Śզַ֘ģʽ
OLED_WR_Byte(0x02,OLED_CMD); //[1:0],00ì�ַ֘ģʽ;01ìѐַ֘ģʽ;10,ҳַ֘ģʽ;Ĭɏ10;
OLED_WR_Byte(0xA1,OLED_CMD); //ט֨ӥʨ׃,bit0:0,0->0;1,0->127;
OLED_WR_Byte(0xC0,OLED_CMD); //ʨ׃COMɨĨв;bit3:0,Ǖͨģʽ;1,ט֨ӥģʽ COM[N-1]->COM0;N:Ƚ֯·˽
OLED_WR_Byte(0xDA,OLED_CMD); //ʨ׃COMӲݾӽޅƤ׃
OLED_WR_Byte(0x12,OLED_CMD); //[5:4]Ƥ׃
OLED_WR_Byte(0x81,OLED_CMD); //ה҈ʨ׃
OLED_WR_Byte(0xEF,OLED_CMD); //1~255;Ĭɏ0X7F (�ʨ׃,ԽճԽ�)
OLED_WR_Byte(0xD9,OLED_CMD); //ʨ׃ԤԤ֧לǚ
OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;
OLED_WR_Byte(0xDB,OLED_CMD); //ʨ׃VCOMH ֧ѹѶÊ
OLED_WR_Byte(0x30,OLED_CMD); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
OLED_WR_Byte(0xA4,OLED_CMD); //ȫߖДʾߪǴ;bit0:1,ߪǴ;0,ژҕ;(їǁ/ۚǁ)
OLED_WR_Byte(0xA6,OLED_CMD); //ʨ׃Дʾʽ;bit0:1,״РДʾ;0,ֽӣДʾ
OLED_WR_Byte(0xAF,OLED_CMD); //ߪǴДʾ
OLED_Clear();
}