使用ESP32,DHT11,STM32F103c8t6,OLED实现温湿度显示+远程控制LED的亮灭,同时在EMQX收集温湿度数据。
  m9mBCTuLLJjb 2023年11月02日 57 0

硬件:stm32f103c8t6,dht11,esp32,oled,led,杜邦线,面包板,stlink。

软件:keil5,VScode-platformio。

Esp32代码:

#include "SoftwareSerial.h"

#include <Arduino.h>

#include<WiFi.h>

#include<PubSubClient.h>

#include<ArduinoJson.h>

 const char *ssid = "ASUS";  // 输入你的WiFi名称

const char *password = "12345555";  // 输入你的WiFi密码

const char *mqttServer="124.220.31.151";

const int mqttPort=1883;       //服务器TCP协议的端口号

const char *mqttUser="admin";   //登录服务器所需的用户名

const char *mqttPsw="admin";   //密码

SoftwareSerial esp_serial(17,16);


char item[]="";


int i =0 ;


 WiFiClient espClient;     //实例WiFiClient对象

PubSubClient client(espClient);


void WiFi_Connect()

{

  WiFi.begin(ssid,password);

while(WiFi.status() !=WL_CONNECTED)

 {

    delay(300);

    Serial.print(".");


 }

}


void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived [");

  Serial.print(topic);

  Serial.print("] ");

  for (int i=0;i<length;i++) {

    Serial.print((char)payload[i]);

  }

if((char)payload[0]=='1')

{

digitalWrite(18,HIGH);

}

else

{

digitalWrite(18,LOW);

}

  Serial.println();

}


void reconnect() {

  // Loop until we're reconnected

  while (!client.connected()) {

    Serial.print("Attempting MQTT connection...");

    // Attempt to connect

    if (client.connect("arduinoClient")) {

      Serial.println("connected");

      // Once connected, publish an announcement...


      client.subscribe("sub");

    } else {

      Serial.print("failed, rc=");

      Serial.print(client.state());

      Serial.println(" try again in 5 seconds");

      // Wait 5 seconds before retrying

      delay(5000);

    }

  }

}


void setup() {

  pinMode(18,OUTPUT);

 esp_serial.begin(9600);  

  Serial.begin(115200);  

    client.setCallback(callback);


   Serial.print("Connecting...");

    WiFi_Connect();


    Serial.println("wifi Connect");

    Serial.println("IP address:");

    Serial.println(WiFi.localIP());

client.setServer(mqttServer,mqttPort);    //设置服务器地址,端口

  while(!client.connected()){        //判断是否连接到服务器

    Serial.println("正连接至MQTT服务器...");

    if(client.connect("ESP32Client",mqttUser,mqttPsw)){    

    //"ESP32Client"为你的客户端ID,可自定义

      Serial.println("已连接!");  

    }else{

      Serial.println("连接失败");

      Serial.println(client.state());    //重连函数

      delay(2000);  

    }  

  }

  client.subscribe("sub");            //订阅的主题

  Serial.println("已订阅主题,等待消息...");


}


void loop() {

    if (!client.connected()) {

    reconnect();

  }

  client.loop();


  while(esp_serial.available()>0)

  {

    item[i]=char(esp_serial.read());

    i++;

    delay(2);

  }


Serial.println(item);

 client.publish("22",item);    //向ESP32主题下发送信息"Hello world from ESP32"

delay(1000);

i=0;

}



; PlatformIO Project Configuration File

;

;   Build options: build flags, source filter

;   Upload options: custom upload port, speed and extra flags

;   Library options: dependencies, extra library storages

;   Advanced options: extra scripting

;

; Please visit documentation for the other options and examples

; https://docs.platformio.org/page/projectconf.html


[env:esp32dev]

platform = espressif32

board = esp32dev

framework = arduino

monitor_speed = 115200

lib_deps =

    knolleary/PubSubClient@^2.8

    bblanchon/ArduinoJson@^6.21.3

    plerup/EspSoftwareSerial@^8.1.0


