[DHT11] add testing code, currently not functional
This commit is contained in:
		
							parent
							
								
									9370ddbadd
								
							
						
					
					
						commit
						4b467f6701
					
				
							
								
								
									
										17
									
								
								Core/Inc/DHT11.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Core/Inc/DHT11.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					#ifndef __DHT11
 | 
				
			||||||
 | 
					#define __DHT11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define SKIP_LOW while (!(GPIOD->IDR & 0x0800)) {}
 | 
				
			||||||
 | 
					#define SKIP_HIGH while (GPIOD->IDR & 0x0800) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct DHT11_Data {
 | 
				
			||||||
 | 
						uint8_t humid_integral;
 | 
				
			||||||
 | 
						uint8_t humid_decimal;
 | 
				
			||||||
 | 
						uint8_t temp_integral;
 | 
				
			||||||
 | 
						uint8_t temp_decimal;
 | 
				
			||||||
 | 
						uint8_t crc;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void DHT11_run_test(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
							
								
								
									
										90
									
								
								Core/Src/DHT11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								Core/Src/DHT11.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,90 @@
 | 
				
			|||||||
 | 
					#include "main.h"
 | 
				
			||||||
 | 
					#include "generic_macros.h"
 | 
				
			||||||
 | 
					#include "lcd.h"
 | 
				
			||||||
 | 
					#include "DHT11.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern TIM_HandleTypeDef htim2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void wait(uint32_t wait_us)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						uint32_t target_time = TIM2->CNT;
 | 
				
			||||||
 | 
						target_time += wait_us << 4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						while (TIM2->CNT < target_time) {}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static size_t dht11_measure_high_duration(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						uint32_t start_time = TIM2->CNT;
 | 
				
			||||||
 | 
						SKIP_HIGH;
 | 
				
			||||||
 | 
						uint32_t end_time = TIM2->CNT;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// elapsed_time > 49us ? 1 : 0
 | 
				
			||||||
 | 
						return (end_time - start_time) > (49 << 4);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static size_t dht11_read_value(struct DHT11_Data *data)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						// treat data as a normal array to simplify read loop
 | 
				
			||||||
 | 
						uint8_t *buffer = (uint8_t *) data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						register uint8_t read_register = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						TIM2->CNT = 0;
 | 
				
			||||||
 | 
						HAL_TIM_Base_Start(&htim2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						GPIOD->MODER |= 0x00400000;      // enable output mode on GPIOD 11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						GPIOD->BSRR = GPIO_PIN_11;       // enable DHT11
 | 
				
			||||||
 | 
						wait(18000);                     // hold HIGH for 18 ms
 | 
				
			||||||
 | 
						GPIOD->BSRR = GPIO_PIN_11 << 16; // start signal
 | 
				
			||||||
 | 
						wait(20);                        // hold it for 20 us
 | 
				
			||||||
 | 
						GPIOD->BSRR = GPIO_PIN_11;       // pull up, DHT will now take control over the connection
 | 
				
			||||||
 | 
						wait(40);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						GPIOD->MODER &= 0xFF3FFFFF;      // switch GPIOD 11 to input mode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// reading transmission from DHT11
 | 
				
			||||||
 | 
						SKIP_LOW;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// waiting for 50 ms max
 | 
				
			||||||
 | 
						for (size_t i = TIM2->CNT + (50000 << 4); TIM2->CNT >= i; )
 | 
				
			||||||
 | 
							if (!(GPIOD->IDR & 0x0800))
 | 
				
			||||||
 | 
								goto reading_data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// if didn't exit through goto, then DHT11 awaiting has timed out
 | 
				
			||||||
 | 
						return 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					reading_data:
 | 
				
			||||||
 | 
						// read 5 bytes of data
 | 
				
			||||||
 | 
						for (size_t i = 0; i < 5; i++) {
 | 
				
			||||||
 | 
							for (register size_t j = 0; j < 8; j++) {
 | 
				
			||||||
 | 
								read_register <<= 1;
 | 
				
			||||||
 | 
								SKIP_LOW;
 | 
				
			||||||
 | 
								read_register |= dht11_measure_high_duration();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							buffer[i] = read_register;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						HAL_TIM_Base_Stop(&htim2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void DHT11_run_test(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						DISPLAY_CLEAR;
 | 
				
			||||||
 | 
						display_write_data_seq("DHT11 Temp+Humid");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct DHT11_Data data;
 | 
				
			||||||
 | 
						size_t result = dht11_read_value(&data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (result == 1) {
 | 
				
			||||||
 | 
							DISPLAY_SET_CURSOR(1, 1);
 | 
				
			||||||
 | 
							display_write_data_seq("START TIMEOUT");
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							DISPLAY_SET_CURSOR(1, 1);
 | 
				
			||||||
 | 
							display_write_data_seq("OK");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -78,7 +78,8 @@ void ((*executors[])(void)) = {
 | 
				
			|||||||
		LIS302DL_run_test_dynamic,
 | 
							LIS302DL_run_test_dynamic,
 | 
				
			||||||
		MP45DT02_run_test,
 | 
							MP45DT02_run_test,
 | 
				
			||||||
		LSM9DS1_test_accel,
 | 
							LSM9DS1_test_accel,
 | 
				
			||||||
		LSM9DS1_test_magnet
 | 
							LSM9DS1_test_magnet,
 | 
				
			||||||
 | 
							DHT11_run_test
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ((*cleanup_functions[])(void)) = {
 | 
					void ((*cleanup_functions[])(void)) = {
 | 
				
			||||||
@ -92,6 +93,7 @@ void ((*cleanup_functions[])(void)) = {
 | 
				
			|||||||
		NULL,
 | 
							NULL,
 | 
				
			||||||
		NULL,
 | 
							NULL,
 | 
				
			||||||
		LSM9DS1_cleanup_accel,
 | 
							LSM9DS1_cleanup_accel,
 | 
				
			||||||
 | 
							NULL,
 | 
				
			||||||
		NULL
 | 
							NULL
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -106,6 +108,7 @@ int delay_between_runs[] = {
 | 
				
			|||||||
		200,
 | 
							200,
 | 
				
			||||||
		-1,
 | 
							-1,
 | 
				
			||||||
		-1,
 | 
							-1,
 | 
				
			||||||
 | 
							-1,
 | 
				
			||||||
		-1
 | 
							-1
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -133,7 +136,7 @@ static void MX_I2S2_Init(void);
 | 
				
			|||||||
void switch_to_next_test(void)
 | 
					void switch_to_next_test(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	current_executor_id += 1;
 | 
						current_executor_id += 1;
 | 
				
			||||||
	current_executor_id %= 11;
 | 
						current_executor_id %= 12;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void buttons_switch_to_interrupt(void)
 | 
					void buttons_switch_to_interrupt(void)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user