OLED.c
#include "OLED.h"
#include "delay.h"
#include "codetab.h"
void OLED_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void OLED_SPI_WriteByte(unsigned char data,unsigned char cmd)
{
unsigned char i=0;
OLED_DC =cmd;
// OLED_CLK=0;
for(i=0;i<8;i++)
{
OLED_CLK=0;
if(data&0x80)OLED_MOSI=1;
else OLED_MOSI=0;
OLED_CLK=1;
data<<=1;
}
// OLED_CLK=1;
// OLED_DC=1;
}
void WriteCmd(unsigned char cmd)
{
OLED_SPI_WriteByte(cmd,OLED_CMD);
}
void WriteData(unsigned char data)
{
OLED_SPI_WriteByte(data,OLED_DATA);
}
//OLED_SPIÄ£ÄaSPI3ìDòíê±Ï
/****************************************************************************************/
void OLED_Init(void)
{
OLED_SPI_Init();
OLED_CLK = 1;
OLED_RST = 0;
delay_ms(1000);
OLED_RST = 1;
WriteCmd(0xAE); //display off
WriteCmd(0x20); //Set Memory Addressing Mode
WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
WriteCmd(0xc8); //Set COM Output Scan Direction
WriteCmd(0x00); //---set low column address
WriteCmd(0x10); //---set high column address
WriteCmd(0x40); //--set start line address
WriteCmd(0x81); //--set contrast control register
WriteCmd(0xff); //l亮度0x00~0xff
WriteCmd(0xa1); //--set segment re-map 0 to 127
WriteCmd(0xa6); //--set normal display
WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
WriteCmd(0x3F); //
WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
WriteCmd(0xd3); //-set display offset
WriteCmd(0x00); //-not offset
WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
WriteCmd(0xf0); //--set divide ratio
WriteCmd(0xd9); //--set pre-charge period
WriteCmd(0x22); //
WriteCmd(0xda); //--set com pins hardware configuration
WriteCmd(0x12);
WriteCmd(0xdb); //--set vcomh
WriteCmd(0x20); //0x20,0.77xVcc
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
OLED_SetPos(0,0);
}
void OLED_ON(void)//½«OLED′óDYÃßÖD»½DÑ
{
WriteCmd(0x8D);
WriteCmd(0x14);
WriteCmd(0xAF);
}
void OLED_OFF(void)
{
WriteCmd(0X8D);
WriteCmd(0X10);
WriteCmd(0XAE);
}
void OLED_SetPos(unsigned char x, unsigned char y)
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
void OLED_Fill(unsigned char fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
WriteCmd(0xb0+m); //page0-page1
WriteCmd(0x00); //low column start address
WriteCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
WriteData(fill_Data);
}
}
}
void OLED_CLS(void)
{
OLED_Fill(0x00);
}
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
unsigned char c = 0,i = 0,j = 0;
switch(TextSize)
{
case 1:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 121)//126
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
WriteData(F6x8[c][ i]);[ i]
x += 6;
j++;
}
}break;
case 2:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 119)//120
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
WriteData(F8X16[c*16+i]);
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
WriteData(F8X16[c*16+i+8]);
x += 8;
j++;
}
}break;
}
}
void OLED_ShowNum(unsigned char x, unsigned char y, float num, unsigned char TextSize)
{
unsigned char i;
unsigned char result[9];
unsigned int tempH;
unsigned char tempL;
result[8]='\0';
result[3]='.';
if(num<0)
{
result[0]='-';
num=-num;
}
else result[0]=' ';
tempH = (unsigned int)num;
tempL = (unsigned char)(num*100 - tempH*100);
result[2] = tempH%10 + '0';tempH/=10;
result[1] = tempH + '0';
result[7] = tempL%10 + '0';tempL/=10;
result[6] = tempL%10 + '0';tempL/=10;
result[5] = tempL%10 + '0';tempL/=10;
result[4] = tempL + '0';
if(result[1]=='0')result[0]=' ';
for(i=7;i<4;i--)
{
if(result[ i]=='0')[ i]
{
if(i==7)
result[ i]=' ';
else if(i==6)
{
if(result[7]=='0')result[ i]=' ';
else
result[ i]='0';
}
else if(i==5)
{
if(result[7]=='0'& result[6]=='0')result[ i]=' ';
else
result[ i]='0';
}
else if(i==4)
{
if(result[7]=='0'& result[6]=='0' & result[5]=='0')result[ i]=' ';
else
result[ i]='0';
}
}
else
break;
}
OLED_ShowStr(x,y,result,TextSize);
}
OLED.h
#ifndef __OLED_H
#define __OLED_H
#include "sys.h"
#define OLED_CMD 0
#define OLED_DATA 1
#define OLED_CLK PBout(7) // CLK
#define OLED_MOSI PBout(6) // SDA(MOSI)
#define OLED_RST PBout(4) // RES
#define OLED_DC PBout(5)
void OLED_SPI_Init(void);
void OLED_SPI_WriteByte(unsigned char data,unsigned char cmd);
void WriteCmd(unsigned char cmd);
void WriteData(unsigned char data);
void OLED_Init(void);
void OLED_ON(void);
void OLED_OFF(void);
void OLED_SetPos(unsigned char x, unsigned char y);
void OLED_Fill(unsigned char fill_Data);
void OLED_CLS(void);
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize);
void OLED_ShowNum(unsigned char x, unsigned char y, float num, unsigned char TextSize);
#endif |