STM32f103c8t6代码:

Delay.h

#ifndef __DELAY_H

#define __DELAY_H


void Delay_us(uint32_t us);

void Delay_ms(uint32_t ms);

void Delay_s(uint32_t s);


#endif


Delay.c

#include "stm32f10x.h"


/**

微秒级延时

延时时长,范围:0~233015

  */

void Delay_us(uint32_t xus)

{

SysTick->LOAD = 72 * xus; //设置定时器重装值

SysTick->VAL = 0x00; //清空当前计数值

SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器

while(!(SysTick->CTRL & 0x00010000)); //等待计数到0

SysTick->CTRL = 0x00000004; //关闭定时器

}


/**

毫秒级延时

延时时长,范围:0~4294967295

  */

void Delay_ms(uint32_t xms)

{

while(xms--)

{

Delay_us(1000);

}

}


/**

秒级延时

延时时长,范围:0~4294967295

  */

void Delay_s(uint32_t xs)

{

while(xs--)

{

Delay_ms(1000);

}

}


LED..h:

#ifndef __LED_H

#define __LED_H


void LED_Init(void);

void LED1_ON(void);

void LED1_OFF(void);

void LED1_Turn(void);

void LED2_ON(void);

void LED2_OFF(void);

void LED2_Turn(void);


#endif


LED.c:

#include "stm32f10x.h"                  // Device header


void LED_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);


GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);

}


void LED1_ON(void)

{

GPIO_ResetBits(GPIOA, GPIO_Pin_1);

}


void LED1_OFF(void)

{

GPIO_SetBits(GPIOA, GPIO_Pin_1);

}


void LED1_Turn(void)

{

if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)

{

GPIO_SetBits(GPIOA, GPIO_Pin_1);

}

else

{

GPIO_ResetBits(GPIOA, GPIO_Pin_1);

}

}


void LED2_ON(void)

{

GPIO_ResetBits(GPIOA, GPIO_Pin_2);

}


void LED2_OFF(void)

{

GPIO_SetBits(GPIOA, GPIO_Pin_2);

}


void LED2_Turn(void)

{

if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0)

{

GPIO_SetBits(GPIOA, GPIO_Pin_2);

}

else

{

GPIO_ResetBits(GPIOA, GPIO_Pin_2);

}

}


Key.h:

#ifndef __KEY_H

#define __KEY_H


void Key_Init(void);

uint8_t Key_GetNum(void);


#endif


Key.c:

#include "stm32f10x.h"                  // Device header

#include "Delay.h"


void Key_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

}


uint8_t Key_GetNum(void)

{

uint8_t KeyNum = 0;

if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)

{

Delay_ms(20);

while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);

Delay_ms(20);

KeyNum = 1;

}

if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)

{

Delay_ms(20);

while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);

Delay_ms(20);

KeyNum = 2;

}


return KeyNum;

}


OLED.h

#ifndef __OLED_H

#define __OLED_H


void OLED_Init(void);

void OLED_Clear(void);

void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);

void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);

void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);

void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);

void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);

void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);


#endif


OLED.c:

#include "stm32f10x.h"

#include "OLED_Font.h"


/*引脚配置*/

#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))

#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))


/*引脚初始化*/

void OLED_I2C_Init(void)

{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_Init(GPIOB, &GPIO_InitStructure);


OLED_W_SCL(1);

OLED_W_SDA(1);

}


/**

开始

  */

void OLED_I2C_Start(void)

{

OLED_W_SDA(1);

OLED_W_SCL(1);

OLED_W_SDA(0);

OLED_W_SCL(0);

}


/**

停止

  */

void OLED_I2C_Stop(void)

{

OLED_W_SDA(0);

OLED_W_SCL(1);

OLED_W_SDA(1);

}


/**

发送一个字节

要发送的一个字节

  */

