@@ -8,12 +8,15 @@
#include "usb_desc.h"
#include "usb_pwr.h"
#define SSR_PERIOD 200
#define LED_POWER GPIOB,GPIO_Pin_9
#define LED_STAT GPIOA,GPIO_Pin_15
#define MAX_CS GPIOB,GPIO_Pin_12
// TODO: Grab buttonpresses with interrupts
// TODO: Eliminate screen buffer since we aren't using it...
#define SW_BTN GPIOB, GPIO_Pin_3
#define SW_UP GPIOB, GPIO_Pin_4
#define SW_DOWN GPIOB, GPIO_Pin_6
@@ -28,6 +31,20 @@ uint8_t Send_Buffer[64];
uint32_t packet_sent=1;
uint32_t packet_receive=1;
// State definition
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_PREHEAT_BREW,
STATE_MAINTAIN_BREW,
STATE_PREHEAT_STEAM,
STATE_MAINTAIN_STEAM,
};
char* itoa(int16_t i, char b[]){
char const digit[] = "0123456789";
@@ -113,6 +130,7 @@ int main(void)
// Init clocks
SystemInit();
// Init GPIO
init_gpio();
// Init USB
@@ -120,53 +138,38 @@ int main(void)
//USB_Interrupts_Config();
//USB_Init();
// Turn on power LED
GPIO_SetBits(LED_POWER);
RCC_ClocksTypeDef RCC_Clocks;
// TODO: Awesome pwm of power LED (TIM4_CH4 or TIM11_CH1)
// TODO: PWM of stat led (TIM3_CH2)
// SysTick end of count event each 1ms
// Configure 1ms SysTick (change if more temporal resolution needed)
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
GPIO_ResetBits(LED_STAT);
Delay(100);
Delay(500);
GPIO_ResetBits(LED_POWER);
// Init SPI busses
init_spi();
// Init OLED over SPI
ssd1306_Init();
ssd1306_block_write();
ssd1306_DrawString("therm 0.1", 0, 40);
uint8_t toggle = 0;
// Startup screen
ssd1306_DrawString("therm v0.1", 1, 40);
ssd1306_DrawString("protofusion.org/therm", 3, 0);
Delay(1500);
int16_t temp = -231;
// Main loop
while(1)
{
//ssd1306_block_write();
// Process sensor inputs [TODO: 5hz?]
// Process sensor inputs
process();
// Run state machine [TODO: 50hz?]
// Run state machine
machine();
// probably just passed the actual port
//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);
Delay(50);
}
@@ -189,21 +192,25 @@ void update_temp() {
ssd1306_DrawString("Fatal Error", 2, 35);
else if(temp_pre & 0b0000000000000001) {
ssd1306_DrawString("TC Fault", 2, 35);
ssd1306_DrawString("Error: No TC", 2, 40);
temp = 0;
temp_frac = 0;
uint8_t sign = temp >> 15;// top bit is sign
else
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
if(sign) {
temp = -temp_pre;
else {
temp = temp_pre;
// Deassert CS
@@ -211,12 +218,14 @@ void update_temp() {
GPIO_SetBits(MAX_CS);
uint32_t ticks = 0;
uint32_t last_ssr_on = 0;
uint32_t last_led = 0;
int32_t setpoint = 0;
uint16_t k_p = 1;
uint16_t k_i = 1;
uint16_t k_d = 1;
uint8_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
// Process things
void process()
@@ -225,27 +234,25 @@ void process()
// TODO: Add calibration offset (linear)
// Perform PID calculations
//if(
GPIO_SetBits(LED_STAT);
// Write output to SSR
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)
last_ssr_on = ticks;
// Kill SSR after elapsed period less than SSR_PERIOD
if(ticks - last_ssr_on > ssr_output || !ssr_output)
void draw_setpoint() {
char tempstr[3];
@@ -502,11 +509,9 @@ void machine()
} break;
// Something is terribly wrong
default:
ssd1306_DrawString("therm :: BAD BAD", 0, 40);
state = STATE_IDLE;
@@ -543,6 +548,7 @@ void TimingDelay_Decrement(void)
TimingDelay--;
ticks++;
Status change: