OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统

空气检测系统

  • 概要
  • 空气检测系统功能
  • 项目使用元件
    • ZE08-CH20电化学甲醛传感器
      • 产品简介
      • 接线图
      • 管脚定义
      • 通讯协议
        • 通用设置
        • 主动上传式数据显示格式
          • 校验和验算
      • ZE08-CH2O初始化代码
      • 效果
      • 注意事项
    • DHT11
      • 接线
      • 时序
      • 代码
    • OLED
    • RTC

概要

小弟是一名物联网工程专业的大三学生,最近疫情都在家里上学校和校外的网课。刚好校外的网课教完了STM32F407(小弟在高二下的时候自学了一遍STM32F103),也要求学生每个人做一个项目来反应出每个人的水平。于是我决定做一个类似于空气净化器的空气检测系统。
因为小弟水平有限可能有些地方会有错误,望大家指教。

空气检测系统功能

  1. 使用了ZE08-CH20电化学甲醛传感器检测空气中的甲醛浓度,并且显示在OLED(128*64)上
  2. 使用DHT11温湿度传感器将室内的温湿度实时显示于OLED中
  3. OLED可实时显示当前时间日期,并且可以使用按键进行修改,也可以使用蓝牙手机的串口APP进行修改。
  4. 使用了直流电机模拟出空气净化器的排气扇功能,当ZE08-CH20电化学甲醛传感器实时监测出室内的甲醛浓度大于0.1mg/m³,则会启动电机转动。
  5. 可以手动开启直流电机转动,通过按键调整电机转动速度。

项目使用元件

ZE08-CH20电化学甲醛传感器

产品简介

ZE08-CH2O型电化学甲醛模组是一个通用型、小型化
模组。利用电化学原理对空气中存在的CH2O进行探测,具
有良好的选择性,稳定性。内置温度传感器,可进行温度
补偿;同时具有数字输出与模拟电压输出,方便使用。
ZE08-CH2O是将成熟的电化学检测技术与精良的电路设计
紧密结合,设计制造出的通用型气体模组。

接线图

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第1张图片

管脚定义

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第2张图片
该模块提供了UART输出和DAC输出两种方式,个人选择了UART输出方式中的主动上传式(每隔1s发送一次浓度值)。

通讯协议

通用设置

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第3张图片

主动上传式数据显示格式

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第4张图片
根据注释:通过串口读出来的数据要经过处理!
气体浓度值(PPB)=(Byte4x256+Byte5)
PPB转换为PPM:PPB/1000
xPPMx1.25=xmg/m³

校验和验算

在这里插入图片描述
大家可以得到的数据可以用这条数据验证一下,我是没有问题的

ZE08-CH2O初始化代码

实际上就是串口的初始化代码,比较简单

#include"ze08_ch2o.h"

//甲醛模组初始化
//PA2--U2TX
//PA3--U2RX
void ZE08_CH2O_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);//打开GPIOA时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//打开USART2时钟
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;//PA2 PA3
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//无上下拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);//PA2 U2TX
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//PA3 U2RX
	
	USART_InitStruct.USART_Mode = USART_Mode_Rx;// | USART_Mode_Tx;//
	USART_InitStruct.USART_BaudRate = 9600;//波特率
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流量控制
	USART_InitStruct.USART_Parity = USART_Parity_No;//无奇偶校验位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
	USART_Init(USART2,&USART_InitStruct);
	
	USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
	
	USART_Cmd(USART2,ENABLE);
	
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
#ifndef __ZE08_CH2O_H
#define __ZE08_CH2O_H

#include "stm32f4xx.h"

//甲醛模组初始化
void ZE08_CH2O_Init(void);

#endif

处理串口接受中断的处理函数:

