From fb71edfb6bc0702aa2ee1cc73d0822bbc37674ad Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Sun, 24 Jan 2021 09:23:12 +0000 Subject: working-ish sensor --- humidity_sensors/app/gpio.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 humidity_sensors/app/gpio.c (limited to 'humidity_sensors/app/gpio.c') diff --git a/humidity_sensors/app/gpio.c b/humidity_sensors/app/gpio.c new file mode 100644 index 0000000..38d57d3 --- /dev/null +++ b/humidity_sensors/app/gpio.c @@ -0,0 +1,77 @@ +#include "project.h" + +u8 +GPIO_ReadInputData (GPIO_TypeDef *GPIOx) +{ + return ((u8) GPIOx->IDR); +} + +void +GPIO_WriteHigh (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins) +{ + GPIOx->ODR |= (u8) PortPins; +} + +void +GPIO_WriteLow (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins) +{ + GPIOx->ODR &= (u8) (~PortPins); +} + + +/** + * @brief Initializes the GPIOx according to the specified parameters. + * @param GPIOx : Select the GPIO peripheral number (x = A to I). + * @param GPIO_Pin : This parameter contains the pin number, it can be any value + * of the @ref GPIO_Pin_TypeDef enumeration. + * @param GPIO_Mode : This parameter can be a value of the + * @ref GPIO_Mode_TypeDef enumeration. + * @retval None + */ + +void +GPIO_Init (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin, + GPIO_Mode_TypeDef GPIO_Mode) +{ + /*----------------------*/ + /* Check the parameters */ + /*----------------------*/ + + /* Reset corresponding bit to GPIO_Pin in CR2 register */ + GPIOx->CR2 &= (u8) (~ (GPIO_Pin)); + + /*-----------------------------*/ + /* Input/Output mode selection */ + /*-----------------------------*/ + + if ((((u8) (GPIO_Mode)) & (u8) 0x80) != (u8) 0x00) { /* Output mode */ + if ((((u8) (GPIO_Mode)) & (u8) 0x10) != (u8) 0x00) /* High level */ + GPIOx->ODR |= (u8) GPIO_Pin; + else /* Low level */ + GPIOx->ODR &= (u8) (~ (GPIO_Pin)); + + /* Set Output mode */ + GPIOx->DDR |= (u8) GPIO_Pin; + } else { /* Input mode */ + /* Set Input mode */ + GPIOx->DDR &= (u8) (~ (GPIO_Pin)); + } + + /*------------------------------------------------------------------------*/ + /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */ + /*------------------------------------------------------------------------*/ + + if ((((u8) (GPIO_Mode)) & (u8) 0x40) != (u8) 0x00) /* Pull-Up or Push-Pull */ + GPIOx->CR1 |= (u8) GPIO_Pin; + else /* Float or Open-Drain */ + GPIOx->CR1 &= (u8) (~ (GPIO_Pin)); + + /*-----------------------------------------------------*/ + /* Interrupt (Input) or Slope (Output) modes selection */ + /*-----------------------------------------------------*/ + + if ((((u8) (GPIO_Mode)) & (u8) 0x20) != (u8) 0x00) /* Interrupt or Slow slope */ + GPIOx->CR2 |= (u8) GPIO_Pin; + else /* No external interrupt or No slope control */ + GPIOx->CR2 &= (u8) (~ (GPIO_Pin)); +} -- cgit v1.2.3