Changeset - 255811a3e69c
[Not reviewed]
default
0 1 0
Ethan Zonca - 10 years ago 2014-08-23 22:28:17
ez@ethanzonca.com
Get fractional part
1 file changed with 6 insertions and 0 deletions:
main.c
6
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -81,234 +81,240 @@ int main(void)
 
 
    RCC_ClocksTypeDef RCC_Clocks;
 
 
    // SysTick end of count event each 1ms
 
    RCC_GetClocksFreq(&RCC_Clocks);
 
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
 
 
    GPIO_ResetBits(LED_STAT);
 
    Delay(100);
 
    GPIO_SetBits(LED_POWER);
 
    Delay(500);
 
    GPIO_ResetBits(LED_POWER);
 
 
    init_spi();
 
 
    ssd1306_Init();
 
    ssd1306_block_write();
 
    ssd1306_DrawString("therm 0.1", 0, 40);
 
    ssd1306_DrawString("Status: Idle", 2, 40);
 
  //  ssd1306_block_write();
 
    ssd1306_test();
 
 
    uint8_t toggle = 0;
 
 
    int16_t temp = -231;
 
 
    while(1)
 
    {
 
        //ssd1306_block_write();
 
 
        // Process sensor inputs [TODO: 5hz?]
 
        process();
 
 
        // Run state machine [TODO: 50hz?]
 
        machine(); 
 
        // probably just passed the actual port
 
 
        // TODO: Grab buttonpresses with interrupts
 
        uint8_t sw_btn = GPIO_ReadInputDataBit(SW_BTN);
 
        //uint8_t sw_up = GPIO_ReadInputDataBit(SW_UP);
 
        //uint8_t sw_down = GPIO_ReadInputDataBit(SW_DOWN);
 
        //uint8_t sw_left = GPIO_ReadInputDataBit(SW_LEFT);
 
        //uint8_t sw_right = GPIO_ReadInputDataBit(SW_RIGHT);
 
 
        if(!sw_btn) {
 
            GPIO_ToggleBits(LED_STAT);
 
            if(!toggle) {
 
                GPIO_SetBits(GPIOB, GPIO_Pin_13);
 
                toggle = ! toggle;
 
            }
 
            else  {
 
                GPIO_ResetBits(GPIOB, GPIO_Pin_13);
 
                toggle = ! toggle;
 
            }    
 
        }
 
 
        GPIO_SetBits(LED_POWER);
 
        Delay(50);
 
        GPIO_ResetBits(LED_POWER);
 
        Delay(50);
 
    }
 
}
 
 
int32_t temp = 0;
 
int32_t setpoint = 0;
 
int32_t p = 1;
 
int32_t i = 1;
 
int32_t d = 1;
 
 
 
// Process things
 
void process()
 
{
 
    // Assert CS
 
    GPIO_ResetBits(MAX_CS);
 
    Delay(1);
 
 
    // This may not clock at all... might need to send 16 bits first
 
    SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data
 
    //SPI_I2S_SendData(SPI2, 0xAA); // send dummy data
 
    uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2);
 
 
    if(temp_pre & 0b0000000000000010) {
 
        ssd1306_DrawString("Fatal Error", 3, 35);
 
    }
 
    else if(temp_pre & 0b0000000000000001) {
 
        ssd1306_DrawString("TC Fault", 3, 35);
 
    }
 
    else {
 
        ssd1306_DrawString("TempSense OK", 3, 35);
 
    }
 
 
    uint8_t sign = temp >> 15;// top bit is sign
 
 
 
    temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
    uint8_t frac = temp_pre & 0b11; // get fractional part
 
    frac *= 25; // each bit is .25 a degree, up to fixed point
 
    temp_pre = temp_pre >> 2; // Drop 2 fractional bits 
 
 
    int16_t temp = 0;
 
    if(sign) {
 
        temp = -temp_pre;
 
    }
 
    else {
 
        temp = temp_pre;
 
    }
 
 
 
    // Deassert CS
 
    Delay(1);
 
    GPIO_SetBits(MAX_CS);
 
 
 
    //////////////////////////
 
    // Calc internal temp   //
 
    //////////////////////////
 
/*    temp = temp >> 4; // Drop last 4 bits, no need for them
 
    float internal_temp = temp & 0x7FF // Lower 11bits are internal temp
 
    
 
    // Check internal temp sign
 
    if(temp & 0x800) {
 
        // Convert to negative value by extending sign and casting to signed type.
 
        int16_t tmp = 0xF800 | (temp & 0x7FF);
 
        internal_temp = tmp;
 
    }
 
    internal_temp *= 0.0625; // LSB = 0.0625 degrees
 
    // Now we have a good internal temp!
 
*/
 
    //////////////////////////
 
    // Calc external temp   //
 
    //////////////////////////
 
 
    if(temp > 0) {
 
        GPIO_SetBits(LED_STAT);
 
    }
 
    char tempstr[9];
 
    itoa(temp_pre, tempstr);
 
    ssd1306_DrawString("Temp: ", 1, 40);
 
    ssd1306_DrawString("    ", 1, 70);
 
    ssd1306_DrawString(tempstr, 1, 70);
 
    itoa(frac, tempstr);
 
    ssd1306_DrawString("  ", 1, 90);
 
    ssd1306_DrawString(tempstr, 1, 90);
 
 
/*
 
    if((!retval || (temp & 0x2) != 0))
 
    {
 
        ssd1306_DrawString("!TempCOMMS", 3, 35);
 
        //return; // Comms error - this is happening right now
 
    }
 
 
    else if((temp & 0x4)!= 0)
 
    {
 
        ssd1306_DrawString("!OpenThermocouple", 3, 40);
 
        //return; // Open thermocouple
 
    }
 
 
    temp = (temp & 0x7FF8) >> 5;
 
*/
 
 
    // TODO: Add calibration offset (linear)
 
 
 
    // Perform PID calculations
 
 
    // Write output to SSR
 
}
 
 
 
 
enum state {
 
    STATE_IDLE = 0,
 
    STATE_SETP,
 
    STATE_SETI,
 
    STATE_SETD,
 
 
    STATE_PREHEAT_BREW,
 
    STATE_MAINTAIN_BREW,
 
    STATE_PREHEAT_STEAM,
 
    STATE_MAINTAIN_STEAM,
 
};
 
 
 
uint8_t state = STATE_IDLE;
 
 
// State machine
 
void machine()
 
{
 
    
 
    switch(state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_DrawString("therm :: idle ", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETP;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
        } break;
 
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_DrawString("therm :: set p", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETI;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_DrawString("therm :: set i", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETD;
 
            }
 
 
            // Event Handler
 
            // N/A
0 comments (0 inline, 0 general)