目录
一、oled屏
可参考STM32—IIC详解入门(oled屏显示字)_wlkq~的博客-CSDN博客
上面链接为硬件IIC,下面代码是软件IIC
部分代码(oled相关代码)
void delay_us(uint32_t us)//微妙延时
{
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
while (delay--)
{
;
}
}
void iic_Start(void)//起始信号
{
SCL_LOW;
SDA_HIGH;
SCL_HIGH;
delay_us(5);
SDA_LOW;
delay_us(5);
}
void iic_Stop(void)//终止信号
{
SCL_HIGH;
SDA_LOW;
delay_us(5);
SDA_HIGH;
delay_us(5);
}
//应答
void iic_Ack()
{
SDA_LOW; //虚拟应答,因为OLEd屏幕坏了无应答,所以无需读引脚
SCL_HIGH;
SCL_LOW;
}
//iic发送一字节数据
void IIC_send_byte(uint8_t data)
{
SCL_LOW;
for(uint8_t i=0;i<8;i++){
if(data & 0x80){
SDA_HIGH;
}else{
SDA_LOW;
}
SCL_HIGH;
SCL_LOW;
data = data<< 1; /* 将下一位移至最高位 */
}
}
//OLED写指令
void oledWriteCmd(unsigned char writeCmd)
{
iic_Start();
IIC_send_byte(0x78);
iic_Ack();
IIC_send_byte(0x00);
iic_Ack();
IIC_send_byte(writeCmd);
iic_Ack();
iic_Stop();
}
//写数据
void Oled_Write_Data(unsigned char writedata)
{
iic_Start();//
IIC_send_byte(0x78);
iic_Ack();
IIC_send_byte(0x40);
iic_Ack();
IIC_send_byte(writedata);
iic_Ack();
iic_Stop();
}
//OLED清屏
void olceClean()
{
int i,j;
for(i=0;i<8;i++){
oledWriteCmd(0xB0 + i); //选择PAGE
oledWriteCmd(0x00); //选择列
oledWriteCmd(0x10);
for(j = 0;j < 128; j++){
Oled_Write_Data(0); //写入字符0
}
}
}
//OLCD初始化
void oledInit(void)
{
HAL_Delay(500);
oledWriteCmd(0xAE);
oledWriteCmd(0x00);
oledWriteCmd(0x10);
oledWriteCmd(0x40);
oledWriteCmd(0xB0);
oledWriteCmd(0x81);
oledWriteCmd(0xFF);
oledWriteCmd(0xA1);
oledWriteCmd(0xA6);
oledWriteCmd(0xA8);
oledWriteCmd(0x3F);
oledWriteCmd(0xC8);
oledWriteCmd(0xD3);
oledWriteCmd(0x00);
oledWriteCmd(0xD5);
oledWriteCmd(0x80);
oledWriteCmd(0xD8);
oledWriteCmd(0x05);
oledWriteCmd(0xD9);
oledWriteCmd(0xF1);
oledWriteCmd(0xDA);
oledWriteCmd(0x12);
oledWriteCmd(0xDB);
oledWriteCmd(0x30);
oledWriteCmd(0x8D);
oledWriteCmd(0x14);
oledWriteCmd(0xAF);
}
//以下代码厂家提供
void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
unsigned int i;
oledWriteCmd(0xb0+(row*2-2)); //page 0
oledWriteCmd(0x00+(col&0x0f)); //low
oledWriteCmd(0x10+(col>>4)); //high
for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
Oled_Write_Data(F8X16[i]); //写数据oledTable1
}
oledWriteCmd(0xb0+(row*2-1)); //page 1
oledWriteCmd(0x00+(col&0x0f)); //low
oledWriteCmd(0x10+(col>>4)); //high
for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
Oled_Write_Data(F8X16[i]); //写数据oledTable1
}
}
//以下代码厂家提供
/******************************************************************************/
// 函数名称:Oled_Show_Char
// 输入参数:oledChar
// 输出参数:无
// 函数功能:OLED显示单个字符
/******************************************************************************/
void Oled_Show_Str(char row,char col,char *str){//页、列、字符串
while(*str!=0){
Oled_Show_Char(row,col,*str);
str++;
col += 8;
}
}
二、DHT11温湿度传感器
1.总时序图(主机信号由代码设置波形,DHT信号由DHT11发出)
2.初始化部分 检测模块是否存在(检测下图DHT响应信号有没有被拉低)
部分代码初始化
//初始化
void DHT11_Start()
{
//下面是主机信号
DHT_GPIO_Init(GPIO_MODE_OUTPUT_PP);//PA5设置为输出推挽模式
DHT_HIGH;
DHT_LOW;
HAL_Delay(30);//ms
DHT_HIGH;
DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
//下面是DHT信号,如果符合DHT信号时序图就进行运行
while(DHT_VALUE);
//dht=0
while(!DHT_VALUE);
//dht=1
while(DHT_VALUE);//之后开始传输数据
//dht=0
}
3.接收数据部分(有效数据都是高电平,但持续时间不一样,可通过延时后是否被拉低来判断是0还是1)
DHT11传输0的时序分析
DHT11传输1的时序分析
4.数据格式
只有一根数据线DAT,发送序列指令给DHT11模块,模块就会传输一次完整的数据为40bit(8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据+8bit校验),高位先出。
部分代码数据接收
void DHT11_Read()
{
int i;
int j;
char tmp;//移位,存放8个bit
char flag;//标志位
DHT11_Start();//重启进入高速模式之后才发送bit
DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
for(i = 0;i < 5;i++){//取4个字节
for(j=0;j<8;j++){//每个字节取8个bit
while(!DHT_VALUE);//卡g点数据来会从0变成1
delay_us(40); //延时读取
if(DHT_VALUE == 1){
flag = 1;
while(DHT_VALUE);//等待变位70us下一个bit
}else{
flag = 0;
}
tmp = tmp << 1;//左移一位或者 tmp<<=1,为了使先出来的bit到高位
tmp |= flag;//可以为 tmp=tmp|flag
}
data[i] = tmp;//得到8个bit为一个字节,存放在数组里
}
}
三、实验(oled屏显示温湿度)
1.接线
oled:SCL->B5 SDA->B6 DH11:DAT->A5
2.配置
1.SYS
2.RCC
4.GPIO(全部设置为输出且输出高电平)A5既作为输入,也作为输出,则不能直接在CubeMX里配置,需要自己写代码。
3.代码
1.main.c代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "oled.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define DHT_LOW HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
#define DHT_HIGH HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
#define DHT_VALUE HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5)
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void DHT_GPIO_Init(uint32_t Mode)//初始化GPIO口,PA5口
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();//时钟
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = Mode;//输出还是输入
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void DHT11_Start()
{
//下面是主机信号
DHT_GPIO_Init(GPIO_MODE_OUTPUT_PP);//PA5设置为输出推挽模式
DHT_HIGH;
DHT_LOW;
HAL_Delay(30);//ms
DHT_HIGH;
DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
//下面是DHT信号,如果符合DHT信号时序图就进行运行
while(DHT_VALUE);
//dht=0
while(!DHT_VALUE);
//dht=1
while(DHT_VALUE);//之后开始传输数据
//dht=0
}
uint8_t data[5];
void DHT11_Read()
{
int i;
int j;
char tmp;//移位,存放8个bit
char flag;//标志位
DHT11_Start();//重启进入高速模式之后才发送bit
DHT_GPIO_Init(GPIO_MODE_INPUT);//PA5设置为输入模式
for(i = 0;i < 5;i++){//取4个字节
for(j=0;j<8;j++){//每个字节取8个bit
while(!DHT_VALUE);//卡g点数据来会从0变成1
delay_us(40); //延时读取
if(DHT_VALUE == 1){
flag = 1;
while(DHT_VALUE);//等待变位70us下一个bit
}else{
flag = 0;
}
tmp = tmp << 1;//左移一位或者 tmp<<=1,为了使先出来的bit到高位
tmp |= flag;//可以为 tmp=tmp|flag
}
data[i] = tmp;//得到8个bit为一个字节,存放在数组里
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
char SpeedMes[16];
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
oledInit();//OLED初始化
oledWriteCmd(0x20); //选择寻址方式
oledWriteCmd(0x02); //02是页寻址、00是水平寻址、01是垂直寻址模式
olceClean();//清屏
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
olceClean();
DHT11_Read();
sprintf(SpeedMes,"H:%d.%d",data[0], data[1]);//每一秒换一次 juli代替%d 然后将引号字符串放在数组里
Oled_Show_Str(1,1,SpeedMes);
sprintf(SpeedMes,"T:%d.%d",data[2], data[3]);
Oled_Show_Str(2,1,SpeedMes);
HAL_Delay(600);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2.oledfont.h头文件(需添加)
//
const unsigned char F8X16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
3.oled.c文件(需添加)
#include "main.h"
#include "gpio.h"
#include "oled.h"
#include "oledfont.h"
void delay_us(uint32_t us)
{
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
while (delay--)
{
;
}
}
void iic_Start(void)//起始信号
{
SCL_LOW;
SDA_HIGH;
SCL_HIGH;
delay_us(5);
SDA_LOW;
delay_us(5);
}
void iic_Stop(void)//终止信号
{
SCL_HIGH;
SDA_LOW;
delay_us(5);
SDA_HIGH;
delay_us(5);
}
//应答
void iic_Ack()
{
SDA_LOW; //虚拟应答,因为OLEd屏幕坏了无应答
SCL_HIGH;
SCL_LOW;
}
//iic发送一字节数据
void IIC_send_byte(uint8_t data)
{
SCL_LOW;
for(uint8_t i=0;i<8;i++){
if(data & 0x80){
SDA_HIGH;
}else{
SDA_LOW;
}
SCL_HIGH;
SCL_LOW;
data = data<< 1; /* 将下一位移至最高位 */
}
//SCL_HIGH; /* 发送完成,释放数据线*/
}
//OLED写指令
void oledWriteCmd(unsigned char writeCmd)
{
iic_Start();
IIC_send_byte(0x78);
iic_Ack();
IIC_send_byte(0x00);
iic_Ack();
IIC_send_byte(writeCmd);
iic_Ack();
iic_Stop();
}
//写数据
void Oled_Write_Data(unsigned char writedata)
{
iic_Start();//
IIC_send_byte(0x78);
iic_Ack();
IIC_send_byte(0x40);
iic_Ack();
IIC_send_byte(writedata);
iic_Ack();
iic_Stop();
}
//OLED清屏
void olceClean()
{
int i,j;
for(i=0;i<8;i++){
oledWriteCmd(0xB0 + i); //选择PAGE
oledWriteCmd(0x00); //选择列
oledWriteCmd(0x10);
for(j = 0;j < 128; j++){
Oled_Write_Data(0); //写入字符0
}
}
}
//OLCD初始化
void oledInit(void)
{
HAL_Delay(500);
oledWriteCmd(0xAE);
oledWriteCmd(0x00);
oledWriteCmd(0x10);
oledWriteCmd(0x40);
oledWriteCmd(0xB0);
oledWriteCmd(0x81);
oledWriteCmd(0xFF);
oledWriteCmd(0xA1);
oledWriteCmd(0xA6);
oledWriteCmd(0xA8);
oledWriteCmd(0x3F);
oledWriteCmd(0xC8);
oledWriteCmd(0xD3);
oledWriteCmd(0x00);
oledWriteCmd(0xD5);
oledWriteCmd(0x80);
oledWriteCmd(0xD8);
oledWriteCmd(0x05);
oledWriteCmd(0xD9);
oledWriteCmd(0xF1);
oledWriteCmd(0xDA);
oledWriteCmd(0x12);
oledWriteCmd(0xDB);
oledWriteCmd(0x30);
oledWriteCmd(0x8D);
oledWriteCmd(0x14);
oledWriteCmd(0xAF);
}
//以下代码厂家提供
void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
unsigned int i;
oledWriteCmd(0xb0+(row*2-2)); //page 0
oledWriteCmd(0x00+(col&0x0f)); //low
oledWriteCmd(0x10+(col>>4)); //high
for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
Oled_Write_Data(F8X16[i]); //写数据oledTable1
}
oledWriteCmd(0xb0+(row*2-1)); //page 1
oledWriteCmd(0x00+(col&0x0f)); //low
oledWriteCmd(0x10+(col>>4)); //high
for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
Oled_Write_Data(F8X16[i]); //写数据oledTable1
}
}
//以下代码厂家提供
/******************************************************************************/
// 函数名称:Oled_Show_Char
// 输入参数:oledChar
// 输出参数:无
// 函数功能:OLED显示单个字符
/******************************************************************************/
void Oled_Show_Str(char row,char col,char *str){//页、列、字符串
while(*str!=0){
Oled_Show_Char(row,col,*str);
str++;
col += 8;
}
}
4.oled.h文件(需添加)
#define SCL_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET)//SCL高
#define SCL_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET)
#define SDA_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET)//SDA高
#define SDA_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
void olceClean(void);
void oledInit(void);
void Oled_Show_Str(char row,char col,char *str);
void oledWriteCmd(unsigned char writeCmd);
void delay_us(unsigned int us);
四、工程添加.c和.h文件可以参考
STM32—SPI详解入门(使用SPI通讯读写W25Q128模块)_spi读写寄存器_wlkq~的博客-CSDN博客文章里面的第九、STM32工程添加.c和.h文件