Changeset - d651f75c8fbc
[Not reviewed]
ph
0 3 2
matthewreed - 7 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
 
@@ -49,7 +49,7 @@
 
  * @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   */
Makefile
Show inline comments
 
@@ -69,6 +69,7 @@ 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 \
Src/main.c
Show inline comments
 
@@ -4,6 +4,7 @@
 
#include "system.h"
 
#include "led.h"
 
#include "protocol.h"
 
#include "ph.h"
 
 
 
int main(void)
 
@@ -64,6 +65,8 @@ int main(void)
 
        if (HAL_GetTick() - loop_timer_data >= (protocol_get_settings()->val.data_rate))
 
        {
 
 
            protocol_send_data(NONE, 0, get_ph());
 
 
            loop_timer_data = HAL_GetTick();
 
        }
 
    }
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)