//串口2接收中断函数
void USART2_IRQHandler(void)
{
	static int i = 0;//数组下标
	static int flag_data = 0;
	unsigned char res;
	if(USART_GetITStatus(USART2,USART_IT_RXNE) == 1)
	{
		USART_ClearITPendingBit(USART2,USART_IT_RXNE);
		res = USART_ReceiveData(USART2);
		if(res == 0xFF)//接收到了0xFF之后表明数据安全
		{
			flag_data = 1;
		}
		if(res != 0xFF && flag_data == 1)//接收其它的数据
		{
			buff_usart2[i] = res;
			i++;
			if(i == 8)
			{
				i = 0;
				flag_data = 0;//重新要开始接收0xFF
				flag_usart2 = 1;//数据接收完毕
			}
		}
	}
}

主函数对接受完数据的处理
注意我这里注释掉了给大家看的测试语句因为我把效果放在了OLED屏幕上面

if(flag_usart2 == 1)//串口接收中断2标志位 接受ZE08——CH2O传来的数据
{
			ppb = buff_usart2[3]*256+buff_usart2[4];//气体浓度值
			ppm = ppb /1000.0;//单位面积内的浓度值
			
			//printf("ppb:%d\r\n",ppb);
			//printf("ppm:%f\r\n",ppm);这里是给大家看的测试语句 使用串口1发送
			
			//ppm = 0.1;
			if(ppm >= 0.08)//假如ppm大于0.08 0.08*1.25 = 0.1超过预警值
			{
				CH2O_flag = 1;//甲醛超标标志置1
			}
			else
			{
				CH2O_flag = 0;//甲醛超标标志置0
			}
			
			memset(buff_usart2,'\0',255);
			flag_usart2 = 0;//清除串口2接受中断
}

效果

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第5张图片

购买链接:ZE08-CH2O电化学甲醛传感器 甲醛气体检测模块 UART

原理图:https://pan.baidu.com/s/1NRkPzLLyvwUrcE4Bueumtg,提取码:7e6k

注意事项

该模块使用比较简单,但注意的是酒精可以对其进行干扰。我用酒精喷雾喷洒到空气中,该传感器立即检测出较大的数值,说明还是比较灵敏的。

注意注意千万不要放在太阳底下暴晒否则会测不出效果切记切记

DHT11

接线

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第6张图片
1–VCC,接3.3V电源
2–DQ,数据引脚,接单片机I/O脚,要根据时序设置为输入引脚还是输出引脚
3–NC,无用引脚,这里接地
4–GND,接地

时序

DHT11采用单总线数据格式
数据分小数部分和整数部分

一次完整的数据传输为40bit 高位先出

数据格式:8bit湿度整数数据+8bit湿度小数数据+8bit温度整数数据+8bit温度小数数据+8bit校验和

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第7张图片

OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第8张图片
OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第9张图片
OLED+RTC+DHT11+直流电机+ZE08-CH2O 的STM32F407空气检测系统_第10张图片

代码

主机发送起始信号:

  1. 一开始为高电平
  2. 拉低至少18us
  3. 拉高20~40us
  4. 先判断是否为高电平,如果仍是高电平则退出函数表示DHT11初始化错误
  5. 判断总线是否接受到80us低电平信号
  6. 判断总线是否接收到80us高电平信号

判断接收到的数据是1还是0:

  1. 判断接受50us低电平信号
  2. 延时40us
  3. 读总线看是否为高电平,是高电平则输出1;不是高电平则输出0
  4. 延时30~40us,读总线数据如果仍然是高点皮,则返回一个错误

总体来说,看着时序参考代码准没错,只要时序没搞错,代码都是差不多的

#include "dht11.h"

//主机输出
//PG9 DQ
void DHT11_PG9_Out(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);//打开GPIOG时钟
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//PG9
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//输出
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//不接上下拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOG,&GPIO_InitStruct);
}

//主机输入
//PG9 DQ
void DHT11_PG9_In(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//PG9
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;//输入
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//不接上下拉
	GPIO_Init(GPIOG,&GPIO_InitStruct);
}

