Changeset - 7e79a737e34d
[Not reviewed]
cortex-f0
0 1 0
Ethan Zonca - 10 years ago 2015-01-25 15:42:17
ez@ethanzonca.com
Cleanup
1 file changed with 26 insertions and 36 deletions:
main.c
26
36
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -85,15 +85,12 @@ int main(void)
 
    MX_USB_DEVICE_Init();
 
 
    // USB startup delay
 
    HAL_Delay(1000);
 
    HAL_GPIO_WritePin(LED_POWER, 1);
 
 
 
    /// BEGIN IMPORT/////////////////////////////////////////
 
    
 
    // TODO: Awesome pwm of power LED 
 
 
    // Configure 1ms SysTick (change if more temporal resolution needed) 
 
    //RCC_ClocksTypeDef RCC_Clocks;
 
    //RCC_GetClocksFreq(&RCC_Clocks);
 
    //SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
 
@@ -113,32 +110,22 @@ int main(void)
 
    ssd1306_clearscreen();
 
    
 
    restore_settings();
 
    if(boottobrew)
 
      state = STATE_PREHEAT_BREW; // Go to brew instead of idle if configured thusly
 
 
 
    // Main loop
 
    while(1)
 
    {
 
        // Process sensor inputs
 
        process();
 
 
        // Run state machine
 
        machine(); 
 
    }
 
 
/* OLD MAIN 
 
 
    for (;;) {
 
 
	CDC_Transmit_FS("a", 1); //sizeof(str)); for(count=0; count<32000000; count++); //#endif
 
        HAL_Delay(300);
 
    }
 
*/
 
 
}
 
 
/** System Clock Configuration
 
*/
 
void SystemClock_Config(void)
 
{
 
@@ -164,48 +151,41 @@ void SystemClock_Config(void)
 
 
  __SYSCFG_CLK_ENABLE();
 
 
}
 
 
 
 
 
// Read temperature and update global temp vars
 
int32_t temp = 0;
 
uint8_t temp_frac = 0;
 
uint8_t state_resume = 0;
 
 
 
// FIXME: Now this needs to work 8bits at a time, or change the port mode on the fly
 
void update_temp() {
 
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 
    HAL_Delay(100);
 
 
    // This may not clock at all... might need to send 16 bits first
 
    // SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data
 
 
    uint8_t rxdatah[1] = {0x00};
 
    uint8_t rxdatal[1] = {0x00};
 
 
    HAL_SPI_Receive(&hspi1, rxdatah, 1, 100);
 
    HAL_SPI_Receive(&hspi1, rxdatal, 1, 100);
 
 
    // Deassert CS
 
    // Release CS
 
    HAL_Delay(1);
 
    HAL_GPIO_WritePin(MAX_CS, 1);
 
 
 
    //OLD: SPI_I2S_SendData(SPI2, 0xAA); // send dummy data
 
    // OLD:   uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2);
 
    
 
    // Assemble data array into one var
 
    uint16_t temp_pre = rxdatal[0] | (rxdatah[0]<<8);
 
 
    if(temp_pre & 0b0000000000000010) {
 
        ssd1306_clearscreen();
 
        ssd1306_DrawString("Fatal Error", 3, 35);
 
        HAL_Delay(100);
 
        state = STATE_TC_ERROR;
 
    }
 
    else if(temp_pre & 0b0000000000000001 && !ignore_tc_error) {
 
        state_resume = state;
 
        state = STATE_TC_ERROR;
 
        temp = 0;
 
@@ -223,39 +203,49 @@ void update_temp() {
 
 
        temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
        temp_frac = temp_pre & 0b11; // get fractional part
 
        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) {
 
            temp = -temp_pre;
 
            signint = -1;
 
        }
 
        else {
 
            temp = temp_pre;
 
            signint = 1;
 
        }
 
 
        if(temp_units == TEMP_UNITS_FAHRENHEIT) {
 
            temp *= 9; // fixed point mul by 1.8
 
            temp /= 5;
 
            temp += 32;
 
        // Convert to Fahrenheit
 
        if(temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
            temp = signint * ((temp_pre*100) + temp_frac);
 
            temp = temp * 1.8;
 
            temp += 3200;
 
            temp_frac = temp % 100;
 
            temp /= 100;
 
        }
 
 
            temp_frac *= 9;
 
            temp_frac /= 5;
 
            temp_frac += 32;
 
            temp += temp_frac/100; // add overflow to above
 
            temp_frac %= 100;
 
        // Use Celsius values
 
        else
 
        {
 
            temp = temp_pre * signint;
 
        }
 
    }
 
 
    // Print temp to cdc
 
    CDC_Transmit_FS("Temp: ", 6);
 
    char tempstr[6];
 
    zitoa(temp, tempstr);
 
    CDC_Transmit_FS(tempstr, 1); //sizeof(tempstr));
 
    CDC_Transmit_FS(tempstr, sizeof(tempstr));
 
 
    CDC_Transmit_FS("\r\n", 2);
 
    CDC_Transmit_FS("\r\n", 2);
 
    CDC_Transmit_FS("\r\n", 2);
 
    CDC_Transmit_FS("\r\n", 2);
 
    CDC_Transmit_FS("\r\n", 2);
 
 
}
 
 
 
// 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.
0 comments (0 inline, 0 general)