github工程地址git
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "fonts.h"
#include "ssd1306.h"
#include "dwt_stm32_delay.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* 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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2;
uint16_t sum, RH, TEMP;
int temp_low, temp_high, rh_low, rh_high;
char temp_char1[2], temp_char2, rh_char1[2], rh_char2;
uint8_t check = 0;
GPIO_InitTypeDef GPIO_InitStruct;
void set_gpio_output (void)
{
/*Configure GPIO pin output: PA2 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void set_gpio_input (void)
{
/*Configure GPIO pin input: PA2 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void DHT11_start (void)
{
set_gpio_output (); // set the pin as output
HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, 0); // pull the pin low
DWT_Delay_us (18000); // wait for 18ms
set_gpio_input (); // set as input
}
void check_response (void)
{
DWT_Delay_us (40);
if (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1)))
{
DWT_Delay_us (80);
if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))) check = 1;
}
while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))); // wait for the pin to go low
}
uint8_t read_data (void)
{
uint8_t i,j;
for (j=0;j<8;j++)
{
while (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))); // wait for the pin to go high
DWT_Delay_us (40); // wait for 40 us
if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1)) == 0) // if the pin is low
{
i&= ~(1<<(7-j)); // write 0
}
else i|= (1<<(7-j)); // if the pin is high, write 1
while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))); // wait for the pin to go low
}
return i;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
DWT_Delay_Init ();
SSD1306_Init(); // initialise
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
DHT11_start ();
check_response ();
Rh_byte1 = read_data ();
Rh_byte2 = read_data ();
Temp_byte1 = read_data ();
Temp_byte2 = read_data ();
sum = read_data();
if (sum == (Rh_byte1+Rh_byte2+Temp_byte1+Temp_byte2)) // if the data is correct
{
SSD1306_GotoXY (0,0);
SSD1306_Puts("TEMP:- ",&Font_11x18, 1);
SSD1306_Putc((Temp_byte1/10)+48,&Font_11x18, 1);
SSD1306_Putc((Temp_byte1%10)+48,&Font_11x18, 1);
SSD1306_Puts("C",&Font_11x18, 1);
//display
SSD1306_GotoXY (0,30);
SSD1306_Puts("RH:- ",&Font_11x18, 1);
SSD1306_Putc((Rh_byte1/10)+48,&Font_11x18, 1);
SSD1306_Putc((Rh_byte1%10)+48,&Font_11x18, 1);
SSD1306_Puts("%",&Font_11x18, 1);
SSD1306_UpdateScreen();
HAL_Delay (1000);
}
}
/* USER CODE END 3 */
}
複製代碼
github工程地址github
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "fonts.h"
#include "ssd1306.h"
#include "dwt_stm32_delay.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* 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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t local_time, sensor_time;
uint32_t distance;
uint32_t hcsr04_read (void)
{
local_time=0;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // pull the TRIG pin HIGH
DWT_Delay_us(2); // wait for 2 us
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // pull the TRIG pin HIGH
DWT_Delay_us(10); // wait for 10 us
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // pull the TRIG pin low
// read the time for which the pin is high
while (!(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2))); // wait for the ECHO pin to go high
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2)) // while the pin is high
{
local_time++; // measure time for which the pin is high
DWT_Delay_us (1);
}
return local_time*2;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
DWT_Delay_Init ();
SSD1306_Init(); // initialise
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
sensor_time = hcsr04_read();
distance = sensor_time * .034/2;
SSD1306_GotoXY (0,0);
SSD1306_Puts ("Dist= ",&Font_11x18, 1);
SSD1306_Putc ((distance/100)+48,&Font_11x18, 1);
SSD1306_Putc (((distance%100)/10)+48,&Font_11x18, 1);
SSD1306_Putc (((distance%10))+48,&Font_11x18, 1);
SSD1306_Puts("CM",&Font_11x18, 1);
SSD1306_UpdateScreen();
HAL_Delay(200);
}
/* USER CODE END 3 */
}
複製代碼