STM32 4SPI模式点OLED(SSD1306驱动)屏

参考资料
STM32学习笔记之OLED屏
OLED显示模块(原理讲解、STM32实例操作)
正点原子OLED教学视频

#####占坑待编辑#####
SPI使用逻辑分析仪抓的波形分析:【此模块没有CS信号】
》》写配置字:0xAE
DC首先拉低,
我们在此处认为SCLK的上升沿为触发,读出SDIN在此DC低电平周期的值为0xAE。
然后DC置高,写配置周期结束。
写配置字0xAE
》》写数据:0xAA
和配置字不同的区别为DC不需要拉低。
写数据0xAA

oled_diy_display() 方法调试用,用于验证OLED显示方式
#####占坑待编辑#####
STM32 4SPI模式点OLED(SSD1306驱动)屏_第1张图片
STM32 4SPI模式点OLED(SSD1306驱动)屏_第2张图片
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//oled 初始化
void oled_init(void){
	oled_gpio_init();

	OLED_RES_LOW();
	systick_delay(100);
	OLED_RES_HIGH();
	SPI_DELAY(SPI_DELAY_CNT);

	oled_set_config(0xAE);
	oled_set_config(0xD5);
	oled_set_config(0x80);
	oled_set_config(0xA8);
	oled_set_config(0x3F);
	oled_set_config(0xD3);
	oled_set_config(0x00);
	oled_set_config(0x40);
	oled_set_config(0x8D);
	oled_set_config(0x14);
	oled_set_config(0xA1);
	oled_set_config(0xC8);
	oled_set_config(0xDA);
	oled_set_config(0x12);
	oled_set_config(0x81);
	oled_set_config(0x66);
	oled_set_config(0xD9);
	oled_set_config(0x22);
	oled_set_config(0xDB);
	oled_set_config(0x30);
	oled_set_config(0xA4);
	oled_set_config(0xA6);
	oled_clear();
	SPI_DELAY(SPI_DELAY_CNT);
	oled_set_config(0xAF);
	systick_delay(100);
}

#define OLED_RES_HIGH()							OLED_RES_GRP->BSRR = OLED_RES_PIN
#define OLED_RES_LOW()							OLED_RES_GRP->BRR = OLED_RES_PIN
#define OLED_SDIN_HIGH()						OLED_SDIN_GRP->BSRR = OLED_SDIN_PIN
#define OLED_SDIN_LOW()							OLED_SDIN_GRP->BRR = OLED_SDIN_PIN
#define OLED_SCLK_HIGH()						OLED_SCLK_GRP->BSRR = OLED_SCLK_PIN
#define OLED_SCLK_LOW()							OLED_SCLK_GRP->BRR = OLED_SCLK_PIN
#define OLED_DC_HIGH()							OLED_DC_GRP->BSRR = OLED_DC_PIN
#define OLED_DC_LOW()							OLED_DC_GRP->BRR = OLED_DC_PIN

#define SPI_DELAY_CNT							4
#define	SPI_DELAY(cnt)							{u8 tmpcnt = cnt;while(tmpcnt--){ \
	asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); \
	asm("NOP");asm("NOP"); \
}}

//写配置
void oled_set_config(u8 cmd){
	u8 i;
	OLED_DC_LOW();
	for(i=0; i<8; i++){
		OLED_SCLK_LOW();
		if(cmd&0x80){
			OLED_SDIN_HIGH();
		}else{
			OLED_SDIN_LOW();
		}
		OLED_SCLK_HIGH();
		cmd<<=1;
	}
	OLED_DC_HIGH();
}

//写数据
void oled_sent_data(u8 dat){
	u8 i;
	OLED_DC_HIGH();
	for(i=0; i<8; i++){
		OLED_SCLK_LOW();
		if(dat&0x80){
			OLED_SDIN_HIGH();
		}else{
			OLED_SDIN_LOW();
		}
		OLED_SCLK_HIGH();
		dat<<=1;
	}
	OLED_DC_HIGH();
}


//清屏
void oled_clear(void){
	u8 i, n;
	for(i=0; i<8; i++){		//page
		for(n=0; n<128; n++){
			g_oled_gram[n][i] = 0x00;
		}
	}
	oled_refresh_gram();
}

//自定义显示 验证想法
void oled_diy_display(u8 dat){
	u8 i, n;
	for(i=0; i<8; i++){		//page
		for(n=0; n<128; n++){
			g_oled_gram[n][i] = dat;
		}
	}
	oled_refresh_gram();
}


//刷新屏幕显示
void oled_refresh_gram(void){
	u8 i, n;
	for(i=0; i<8; i++){
		oled_set_config(0xB0+i);	//page
		oled_set_config(0x00);		//列低地址
		oled_set_config(0x10);		//列高地址
		for(n=0; n<128; n++){
			oled_sent_data(g_oled_gram[n][i]);
		}
	}
}

源代码下载:[STM32_SPI_OLED.zip] (https://download.csdn.net/download/wodenidejingaide/10879219)

我设置的积分应该是2分,如果发现积分异常(CSDN后台自动改高),可以留言联系我私发给你

你可能感兴趣的:(单片机,C语言,stm32)