STM32 SPI通信--OLED
一、0.96寸OLED
二、原理圖
二、GPIO模擬SPI
1. 硬件連接
通過(guò)引腳和模塊電路圖可以分析出SPI的電路連接
OLED STM32
GND <----------> GND
VCC <----------> 3.3V
D0 <----------> PA4(CLK)
D1 <----------> PA3(MOSI)
RES <----------> PA2(RET復(fù)位)
DC <----------> PA1(命令|數(shù)據(jù)dc)
CS <----------> GND
2. 軟件驅(qū)動(dòng)
由OLED模塊數(shù)據(jù)手冊(cè)上SPI的操作時(shí)序圖寫(xiě)出由GPIO口模擬的SPI驅(qū)動(dòng)代碼,模塊只支持向模塊寫(xiě)數(shù)據(jù)不能讀數(shù)據(jù),所以只需要寫(xiě)SPI發(fā)送即可
2.1 “SPI.h”
#define OLED_CMD 0 //命令聲明
#define OLED_DATA 1 //數(shù)據(jù)聲明
#define OLED_CLK PAout(4) // CLK時(shí)鐘 d0
#define OLED_MOSI PAout(3) // MOSI d1
#define OLED_RST PAout(2) // RET復(fù)位 ret
#define OLED_DC PAout(1) // 命令|數(shù)據(jù) dc (0表傳輸命令1表傳輸數(shù)據(jù))
void OLED_SPI_Init(void); //配置MCU的SPI
void SPI_WriteByte(uint8_t addr,uint8_t data); //向寄存器地址寫(xiě)一個(gè)byte的數(shù)據(jù)
void WriteCmd(unsigned char cmd); //寫(xiě)命令
void WriteDat(unsigned char data); //寫(xiě)數(shù)據(jù)
2.2 “SPI.c”
void OLED_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);//使能PA端口時(shí)鐘
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);//根據(jù)設(shè)定參數(shù)初始化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對(duì)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);//畫(huà)點(diǎn)
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個(gè)數(shù)字
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;
//從上電到下面開(kāi)始初始化要有足夠的時(shí)間,即等待RC復(fù)位完畢
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); //設(shè)置電荷泵
WriteCmd(0X14); //開(kāi)啟電荷泵
WriteCmd(0XAF); //OLED喚醒
}
void OLED_OFF(void)
{
WriteCmd(0X8D); //設(shè)置電荷泵
WriteCmd(0X10); //關(guān)閉電荷泵
WriteCmd(0XAE); //OLED休眠
}
void OLED_Refresh_Gram(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
WriteCmd(0xb0+i); //設(shè)置頁(yè)地址(0~7)
WriteCmd(0x00); //設(shè)置顯示位置—列低地址
WriteCmd(0x10); //設(shè)置顯示位置—列高地址
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); //得到字體一個(gè)字符對(duì)應(yīng)點(diǎn)陣集所占的字節(jié)數(shù)
chr=chr-