void OLED_I2C_SendByte(uint8_t Byte)

{

uint8_t i;

for (i = 0; i < 8; i++)

{

OLED_W_SDA(Byte & (0x80 >> i));

OLED_W_SCL(1);

OLED_W_SCL(0);

}

OLED_W_SCL(1); //额外的一个时钟,不处理应答信号

OLED_W_SCL(0);

}


/**

写命令

要写入的命令

  */

void OLED_WriteCommand(uint8_t Command)

{

OLED_I2C_Start();

OLED_I2C_SendByte(0x78); //从机地址

OLED_I2C_SendByte(0x00); //写命令

OLED_I2C_SendByte(Command);

OLED_I2C_Stop();

}


/**

写数据

要写入的数据

  */

void OLED_WriteData(uint8_t Data)

{

OLED_I2C_Start();

OLED_I2C_SendByte(0x78); //从机地址

OLED_I2C_SendByte(0x40); //写数据

OLED_I2C_SendByte(Data);

OLED_I2C_Stop();

}


/**

设置光标位置

以左上角为原点,向下方向的坐标,范围:0~7

以左上角为原点,向右方向的坐标,范围:0~127

  */

void OLED_SetCursor(uint8_t Y, uint8_t X)

{

OLED_WriteCommand(0xB0 | Y); //设置Y位置

OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位

OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位

}


/**

清屏

  */

void OLED_Clear(void)

{  

uint8_t i, j;

for (j = 0; j < 8; j++)

{

OLED_SetCursor(j, 0);

for(i = 0; i < 128; i++)

{

OLED_WriteData(0x00);

}

}

}


/**

显示一个字符

行位置,范围:1~4

列位置,范围:1~16

要显示的一个字符,范围:ASCII可见字符

  */

void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)

{      

uint8_t i;

OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分

for (i = 0; i < 8; i++)

{

OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容

}

OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分

for (i = 0; i < 8; i++)

{

OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容

}

}


/**

显示字符串

起始行位置,范围:1~4

起始列位置,范围:1~16

要显示的字符串,范围:ASCII可见字符

  */

void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)

{

uint8_t i;

for (i = 0; String[i] != '\0'; i++)

{

OLED_ShowChar(Line, Column + i, String[i]);

}

}


/**

次方函数

返回值等于X的Y次方

  */

uint32_t OLED_Pow(uint32_t X, uint32_t Y)

{

uint32_t Result = 1;

while (Y--)

{

Result *= X;

}

return Result;

}


/**

显示数字(十进制,正数)

起始行位置,范围:1~4

起始列位置,范围:1~16

要显示的数字,范围:0~4294967295

要显示数字的长度,范围:1~10

  */

void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)

{

uint8_t i;

for (i = 0; i < Length; i++)

{

OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');

}

}


/**

显示数字(十进制,带符号数)

起始行位置,范围:1~4

起始列位置,范围:1~16

要显示的数字,范围:-2147483648~2147483647

要显示数字的长度,范围:1~10

  */

void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)

{

uint8_t i;

uint32_t Number1;

if (Number >= 0)

{

OLED_ShowChar(Line, Column, '+');

Number1 = Number;

}

else

{

OLED_ShowChar(Line, Column, '-');

Number1 = -Number;

}

for (i = 0; i < Length; i++)

{

OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');

}

}


/**

显示数字(十六进制,正数)

起始行位置,范围:1~4

起始列位置,范围:1~16

要显示的数字,范围:0~0xFFFFFFFF

要显示数字的长度,范围:1~8

  */

void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)

{

uint8_t i, SingleNumber;

for (i = 0; i < Length; i++)

{

SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;

if (SingleNumber < 10)

{

OLED_ShowChar(Line, Column + i, SingleNumber + '0');

}

else

{

OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');

}

}

}


/**

显示数字(二进制,正数)

起始行位置,范围:1~4

起始列位置,范围:1~16

要显示的数字,范围:0~1111 1111 1111 1111

要显示数字的长度,范围:1~16

  */

