STM32F407库函数SPI模式OLED的oled.c函数 oled.h函数 main.c函数
时间: 2023-07-30 16:08:02 浏览: 170
以下是使用STM32F407库函数SPI模式控制OLED的示例代码:
oled.h:
```c
#ifndef __OLED_H
#define __OLED_H
#include "stm32f4xx.h"
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
#define OLED_CS_SET() GPIO_SetBits(GPIOB,GPIO_Pin_12)//片选信号置高
#define OLED_CS_CLR() GPIO_ResetBits(GPIOB,GPIO_Pin_12)//片选信号置低
#define OLED_DC_SET() GPIO_SetBits(GPIOB,GPIO_Pin_13)//数据/命令选择信号置高
#define OLED_DC_CLR() GPIO_ResetBits(GPIOB,GPIO_Pin_13)//数据/命令选择信号置低
#define OLED_RST_SET() GPIO_SetBits(GPIOB,GPIO_Pin_14)//复位信号置高
#define OLED_RST_CLR() GPIO_ResetBits(GPIOB,GPIO_Pin_14)//复位信号置低
void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t);
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode);
void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size);
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *p,uint8_t size);
void OLED_Set_Pos(uint8_t x,uint8_t y);
void OLED_WR_Byte(uint8_t dat,uint8_t cmd);
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no,uint8_t size);
void OLED_DrawBMP(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t BMP[]);
void OLED_ShowPicture(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t *ptr);
#endif
```
oled.c:
```c
#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"
//OLED缓存区
uint8_t OLED_GRAM[128][8];
//向OLED写入一个字节
void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
{
uint8_t i;
if(cmd) OLED_DC_SET();
else OLED_DC_CLR();
OLED_CS_CLR();
for(i=0;i<8;i++)
{
if(dat&0x80) GPIO_SetBits(GPIOB,GPIO_Pin_15);
else GPIO_ResetBits(GPIOB,GPIO_Pin_15);
GPIO_SetBits(GPIOB,GPIO_Pin_10);//时钟信号置高
dat<<=1; //准备写入下一位
GPIO_ResetBits(GPIOB,GPIO_Pin_10);//时钟信号置低
}
OLED_CS_SET();
OLED_DC_SET();
}
//OLED初始化
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
//PB12~14输出模式配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽型输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//速度100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
//PB15输出模式配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽型输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//速度100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
OLED_RST_SET();//复位信号置高
delay_ms(100);
OLED_RST_CLR();//复位信号置低
delay_ms(100);
OLED_RST_SET();//复位信号置高
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_Clear();//清屏
}
//OLED清屏函数
void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte(0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte(0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}
//OLED画点函数
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t)
{
uint8_t pos,bx,tmp=0;
if(x>127||y>63)return;//超出范围了.
pos=7-y/8;
bx=y%8;
tmp=1<<(7-bx);
if(t)OLED_GRAM[x][pos]|=tmp;
else OLED_GRAM[x][pos]&=~tmp;
OLED_Set_Pos(x,y);
OLED_WR_Byte(OLED_GRAM[x][pos],OLED_DATA);
}
//OLED显示字符函数
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode)
{
uint8_t temp,t,t1;
uint8_t y0=y;
uint8_t csize=(size/8+((size%8)?1:0))*(size/2);
chr=chr-' ';//得到偏移后的值
for(t=0;t<csize;t++)
{
if(size==12)temp=asc2_1206[chr][t]; //调用1206字体
else temp=asc2_1608[chr][t]; //调用1608字体
for(t1=0;t1<8;t1++)
{
if(temp&0x80)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if(y>=64)
{
y=0;
x++;
}
if(x>=128)return;//超出范围了
}
}
}
//OLED显示数字函数
void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size)
{
uint8_t t,temp;
uint8_t enshow=0;
for(t=0;t<len;t++)
{
temp=(num/10^(len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
}
}
//OLED显示字符串函数
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *p,uint8_t size)
{
while((*p<='~')&&(*p>=' '))//判断是不是非法字符!
{
if(x>120){x=0;y+=size;}
if(y>56){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,size,1);
x+=size/2;
p++;
}
}
//OLED设置位置函数
void OLED_Set_Pos(uint8_t x,uint8_t y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}
//OLED显示汉字函数
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no,uint8_t size)
{
uint8_t t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<size;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
}
OLED_Set_Pos(x,y+1);
for(t=0;t<size;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
}
}
//OLED显示BMP图片函数
void OLED_DrawBMP(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t BMP[])
{
uint32_t j=0;
uint8_t x,y;
if(y1%8==0) y=y1/8;
else y=y1/8+1;
for(y=y0;y<y1;y++)
{
OLED_Set_Pos(x0,y);
for(x=x0;x<x1;x++)
{
OLED_WR_Byte(BMP[j++],OLED_DATA);
}
}
}
//OLED显示图片函数
void OLED_ShowPicture(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t *ptr)
{
uint32_t j=0;
uint8_t x,y;
if(y1%8==0) y=y1/8;
else y=y1/8+1;
for(y=y0;y<y1;y++)
{
OLED_Set_Pos(x0,y);
for(x=x0;x<x1;x++)
{
OLED_WR_Byte(*(ptr+j++),OLED_DATA);
}
}
}
```
main.c:
```c
#include "oled.h"
int main(void)
{
OLED_Init(); //OLED初始化
OLED_ShowString(0,0,"Hello World!",12); //OLED显示字符串
while(1);
}
```
阅读全文