Changeset - 512edacc3586
[Not reviewed]
cortex-f0
0 3 2
Ethan Zonca - 9 years ago 2015-08-23 18:52:28
ez@ethanzonca.com
Factor out temperature sensor read operations
5 files changed with 122 insertions and 91 deletions:
0 comments (0 inline, 0 general)
Makefile
Show inline comments
 
@@ -10,7 +10,7 @@
 

	
 

	
 
# SOURCES: list of sources in the user application
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c storage.c flash.c
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c storage.c flash.c max31855.c
 

	
 
# TARGET: name of the user application
 
TARGET = main
config.h
Show inline comments
 
#ifndef CONFIG_H
 
#define CONFIG_H
 

	
 
// Temperature sensor type
 
#define MAX31855_TC_SENSOR
 
//#define MAX31865_RTD_SENSOR
 

	
 

	
 
// Virtual serial port transmit rate
 
#define VCP_TX_FREQ 1000
 

	
 
// Solid-state relay maximum on-time
 
#define SSR_PERIOD 200
 

	
 
// Interval of PID calculations
 
#define PID_PERIOD 120
 

	
 

	
 

	
 
// Pin settings
 
#define LED_POWER GPIOF,GPIO_PIN_0
 

	
 
#define MAX_CS GPIOA,GPIO_PIN_15
 

	
 
#define SW_BTN  GPIOB, GPIO_PIN_4
main.c
Show inline comments
 
@@ -3,6 +3,7 @@
 
#include "config.h"
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "max31855.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "flash.h"
 
@@ -147,94 +148,6 @@ void SystemClock_Config(void)
 
 
}
 
 
 
// Grab temperature reading from MAX31855
 
void update_temp() {
 
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 
 
    uint8_t rxdatah[1] = {0x00};
 
    uint8_t rxdatal[1] = {0x00};
 
 
    HAL_SPI_Receive(&hspi1, rxdatah, 1, 100);
 
    HAL_SPI_Receive(&hspi1, rxdatal, 1, 100);
 
 
    // Release CS
 
    HAL_GPIO_WritePin(MAX_CS, 1);
 
 
    // Assemble data array into one var
 
    uint16_t temp_pre = rxdatal[0] | (rxdatah[0]<<8);
 
/*
 
    if(temp_pre & 0b010) {
 
        ssd1306_clearscreen();
 
        HAL_Delay(400); // FIXME: remove?
 
        status.tc_errno = 4;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    } */
 
    if(temp_pre & 0b001 && !set.ignore_tc_error) {
 
        status.tc_errno = 1;
 
        HAL_Delay(400); // FIXME: remove?
 
        status.state_resume = status.state;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    }/*
 
    else if(temp_pre & 0b100 && !set.ignore_tc_error) {
 
        status.tc_errno = 8;
 
        HAL_Delay(400); // FIXME: remove?
 
        status.state_resume = status.state;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    }*/
 
    else 
 
    {
 
        //if(status.state == STATE_TC_ERROR)
 
        //{
 
        //    status.state = status.state_resume;
 
        //    ssd1306_clearscreen();
 
        //}
 
 
        uint8_t sign = status.temp >> 15;// top bit is sign
 
 
        temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
        status.temp_frac = temp_pre & 0b11; // get fractional part
 
        status.temp_frac *= 25; // each bit is .25 a degree, up to fixed point
 
        temp_pre = temp_pre >> 2; // Drop 2 fractional bits 
 
 
        int8_t signint;
 
 
        if(sign) {
 
            signint = -1;
 
        }
 
        else {
 
            signint = 1;
 
        }
 
 
        // Convert to Fahrenheit
 
        if(set.temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
            status.temp = signint * ((temp_pre*100) + status.temp_frac);
 
            status.temp = status.temp * 1.8;
 
            status.temp += 3200;
 
            status.temp_frac = status.temp % 100;
 
            status.temp /= 100;
 
            status.temp += set.temp_offset;
 
        }
 
 
        // Use Celsius values
 
        else
 
        {
 
            status.temp = temp_pre * signint;
 
            status.temp += set.temp_offset;
 
        }
 
    }
 
}
 
 
 
