初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-24
|
思考了一下午,没有想出头绪,请各位大神不吝赐教,感激不尽,这是野火STM32迷你板驱动OLED 的SPI方式 ,不知哪里有问题
#include "oled_spi.h"
#include "codetab.h"
#include "delay.h"
void OLED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*Config gpio SCL=PA4,SDA=PA5,RST=PA6,DC=PA7*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void OLED_SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4 ;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge ;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode =SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void OLED_Config(void)
{
OLED_GPIO_Config();
OLED_SPI_Config();
}
/*写数据*/
/*SCL=PA4,SDA=PA5,RST=PA6,DC=PA7*/
void OLED_WrDat(unsigned char data)
{
int i= 8;
GPIO_SetBits(GPIOA, GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while(i--)
{
if(data&0x80)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
}
GPIO_SetBits(GPIOA, GPIO_Pin_4);
DelayUs(4);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
data<<=1;
}
}
/*SCL=PA4,SDA=PA5,RST=PA6,DC=PA7*/
void OLED_WrCmd(unsigned char cmd)
{
int i = 8;
GPIO_ResetBits(GPIOA, GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while(i--)
{
if(cmd&0x80)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
}
GPIO_SetBits(GPIOA, GPIO_Pin_4);
DelayUs(4);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
cmd<<=1;
}
}
/*SCL=PA4,SDA=PA5,RST=PA6,DC=PA7*/
void OLED_Init(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
DelayMs(100); //这里的延时很重要
GPIO_SetBits(GPIOA, GPIO_Pin_6);
OLED_WrCmd(0xAE); //display off
OLED_WrCmd(0x20); //Set Memory Addressing Mode
OLED_WrCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
OLED_WrCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
OLED_WrCmd(0xc8); //Set COM Output Scan Direction
OLED_WrCmd(0x21); //set column address
OLED_WrCmd(0x00); //---set low column address
OLED_WrCmd(0x10); //---set high column address
OLED_WrCmd(0x40); //--set start line address 设置显示开始行
OLED_WrCmd(0x81); //--set contrast control register
OLED_WrCmd(0xff); //亮度调节 0x00~0xff
OLED_WrCmd(0xa0); //--set segment re-map 0 to 127
OLED_WrCmd(0xa6); //--set normal display
OLED_WrCmd(0xa8); //--set multiplex ratio(1 to 64)
OLED_WrCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
OLED_WrCmd(0xd3); //-set display offset
OLED_WrCmd(0xd5); //--set display clock divide ratio/oscillator frequency
OLED_WrCmd(0xd9); //--set pre-charge period
OLED_WrCmd(0x22); //--set page address
OLED_WrCmd(0xda); //--set com pins hardware configuration
OLED_WrCmd(0xdb); //--set Vcomh deselect level
OLED_WrCmd(0x2e); //deactivate scroll
OLED_WrCmd(0x2f);//Activate scroll
OLED_WrCmd(0xa3);//set vertical scroll area
OLED_WrCmd(0x8d); //--set DC-DC enable 电荷泵设置
OLED_WrCmd(0x14); //bit2,开启/关闭
OLED_WrCmd(0xaf); //--turn on oled panel
OLED_Fill(0x00);
OLED_SetPos(0, 0);
}
/**
* @brief OLED_SetPos,设置光标
* @param x,光标x位置
* y,光标y位置
* @retval 无
*/
void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
OLED_WrCmd(0xb0+y);
OLED_WrCmd(((x&0xf0)>>4)|0x10);
OLED_WrCmd((x&0x0f)|0x01);
}
/**
* @brief OLED_Fill,填充整个屏幕
* @param fill_Data:要填充的数据
* @retval 无
*/
void OLED_Fill(unsigned char fill_Data)//全屏填充
{
unsigned char m,n;
for(m=0;m<8;m++)
{
OLED_WrCmd(0xb0+m); //page0-page1
OLED_WrCmd(0x00); //low column start address
OLED_WrCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
OLED_WrDat(fill_Data);
}
}
}
/**
* @brief OLED_CLS,清屏
* @param 无
* @retval 无
*/
void OLED_CLS(unsigned char fill_Data)//清屏
{
unsigned char m,n;
for(m=0;m<8;m++)
{
OLED_WrCmd(0xb0+m); //page0-page1
OLED_WrCmd(0x00); //low column start address
OLED_WrCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
OLED_WrDat(fill_Data);
}
}
}
/**
* @brief OLED_ON,将OLED从休眠中唤醒
* @param 无
* @retval 无
*/
void OLED_ON(void)
{
OLED_WrCmd(0X8D); //设置电荷泵
OLED_WrCmd(0X14); //开启电荷泵
OLED_WrCmd(0XAF); //OLED唤醒
}
/**
* @brief OLED_OFF,让OLED休眠 -- 休眠模式下,OLED功耗不到10uA
* @param 无
* @retval 无
*/
void OLED_OFF(void)
{
OLED_WrCmd(0X8D); //设置电荷泵
OLED_WrCmd(0X10); //关闭电荷泵
OLED_WrCmd(0XAE); //OLED休眠
}
/**
* @brief OLED_ShowStr,显示codetab.h中的ASCII字符,有6*8和8*16可选择
* @param x,y : 起始点坐标(x:0~127, y:0~7);
* ch[] :- 要显示的字符串;
* TextSize : 字符大小(1:6*8 ; 2:8*16)
* @retval 无
*/
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 > 126)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
OLED_WrCmd(F6x8[c][i]);
x += 6;
j++;
}
}break;
case 2:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
OLED_WrCmd(F8X16[c*16+i]);
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
OLED_WrCmd(F8X16[c*16+i+8]);
x += 8;
j++;
}
}break;
}
}
/**
* @brief OLED_ShowCN,显示codetab.h中的汉字,16*16点阵
* @param x,y: 起始点坐标(x:0~127, y:0~7);
* N:汉字在codetab.h中的索引
* @retval 无
*/
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
unsigned char wm=0;
unsigned int adder=32*N;
OLED_SetPos(x , y);
for(wm = 0;wm < 16;wm++)
{
OLED_WrCmd(F16x16[adder]);
adder += 1;
}
OLED_SetPos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
OLED_WrCmd(F16x16[adder]);
adder += 1;
}
}
/**
* @brief OLED_DrawBMP,显示BMP位图
* @param x0,y0 :起始点坐标(x0:0~127, y0:0~7);
* x1,y1 : 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
* @retval 无
*/
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0)
y = y1/8;
else
y = y1/8 + 1;
for(y=y0;y<y1;y++)
{
OLED_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
OLED_WrCmd(BMP[j++]);
}
}
}
|
|