BASIC4MCU | 질문게시판 | 미세먼지 센서를 해보는데
페이지 정보
작성자 라칸 작성일2023-01-30 15:56 조회5,014회 댓글3건본문
stm32에서 sds011 미세먼지 센서를 uart로 연결시켜서 데이터를 읽어오게하려는데
데이터를 1개도 못가져오고 있습니다
처음에서는 dma같은걸로 했는데
data header는 0xAA, data tail는 0xAB가 되야하는데 둘다 안나와서요
인터넷를 좀더 찾아봐서 예시 프로젝트 내용을 가져와서 하는데
receive로 데이터를 못가져오는지 data_receive에 아무것도 안나오고 있습니다 이걸 어떻게 해야하나요?
뭐가 문제인데 receive도 안되는건지 알아야 뭐 수정을하는데
sdsInit(&sds, &huart8); 부분에서는 반응이 있어서 huart_sds에 huart8이 나오는데 수신 데이터 나오는 곳에는 0x00만 있는걸 보면 수신자체가 안되고 있는거 같습니다
일단 미세먼지 센서의 팬?이 돌아가고 있는거 같고 전압도 출력이 나오고 있습니다
sds011.c, sds011.h는 그대로 쓰고 있습니다
sds011 datasheet : https://cdn-reichelt.de/documents/datenblatt/X200/SDS011-DATASHEET.pdf
example : https://github.com/SimpleMethod/STM32-SDS011
[code]
SDS sds; uint8_t DUST_Data[50]; uint16_t dust_size = 0; int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MPU Configuration--------------------------------------------------------*/ MPU_Config(); /* Enable I-Cache---------------------------------------------------------*/ SCB_EnableICache(); /* Enable D-Cache---------------------------------------------------------*/ SCB_EnableDCache(); /* 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_DMA_Init(); MX_TIM6_Init(); MX_UART7_Init(); MX_USART3_UART_Init(); MX_UART5_Init(); MX_USART1_UART_Init(); MX_USART6_UART_Init(); MX_UART8_Init(); MX_SPI2_Init(); MX_SPI5_Init(); MX_I2C1_Init(); MX_RNG_Init(); MX_TIM3_Init(); MX_USB_OTG_FS_PCD_Init(); MX_RTC_Init(); MX_TIM7_Init(); MX_FMC_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_DMA(&huart3, (uint8_t*)RX_BUFFER3, RX_BUFFER3_SIZE); HAL_UART_Receive_DMA(&huart6, (uint8_t*)RX_BUFFER6, RX_BUFFER_SIZE); sdsInit(&sds, &huart8); /* USER CODE END 2 */ /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ HAL_TIM_Base_Start_IT(&htim6); HAL_TIM_Base_Start_IT(&htim7); /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* definition and creation of defaultTask */ osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 256); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL); /* definition and creation of TcpTask */ osThreadDef(TcpTask, Tcp_Task, osPriorityIdle, 0, 256); TcpTaskHandle = osThreadCreate(osThread(TcpTask), NULL); /* definition and creation of Usart3Task */ osThreadDef(Usart3Task, Usart3_Task, osPriorityIdle, 0, 256); Usart3TaskHandle = osThreadCreate(osThread(Usart3Task), NULL); /* definition and creation of Uart7Task */ osThreadDef(Uart7Task, Uart7_Task, osPriorityIdle, 0, 256); Uart7TaskHandle = osThreadCreate(osThread(Uart7Task), NULL); /* definition and creation of Uart5Task */ osThreadDef(Uart5Task, Uart5_Task, osPriorityIdle, 0, 256); Uart5TaskHandle = osThreadCreate(osThread(Uart5Task), NULL); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ // HAL_UART_Transmit(&huart3,"START! READY!\r\n",13,0xFFFFFFFF); /* USER CODE END RTOS_THREADS */ /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
//************** void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){ sds_uart_RxCpltCallback(&sds,huart); }
//********************************** void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == TIM7) { dust_size = sprintf(DUST_Data, "%d %d \n\r", sdsGetPm2_5(&sds), sdsGetPm10(&sds)); HAL_UART_Transmit(&huart3, DUST_Data, dust_size, HAL_MAX_DELAY); } }
[/code]
sds011.c
[code]
#include "sds011.h" uint8_t Sds011_SleepCommand[] = { 0xAA, 0xB4, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB }; uint8_t Sds011_WorkingMode[] = { 0xAA, 0xB4, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0xAB }; /** * Library initialization. * * @param sds pointer to SDS_t handle structure * @param huart_sds pointer to UART handle structure */ void sdsInit(SDS* sds, const UART_HandleTypeDef* huart_sds) { sds->huart_sds=(UART_HandleTypeDef *)huart_sds; HAL_UART_Transmit(sds->huart_sds,(uint8_t*)Sds011_WorkingMode, 19,30); HAL_UART_Receive_IT(sds->huart_sds, sds->data_receive, 10); } /** * Calculation of the checksum * * @param buff array with a message to send to the device * @return calculated checksum */ uint8_t getCRC(uint8_t buff[]) { uint8_t crc = 0; for (uint8_t i = 2; i < 17; ++i) { crc += buff[i]; } return crc; } /** * Enter the device into sleep mode * * @param sds pointer to SDS_t handle structure * @return status of the message transmission */ int8_t sdsSleepMode(SDS* sds) { return HAL_UART_Transmit(sds->huart_sds, (uint8_t*)Sds011_SleepCommand,19,30)==HAL_OK ? 1:0; } /** * Enter the device into working mode * * @param sds pointer to SDS_t handle structure * @return status of the message transmission */ int8_t sdsWorkingMode(SDS* sds) { return HAL_UART_Transmit(sds->huart_sds, (uint8_t*)Sds011_WorkingMode,19,30)==HAL_OK ? 1:0; } /** * Send any message to the device * * @param sds pointer to SDS_t handle structure * @param data_buffer array with a message to send to the device * @param message length * @return status of the message transmission */ int8_t sdsSend(SDS* sds, const uint8_t* data_buffer, const uint8_t length) { return HAL_UART_Transmit(sds->huart_sds,(uint8_t *) data_buffer,length,30)==HAL_OK ? 1:0; } /** * Getting the value of PM 2.5 * * @param sds pointer to SDS_t handle structure * @return value of PM 2.5 */ uint16_t sdsGetPm2_5(SDS* sds) { return sds->pm_2_5; } /** * Getting the value of PM 10 * * @param sds pointer to SDS_t handle structure * @return value of PM 10 */ uint16_t sdsGetPm10(SDS* sds) { return sds->pm_10; } /** * UART Rx Complete Callback * * @param sds pointer to SDS_t handle structure * @param huart pointer to UART handle structure */ void sds_uart_RxCpltCallback(SDS* sds, UART_HandleTypeDef *huart) { if(huart == sds->huart_sds) { if((sds->data_receive[1] == 0xC0)) { sds->pm_2_5 = ((sds->data_receive[3]<<8)| sds->data_receive[2])/10; sds->pm_10 = ((sds->data_receive[5]<<8)| sds->data_receive[4])/10; } HAL_UART_Receive_IT(sds->huart_sds, sds->data_receive, 10); } }[/code]sds011.h[code] #include "stm32h7xx_hal.h" typedef struct SDS_t { UART_HandleTypeDef* huart_sds; uint16_t pm_2_5; uint16_t pm_10; uint8_t data_receive[19]; } SDS; void sdsInit(SDS* sds, const UART_HandleTypeDef* huart_sds); void sds_uart_RxCpltCallback(SDS* sds, UART_HandleTypeDef *huart); int8_t sdsSend(SDS* sds, const uint8_t *data_buffer, const uint8_t length); uint16_t sdsGetPm2_5(SDS* sds); uint16_t sdsGetPm10(SDS* sds); int8_t sdsWorkingMode(SDS* sds); int8_t sdsSleepMode(SDS* sds); uint8_t getCRC(uint8_t buff[]);[/code]
댓글 3
조회수 5,014master님의 댓글
master 작성일
UART 기본 예제로
1문자 송신 해보고
1문자 수신 해보고
몇개의 문자 송신 해보고
몇개의 문자 수신 해보고
그러고 난 후에 통신 프로토콜을 공부하셔야 합니다.
혼자서 할 때는 오실로스코프나 로직아날라이저 및 PC 통신포트 등을 활용해서 체크 하는 것이 좋습니다.
라칸님의 댓글
라칸 작성일
오실로스코프밖에 없어서 확인하기가 힘든데
일단 미세먼지 센서의 팬?이 돌아가고 있는거 같고
2.5μm(10) PWM 부분과 1μm(2.5) PWM 부분에는 오실로스코프로 찍어봤는데 전압이 안나오고 있고 UART Receive, Transmit에서는 전압이 나오고 있는데
master님의 댓글
master 작성일오실로스코프만 있어도 가능할텐데요