Changeset - ab94f8a0a0c6
[Not reviewed]
ph
0 5 0
matthewreed - 7 years ago 2018-08-24 20:02:34

Read analog ph sensor
5 files changed with 44 insertions and 15 deletions:
0 comments (0 inline, 0 general)
Inc/config.h
Show inline comments
 
#ifndef _CONFIG_H_
 
#define _CONFIG_H_
 

	
 
#include "stm32f0xx_hal.h"
 

	
 
#define DEFAULT_CAN_ID 0x208
 
#define DEFAULT_BROADCAST_ID 0x00000000
 
#define DEFAULT_DATA_RATE 1000
 
#define DEFAULT_LED_BRIGHTNESS 100
 
#define DEFAULT_LED_BRIGHTNESS 20
 

	
 
typedef struct {
 
    uint8_t x;
 
    uint16_t y;
 
    uint32_t z;
 
} flash_user_vars_t;
 

	
 
#endif //_CONFIG_H_
Inc/ph.h
Show inline comments
 
#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);
 
void ph_init(void);
 
void ph_update(void);
 
float ph_get(void);
 
float ph_convert(uint32_t ph_raw);
 
 
#endif /*_GPIO_H_ */
Makefile
Show inline comments
 
@@ -38,25 +38,26 @@ Application/User \
 
Drivers
 
 
# firmware library path
 
PERIFLIB_PATH = 
 
 
# Build path
 
BUILD_DIR = build
 
 
######################################
 
# source
 
######################################
 
# C sources
 
C_SOURCES =  \
 
C_SOURCES =  \
 
Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_adc.c \
 
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 \
 
@@ -156,24 +157,27 @@ CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT
 
#######################################
 
# 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
 
 
# flash via serial wire debug 
 
flash: all
 
	st-flash --reset write $(BUILD_DIR)/$(TARGET).bin 0x8000000
 
 
#######################################
 
# 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) 
Src/main.c
Show inline comments
 
@@ -4,24 +4,25 @@
 
#include "system.h"
 
#include "led.h"
 
#include "protocol.h"
 
#include "ph.h"
 
 
 
int main(void)
 
{
 
 
    HAL_Init();
 
    system_init();
 
    gpio_init();
 
    ph_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;
 
@@ -45,36 +46,37 @@ int main(void)
 
 
            loop_timer_10ms = HAL_GetTick();
 
        }
 
 
        if (HAL_GetTick() - loop_timer_200ms >= 200)
 
        {
 
 
            loop_timer_200ms = HAL_GetTick();
 
        }
 
 
        if (HAL_GetTick() - loop_timer_1s >= 1000)
 
        {
 
        	ph_update();
 
 
            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());
 
            protocol_send_data(WATER_PH, 0, ph_get());
 
 
            loop_timer_data = HAL_GetTick();
 
        }
 
    }
 
 
}
 
 
bool protocol_set_output(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    return result;
Src/ph.c
Show inline comments
 
#include "ph.h"
 
 
ADC_HandleTypeDef hadc;
 
DMA_HandleTypeDef hdma_adc;
 
 
uint32_t adc_reading;
 
 
float ph_readings[10] = {0};
 
uint8_t ph_i = 0;
 
 
void init(void)
 
void ph_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.Pin = GPIO_PIN_0;
 
    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;
 
@@ -51,22 +47,47 @@ void init(void)
 
    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);
 
 
    //fill the rolling average buffer with real readings so the first reading isn't ~0
 
	for (uint8_t i = 0; i < 10; i++)
 
	{
 
		ph_update();
 
		HAL_Delay(100);
 
	}
 
}
 
 
float get_ph(void)
 
void ph_update(void)
 
{
 
	uint32_t ph_raw = adc_reading;
 
    ph_readings[ph_i] = ph_convert(ph_raw); //convert adc counts to pH
 
    ph_i = (ph_i + 1) % 10;
 
}
 
 
float ph_get(void)
 
{
 
    float ph = 0;
 
 
    ph = (float) adc_reading;
 
    for (uint8_t i = 0; i < 10; i++)
 
    {
 
    	ph += ph_readings[i];
 
    }
 
    ph = ph / 10;
 
 
    return ph;
 
}
 
 
float ph_convert(uint32_t ph_raw)
 
{
 
	float ph = (float)ph_raw;
 
 
	return ph;
 
}
0 comments (0 inline, 0 general)