[STM32] 8cm 초음파 센서 줄자 / 온도계

2021. 11. 9. 23:52코딩/STM32

ultrasonic.c

#include "main.h"
#include "led_onoff.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TRIG_PORT GPIOC
#define TRIG_PIN GPIO_PIN_5

extern volatile int TIM11_10ms_LED_counter;
extern volatile int TIM11_10ms_ultrasonic_counter;

uint32_t distance=0;	//거리
uint8_t ic_cpt_flag=0;	//rising edge/falling edge를 detect하는 flag변수
//rising edge/falling edge INT가 발생시 이쪽으로 침입
uint8_t led_flag = 0;


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	static uint8_t is_first_captured=0;

	if (is_first_captured == 0)	//rising edge
	{
		__HAL_TIM_SET_COUNTER(htim,0);
		is_first_captured=1;	//rising edge detect flag set
	}
	else if (is_first_captured == 1)	//falling edge INT detect
	{
		is_first_captured = 0;
		distance = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
		ic_cpt_flag=1;
	}

}
void ultrasonic_processing()
{

	if (TIM11_10ms_ultrasonic_counter >= 100)	//timer 1sec reached
	{
		TIM11_10ms_ultrasonic_counter=0;
		make_trigger();
		if (ic_cpt_flag==1)
		{
			ic_cpt_flag=0;
			distance = distance * 0.034 / 2;	//1us 이동하는데 0.034cm 이동 나누기2 한이유는 왕복이기 때문이다.
			printf("distance : %d\n", distance);
		}
	}
}
void led_cm_mode()
{
	switch("%d",distance)
	{
	case 1:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x01, GPIO_PIN_SET);
		break;
	case 2:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x03, GPIO_PIN_SET);
		break;
	case 3:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x07, GPIO_PIN_SET);
		break;
	case 4:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x0f, GPIO_PIN_SET);
		break;
	case 5:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x1f, GPIO_PIN_SET);
		break;
	case 6:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x3f, GPIO_PIN_SET);
		break;
	case 7:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0x7f, GPIO_PIN_SET);
		break;
	case 8:
		buzzer_off();
		led_all_off();
		HAL_GPIO_WritePin(GPIOB, 0xff, GPIO_PIN_SET);
		break;
	default :
		led_all_off();
		buzzer_on();
		if (TIM11_10ms_LED_counter >= 100)
				{
					TIM11_10ms_LED_counter = 0;
					led_flag = !led_flag;

					if(led_flag)	led_all_off();
					else			led_all_on();
				}


	}
}
void make_trigger(void)
{
	HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET);
	delay_us(2);
	HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET);
	delay_us(10);
	HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET);

}

void buzzer_on()
{
	HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
}

void buzzer_off()
{
	HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_RESET);
}

 

DHT11.c

#include "main.h"
#include "DHT11.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// extern TIM_HandleTypeDef htim;
extern void FND_update(uint16_t value);
extern void delay_us (unsigned long us);
extern volatile int TDHT11_timer_counter;
extern volatile int TIM11_10ms_counter;

extern void buzzer_on();
extern void buzzer_off();
void DHT11_main(void)
{
	uint8_t i_RH, d_RH, i_Tmp, d_Tmp;

	// HAL_TIM_Base_Start_IT(&htim2);
	DHT11_Init();

	while(1)
	{
		DHT11_trriger();
		DHT11_DataLine_Input();
		DHT11_dumi_read();

		i_RH = DHT11_rx_Data();
		d_RH = DHT11_rx_Data();
		i_Tmp = DHT11_rx_Data();
		d_Tmp = DHT11_rx_Data();

		DHT11_DataLine_Output();
		HAL_GPIO_WritePin(DHT11_PORT, DHT11_DATA_RIN, GPIO_PIN_SET);
		printf("[Tmp]%d\n",(int)i_Tmp);
		printf("[Wet]%d\n",(int)i_RH);
		// FND_Update(i_Tmp*100 + i_RH);

	}

}

void DHT11_processing(void)
{
	uint8_t i_RH, d_RH, i_Tmp, d_Tmp;

	if (TIM11_10ms_counter >= 150)	//1500ms
	{
		TIM11_10ms_counter=0;
		DHT11_trriger();
		DHT11_DataLine_Input();
		DHT11_dumi_read();

		i_RH = DHT11_rx_Data();
		d_RH = DHT11_rx_Data();
		i_Tmp = DHT11_rx_Data();
		d_Tmp = DHT11_rx_Data();

		DHT11_DataLine_Output();
		HAL_GPIO_WritePin(DHT11_PORT, DHT11_DATA_RIN, GPIO_PIN_SET);
		printf("[Tmp]%d\n",(int)i_Tmp);
		printf("[Wet]%d\n",(int)i_RH);
		//FND_update(i_Tmp*100 + i_RH);
		if((int)i_Tmp >= 28)
		{
			HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
			HAL_Delay(1000);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_RESET);
		}
	}
}
void DHT11_Init(void)
{
	HAL_GPIO_WritePin(DHT11_PORT, DHT11_DATA_RIN, GPIO_PIN_SET);
	HAL_Delay(3000);
	return;
}


void DHT11_trriger(void)
{
	HAL_GPIO_WritePin(DHT11_PORT, DHT11_DATA_RIN, GPIO_PIN_RESET);
	HAL_Delay(20);

	HAL_GPIO_WritePin(DHT11_PORT, DHT11_DATA_RIN, GPIO_PIN_SET);
	delay_us(7);
	return;
}


void DHT11_DataLine_Input(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	/*Configure GPIO pin : PH0 */
	GPIO_InitStruct.Pin = DHT11_DATA_RIN;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;			//Change Output to Input
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStruct);

	return;
}


void DHT11_DataLine_Output(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	/*Configure GPIO pin : PH0 */
	GPIO_InitStruct.Pin = DHT11_DATA_RIN;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;			//Change Input to Output
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStruct);

	return;
}


uint8_t DHT11_rx_Data(void)
{
	uint8_t rx_data = 0;

	for(int i = 0; i < 8; i++)
	{
		//when Input Data == 0
		while( 0 == HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN) );
#if 1
		delay_us(40);
#else  // org
		delay_us(16);
#endif
		rx_data<<=1;

		//when Input Data == 1
		if(HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN))
		{
			rx_data |= 1;
		}
		while( 1 == HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN) );
	}
	return rx_data;
}


void DHT11_dumi_read(void)
{
	while( 1 == HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN) );
	while( 0 == HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN) );
	while( 1 == HAL_GPIO_ReadPin(DHT11_PORT, DHT11_DATA_RIN) );
	return;
}

 

 

 

'코딩 > STM32' 카테고리의 다른 글

[STM32] Elevator project 동작 동영상  (0) 2021.11.09
[STM32]내부RTC Get/Set 동작  (0) 2021.11.09
[STM32] Stepmotor FOR/BACKWARD/STOP+PC command  (0) 2021.11.09
[STM32] photo coupler LED 점등  (0) 2021.11.09
[STM32] LED / 3 BUTTON SYSTICK  (0) 2021.11.09