Changeset - d651f75c8fbc
[Not reviewed]
ph
0 3 2
matthewreed - 8 years ago 2017-10-05 18:41:17

Added ability to read ph meter from analog input
5 files changed with 90 insertions and 1 deletions:
0 comments (0 inline, 0 general)
Inc/ph.h
Show inline comments
 
new file 100644
 
#ifndef _PH_H_
 
#define _PH_H_
 
 
#include "stm32f0xx_hal.h"
 
#include <stdbool.h>
 
#include <led.h>
 
#include <protocol.h>
 
#include <config.h>
 
 
void init(void);
 
float get_ph(void);
 
 
#endif /*_GPIO_H_ */
Inc/stm32f0xx_hal_conf.h
Show inline comments
 
/**
 
  ******************************************************************************
 
  * @file    stm32f0xx_hal_conf.h
 
  * @brief   HAL configuration file.
 
  ******************************************************************************
 
  * @attention
 
  *
 
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
 
  *
 
  * Redistribution and use in source and binary forms, with or without modification,
 
  * are permitted provided that the following conditions are met:
 
  *   1. Redistributions of source code must retain the above copyright notice,
 
  *      this list of conditions and the following disclaimer.
 
  *   2. Redistributions in binary form must reproduce the above copyright notice,
 
  *      this list of conditions and the following disclaimer in the documentation
 
  *      and/or other materials provided with the distribution.
 
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
 
  *      may be used to endorse or promote products derived from this software
 
  *      without specific prior written permission.
 
  *
 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  *
 
  ******************************************************************************
 
  */ 
 
 
/* Define to prevent recursive inclusion -------------------------------------*/
 
#ifndef __STM32F0xx_HAL_CONF_H
 
