diff options
-rw-r--r-- | pcr-controller/Makefile | 46 | ||||
-rw-r--r-- | pcr-controller/pcr-controller.c | 219 | ||||
-rw-r--r-- | pcr-controller/pwm.c | 48 | ||||
-rw-r--r-- | pcr-controller/pwm.h | 10 |
4 files changed, 323 insertions, 0 deletions
diff --git a/pcr-controller/Makefile b/pcr-controller/Makefile new file mode 100644 index 0000000..7a8217f --- /dev/null +++ b/pcr-controller/Makefile @@ -0,0 +1,46 @@ +##
+## spreadspace avr utils
+##
+##
+## Copyright (C) 2013 Christian Pointner <equinox@spreadspace.org>
+##
+## This file is part of spreadspace avr utils.
+##
+## spreadspace avr utils is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## any later version.
+##
+## spreadspace avr utils is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+##
+
+NAME := pcr-controller
+BOARD_TYPE := teensy2
+OBJ := $(NAME).o pwm.o
+LIBS := util led lufa-descriptor-usbserial anyio onewire ds1820
+EXTERNAL_LIBS := lufa
+RESET_FUNC := ../tools/reset_lufa_cdc
+RESET_PARAM := 'r'
+
+LUFA_PATH := ../contrib/LUFA-120219
+LUFA_OPTS = -D USB_DEVICE_ONLY
+LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
+LUFA_OPTS += -D ORDERED_EP_CONFIG
+LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
+LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
+LUFA_OPTS += -D USE_FLASH_DESCRIPTORS
+LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
+LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT
+
+LUFA_OPTS += -D USB_MANUFACTURER="L\"xro\""
+LUFA_OPTS += -D USB_PRODUCT="L\"$(NAME)\""
+
+LUFA_COMPONENTS := USB USBCLASS SERIAL
+
+include ../include.mk
diff --git a/pcr-controller/pcr-controller.c b/pcr-controller/pcr-controller.c new file mode 100644 index 0000000..583d054 --- /dev/null +++ b/pcr-controller/pcr-controller.c @@ -0,0 +1,219 @@ +/* + * OpenPCR Teensy Controller Code + * + * + * Copyright (C) 2013 Bernhard Tittelbach <xro@realraum.at> +* uses avr-utils, anyio & co by Christian Pointner <equinox@spreadspace.org> + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include <avr/io.h> +#include <avr/wdt.h> +#include <avr/interrupt.h> +#include <avr/power.h> +#include <stdio.h> +#include <stdlib.h> + +#include "util.h" +#include "led.h" +#include "anyio.h" + +#include "onewire.h" +#include "ds1820.h" + +#include "pwm.h" + +#define PIN_HIGH(PORT, PIN) PORT |= (1 << PIN) +#define PIN_LOW(PORT, PIN) PORT &= ~(1 << PIN) +#define PINMODE_OUTPUT PIN_HIGH //just use DDR instead of PORT +#define PINMODE_INPUT PIN_LOW //just use DDR instead of PORT + +#define OP_SETBIT |= +#define OP_CLEARBIT &= ~ +#define OP_CHECK & +#define PIN_SW(PORTDDRREG, PIN, OP) PORTDDRREG OP (1 << PIN) + +#define HIGHv OP_SETBIT +#define LOWv OP_CLEARBIT + +#define PUMP_PIN PINB3 +#define PELTIER_INA PINF7 +#define PELTIER_INB PINB6 +#define PELETIER_PWM_EN PINB5 +#define TOPHEAT_PIN PIND7 + + + +uint8_t num_temp_sensors_ = 0; +uint16_t raw_temp_ = 0; +uint16_t target_temp_ = 0; +uint16_t pid_p_ = 0; +uint16_t pid_i_ = 0; +uint16_t pid_d_ = 0; +uint16_t pid_err_ = 0; + +void queryAndSaveTemperature(uint8_t bit_resolution) +{ + uint8_t sensor_index = 0; + + if (num_temp_sensors_ == 0) + { + num_temp_sensors_ = ds1820_discover(); + } + + for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++) + { + ds1820_set_resolution(sensor_index, bit_resolution); + ds1820_start_measuring(sensor_index); + } + + ds1820_wait_conversion_time(bit_resolution); + + for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++) + { + raw_temp_ = ds1820_read_temperature(sensor_index); + if (raw_temp_ != DS1820_ERROR) + { + break; //we need only one successfully read value + } + } +} + +void printRawTemp(int16_t raw_temp) +{ + printf("%d.%02d", raw_temp / 16, 100 * (raw_temp % 16) / 16); +} + +void printTemperature(void) +{ + if (num_temp_sensors_ == 0) + { + printf("ERROR: No DS1820 sensors on 1wire bus, thus no temperature\r\n"); + return; + } + if (raw_temp_ == DS1820_ERROR) + { + printf("ERROR talking to DS18b20, no valid temperature!\r\n"); + } else { + printf("Temp: "); + printRawTemp(raw_temp_); + printf("\r\n"); + } +} + +void readIntoBuffer(char *buffer, uint8_t buflen) +{ + while (anyio_bytes_received() == 0); + int ReceivedByte=0; + do { + ReceivedByte = fgetc(stdin); + if (ReceivedByte != EOF) + { + *buffer = (char) ReceivedByte; + buffer++; + buflen --; + } + } while (ReceivedByte != '\n' && ReceivedByte != '\r' && buflen > 1); + *buffer = 0; +} + +int16_t readNumber(void) +{ + char buffer[20]; + readIntoBuffer(buffer, 20); + return atoi(buffer); +} + +void handle_cmd(uint8_t cmd) +{ + switch(cmd) { + case ' ': + case '\n': + case '\r': + return; + case 'R': + case 'r': reset2bootloader(); break; + case 's': printTemperature(); return; + case 'L': led_toggle(); break; + case 't': + printf("TargetTemp: "); + printRawTemp(target_temp_); + printf("\r\n"); + return; + case 'p': + case 'i': + case 'd': + printf("PID P: %d\r\nPID I: %d\r\nPID D: %d\r\n", pid_p_, pid_i_, pid_d_); + return; + case 'T': target_temp_ = readNumber(); break; + case 'P': pid_p_ = readNumber(); break; + case 'I': pid_i_ = readNumber(); break; + case 'D': pid_d_ = readNumber(); pwm_set((uint8_t) pid_d_); break; + case 'A': PIN_HIGH(PORTB, PUMP_PIN); break; + case 'a': PIN_LOW(PORTB, PUMP_PIN); break; + case 'B': PIN_HIGH(PORTD, TOPHEAT_PIN); break; + case 'b': PIN_LOW(PORTD, TOPHEAT_PIN); break; + default: printf("ERROR\r\n"); return; + } + printf("OK\r\n"); +} + +int main(void) +{ + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + anyio_init(115200, 0); + sei(); + + led_off(); + owi_init(PINC7, &PINC); + PINMODE_OUTPUT(DDRB, PUMP_PIN); + PIN_LOW(PORTB, PUMP_PIN); + PINMODE_OUTPUT(DDRB, PELETIER_PWM_EN); + PINMODE_OUTPUT(DDRB, PELTIER_INB); + PINMODE_OUTPUT(DDRF, PELTIER_INA); + PINMODE_OUTPUT(DDRD, TOPHEAT_PIN); + + pwm_init(); + + num_temp_sensors_ = ds1820_discover(); + + for(;;) + { + int16_t BytesReceived = anyio_bytes_received(); + while(BytesReceived > 0) + { + int ReceivedByte = fgetc(stdin); + if (ReceivedByte != EOF) + { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + queryAndSaveTemperature(11); + + // PID control + + anyio_task(); + } +} diff --git a/pcr-controller/pwm.c b/pcr-controller/pwm.c new file mode 100644 index 0000000..918d37b --- /dev/null +++ b/pcr-controller/pwm.c @@ -0,0 +1,48 @@ + +#include "pwm.h" + + +#define PWM_VAL OCR1AL +//OC1A + +void pwm_init(void) +{ + DDRB |= (1<<PB5); + TCCR1A = 0; + TCNT1 = 0; + OCR1A = 0; + TCCR1A = (1<<COM1A1) | (1<<WGM10); + TCCR1B = (1<<WGM12); +} + +inline void pwm_on(void) +{ + TCCR1B = (TCCR1A & 0xF8) | (1<<CS10); +} + +inline void pwm_off(void) +{ + TCCR1B = (TCCR1A & 0xF8); + TCNT1 = 0; +} + +void pwm_set(uint8_t val) +{ + if (val > 0) + pwm_on(); + else + pwm_off(); + PWM_VAL = val; +} + +inline void pwm_inc(void) +{ + if(PWM_VAL < 255) + PWM_VAL++; +} + +inline void pwm_dec(void) +{ + if(PWM_VAL > 0) + PWM_VAL--; +} diff --git a/pcr-controller/pwm.h b/pcr-controller/pwm.h new file mode 100644 index 0000000..1e5f184 --- /dev/null +++ b/pcr-controller/pwm.h @@ -0,0 +1,10 @@ +#ifndef PWM_H_INCLUDED_ +#define PWM_H_INCLUDED_ + +#include <avr/io.h> + +void pwm_init(void); +void pwm_set(uint8_t val); + + +#endif
\ No newline at end of file |