void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)

{

uint8_t i;

for (i = 0; i < Length; i++)

{

OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');

}

}


/**

初始化

  */

void OLED_Init(void)

{

uint32_t i, j;


for (i = 0; i < 1000; i++) //上电延时

{

for (j = 0; j < 1000; j++);

}


OLED_I2C_Init(); //端口初始化


OLED_WriteCommand(0xAE); //关闭显示


OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率

OLED_WriteCommand(0x80);


OLED_WriteCommand(0xA8); //设置多路复用率

OLED_WriteCommand(0x3F);


OLED_WriteCommand(0xD3); //设置显示偏移

OLED_WriteCommand(0x00);


OLED_WriteCommand(0x40); //设置显示开始行


OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置


OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置


OLED_WriteCommand(0xDA); //设置COM引脚硬件配置

OLED_WriteCommand(0x12);


OLED_WriteCommand(0x81); //设置对比度控制

OLED_WriteCommand(0xCF);


OLED_WriteCommand(0xD9); //设置预充电周期

OLED_WriteCommand(0xF1);


OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别

OLED_WriteCommand(0x30);


OLED_WriteCommand(0xA4); //设置整个显示打开/关闭


OLED_WriteCommand(0xA6); //设置正常/倒转显示


OLED_WriteCommand(0x8D); //设置充电泵

OLED_WriteCommand(0x14);


OLED_WriteCommand(0xAF); //开启显示


OLED_Clear(); //OLED清屏

}


DHT11.h

#ifndef __DHT11_H

#define __DHT11_H     


//DHT11函数

u8 DHT11_Init(void);




u8 DHT11_Read_Byte(void);

u8 DHT11_Read_Data(u32 *temp,u32 *humi);


void DHT11_IO_OUT(void);

void DHT11_IO_IN(void);


#endif  


DHT11.c

#include "stm32f10x.h"

#include "Delay.h"

#define DHT11_DQ_IN   GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12) //写数据

u32 dat[5]={0x00,0x00,0x00,0x00,0x00};

u32 sum=0;



void DHT11_IO_OUT(void)

{

GPIO_InitTypeDef  GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);

// GPIO_SetBits(GPIOB,GPIO_Pin_12);    //高电平  

}


void DHT11_IO_IN(void)

{

GPIO_InitTypeDef    GPIO_InitStructure;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);

// GPIO_SetBits(GPIOB,GPIO_Pin_12);    //高电平  

}



u8 DHT11_Read_Byte(void)

{

u8 retry=0;

u8 i,temp; //暂存位数据

u8 ReadDat=0;


for(i=0;i<8;i++)

{

while(DHT11_DQ_IN==0&&retry<100)//等待变为高电平

{

Delay_us(1);

retry++;

}

retry=0;

Delay_us(30);

temp=0;

if(DHT11_DQ_IN==1)

temp=1;

while(DHT11_DQ_IN==1&&retry<100)//等待变低电平

{

Delay_us(1);

retry++;

}

  retry=0;

ReadDat<<=1;

ReadDat|=temp;

}

return ReadDat;

}




u8 DHT11_Read_Data(u32 *temp,u32 *humi)

{

u8 i;

u8 retry=0;

DHT11_IO_OUT();

GPIO_ResetBits(GPIOB,GPIO_Pin_12);

Delay_ms(18);

GPIO_SetBits(GPIOB,GPIO_Pin_12);

Delay_us(40);

DHT11_IO_IN();

Delay_us(20);

if(DHT11_DQ_IN==0)

{

   while(DHT11_DQ_IN==0&&retry<100)

 {

 Delay_us(1);

 retry++;  

 }

 retry=0;

 while(DHT11_DQ_IN==1&&retry<100)

 {

 Delay_us(1);

 retry++;  

 }

   retry=0;


for(i=0;i<5;i++)

{

dat[i]=DHT11_Read_Byte();//读取5byte

}

  Delay_us(50);

}

if((dat[0]+dat[1]+dat[2]+dat[3]) == dat[4])

{


*temp=dat[2];//温度

*humi=dat[0];//湿度



}


return 0;

}