#define __STM32F0xx_HAL_CONF_H
 
 
#ifdef __cplusplus
 
 extern "C" {
 
#endif
 
 
#include "main.h"
 
/* Exported types ------------------------------------------------------------*/
 
/* Exported constants --------------------------------------------------------*/
 
 
/* ########################## Module Selection ############################## */
 
/**
 
  * @brief This is the list of modules to be used in the HAL driver 
 
  */
 
#define HAL_MODULE_ENABLED  
 
/*#define HAL_ADC_MODULE_ENABLED   */
 
#define HAL_ADC_MODULE_ENABLED
 
/*#define HAL_CRYP_MODULE_ENABLED   */
 
#define HAL_CAN_MODULE_ENABLED
 
/*#define HAL_CEC_MODULE_ENABLED   */
 
/*#define HAL_COMP_MODULE_ENABLED   */
 
/*#define HAL_CRC_MODULE_ENABLED   */
 
/*#define HAL_CRYP_MODULE_ENABLED   */
 
/*#define HAL_TSC_MODULE_ENABLED   */
 
/*#define HAL_DAC_MODULE_ENABLED   */
 
/*#define HAL_I2S_MODULE_ENABLED   */
 
/*#define HAL_IWDG_MODULE_ENABLED   */
 
/*#define HAL_LCD_MODULE_ENABLED   */
 
/*#define HAL_LPTIM_MODULE_ENABLED   */
 
/*#define HAL_RNG_MODULE_ENABLED   */
 
/*#define HAL_RTC_MODULE_ENABLED   */
 
/*#define HAL_SPI_MODULE_ENABLED   */
 
#define HAL_TIM_MODULE_ENABLED
 
/*#define HAL_UART_MODULE_ENABLED   */
 
/*#define HAL_USART_MODULE_ENABLED   */
 
/*#define HAL_IRDA_MODULE_ENABLED   */
 
/*#define HAL_SMARTCARD_MODULE_ENABLED   */
 
/*#define HAL_SMBUS_MODULE_ENABLED   */
 
/*#define HAL_WWDG_MODULE_ENABLED   */
 
/*#define HAL_PCD_MODULE_ENABLED   */
 
#define HAL_CORTEX_MODULE_ENABLED
 
#define HAL_DMA_MODULE_ENABLED
 
#define HAL_FLASH_MODULE_ENABLED
 
#define HAL_GPIO_MODULE_ENABLED
 
#define HAL_PWR_MODULE_ENABLED
 
#define HAL_RCC_MODULE_ENABLED
 
#define HAL_I2C_MODULE_ENABLED
 
 
/* ########################## HSE/HSI Values adaptation ##################### */
 
/**
 
  * @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
 
  *        This value is used by the RCC HAL module to compute the system frequency
 
  *        (when HSE is used as system clock source, directly or through the PLL).  
 
  */
 
#if !defined  (HSE_VALUE) 
 
  #define HSE_VALUE    ((uint32_t)12000000) /*!< Value of the External oscillator in Hz */
 
#endif /* HSE_VALUE */
 
 
/**
 
  * @brief In the following line adjust the External High Speed oscillator (HSE) Startup 
 
  *        Timeout value 
 
  */
 
#if !defined  (HSE_STARTUP_TIMEOUT)
 
  #define HSE_STARTUP_TIMEOUT    ((uint32_t)100)   /*!< Time out for HSE start up, in ms */
 
#endif /* HSE_STARTUP_TIMEOUT */
 
 
/**
 
  * @brief Internal High Speed oscillator (HSI) value.
 
  *        This value is used by the RCC HAL module to compute the system frequency
 
  *        (when HSI is used as system clock source, directly or through the PLL). 
 
  */
 
#if !defined  (HSI_VALUE)
 
  #define HSI_VALUE    ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
 
#endif /* HSI_VALUE */
 
 
/**
 
  * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup 
 
  *        Timeout value 
 
  */
 
#if !defined  (HSI_STARTUP_TIMEOUT) 
 
 #define HSI_STARTUP_TIMEOUT   ((uint32_t)5000) /*!< Time out for HSI start up */
 
#endif /* HSI_STARTUP_TIMEOUT */  
 
 
/**
 
  * @brief Internal High Speed oscillator for ADC (HSI14) value.
 
  */
 
#if !defined  (HSI14_VALUE) 
 
#define HSI14_VALUE ((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz.
 
                                             The real value may vary depending on the variations
 
                                             in voltage and temperature.  */
 
#endif /* HSI14_VALUE */
 
 
/**
 
  * @brief Internal High Speed oscillator for USB (HSI48) value.
 
  */
 
#if !defined  (HSI48_VALUE) 
 
#define HSI48_VALUE ((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz.
 
                                             The real value may vary depending on the variations
 
                                             in voltage and temperature.  */
 
#endif /* HSI48_VALUE */
 
 
/**
 
  * @brief Internal Low Speed oscillator (LSI) value.
 
  */
 
#if !defined  (LSI_VALUE) 
 
 #define LSI_VALUE  ((uint32_t)40000)    
 
#endif /* LSI_VALUE */                      /*!< Value of the Internal Low Speed oscillator in Hz
 
                                             The real value may vary depending on the variations
 
                                             in voltage and temperature.  */
 
/**
 
  * @brief External Low Speed oscillator (LSI) value.
 
  */
 
#if !defined  (LSE_VALUE)
 
 #define LSE_VALUE  ((uint32_t)32768)    /*!< Value of the External Low Speed oscillator in Hz */
 
#endif /* LSE_VALUE */     
 
 
#if !defined  (LSE_STARTUP_TIMEOUT)
 
  #define LSE_STARTUP_TIMEOUT    ((uint32_t)5000)   /*!< Time out for LSE start up, in ms */
 
#endif /* LSE_STARTUP_TIMEOUT */
 
 
/* Tip: To avoid modifying this file each time you need to use different HSE,
 
   ===  you can define the HSE value in your toolchain compiler preprocessor. */
 
 
/* ########################### System Configuration ######################### */
 
/**
 
  * @brief This is the HAL system configuration section
 
  */     
 
#define  VDD_VALUE                    ((uint32_t)3300) /*!< Value of VDD in mv */           
 
#define  TICK_INT_PRIORITY            ((uint32_t)0)    /*!< tick interrupt priority (lowest by default)  */            
 
                                                                              /*  Warning: Must be set to higher priority for HAL_Delay()  */
 
                                                                              /*  and HAL_GetTick() usage under interrupt context          */
 
#define  USE_RTOS                     0     
 
#define  PREFETCH_ENABLE              1              
 
#define  INSTRUCTION_CACHE_ENABLE     0
 
#define  DATA_CACHE_ENABLE            0
 
/* ########################## Assert Selection ############################## */
 
/**
 
  * @brief Uncomment the line below to expanse the "assert_param" macro in the 
 
  *        HAL drivers code
 
  */
 
/* #define USE_FULL_ASSERT   1 */
 
 
/* ################## SPI peripheral configuration ########################## */
 
 
/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver
 
* Activated: CRC code is present inside driver
 
* Deactivated: CRC code cleaned from driver
 
*/
 
 
#define USE_SPI_CRC                     0U
 
 
/* Includes ------------------------------------------------------------------*/
 
/**
 
  * @brief Include module's header file 
 
  */
 
 
#ifdef HAL_RCC_MODULE_ENABLED
 
 #include "stm32f0xx_hal_rcc.h"
 
#endif /* HAL_RCC_MODULE_ENABLED */
 
 
#ifdef HAL_GPIO_MODULE_ENABLED
 
 #include "stm32f0xx_hal_gpio.h"
 
#endif /* HAL_GPIO_MODULE_ENABLED */
 
 
#ifdef HAL_DMA_MODULE_ENABLED
 
  #include "stm32f0xx_hal_dma.h"
 
#endif /* HAL_DMA_MODULE_ENABLED */
 
 
#ifdef HAL_CORTEX_MODULE_ENABLED
 
 #include "stm32f0xx_hal_cortex.h"
 
#endif /* HAL_CORTEX_MODULE_ENABLED */
 
 
#ifdef HAL_ADC_MODULE_ENABLED
 
 #include "stm32f0xx_hal_adc.h"
 
#endif /* HAL_ADC_MODULE_ENABLED */
 
 
#ifdef HAL_CAN_MODULE_ENABLED
 
 #include "stm32f0xx_hal_can.h"
 
#endif /* HAL_CAN_MODULE_ENABLED */
 
 
#ifdef HAL_CEC_MODULE_ENABLED
 
 #include "stm32f0xx_hal_cec.h"
 
#endif /* HAL_CEC_MODULE_ENABLED */
 
 
#ifdef HAL_COMP_MODULE_ENABLED
 
 #include "stm32f0xx_hal_comp.h"
 
#endif /* HAL_COMP_MODULE_ENABLED */
 
 
#ifdef HAL_CRC_MODULE_ENABLED
 
 #include "stm32f0xx_hal_crc.h"
 
#endif /* HAL_CRC_MODULE_ENABLED */
 
 
#ifdef HAL_DAC_MODULE_ENABLED
 
 #include "stm32f0xx_hal_dac.h"
 
#endif /* HAL_DAC_MODULE_ENABLED */
 
 
#ifdef HAL_FLASH_MODULE_ENABLED
 
 #include "stm32f0xx_hal_flash.h"
 
#endif /* HAL_FLASH_MODULE_ENABLED */
 
 
#ifdef HAL_I2C_MODULE_ENABLED
 
 #include "stm32f0xx_hal_i2c.h"
 
#endif /* HAL_I2C_MODULE_ENABLED */
 
 
#ifdef HAL_I2S_MODULE_ENABLED
 
 #include "stm32f0xx_hal_i2s.h"
 
#endif /* HAL_I2S_MODULE_ENABLED */
 
 
#ifdef HAL_IRDA_MODULE_ENABLED
Makefile
Show inline comments
 
##########################################################################################################################
 
# File automatically-generated by tool: [projectgenerator] version: [2.25.0] date: [Thu Sep 28 21:25:25 EDT 2017]
 
##########################################################################################################################
 
 
# ------------------------------------------------
 
# Generic Makefile (based on gcc)
 
#
 
# ChangeLog :
 
#	2017-02-10 - Several enhancements + project update mode
 
#   2015-07-22 - first version
 
# ------------------------------------------------
 
 
######################################
 
# target
 
######################################
 
TARGET = protomodule-software
 
 
 
######################################
 
# building variables
 
######################################
 
# debug build?
 
DEBUG = 1
 
# optimization
 
OPT = -Og
 
 
 
#######################################
 
# paths
 
#######################################
 
# source path
 
SOURCES_DIR =  \
 
Drivers/CMSIS \
 
Drivers/STM32F0xx_HAL_Driver \
 
Application/MAKEFILE \
 
Application \
 
Application/User \
 
Drivers
 
 
# firmware library path
 
PERIFLIB_PATH = 
 
 
# Build path
 
BUILD_DIR = build
 
 
######################################
 
# source
 
######################################
 
# C sources
 
C_SOURCES =  \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_can.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c_ex.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc_ex.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dma.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr_ex.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim_ex.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash_ex.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_cortex.c \
 
Src/gpio.c \
 
Src/main.c \
 
Src/stm32f0xx_hal_msp.c \
 
Src/stm32f0xx_it.c \
 
Src/system_stm32f0xx.c \
 
Src/system.c \
 
Src/ph.c \
 
hydrobot-sharedlibs/src/can_buffer.c \
 
hydrobot-sharedlibs/src/can.c \
 
hydrobot-sharedlibs/src/flash.c \
 
hydrobot-sharedlibs/src/led.c \
 
hydrobot-sharedlibs/src/protocol.c
 
 
# ASM sources
 
ASM_SOURCES =  \
 
startup_stm32f042x6.s
 
 
 
######################################
 
# firmware library
 
######################################
 
PERIFLIB_SOURCES = 
 
 
 
#######################################
 
# binaries
 
#######################################
 
BINPATH = 
 
PREFIX = arm-none-eabi-
 
CC = $(PREFIX)gcc
 
AS = $(PREFIX)gcc -x assembler-with-cpp
 
CP = $(PREFIX)objcopy
 
AR = $(PREFIX)ar
 
SZ = $(PREFIX)size
 
HEX = $(CP) -O ihex
 
BIN = $(CP) -O binary -S
 
 
 
#######################################
 
# CFLAGS
 
#######################################
 
# cpu
 
CPU = -mcpu=cortex-m0
 
 
# fpu
 
# NONE for Cortex-M0/M0+/M3
 
 
# float-abi
 
 
 
# mcu
 
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
 
 
# macros for gcc
 
# AS defines
 
AS_DEFS = 
 
 
# C defines
 
C_DEFS =  \
 
-DUSE_HAL_DRIVER \
 
-DSTM32F042x6
 
 
 
# AS includes
 
AS_INCLUDES = 
 
 
# C includes
 
C_INCLUDES =  \
 
-IInc \
 
-IDrivers/STM32F0xx_HAL_Driver/Inc \
 
-IDrivers/STM32F0xx_HAL_Driver/Inc/Legacy \
 
-IDrivers/CMSIS/Device/ST/STM32F0xx/Include \
 
-IDrivers/CMSIS/Include \
 
-Ihydrobot-sharedlibs/inc
 
 
# compile gcc flags
 
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
 
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
 
ifeq ($(DEBUG), 1)
 
CFLAGS += -g -gdwarf-2
 
endif
 
 
 
# Generate dependency information
 
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
 
 
 
#######################################
 
# LDFLAGS
 
#######################################
 
# link script
 
LDSCRIPT = STM32F042K6Tx_FLASH.ld
 
 
# libraries
 
LIBS = -lc -lm -lnosys
 
LIBDIR =
 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
 
 
# default action: build all
 
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
 
 
#######################################
 
# build the application
 
#######################################
 
# list of objects
 
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
 
vpath %.c $(sort $(dir $(C_SOURCES)))
 
# list of ASM program objects
 
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
 
vpath %.s $(sort $(dir $(ASM_SOURCES)))
 
 
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
 
 
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
 
	$(AS) -c $(CFLAGS) $< -o $@
 
 
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
 
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
 
	$(SZ) $@
 
 
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
 
	$(HEX) $< $@
 
	
 
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
 
	$(BIN) $< $@	
 
	
 
$(BUILD_DIR):
 
	mkdir $@		
 
 
#######################################
 
# clean up
 
#######################################
 
clean:
 
	-rm -fR .dep $(BUILD_DIR)
 
  
 
#######################################
 
# dependencies
 
#######################################
 
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
 
 
# *** EOF ***
 
\ No newline at end of file
Src/main.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "can.h"
 
#include "gpio.h"
 
#include "system.h"
 
#include "led.h"
 
#include "protocol.h"
 
#include "ph.h"
 
 
 
int main(void)
 
{
 
 
    HAL_Init();
 
    system_init();
 
    gpio_init();
 
 
    protocol_init(PROTOMODULE);
 
 
    led_init();
 
    led_blink(LED_STATUS, 2000);
 
    led_blink_once(LED_CAN, 2000);
 
    led_set(LED_ERROR, 0);
 
 
    uint32_t loop_timer_1ms = 0;
 
    uint32_t loop_timer_10ms = 0;
 
    uint32_t loop_timer_200ms = 0;
 
    uint32_t loop_timer_1s = 0;
 
    uint32_t loop_timer_2s = 0;
 
    uint32_t loop_timer_data = 0;
 
 
    while (1)
 
    {
 
        can_receive();
 
 
        if (HAL_GetTick() - loop_timer_1ms >= 1)
 
        {
 
            can_process_receive_buffer();
 
            led_update_all();
 
 
            loop_timer_1ms = HAL_GetTick();
 
        }
 
 
        if (HAL_GetTick() - loop_timer_10ms >= 10)
 
        {
 
 
            loop_timer_10ms = HAL_GetTick();
 
        }
 
 
        if (HAL_GetTick() - loop_timer_200ms >= 200)
 
        {
 
 
            loop_timer_200ms = HAL_GetTick();
 
        }
 
 
        if (HAL_GetTick() - loop_timer_1s >= 1000)
 
        {
 
 
            loop_timer_1s = HAL_GetTick();
 
        }
 
        if (HAL_GetTick() - loop_timer_2s >= 2000)
 
        {
 
 
            loop_timer_2s = HAL_GetTick();
 
        }
 
        if (HAL_GetTick() - loop_timer_data >= (protocol_get_settings()->val.data_rate))
 
        {
 
 
            protocol_send_data(NONE, 0, get_ph());
 
 
            loop_timer_data = HAL_GetTick();
 
        }
 
    }
 
 
}
 
 
bool protocol_set_output(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    return result;
 
}
 
 
bool protocol_get_data(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    return result;
 
}
 
 
bool protocol_config(protocol_message_t* message)
 
{
 
    bool result = false;
 
    protocol_data_key_t data = (protocol_data_key_t)(uint16_t)message->data.float_data;
 
 
    return result;
 
}
 
 
bool protocol_calibrate(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    return result;
 
}
Src/ph.c
Show inline comments
 
new file 100644
 
#include "ph.h"
 
 
ADC_HandleTypeDef hadc;
 
DMA_HandleTypeDef hdma_adc;
 
 
uint32_t adc_reading;
 
 
 
void init(void)
 
{
 
    __HAL_RCC_ADC1_CLK_ENABLE();
 
    __HAL_RCC_DMA1_CLK_ENABLE();
 
 
    GPIO_InitTypeDef GPIO_InitStruct;
 
 
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_7;
 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
    GPIO_InitStruct.Pin = GPIO_PIN_0;
 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
    ADC_ChannelConfTypeDef sConfig;
 
 
    hadc.Instance = ADC1;
 
    hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
 
    hadc.Init.Resolution = ADC_RESOLUTION_12B;
 
    hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 
    hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
 
    hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 
    hadc.Init.LowPowerAutoWait = DISABLE;
 
    hadc.Init.LowPowerAutoPowerOff = DISABLE;
 
    hadc.Init.ContinuousConvMode = ENABLE;
 
    hadc.Init.DiscontinuousConvMode = DISABLE;
 
    hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 
    hadc.Init.DMAContinuousRequests = ENABLE;
 
    hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
 
    HAL_ADC_Init(&hadc);
 
 
 
    // Configure DMA
 
    hdma_adc.Instance = DMA1_Channel1;
 
    hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;
 
    hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;
 
    hdma_adc.Init.MemInc = DMA_MINC_ENABLE;
 
    hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
 
    hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
 
    hdma_adc.Init.Mode = DMA_CIRCULAR;
 
    hdma_adc.Init.Priority = DMA_PRIORITY_LOW;
 
    HAL_DMA_Init(&hdma_adc);
 
 
    __HAL_LINKDMA(&hadc, DMA_Handle, hdma_adc);
 
 
    sConfig.Channel = ADC_CHANNEL_0;
 
    sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
 
    sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
 
    HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
 
    HAL_ADC_Start_DMA(&hadc, &adc_reading, 1);
 
}
 
 
float get_ph(void)
 
{
 
    float ph = 0;
 
 
    ph = (float) adc_reading;
 
 
    return ph;
 
}
0 comments (0 inline, 0 general)