实验效果图
本实验中oled和dht11的代码是移值之前stm32中的大部分都是一样 只是在进行引脚初始化和相关时钟初始化有点区别
oled.h
#ifndef __OLED_H
#define __OLED_H
#include "ht32f5xxxx_gpio.h"
#include "stdlib.h"
#define OLED_MODE 0
#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
//-----------------OLED IIC端口定义----------------
/*************进行代码移值头文件只修改了下面这几条语句*************/
#define GPIO_PIN_SDA GPIO_PIN_11
#define GPIO_PIN_SCL GPIO_PIN_12
#define OLED_SCLK_Clr() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SCL,RESET)//CLK拉低
#define OLED_SCLK_Set() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SCL,SET)//拉高
#define OLED_SDIN_Clr() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SDA,RESET)//DIN
#define OLED_SDIN_Set() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SDA,SET)
/*************************************************/
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
#define IIC_SLAVE_ADDR 0x78 //IIC slave device address
//OLED控制用函数
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Init(void);
void OLED_Clear(unsigned dat);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);
void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_ShowCHinese(u8 x,u8 y,u8 no);
void OLED_ShowCHinese_TWO(u8 x,u8 y,u8 no);
void OLED_ShowCHinese_Three(u8 x,u8 y,u8 no);
void OLED_P8x16String(u8 x,u8 y,u8 ch[]);
void OLED_ShowCHinese_six(u8 x,u8 y,u8 no);
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
void Delay_50ms(unsigned int Del_50ms);
void Delay_1ms(unsigned int Del_1ms);
void fill_picture(unsigned char fill_Data);
void Picture(void);
void IIC_Start(void);
void IIC_Stop(void);
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
void IIC_Wait_Ack(void);
#endif
dht11.h
#ifndef _DHT11_H_
#define _DHT11_H_
#include "ht32.h"
#include "delay.h"
#include "ht32_board.h"
#include "ht32_board_config.h"
//定义与DHT11通信使用的引脚
#define dou_dht11_GPIOx GPIO_PA
#define dou_dht11_HT_GPIOx HT_GPIOA
#define dou_dht11_GPIO_PIN GPIO_PIN_0
// dou_dht11 API **************************************************************
//数据定义:
extern uint8_t T_H; //温度高8位
extern uint8_t T_L; //温度低8位
extern uint8_t H_H; //湿度高8位
extern uint8_t H_L; //湿度低8位
void dou_dht11_init(void); //初始化函数
void dou_dht11_get_data(void); //更新数据函数
//void dou_dht11_test(void); //测试函数(死循环)
#endif
dht11.c
#include "dht11.h"
#include "adc.h"
uint8_t T_H,T_L,H_H,H_L; //分别为温度和湿度的高8位 低八位
uint8_t temp_H,temp_L,humi_H,humi_L,checkdata;
//设置通信引脚为输出
static void dou_dht11_pinset_out(void)
{
GPIO_DirectionConfig(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, GPIO_DIR_OUT); //设置方向为输出
GPIO_OpenDrainConfig(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, DISABLE); //输出模式设置为推挽输出
//GPIO_DriveConfig(dou_dht11_HT_GPIOx,dou_dht11_GPIO_PIN,GPIO_DIR_OUT,GPIO_DV_16MA); //设置输出驱动电流能力
}
//设置通信引脚为输入
static void dou_dht11_pinset_in(void)
{
GPIO_DirectionConfig(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, GPIO_DIR_IN); //设置方向为输入
//GPIO_PullResistorConfig(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, GPIO_PR_DISABLE); //除能上下拉电阻
GPIO_PullResistorConfig(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, GPIO_PR_UP);
GPIO_InputConfig(dou_dht11_HT_GPIOx,dou_dht11_GPIO_PIN,ENABLE);
}
//初始化函数
void dou_dht11_init(void)
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
if (dou_dht11_GPIOx == GPIO_PA)
{
CKCUClock.Bit.PA = 1;
}
else if (dou_dht11_GPIOx == GPIO_PB)
{
CKCUClock.Bit.PB = 1;
}
else if (dou_dht11_GPIOx == GPIO_PC)
{
CKCUClock.Bit.PC = 1;
}
else if (dou_dht11_GPIOx == GPIO_PD)
{
CKCUClock.Bit.PD = 1;
}
CKCUClock.Bit.AFIO = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(dou_dht11_GPIOx, dou_dht11_GPIO_PIN, AFIO_MODE_1); //配置引脚工作模式
dou_dht11_pinset_out();
GPIO_WriteOutBits(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, SET);
}
//获取引脚状态
bool dou_dht11_get_databit(void)
{
if (GPIO_ReadInBit(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN) != RESET)
{
return TRUE;
}
else
{
return FALSE;
}
}
//设置引脚输出
void dou_dht11_set_databit(bool level)
{
if (level != FALSE)
{
GPIO_WriteOutBits(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, SET);
}
else
{
GPIO_WriteOutBits(dou_dht11_HT_GPIOx, dou_dht11_GPIO_PIN, RESET);
}
}
//读取一字节数据
static uint8_t dou_dht11_read_byte(void)
{
uint8_t i;
uint8_t data = 0;
for (i = 0; i < 8; i++)
{
data <<= 1;
while ((!dou_dht11_get_databit()));
delay_us(30);
if (dou_dht11_get_databit())
{
data |= 0x1;
while(dou_dht11_get_databit());
}
else
{
}
}
return data;
}
static bool dou_dht11_start_sampling(void)
{
dou_dht11_pinset_out();
//主机拉低18ms? ?
dou_dht11_set_databit(FALSE);
delay_ms(18);
dou_dht11_set_databit(TRUE);
//总线由上拉电阻拉高 主机延时10us
delay_us(20);
//主机设为输入 判断从机响应信号
dou_dht11_pinset_in();
//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行?? ? ?
if (dou_dht11_get_databit() == FALSE) //T !?? ? ?
{
//判断从机是否发出 80us 的低电平响应信号是否结束?? ?
while (dou_dht11_get_databit() == FALSE);
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
while (dou_dht11_get_databit() != FALSE);
return TRUE;
}
return FALSE;
}
void dou_dht11_get_data(void)
{
uint8_t temp;
if (dou_dht11_start_sampling())
{
//printf("DHT11 is ready to transmit data\r\n");
//数据接收状态
humi_H = dou_dht11_read_byte(); //接收湿度高八位
humi_L = dou_dht11_read_byte(); //接收湿度低八位
temp_H = dou_dht11_read_byte();//接收温度高八位
temp_L = dou_dht11_read_byte();//接收温度低八位
checkdata = dou_dht11_read_byte(); //接收效验位
/* Data transmission finishes, pull the bus high */
dou_dht11_pinset_out();
dou_dht11_set_databit(TRUE);
//数据校验
temp = (temp_H + temp_L + humi_H + humi_L);
if (temp == checkdata)
{
H_H = humi_H; // 湿度整数部分
H_L = humi_L; //湿度小数部分
T_H = temp_H; //温度整数部分
T_L = temp_L; //温度小数部分
}
}
}
main.c
#include "ht32.h"
#include "ht32_board.h"
#include "led.h"
#include "delay.h"
#include "uart.h"
#include "dht11.h"
#include "oled.h"
/**********************TH32 oled显示温湿度**********************
Author:小殷
Date:2022-4-22
******************************************************/
uint8_t data = 0;
uint8_t sendbuf[128] = {0};
int main()
{
Led_Init();
USARTx_Init();
OLED_Init();
Usart_SendStr(HT_USART1,(uint8_t *)"------HT32 oled TEST-------\r\n");//循环发送字符串,测试用
OLED_Clear(0);
OLED_ShowString(20,1,"ht32 oled",8);
OLED_ShowString(20,3,(unsigned char *)"C_T:",3); // Current_Temperature 当前温度
OLED_ShowCHinese_Three(80,3,11); //C
OLED_ShowString(20,5,(unsigned char *)"C_H:",3); // Current_Humidity 当前湿度
OLED_ShowCHinese_Three(80,5,13); //.
while(1)
{
// dou_dht11_get_data();//获取温度数据
// sprintf((char *)sendbuf,"当前温度:%2d 当前湿度:%2d\n",dou_dht11_temperature_data_h ,dou_dht11_humidity_data_h);
// Usart_SendStr(HT_USART1,sendbuf);
// delay_ms(500);
dou_dht11_get_data();//获取温度数据
OLED_ShowNum(50,3,T_H/10,3,3);
OLED_ShowNum(60,3,T_L%10,3,3);
OLED_ShowNum(50,5,H_H/10,3,3);
OLED_ShowNum(60,5,H_L%10,3,3);
}
}
工程链接需要自取
https://github.com/FDForerver/HT32F52352.git