Serial.h

#ifndef __SERIAL_H

#define __SERIAL_H


#include <stdio.h>


void Serial_Init(void);

void Serial_SendByte(uint8_t Byte);

void Serial_SendArray(uint8_t *Array, uint16_t Length);

void Serial_SendString(char *String);

void Serial_SendNumber(uint32_t Number, uint8_t Length);

void Serial_Printf(char *format, ...);



uint8_t Serial_GetRxFlag(void);

uint8_t Serial_GetRxData(void);

#endif


Serial.c:

#include "stm32f10x.h"                  // Device header

#include <stdio.h>

#include <stdarg.h>



uint8_t Serial_RxData;

uint8_t Serial_RxFlag;


void Serial_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);


GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_Init(USART1, &USART_InitStructure);


USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_Init(&NVIC_InitStructure);


USART_Cmd(USART1, ENABLE);

}


void Serial_SendByte(uint8_t Byte)

{

USART_SendData(USART1, Byte);

while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

}


void Serial_SendArray(uint8_t *Array, uint16_t Length)

{

uint16_t i;

for (i = 0; i < Length; i ++)

{

Serial_SendByte(Array[i]);

}

}


void Serial_SendString(char *String)

{

uint8_t i;

for (i = 0; String[i] != '\0'; i ++)

{

Serial_SendByte(String[i]);

}

}


uint32_t Serial_Pow(uint32_t X, uint32_t Y)

{

uint32_t Result = 1;

while (Y --)

{

Result *= X;

}

return Result;

}


void Serial_SendNumber(uint32_t Number, uint8_t Length)

{

uint8_t i;

for (i = 0; i < Length; i ++)

{

Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');

}

}


int fputc(int ch, FILE *f)

{

Serial_SendByte(ch);

return ch;

}


void Serial_Printf(char *format, ...)

{

char String[100];

va_list arg;

va_start(arg, format);

vsprintf(String, format, arg);

va_end(arg);

Serial_SendString(String);

}



uint8_t Serial_GetRxFlag(void)

{

if (Serial_RxFlag == 1)

{

Serial_RxFlag = 0;

return 1;

}

return 0;

}


uint8_t Serial_GetRxData(void)

{

return Serial_RxData;

}


void USART1_IRQHandler(void)

{

if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)

{

Serial_RxData = USART_ReceiveData(USART1);

Serial_RxFlag = 1;

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

}

}


main.c

#include "stm32f10x.h"

#include "Delay.h"

#include "OLED.h"

#include "dht11.h"

#include "Serial.h"

extern u32 dat[5];

 int main(void)

 {

 u32 wendu,shidu;  

   OLED_Init(); 端口初始化

 Serial_Init();


 OLED_ShowString(1,1,"wd:");

   OLED_ShowString(3,1,"sd:");


 while(1)

 {


  DHT11_Read_Data(&wendu,&shidu);


温度

 OLED_ShowNum(1,6,wendu,2);  

//湿度

 OLED_ShowNum(3,6,shidu,2);


     Serial_SendString("wd:");

 Serial_SendNumber(wendu,2);

 Serial_SendString(" ");

 Serial_SendString("sd:");

 Serial_SendNumber(shidu,2);

 Serial_SendString("||");

 Delay_ms(1000);    

 }    

}




【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  QtpjMRSUUfXb   2023年12月08日   48   0   0 引脚#include看门狗
  tprTMCWDkFAR   2023年12月07日   30   0   0 头文件#include初始化
  QtpjMRSUUfXb   2023年12月06日   55   0   0 卷积#includeCUDA
  UYSNSBVoGd8R   2023年12月08日   22   0   0 引脚#include#define
m9mBCTuLLJjb