@@ -40,31 +40,24 @@ char* itoa(int16_t i, char b[]){
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '\0';
do{ //Move back, inserting digits as you go
*--p = digit[i%10];
i = i/10;
}while(i);
return b;
}
int32_t round(double x) {
if (x >= 0)
return (long) (x+0.5);
return (long) (x-0.5);
static __IO uint32_t TimingDelay;
// Move to header file
void init_gpio();
void init_spi();
void process();
void machine();
int main(void)
{
// Init clocks
@@ -177,80 +170,42 @@ void process()
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,
@@ -468,25 +423,25 @@ void init_spi(void)
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); /* Enable the SPI */
// MAX IC
SPI_Cmd(SPI2, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; // Andysworkshop
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // From andysworkshop
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // same
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE); /* Enable the SPI */
void init_gpio(void) {
GPIO_InitTypeDef GPIO_InitStruct;
// Enable SPI clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
Status change: