STM32 SPI通信--OLED
扫描二维码
随时随地手机看文章
一、0.96寸OLED
二、原理图
二、GPIO模拟SPI
1. 硬件连接
通过引脚和模块电路图可以分析出SPI的电路连接
OLED STM32
GND <----------> GND
VCC <----------> 3.3V
D0 <----------> PA4(CLK)
D1 <----------> PA3(MOSI)
RES <----------> PA2(RET复位)
DC <----------> PA1(命令|数据dc)
CS <----------> GND
2. 软件驱动
由OLED模块数据手册上SPI的操作时序图写出由GPIO口模拟的SPI驱动代码,模块只支持向模块写数据不能读数据,所以只需要写SPI发送即可
2.1 “SPI.h”
#define OLED_CMD 0 //命令声明
#define OLED_DATA 1 //数据声明
#define OLED_CLK PAout(4) // CLK时钟 d0
#define OLED_MOSI PAout(3) // MOSI d1
#define OLED_RST PAout(2) // RET复位 ret
#define OLED_DC PAout(1) // 命令|数据 dc (0表传输命令1表传输数据)
void OLED_SPI_Init(void); //配置MCU的SPI
void SPI_WriteByte(uint8_t addr,uint8_t data); //向寄存器地址写一个byte的数据
void WriteCmd(unsigned char cmd); //写命令
void WriteDat(unsigned char data); //写数据
2.2 “SPI.c”
void OLED_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);//使能PA端口时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //端口配置
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHz
GPIO_Init(GPIOA,&GPIO_InitStructure);//根据设定参数初始化GPIOA
}
void 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)
{
SPI_WriteByte(cmd,OLED_CMD);
}
void WriteData(unsigned char data)
{
SPI_WriteByte(data,OLED_DATA);
}
三、STM32对OLED模块的控制
3.1 指令集
3.2 “OLED.h”
void OLED_Init(void);//初始化OLED
void OLED_ON(void);//唤醒OLED
void OLED_OFF(void);//OLED休眠
void OLED_Refresh_Gram(void);//更新显存到OLED
void OLED_Clear(void);//清屏
void OLED_DrawPoint(u8 x,u8 y,u8 t);//画点
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);//填充
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode);//显示字符
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);//显示2个数字
void OLED_ShowString(u8 x,u8 y,const u8 *p,u8 size);//显示字符串
3.3 “OLED.c”
//OLED的显存
//存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
u8 OLED_GRAM[128][8];
void OLED_DLY_ms(unsigned int ms)
{
unsigned int a;
while(ms)
{
a=1335;
while(a--);
ms--;
}
}
void OLED_Init(void)
{
OLED_SPI_Init();
OLED_CLK = 1;
OLED_RST = 0;
OLED_DLY_ms(100);
OLED_RST = 1;
//从上电到下面开始初始化要有足够的时间,即等待RC复位完毕
WriteCmd(0xAE); // Display Off (0x00)
WriteCmd(0xD5);
WriteCmd(0x80); // Set Clock as 100 Frames/Sec
WriteCmd(0xA8);
WriteCmd(0x3F); // 1/64 Duty (0x0F~0x3F)
WriteCmd(0xD3);
WriteCmd(0x00); // Shift Mapping RAM Counter (0x00~0x3F)
WriteCmd(0x40 | 0x00); // Set Mapping RAM Display Start Line (0x00~0x3F)
WriteCmd(0x8D);
WriteCmd(0x10 | 0x04); // Enable Embedded DC/DC Converter (0x00/0x04)
WriteCmd(0x20);
WriteCmd(0x02); // Set Page Addressing Mode (0x00/0x01/0x02)
WriteCmd(0xA0 | 0x01); // Set SEG/Column Mapping
WriteCmd(0xC0); // Set COM/x Scan Direction
WriteCmd(0xDA);
WriteCmd(0x02 | 0x10); // Set Sequential Configuration (0x00/0x10)
WriteCmd(0x81);
WriteCmd(0xCF); // Set SEG Output Current
WriteCmd(0xD9);
WriteCmd(0xF1); // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
WriteCmd(0xDB);
WriteCmd(0x40); // Set VCOM Deselect Level
WriteCmd(0xA4 | 0x00); // Disable Entire Display On (0x00/0x01)
WriteCmd(0xA6 | 0x00); // Disable Inverse Display On (0x00/0x01)
WriteCmd(0xAE | 0x01); // Display On (0x01)
OLED_Clear(); //初始清屏
}
void OLED_ON(void)
{
WriteCmd(0X8D); //设置电荷泵
WriteCmd(0X14); //开启电荷泵
WriteCmd(0XAF); //OLED唤醒
}
void OLED_OFF(void)
{
WriteCmd(0X8D); //设置电荷泵
WriteCmd(0X10); //关闭电荷泵
WriteCmd(0XAE); //OLED休眠
}
void OLED_Refresh_Gram(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
WriteCmd(0xb0+i); //设置页地址(0~7)
WriteCmd(0x00); //设置显示位置—列低地址
WriteCmd(0x10); //设置显示位置—列高地址
for(n=0;n<128;n++)WriteData(OLED_GRAM[n][i]);
}
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;
OLED_Refresh_Gram();//更新显示
}
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_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();//更新显示
}
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-