//DHT11初始化
//PG9 DQ
//-1--初始化失败 0--初始化成功
int DHT11_Init(void)
{
	int i = 0;
	
	DHT11_PG9_Out();//主机主动发送信号
	
	GPIO_SetBits(GPIOG,GPIO_Pin_9);//一开始数据线为高
	
	delay_ms(1);
	
	GPIO_ResetBits(GPIOG,GPIO_Pin_9);//数据线拉低
	
	delay_ms(20);//至少拉低18ms
	
	GPIO_SetBits(GPIOG,GPIO_Pin_9);//数据线拉高
	
	delay_us(30);//拉高20-40us
	
	DHT11_PG9_In();//主机作为输入 DQ线作为输入线接收DHT11的信号
	
	while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 1)//如果在1ms之内接收不到DHT11的低电平信号 则退出
	{
		i++;
		delay_us(1);//延时1us
		if(i>=1000)//1us*1000=1ms
		{
			return -1;
		}
	}
	
	i = 0;
	
	while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 0)//DHT11发送80us的低电平信号
	{
		i++;
		delay_us(1);
		if(i>=100)//如果DHT11发送低电平时间大于80us,则退出
		{
			return -1;
		}
	}
	
	i = 0;
	while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 1)//DHT11发送80us的高电平信号
	{
		i++;
		delay_us(1);
		if(i>=100)//如果DHT11发送低电平时间大于80us,则退出
		{
			return -1;
		}
	}
	
	return 0;//DHT11初始化完毕
}

//读取一位字节的数据
unsigned char DHT11_ReadByte(void)
{
	int t = 0;
	int i;
	unsigned char value = 0;//接收到的数据

	//读取8位数据
	for(i = 0; i < 8; i++)
	{
		/*每一个bit数据都以50us低电平开始*/
		t = 0;
		while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 0)//DHT11发送50us低电平
		{
			t++;
			delay_us(1);
			if(t>=60)//大于50us,等于超时
			{
				return 0;
			}
		}
	
		/*根据DHT11的特性,延时40us,再读PA9,读到高电平代表该位数据为1,否则为0*/
		delay_us(40);
		
		if(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 1)//读到高电平,表示这一位数据是1
		{
			/*由于DHT11的数据是高位先出*/
			value |= (1<<(7-i));
			
			t = 0;
			while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 1)//判断它是否一直都为高电平
			{
				t++;
				delay_us(1);
				if(t>=50)//若延时50us后仍为高电平则返回一个错误
				{
					return 0;
				}
			}
		}
	}
	
	t = 0;
	while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9) == 0)//当最后一个bit数据传输完成之后,DHT11拉低总线50us
	{
		t++;
		delay_us(1);
		if(t>= 50)
		{
			break;
		}
	}
	
	return value;
}

//读取DHT11所有的数据
//0--读取成功
//1--读取失败
int DHT11_ReadAllData(unsigned char data[])
{
	int i;
	for(i = 0; i < 5; i++)
	{
		data[i] = DHT11_ReadByte();//读5个字节的数据
	}
	
	if(data[4] == data[3] + data[2] + data[1] + data[0])//第五位是校验和
	{
		return 0;
	}
	else
	{
		return 1;
	}
}


#ifndef __DHT11_H
#define __DHT11_H

#include "stm32f4xx.h"
#include "delay.h"

//主机输出
void DHT11_PG9_Out(void);

//主机输入
void DHT11_PG9_In(void);

//DHT11初始化
//-1--初始化失败 0--初始化成功
int DHT11_Init(void);

//读取一位字节的数据
unsigned char DHT11_ReadByte(void);

//读取DHT11所有的数据
//0--读取成功
//1--读取失败
int DHT11_ReadAllData(unsigned char data[]);

#endif

主函数里的显示代码:

