基于合泰HT32F52352+oled温湿度显示
实验效果图
本实验中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
#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)
#define OLED_SCLK_Set() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SCL,SET)
#define OLED_SDIN_Clr() GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_SDA,RESET)
#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
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"
#define dou_dht11_GPIOx GPIO_PA
#define dou_dht11_HT_GPIOx HT_GPIOA
#define dou_dht11_GPIO_PIN GPIO_PIN_0
extern uint8_t T_H;
extern uint8_t T_L;
extern uint8_t H_H;
extern uint8_t H_L;
void dou_dht11_init(void);
void dou_dht11_get_data(void);
#endif
dht11.c
#include "dht11.h"
#include "adc.h"
uint8_t T_H,T_L,H_H,H_L;
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);
}
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_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();
dou_dht11_set_databit(FALSE);
delay_ms(18);
dou_dht11_set_databit(TRUE);
delay_us(20);
dou_dht11_pinset_in();
if (dou_dht11_get_databit() == FALSE)
{
while (dou_dht11_get_databit() == FALSE);
while (dou_dht11_get_databit() != FALSE);
return TRUE;
}
return FALSE;
}
void dou_dht11_get_data(void)
{
uint8_t temp;
if (dou_dht11_start_sampling())
{
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();
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"
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);
OLED_ShowCHinese_Three(80,3,11);
OLED_ShowString(20,5,(unsigned char *)"C_H:",3);
OLED_ShowCHinese_Three(80,5,13);
while(1)
{
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:
|