.c
#include <OLED.H>
#define Uchar unsigned char
#define Uint unsigned int
#define Ulong unsigned long
#define uchar unsigned char
#define uint unsigned int
unsigned char OLED_GRAM[128][8]={0};
void delay_ms(unsigned int t)
{
Uchar i,j;
while(--t)
{
i=50;
j=100;
while(--j)
while(--i);
}
}
void uDelay(unsigned char l)
{
while(l--);
}
void Delay(unsigned char n)
{
unsigned char i,j,k;
for(k=0;k<n;k++)
{
for(i=0;i<131;i++)
{
for(j=0;j<15;j++)
{
uDelay(203);
}
}
}
}
void I2C_SendByte(u8 txd)
{
unsigned char Idex;
SCL3_0;
for (Idex = 0; Idex < 8; Idex++)
{
if (txd & (0x80))
SDA3_1;
else
SDA3_0;
SCL3_1;
SCL3_0;
txd<<=1;
}
}
u8 I2C_WaitAck()
{
u8 time = 0;
SCL3_1;
uDelay(1);
SDA3_1;
uDelay(1);
while(READ_SDA)
{
time++;
if(time>250)
{
I2C_Stop();
return 1;
}
}
SCL3_0;
return 0;
}
void I2C_Ack3()
{
SCL3_0;
SDA3_0;
SCL3_1;
SCL3_0;
}
void I2C_NAck3()
{
SCL3_0;
SDA3_1;
SCL3_1;
SCL3_0;
}
void I2C_Start()
{
SCL3_1;
SDA3_1;
SDA3_0;
SCL3_0;
}
void I2C_Stop()
{
SCL3_0;
SDA3_0;
SCL3_1;
SDA3_1;
}
void Write_Command3(unsigned char Data)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_WaitAck();
I2C_SendByte(0x00);
I2C_WaitAck();
I2C_SendByte(Data);
I2C_WaitAck();
I2C_Stop();
}
void Write_Data3(unsigned char Data)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_WaitAck();
I2C_SendByte(0x40);
I2C_WaitAck();
I2C_SendByte(Data);
I2C_WaitAck();
I2C_Stop();
}
void LCD_Init3(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(300);
delay_ms(300);
delay_ms(300);
Write_Command3(0xae);
Write_Command3(0x00);
Write_Command3(0x10);
Write_Command3(0x40);
Write_Command3(0xb0);
Write_Command3(0x81);
Write_Command3(0xff);
Write_Command3(0xa1);
Write_Command3(0xa6);
Write_Command3(0xc0);
Write_Command3(0xa8);
Write_Command3(0x3f);
Write_Command3(0xd3);
Write_Command3(0x00);
Write_Command3(0xd5);
Write_Command3(0x80);
Write_Command3(0xd9);
Write_Command3(0xf1);
Write_Command3(0xda);
Write_Command3(0x12);
Write_Command3(0xdb);
Write_Command3(0x40);
Write_Command3(0x8d);
Write_Command3(0x14);
Write_Command3(0xaf);
OLED_Clear();
OLED_Refresh_Gram();
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;
}
void OLED_Refresh_Gram(void)
{
u8 j,n;
for(j=0;j<8;j++)
{
Write_Command3(0x22);
Write_Command3(j);
Write_Command3(0x07);
for(n=0;n<128;n++)Write_Data3(OLED_GRAM[n][j]);
}
}
void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
u8 pos,bx,temp=0;
if(x>127||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;
}
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode)
{
u8 temp,t,t1;
u8 y0=y;
chr=chr-' ';
for(t=0;t<size;t++)
{
if(size==12)temp=asc2_1206[chr][t];
else temp=asc2_1608[chr][t];
for(t1=0;t1<8;t1++)
{
if(temp&0x80)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if((y-y0)==size)
{
y=y0;
x++;
break;
}
}
}
}
void OLED_ShowString(u8 x,u8 y,const u8 *p)
{
#define MAX_CHAR_POSX 122
#define MAX_CHAR_POSY 58
while(*p!='\0')
{
if(x>MAX_CHAR_POSX){x=0;y+=16;}
if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,16,1);
x+=8;
p++;
}
}
void LED_DrawPoint(unsigned short x,unsigned short y,u8 t)
{
unsigned short pos,bx,temp=0;
if(x>575||y>31)return;
pos = y;
bx = x%8;
temp=1<<(bx);
if(t)
{
OLED_GRAM[x/8][pos] |= temp;
}
else
{
OLED_GRAM[x/8][pos] &= ~temp;
}
}
void LED_12_DrawPoint(unsigned short x,unsigned short y,u8 t)
{
unsigned short bx,temp=0;
unsigned short x_add,y_add;
if(x>127 ||y>63)return;
x_add = (x/8)*4 + 3-y/4;
y_add = y%4;
bx = x%8;
temp=1<<(bx);
if(t)
{
OLED_GRAM[x_add][y_add] |= temp;
}
else
{
OLED_GRAM[x_add][y_add] &= ~temp;
}
}
void OLED_ShowPos(unsigned short x,unsigned short y,u8 chr,u8 size,u8 mode)
{
unsigned short temp,t,t1;
unsigned short y0=y;
for(t=0;t<size;t++)
{
if(size==12)temp=asc2_1206[chr][t];
else temp=asc2_1608[chr][t];
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LED_DrawPoint(x,y,mode);
else LED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if((y-y0)==size)
{
y=y0;
x++;
break;
}
}
}
}
void OLED_ShowGBK(u8 x, u8 y, u8 num, u8 size,u8 mode)
{
u8 temp,t,t1;
u8 y0=y;
u8 csize=(size/8 + ((size%8)?1:0)) * size;
for(t=0;t<csize;t++)
{
if(size==16) temp = font_1616[num][t];
else return;
for(t1=0;t1<8;t1++)
{
if(temp&0x80)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if((y-y0)==size)
{
y=y0;
x++;
break;
}
}
}
}
.h
#ifndef _OLED_H
#define _OLED_H
#include <main.h>
#define SCL3_0 GPIO_ResetBits(GPIOB,GPIO_Pin_8)
#define SDA3_0 GPIO_ResetBits(GPIOB,GPIO_Pin_9)
#define SCL3_1 GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define SDA3_1 GPIO_SetBits(GPIOB,GPIO_Pin_9)
#define READ_SDA GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9)
extern volatile unsigned char Error_Flag;
void OLED_ShowGBK(u8 x, u8 y, u8 num, u8 size,u8 mode);
void LED_12_show_font(uint16_t x,uint16_t y,const uint8_t *font,uint8_t mode);
void I2C_Stop();
void LCD_Init3(void);
void OLED_Refresh_Gram(void);
void OLED_Clear(void);
void OLED_ShowString(u8 x,u8 y,const u8 *p);
void OLED_Processing(void);
void LED_DrawPoint(unsigned short x,unsigned short y,u8 t);
void OLED_ShowGBK12x8(u8 x, u8 y, u8 num, u8 size,u8 mode);
#endif
|