else if(menu == 4)//温湿度界面
{
			if(clear_flag == 0)
			{
				OLED_CLS();
				clear_flag = 1;
			}
			
			OLED_ShowCN(40,0,16);//温 中文字
			OLED_ShowCN(56,0,17);//湿 中文字
			OLED_ShowCN(72,0,18);//度 中文字
			
			OLED_ShowCN(0,2,16);//温 中文字
			OLED_ShowCN(16,2,18);//度 中文字
			OLED_ShowStr(32,2,":",2);
			
			OLED_ShowCN(0,4,17);//湿 中文字
			OLED_ShowCN(16,4,18);//度 中文字
			OLED_ShowStr(32,4,":",2);
			
			ret = DHT11_Init();//DHT11初始化
			if(ret == 0)
			{
				ret = DHT11_ReadAllData(DHT11_data);//读取DHT11的数据
				if(ret == 0)
				{
					sprintf((char *)temperature,"%d.%d",DHT11_data[0],DHT11_data[1]);
					sprintf((char *)humidity,"%d.%d",DHT11_data[2],DHT11_data[3]);
				}
			}
			OLED_ShowStr(40,2,temperature,2);//温度值
			OLED_ShowStr(40,4,humidity,2);//湿度值
}

OLED

我使用的OLED是从网上买的:链接: 0.96寸 蓝色 白色 黄蓝双色 IIC通信 小OLED显示屏模块 51单片机.
我购买的是白光的

初始化程序也是使用自己编写的IIC代码结合商家给的STM32F103系列的代码。
IIC的原理小弟在这里就不多说了,网上的资料或者视频都很多。

#include "oled.h"
#include "codetab.h"

//OLED屏IIC初始化函数
//PD1VCC
//PD15 GND
//PE8 CLK
//PE10 SDA
void Oled_IIC_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);//GPIOD时钟
	//打开GPIOE时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_10;//PE8 PE10
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//输出
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//上拉 一般来说CLK SDA在初始化的时候都要接上拉电阻
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOE,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_15;//PD1 PD15
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOD,&GPIO_InitStruct);
	
	GPIO_SetBits(GPIOD,GPIO_Pin_1);//VCC
	GPIO_ResetBits(GPIOD,GPIO_Pin_15);//GND
	
	//保持I2C总线处于空闲状态
	GPIO_SetBits(GPIOE,GPIO_Pin_8);//CLK线拉高
	GPIO_SetBits(GPIOE,GPIO_Pin_10);//SDA线拉高
}

//设置PE10 SDA引脚为输出
void Oled_IIC_SDA_OUT(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;//PE10 SDA
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//输出
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//上拉 一般来说CLK SDA在初始化的时候都要接上拉电阻
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOE,&GPIO_InitStruct);
}

//设置PE10 SDA引脚为输入
void Oled_IIC_SDA_IN(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;//PE10 SDA
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;//输入
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_Init(GPIOE,&GPIO_InitStruct);
}

//IIC开始信号
void Oled_IIC_Start(void)
{
	//PE10 SDA设置为输出
	Oled_IIC_SDA_OUT();
	
	//IIC处于空闲状态
	GPIO_SetBits(GPIOE,GPIO_Pin_8);//CLK高电平
	GPIO_SetBits(GPIOE,GPIO_Pin_10);//SDA高电平
	
	delay_us(5);
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_10);//SDA拉为低电平
	
	delay_us(5);
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_8);//SCL拉低 钳住SDA
}

//IIC停止信号
void Oled_IIC_Stop(void)
{
	//PE10 SDA设置为输出
	Oled_IIC_SDA_OUT();
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_8);//CLK为低电平
	GPIO_ResetBits(GPIOE,GPIO_Pin_10);//SDA为低电平
	
	delay_us(5);
	
	GPIO_SetBits(GPIOE,GPIO_Pin_8);//CLK拉高
	
	delay_us(5);
	
	GPIO_SetBits(GPIOE,GPIO_Pin_10);//SDA拉高
}

//IIC主机发送一个数据给从设备
void Oled_IIC_Send_Byte(u8 data)
{
	int i;
	//PE10 SDA设置为输出
	Oled_IIC_SDA_OUT();
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_8);//CLK 拉低
	GPIO_ResetBits(GPIOE,GPIO_Pin_10);//SDA 拉低
	
	delay_us(5);
	
	for(i = 0; i < 8; i++)
	{
		if(data &(1<<(7-i)))
		{
			GPIO_SetBits(GPIOE,GPIO_Pin_10);//输出1
		}
		else
		{
			GPIO_ResetBits(GPIOE,GPIO_Pin_10);//输出0
		}
		
		delay_us(5);
		
		GPIO_SetBits(GPIOE,GPIO_Pin_8);//时钟线拉高
		
		delay_us(5);
		
		GPIO_ResetBits(GPIOE,GPIO_Pin_8);//时钟线拉低
	}
}

