Changeset - 0a84663f1f40
[Not reviewed]
default
0 1 0
Ethan Zonca - 10 years ago 2014-08-24 00:29:24
ez@ethanzonca.com
Refactored delay, allow setpoint changing, etc
1 file changed with 38 insertions and 21 deletions:
main.c
38
21
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -40,12 +40,13 @@ static __IO uint32_t TimingDelay;
 
 
// Move to header file
 
void init_gpio();
 
void init_spi();
 
void process();
 
void machine();
 
void delay(__IO uint32_t nTime);
 
 
int main(void)
 
{
 
 
    // Init clocks
 
    SystemInit();
 
@@ -76,13 +77,13 @@ int main(void)
 
    ssd1306_Init();
 
    ssd1306_block_write();
 
 
    // Startup screen 
 
    ssd1306_DrawString("therm v0.1", 1, 40);
 
    ssd1306_DrawString("protofusion.org/therm", 3, 0);
 
    Delay(1500);
 
    delay(1500);
 
    ssd1306_block_write();
 
 
    // Main loop
 
    while(1)
 
    {
 
        // Process sensor inputs
 
@@ -98,13 +99,13 @@ int main(void)
 
// Read temperature and update global temp vars
 
int16_t temp = 0;
 
uint8_t temp_frac = 0;
 
void update_temp() {
 
    // Assert CS
 
    GPIO_ResetBits(MAX_CS);
 
    Delay(1);
 
    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);
 
 
@@ -131,13 +132,13 @@ void update_temp() {
 
        else {
 
            temp = temp_pre;
 
        }
 
    }
 
 
    // Deassert CS
 
    Delay(1);
 
    delay(1);
 
    GPIO_SetBits(MAX_CS);
 
}
 
 
uint32_t ticks = 0;
 
uint32_t last_ssr_on = 0;
 
uint32_t last_led = 0;
 
@@ -157,21 +158,21 @@ void process()
 
    if(ticks - last_led > 400) 
 
    {
 
        GPIO_ToggleBits(LED_POWER);
 
        last_led = ticks;
 
    }
 
 
    // Every 200ms, set the SSR on
 
    if(ticks - last_ssr_on > SSR_PERIOD)
 
    // Every 200ms, set the SSR on unless output is 0
 
    if((ticks - last_ssr_on > SSR_PERIOD) && ssr_output > 0)
 
    {
 
        GPIO_SetBits(LED_STAT);
 
        last_ssr_on = ticks;
 
    }
 
    
 
    // Kill SSR after elapsed period less than SSR_PERIOD 
 
    if(ticks - last_ssr_on > ssr_output || !ssr_output)
 
    if(ticks - last_ssr_on > ssr_output)
 
    {
 
        GPIO_ResetBits(LED_STAT);
 
    }
 
}
 
 
void draw_setpoint() {
 
@@ -182,15 +183,12 @@ void draw_setpoint() {
 
    ssd1306_DrawString("-> ", 3, 80);
 
    itoa(setpoint, tempstr);
 
    ssd1306_DrawString("    ", 3, 95);
 
    ssd1306_DrawString(tempstr, 3, 95);
 
}
 
 
 
 
 
 
uint8_t state = STATE_IDLE;
 
uint8_t goto_mode = 2;
 
 
// State machine
 
void machine()
 
{
 
@@ -362,12 +360,19 @@ void machine()
 
            draw_setpoint();
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_BREW;
 
            }
 
 
 
@@ -382,12 +387,19 @@ void machine()
 
            draw_setpoint();
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
@@ -400,12 +412,19 @@ void machine()
 
            draw_setpoint();
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_STEAM;
 
            }
 
 
 
@@ -420,12 +439,19 @@ void machine()
 
            draw_setpoint();
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
@@ -442,29 +468,20 @@ void machine()
 
        // Clear screen on state change
 
        ssd1306_block_write();
 
    }
 
}
 
 
 
/**
 
  * @brief  Inserts a delay time.
 
  * @param  nTime: specifies the delay time length, in 1 ms.
 
  * @retval None
 
  */
 
void Delay(__IO uint32_t nTime)
 
// Delay a number of systicks
 
void delay(__IO uint32_t nTime)
 
{
 
  TimingDelay = nTime;
 
  while(TimingDelay != 0);
 
}
 
 
 
/**
 
  * @brief  Decrements the TimingDelay variable.
 
  * @param  None
 
  * @retval None
 
  */
 
// ISR-triggered decrement of delay and increment of tickcounter
 
void TimingDelay_Decrement(void)
 
{
 
  if (TimingDelay != 0x00)
 
  { 
 
    TimingDelay--;
 
  }
0 comments (0 inline, 0 general)