一、SPI协议
1. SPI简介
SPI是串行外设接口(Serial Peripheral Interface)的缩写。SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,正是出于这种简单易用的特性,如今越来越多的芯片集成了这种通信协议,比如AT91RM9200。
2. SPI通信原理
SPI的通信原理很简单,它以主从方式工作,这种模式通常有一个主设备和一个或多个从设备,需要至少4根线,事实上3根也可以(用于单向传输时,也就是半双工方式)。也是所有基于SPI的设备共有的,它们是SDI(数据输入)、SDO(数据输出)、SCLK(时钟)、CS(片选)。
(1)MOSI– SPI总线主机输出/ 从机输入(SPI Bus Master Output/Slave Input);
(2)MISO– SPI总线主机输入/ 从机输出(SPI Bus Master Input/Slave Output);
(3)SCLK –时钟信号,由主设备产生;
(4)CS – 从设备使能信号,由主设备控制(Chip select),有的IC此pin脚叫SS。
SPI框图如下:
其中CS是控制芯片是否被选中的,也就是说只有片选信号为预先规定的使能信号时(高电位或低电位),对此芯片的操作才有效。这就允许在同一总线上连接多个SPI设备成为可能。
接下来就负责通讯的3根线了。通讯是通过数据交换完成的,这里先要知道SPI是串行通讯协议,也就是说数据是一位一位的传输的。这就是SCLK时钟线存在的原因,由SCK提供时钟脉冲,SDI,SDO则基于此脉冲完成数据传输。数据输出通过 SDO线,数据在时钟上升沿或下降沿时改变,在紧接着的下降沿或上升沿被读取。完成一位数据传输,输入也使用同样原理。这样,在至少8次时钟信号的改变(上沿和下沿为一次),就可以完成8位数据的传输。
在点对点的通信中,SPI接口不需要进行寻址操作,且为全双工通信,显得简单高效。在多个从设备的系统中,每个从设备需要独立的使能信号,硬件上比I2C系统要稍微复杂一些。
3. GPIO口配置
关于SPI的具体工作过程及原理可以参考STM32中文参考手册
链接:https://pan.baidu.com/s/1F2fgQOWyiUYAc2z6TuCPSQ 提取码:3an4
二、OLED原理及应用
1. OLED原理
有机电致发光器件(OLED)属于低电压、高电流的双注人式发光器件,具有发光二极管的性质,所以有机电致发光器件又称之为有机电致发光二极管。当给器件施加一正向的外加偏压后,电子和空穴克服界面势垒,经由阴极和阳极注人到有机材料中,在外加电场的作用下迁移至发光层,在发光层内,当电子和空穴相遇时,由于库仑力的相互作用而形成暂态激子,由于暂态激子具有较高的能量处于不稳定态,所以一小部分激子可以通过晶格振动,将一部分能量传递给声子而消耗掉,另外的激子则发生复合,最终电子落人空穴,同时向外释放出一定的能量,而发光材料原子的最外层电子吸收这些能量后将处于激发态,当激发态的电子跃迁至基态时,向外辐射出光子,从而就产生了电致发光现象。有机电致发光器件的发光原理可以很简单的用三个步骤来说明,如图所示
)]
7针0.96英寸SPI OLED屏如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EVspQHWm-1637940303805)( )]
OLED引脚与STM32的连接如下:
2. OLED显示
OLED主要函数
olde.h
#ifndef __OLED_H
#define __OLED_H
#include "sys.h"
#define PAGE_SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define YLevel 0xB0
#define Brightness 0xFF
#define WIDTH 128
#define HEIGHT 64
#define OLED_CMD 0
#define OLED_DATA 1
#define OLED_CS GPIO_Pin_11
#define OLED_DC GPIO_Pin_10
#define OLED_RST GPIO_Pin_12
#define OLED_CS_Clr() GPIO_ResetBits(GPIOB,OLED_CS)
#define OLED_CS_Set() GPIO_SetBits(GPIOB,OLED_CS)
#define OLED_DC_Clr() GPIO_ResetBits(GPIOB,OLED_DC)
#define OLED_DC_Set() GPIO_SetBits(GPIOB,OLED_DC)
#define OLED_RST_Clr() GPIO_ResetBits(GPIOB,OLED_RST)
#define OLED_RST_Set() GPIO_SetBits(GPIOB,OLED_RST)
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_Reset(void);
void OLED_Init_GPIO(void);
void OLED_Init(void);
void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color);
void OLED_Display(void);
void OLED_Clear(unsigned dat);
#endif
olde.c
#include "oled.h"
#include "stdlib.h"
#include "string.h"
#include "delay.h"
#include "spi.h"
static unsigned char OLED_buffer[1024] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
OLED_DC_Set();
}
else
{
OLED_DC_Clr();
}
OLED_CS_Clr();
SPI_WriteByte(dat);
OLED_CS_Set();
}
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(YLevel+y/PAGE_SIZE,OLED_CMD);
OLED_WR_Byte((((x+2)&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte(((x+2)&0x0f),OLED_CMD);
}
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD);
OLED_WR_Byte(0X14,OLED_CMD);
OLED_WR_Byte(0XAF,OLED_CMD);
}
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD);
OLED_WR_Byte(0X10,OLED_CMD);
OLED_WR_Byte(0XAE,OLED_CMD);
}
void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color)
{
if(color)
{
OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]|= (1<<(y%PAGE_SIZE))&0xff;
}
else
{
OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]&= ~((1<<(y%PAGE_SIZE))&0xff);
}
}
void OLED_Display(void)
{
u8 i,n;
for(i=0;i<PAGE_SIZE;i++)
{
OLED_WR_Byte (YLevel+i,OLED_CMD);
OLED_WR_Byte (XLevelL,OLED_CMD);
OLED_WR_Byte (XLevelH,OLED_CMD);
for(n=0;n<WIDTH;n++)
{
OLED_WR_Byte(OLED_buffer[i*WIDTH+n],OLED_DATA);
}
}
}
void OLED_Clear(unsigned dat)
{
if(dat)
{
memset(OLED_buffer,0xff,sizeof(OLED_buffer));
}
else
{
memset(OLED_buffer,0,sizeof(OLED_buffer));
}
OLED_Display();
}
void OLED_Reset(void)
{
OLED_RST_Set();
delay_ms(100);
OLED_RST_Clr();
delay_ms(100);
OLED_RST_Set();
}
void OLED_Init_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_15);
}
void OLED_Init(void)
{
OLED_Init_GPIO();
delay_ms(200);
OLED_Reset();
OLED_WR_Byte(0xAE,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0xB0,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xFF,OLED_CMD);
OLED_WR_Byte(0xA1,OLED_CMD);
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_WR_Byte(0xA8,OLED_CMD);
OLED_WR_Byte(0x3F,OLED_CMD);
OLED_WR_Byte(0xC8,OLED_CMD);
OLED_WR_Byte(0xD3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xD5,OLED_CMD);
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD);
OLED_WR_Byte(0XF1,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD);
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);
OLED_WR_Byte(0x30,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD);
}
2.1 姓名学号显示
①准备工作及部分代码
我们需要先下载一个PCtoLCD2002完美版以获得相应汉字的点阵编码。下载好后设置为顺向取模,如图:
设置好后,在输入框输入汉字,点击生成字模,如图所示:
将汉字的点阵编码加入到oledfont.h文件中,由于取的是16×16的,所以加入到typFNT_GB16 cfont16中,如图:
姓名学号显示主要代码(其中已经编写好的GUI_ShowString()函数和GUI_ShowCHinses()函数用来显示数字和汉字):
int main(void)
{
delay_init();
NVIC_Configuration();
IIC_Init();
OLED_Init();
OLED_Clear(0);
while(1){
GUI_ShowCHinese(28,0,16,"通信工程",1);
GUI_ShowCHinese(28,20,16,"麦甜",1);
GUI_ShowString(4,48,"631907030617",16,1);
delay_ms(1500);
delay_ms(1500);
}
}
②实验结果
2.2 滚屏显示
①代码
int main(void)
{
delay_init();
NVIC_Configuration();
IIC_Init();
OLED_Init();
OLED_Clear(0);
OLED_WR_Byte(0x2e,OLED_CMD);
OLED_WR_Byte(0x26,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x07,OLED_CMD);
OLED_WR_Byte(0x09,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xFF,OLED_CMD);
GUI_ShowCHinese(28,0,16,"萤火一万年",1);
OLED_WR_Byte(0x2f,OLED_CMD);
delay_ms(1500);
}
②实验结果
2.3 温湿度显示
①代码
int main(void)
{
delay_init();
NVIC_Configuration();
uart_init(115200);
IIC_Init();
OLED_Init();
OLED_Clear(0);
while(1){
read_AHT20_once();
GUI_DrawLine(0, 10, WIDTH-1, 10,1);
GUI_DrawLine(WIDTH/2-1,11,WIDTH/2-1,HEIGHT-1,1);
GUI_DrawLine(WIDTH/2-1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1);
GUI_ShowString(0,1,"2021-11-24",8,1);
GUI_ShowString(70,1,"Wednesday",8,1);
GUI_ShowString(14,HEIGHT-1-10,"Cloudy",8,1);
GUI_ShowString(WIDTH/2+1,13,"TEMP",8,1);
GUI_DrawCircle(WIDTH-20, 25, 1,2);
GUI_ShowString(WIDTH-15,20,"C",16,1);
GUI_ShowNum(WIDTH/2+8,20,T1/10,2,16,1);
GUI_ShowString(WIDTH-41,26,".",8,1);
GUI_ShowNum(WIDTH-35,20,T1%10,1,16,1);
GUI_ShowString(WIDTH/2+1,39,"HUM",8,1);
GUI_ShowNum(WIDTH/2+8,46,H1/10,2,16,1);
GUI_ShowString(WIDTH-41,52,".",8,1);
GUI_ShowNum(WIDTH-35,46,H1%10,1,16,1);
GUI_ShowString(WIDTH-21,46,"%",16,1);
GUI_DrawBMP(6,16,51,32, BMP5, 1);
delay_ms(2000);
}
}
②实验结果
可以看到用手捏住温湿度传感器温度变大了。
下面附上全部实验的完整代码 链接:https://pan.baidu.com/s/1HWfx6APfdpOuPyeimmDWTQ 提取码:3an4
三、总结
经过本次实验对嵌入式有了更深的理解,也学到了很多东西,通过软件模拟的方式实现了IIC和SPI协议。也了解到OLED身上具有许多优点,比如OLED器件单个像素可以相当小,非常适合应用在微显示设备中;具有快速响应特性;发光转化效率高等。
四、参考文献
https://baike.sogou.com/kexue/d29012153642006538.htm?ch=fromsearch
https://blog.csdn.net/qq_41675500/article/details/121529496?spm=1001.2014.3001.5501
|