//IIC主机接收应答信号
u8 Oled_IIC_Receive_ACK(void)
{
	u8 ack;
	
	Oled_IIC_SDA_IN();//设置SDA引脚为输入
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_8);//拉低时钟线
	
	delay_us(5);
	
	GPIO_SetBits(GPIOE,GPIO_Pin_8);//拉高时钟线 可以传输数据
	
	if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_10) == 0)
	{
		ack = 0;
	}
	if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_10) == 1)
	{
		ack = 1;
	}		
	
	delay_us(5);
	
	GPIO_ResetBits(GPIOE,GPIO_Pin_8);//拉低时钟线 为下一次发送数据或接收数据做准备
	
	return ack;
}

//OLED屏IIC写一个字节函数
void Oled_IIC_Write_Byte(unsigned char addr,unsigned char data)
{
	u8 ack;
	
	//起始信号
	Oled_IIC_Start();

	//发送设备地址
	Oled_IIC_Send_Byte(OLED_ADDRESS);
	ack = Oled_IIC_Receive_ACK();//是否收到应答信号
	if(ack == 1)
	{
		return ;
	}
	
	Oled_IIC_Send_Byte(addr);//发送要写入的地址
	ack = Oled_IIC_Receive_ACK();//是否收到应答信号
	if(ack == 1)
	{
		return ;
	}
	
	Oled_IIC_Send_Byte(data);//发送一个字节的数据
		
	ack = Oled_IIC_Receive_ACK();//是否收到应答信号
	if(ack == 1)
	{
		return;
	}
	
	Oled_IIC_Stop();
}

void WriteCmd(unsigned char I2C_Command)//写命令
{
	Oled_IIC_Write_Byte(0x00, I2C_Command);
}

void WriteDat(unsigned char I2C_Data)//写数据
{
	Oled_IIC_Write_Byte(0x40, I2C_Data);
}

void OLED_Init(void)
{
	delay_ms(100); //这里的延时很重要
	
	WriteCmd(0xAE); //display off
	WriteCmd(0x20);	//Set Memory Addressing Mode	
	WriteCmd(0x10);	//00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
	WriteCmd(0xb0);	//Set Page Start Address for Page Addressing Mode,0-7
	WriteCmd(0xc8);	//Set COM Output Scan Direction
	WriteCmd(0x00); //---set low column address
	WriteCmd(0x10); //---set high column address
	WriteCmd(0x40); //--set start line address
	WriteCmd(0x81); //--set contrast control register
	WriteCmd(0xff); //亮度调节 0x00~0xff
	WriteCmd(0xa1); //--set segment re-map 0 to 127
	WriteCmd(0xa6); //--set normal display
	WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
	WriteCmd(0x3F); //
	WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
	WriteCmd(0xd3); //-set display offset
	WriteCmd(0x00); //-not offset
	WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
	WriteCmd(0xf0); //--set divide ratio
	WriteCmd(0xd9); //--set pre-charge period
	WriteCmd(0x22); //
	WriteCmd(0xda); //--set com pins hardware configuration
	WriteCmd(0x12);
	WriteCmd(0xdb); //--set vcomh
	WriteCmd(0x20); //0x20,0.77xVcc
	WriteCmd(0x8d); //--set DC-DC enable
	WriteCmd(0x14); //
	WriteCmd(0xaf); //--turn on oled panel
}

void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{ 
	WriteCmd(0xb0+y);
	WriteCmd(((x&0xf0)>>4)|0x10);
	WriteCmd((x&0x0f)|0x01);
}