// PID implementation
 
// TODO: Make struct that has the last_temp and i_state in it, pass by ref. Make struct that has other input values maybe.
 
int16_t last_pid_temp = 0;
 
@@ -308,7 +221,13 @@ void process()
 
 
    if((ticks - last_pid > PID_PERIOD))
 
    {
 
        update_temp(); // Read MAX31855
 
        #ifdef MAX31855_TC_SENSOR
 
        max31855_readtemp(&hspi1, &set, &status); // Read MAX31855
 
        #endif
 
 
        #ifdef MAX31865_RTD_SENSOR
 
        max31865_readtemp(&set, &status);
 
        #endif
 
 
    HAL_GPIO_TogglePin(LED_POWER);
 
max31855.c
Show inline comments
 
new file 100644
 
#include "stm32f0xx_hal.h"
 

	
 
#include "config.h"
 
#include "stringhelpers.h"
 
#include "states.h"
 
#include "gpio.h"
 

	
 
// Grab temperature reading from MAX31855
 
void max31855_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status)
 
{
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 

	
 
    uint8_t rxdatah[1] = {0x00};
 
    uint8_t rxdatal[1] = {0x00};
 

	
 
    HAL_SPI_Receive(hspi1, rxdatah, 1, 100);
 
    HAL_SPI_Receive(hspi1, rxdatal, 1, 100);
 

	
 
    // Release CS
 
    HAL_GPIO_WritePin(MAX_CS, 1);
 

	
 
    // Assemble data array into one var
 
    uint16_t temp_pre = rxdatal[0] | (rxdatah[0]<<8);
 
/*
 
    if(temp_pre & 0b010) {
 
        ssd1306_clearscreen();
 
        HAL_Delay(400); // FIXME: remove?
 
        status.tc_errno = 4;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    } */
 
    if(temp_pre & 0b001 && !set->ignore_tc_error) {
 
        status->tc_errno = 1;
 
        HAL_Delay(400); // FIXME: remove?
 
        status->state_resume = status->state;
 
        status->state = STATE_TC_ERROR;
 
        status->temp = 0;
 
        status->temp_frac = 0;
 
    }/*
 
    else if(temp_pre & 0b100 && !set.ignore_tc_error) {
 
        status.tc_errno = 8;
 
        HAL_Delay(400); // FIXME: remove?
 
        status.state_resume = status.state;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    }*/
 
    else 
 
    {
 
        //if(status.state == STATE_TC_ERROR)
 
        //{
 
        //    status.state = status.state_resume;
 
        //    ssd1306_clearscreen();
 
        //}
 

	
 
        uint8_t sign = status->temp >> 15;// top bit is sign
 

	
 
        temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
        status->temp_frac = temp_pre & 0b11; // get fractional part
 
        status->temp_frac *= 25; // each bit is .25 a degree, up to fixed point
 
        temp_pre = temp_pre >> 2; // Drop 2 fractional bits 
 

	
 
        int8_t signint;
 

	
 
        if(sign) {
 
            signint = -1;
 
        }
 
        else {
 
            signint = 1;
 
        }
 

	
 
        // Convert to Fahrenheit
 
        if(set->temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
            status->temp = signint * ((temp_pre*100) + status->temp_frac);
 
            status->temp = status->temp * 1.8;
 
            status->temp += 3200;
 
            status->temp_frac = status->temp % 100;
 
            status->temp /= 100;
 
            status->temp += set->temp_offset;
 
        }
 

	
 
        // Use Celsius values
 
        else
 
        {
 
            status->temp = temp_pre * signint;
 
            status->temp += set->temp_offset;
 
        }
 
    }
 
}
 

	
 

	
max31855.h
Show inline comments
 
new file 100644
 
#ifndef MAX31855_H
 
#define MAX31855_H
 

	
 
void max31855_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status);
 

	
 
#endif
0 comments (0 inline, 0 general)