void OLED_Fill(unsigned char fill_Data)//全屏填充
{
	unsigned char m,n;
	for(m=0;m<8;m++)
	{
		WriteCmd(0xb0+m);		//page0-page1
		WriteCmd(0x00);		//low column start address
		WriteCmd(0x10);		//high column start address
		for(n=0;n<128;n++)
			{
				WriteDat(fill_Data);
			}
	}
}

void OLED_CLS(void)//清屏
{
	OLED_Fill(0x00);
}

//--------------------------------------------------------------
// Prototype      : void OLED_ON(void)
// Calls          : 
// Parameters     : none
// Description    : 将OLED从休眠中唤醒
//--------------------------------------------------------------
void OLED_ON(void)
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0X14);  //开启电荷泵
	WriteCmd(0XAF);  //OLED唤醒
}

//--------------------------------------------------------------
// Prototype      : void OLED_OFF(void)
// Calls          : 
// Parameters     : none
// Description    : 让OLED休眠 -- 休眠模式下,OLED功耗不到10uA
//--------------------------------------------------------------
void OLED_OFF(void)
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0X10);  //关闭电荷泵
	WriteCmd(0XAE);  //OLED休眠
}

//--------------------------------------------------------------
// Prototype      : void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
// Calls          : 
// Parameters     : x,y -- 起始点坐标(x:0~127, y:0~7); ch[] -- 要显示的字符串; TextSize -- 字符大小(1:6*8 ; 2:8*16)
// Description    : 显示codetab.h中的ASCII字符,有6*8和8*16可选择
//--------------------------------------------------------------
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
	unsigned char c = 0,i = 0,j = 0;
	switch(TextSize)
	{
		case 1:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 126)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<6;i++)
					WriteDat(F6x8[c][i]);
				x += 6;
				j++;
			}
		}break;
		case 2:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 120)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<8;i++)
					WriteDat(F8X16[c*16+i]);
				OLED_SetPos(x,y+1);
				for(i=0;i<8;i++)
					WriteDat(F8X16[c*16+i+8]);
				x += 8;
				j++;
			}
		}break;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
// Calls          : 
// Parameters     : x,y -- 起始点坐标(x:0~127, y:0~7); N:汉字在codetab.h中的索引
// Description    : 显示codetab.h中的汉字,16*16点阵
//--------------------------------------------------------------
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
	unsigned char wm=0;
	unsigned int  adder=32*N;
	OLED_SetPos(x , y);
	for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]);
		adder += 1;
	}
	OLED_SetPos(x,y + 1);
	for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]);
		adder += 1;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);
// Calls          : 
// Parameters     : x0,y0 -- 起始点坐标(x0:0~127, y0:0~7); x1,y1 -- 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
// Description    : 显示BMP位图
//--------------------------------------------------------------
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
	unsigned int j=0;
	unsigned char x,y;

  if(y1%8==0)
		y = y1/8;
  else
		y = y1/8 + 1;
	for(y=y0;y<y1;y++)
	{
		OLED_SetPos(x0,y);
    for(x=x0;x<x1;x++)
		{
			WriteDat(BMP[j++]);
		}
	}
}

#ifndef __OLED_H
#define __OLED_H

#include "stm32f4xx.h"
#include "delay.h"


//OLED屏在IIC总线上的地址
#define OLED_ADDRESS 0x78 

//OLED屏IIC初始化函数
void Oled_IIC_Init(void);

//IIC SDA线设置为输出
void Oled_IIC_SDA_OUT(void);

//IIC SDA线设置为输入
void Oled_IIC_SDA_IN(void);

//IIC 开始信号
void Oled_IIC_Start(void);

//IIC停止信号
void Oled_IIC_Stop(void);

//IIC主机发送一个数据给从设备
void Oled_IIC_Send_Byte(u8 data);

//IIC主机接收应答信号
u8 Oled_IIC_Receive_ACK(void);

//OLED屏IIC写一个字节函数
void Oled_IIC_Write_Byte(unsigned char addr,unsigned char data);

void WriteCmd(unsigned char I2C_Command);
void WriteDat(unsigned char I2C_Data);
void OLED_Init(void);
void OLED_SetPos(unsigned char x, unsigned char y);
void OLED_Fill(unsigned char fill_Data);
void OLED_CLS(void);
void OLED_ON(void);
void OLED_OFF(void);
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize);
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N);
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);



#endif

字库编码:

/***************************16*16的点阵字体取模方式:共阴——列行式——逆向输出*********/
unsigned char F16x16[] =
{
	0x00,0x20,0x18,0xC7,0x44,0x44,0x44,0x44,0xFC,0x44,0x44,0x44,0x44,0x04,0x00,0x00,
	0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00,/*"年",0*/

	0x00,0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00,
	0x80,0x40,0x30,0x0F,0x02,0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00,/*"月",1*/

	0x80,0x80,0x80,0xBE,0xA2,0xA2,0xA2,0xA2,0xA2,0xA2,0xA2,0xBE,0x80,0x80,0x80,0x00,
	0x00,0x00,0x00,0x06,0x05,0x04,0x04,0x04,0x44,0x84,0x44,0x3C,0x00,0x00,0x00,0x00,/*"号",2*/
	
	0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00,
	0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00,/*"时",3*/

	0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00,
	0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00,/*"间",4*/

	0x00,0x00,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0xFF,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,0x00,0x00,/*"日",5*/

	0x00,0x04,0xFF,0x24,0x24,0x24,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
	0x88,0x48,0x2F,0x09,0x09,0x19,0xAF,0x48,0x30,0x0F,0x02,0x42,0x82,0x7F,0x00,0x00,/*"期",6*/

	0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00,
	0x00,0x00,0x07,0x02,0x02,0x02,0x02,0xFF,0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,/*"甲",7*/

	0xF2,0x12,0xFE,0x12,0xFE,0x12,0xF2,0x00,0x84,0x4F,0x24,0x14,0x24,0x4F,0x84,0x00,
	0xFF,0x4A,0x49,0x48,0x49,0x49,0xFF,0x00,0x40,0x49,0x49,0x7F,0x49,0x49,0x40,0x00,/*"醛",8*/
	
	0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00,
	0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,/*"当",9*/

	0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00,
	0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,/*"前",10*/

	0x00,0x80,0x60,0xF8,0x07,0x04,0xE4,0xA4,0xA4,0xBF,0xA4,0xA4,0xE4,0x04,0x00,0x00,
	0x01,0x00,0x00,0xFF,0x40,0x40,0x7F,0x4A,0x4A,0x4A,0x4A,0x4A,0x7F,0x40,0x40,0x00,/*"值",11*/
	
	0x10,0x0C,0x44,0x24,0x14,0x04,0x05,0x06,0x04,0x04,0x14,0x24,0x44,0x14,0x0C,0x00,
	0x00,0x40,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x40,0x00,0x00,/*"空",12*/

	0x20,0x10,0x4C,0x47,0x54,0x54,0x54,0x54,0x54,0x54,0x54,0xD4,0x04,0x04,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x30,0x40,0xF0,0x00,/*"气",13*/

	0x44,0x58,0xC0,0xFF,0x50,0x4C,0x80,0x40,0xB0,0x8E,0x80,0x8F,0xB0,0x40,0x80,0x00,
	0x08,0x06,0x01,0xFF,0x01,0x06,0x80,0x40,0x30,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,/*"粉",14*/

	0x00,0x40,0x20,0x10,0x0C,0x00,0x00,0x7F,0x00,0x00,0x04,0x08,0x10,0x60,0x00,0x00,
	0x40,0x40,0x44,0x44,0x44,0x44,0x44,0x7F,0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,/*"尘",15*/
	
	0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
	0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,/*"温",16*/

	0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
	0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00,/*"湿",17*/

	0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,
	0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,/*"度",18*/
	
	0x40,0x20,0xF8,0x07,0xF0,0xA0,0x90,0x4C,0x57,0x24,0xA4,0x54,0x4C,0x80,0x80,0x00,
	0x00,0x00,0xFF,0x00,0x1F,0x80,0x92,0x52,0x49,0x29,0x24,0x12,0x08,0x00,0x00,0x00,/*"修",19*/

	0x04,0x84,0x84,0x84,0x84,0xFC,0x40,0x30,0xCC,0x0B,0x08,0x08,0xF8,0x08,0x08,0x00,
	0x00,0x7F,0x20,0x10,0x10,0x08,0x80,0x40,0x21,0x16,0x08,0x16,0x21,0x40,0x80,0x00,/*"改",20*/
	
	0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,
	0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00,/*"电",21*/

	0x10,0x10,0xD0,0xFF,0x90,0x10,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,
	0x04,0x03,0x00,0xFF,0x00,0x83,0x60,0x1F,0x00,0x00,0x00,0x3F,0x40,0x40,0x78,0x00,/*"机",22*/

};

/************************************6*8的点阵************************************/
const unsigned char F6x8[][6] =
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
	0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
	0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
	0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
	0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
	0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
	0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
	0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
	0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
	0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
	0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
	0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
	0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
	0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
	0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
	0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
	0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
	0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
	0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
	0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
	0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
	0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
	0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
	0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
	0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
	0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
	0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
	0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
	0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
	0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
	0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
	0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
	0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
	0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
	0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
	0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
	0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
	0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
	0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
	0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
	0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
	0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
	0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
	0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
	0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
	0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
	0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
	0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
	0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
	0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
	0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
	0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
	0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
	0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
	0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
	0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
	0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
	0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
	0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
	0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
	0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
	0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
	0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
	0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
	0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
	0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
	0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
	0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
	0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
	0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
	0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
	0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
	0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
	0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
	0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
	0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
	0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
	0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
	0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
	0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
	0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
	0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
	0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
	0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
	0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
	0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
	0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
	0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
	0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
	0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
	0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
	0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};
/****************************************8*16的点阵************************************/
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
};

RTC

RTC的原理网上也有很多,直接上代码:

#include "rtc.h"

//RTC初始化
void rtc_init(void)
{
	RTC_InitTypeDef RTC_InitStruct;
	RTC_TimeTypeDef RTC_TimeStruct;
	RTC_DateTypeDef RTC_DateStruct;
	
	//打开电源管理器时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
	
	//使能后备寄存器访问
	PWR_BackupAccessCmd(ENABLE);
	
	//打开备用域复位
	//RCC_BackupResetCmd(ENABLE);
	
	//关闭备用域复位
	//RCC_BackupResetCmd(DISABLE);
	
	//如果RTC后备寄存器0的值不是0x5050 则代表是第一次初始化RTC
	if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5100)
	{
		//打开LSE时钟
		RCC_LSEConfig(RCC_LSE_ON);
		while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == 0);//等待LSE时钟就绪
		
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//LSE作为RTC时钟
		RCC_RTCCLKCmd(ENABLE);//使能
	
		RTC_InitStruct.RTC_AsynchPrediv = 0x7F;//异步预分频系数
		RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;//24小时制
		RTC_InitStruct.RTC_SynchPrediv = 0xFF;//同步预分频系数
		RTC_Init(&RTC_InitStruct);
		
		RTC_TimeStruct.RTC_H12 = RTC_H12_AM;//AM或24小时制
		RTC_TimeStruct.RTC_Hours = 16;//小时
		RTC_TimeStruct.RTC_Minutes = 47;//分钟
		RTC_TimeStruct.RTC_Seconds = 30;//秒
		RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct);
		
		RTC_DateStruct.RTC_Date = 16;//日期
		RTC_DateStruct.RTC_Month = RTC_Month_June;//月份
		RTC_DateStruct.RTC_Year = 20;//年
		RTC_DateStruct.RTC_WeekDay = RTC_Weekday_Tuesday;//星期
		RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct);
		
		RTC_WriteBackupRegister(RTC_BKP_DR0,0x5100);	//标记已经初始化过了
	}
}

剩下的有空写

你可能感兴趣的:(STM32)