From 443faa2a2de70f118817c419d80176c7ba4e2715 Mon Sep 17 00:00:00 2001 From: Jan Michel Date: Mon, 5 Jan 2015 17:47:48 +0100 Subject: [PATCH] Update for cooler --- atmega32u4/cooler/1wire.c | 166 +++++++++++++++++++++++++++++++++++ atmega32u4/cooler/1wire.h | 32 +++++++ atmega32u4/cooler/Makefile | 5 +- atmega32u4/cooler/cooler.c | 59 ++++++++----- atmega32u4/cooler/main.h | 29 ++++++ atmega32u4/cooler/pinout.txt | 11 +++ 6 files changed, 280 insertions(+), 22 deletions(-) create mode 100644 atmega32u4/cooler/1wire.c create mode 100644 atmega32u4/cooler/1wire.h create mode 100644 atmega32u4/cooler/main.h create mode 100644 atmega32u4/cooler/pinout.txt diff --git a/atmega32u4/cooler/1wire.c b/atmega32u4/cooler/1wire.c new file mode 100644 index 0000000..5d23cdb --- /dev/null +++ b/atmega32u4/cooler/1wire.c @@ -0,0 +1,166 @@ +/************************************************************************/ +/* */ +/* Access Dallas 1-Wire Devices */ +/* */ +/* Author: Peter Dannegger */ +/* danni@specs.de */ +/* */ +/************************************************************************/ +#include "main.h" + + + +uint8_t w1_reset(void) +{ + uint8_t err; + + W1_OUT &= ~(1<>= 1; + if( j ) + b |= 0x80; + }while( --i ); + return b; +} + + +uint8_t w1_byte_rd( void ) +{ + return w1_byte_wr( 0xFF ); +} + + +uint8_t w1_rom_search( uint8_t diff, uint8_t *id ) +{ + uint8_t i, j, next_diff; + uint8_t b; + + if( w1_reset() ) + return PRESENCE_ERR; // error, no device found + w1_byte_wr( SEARCH_ROM ); // ROM search command + next_diff = LAST_DEVICE; // unchanged on last device + i = 8 * 8; // 8 bytes + do{ + j = 8; // 8 bits + do{ + b = w1_bit_io( 1 ); // read bit + if( w1_bit_io( 1 ) ){ // read complement bit + if( b ) // 11 + return DATA_ERR; // data error + }else{ + if( !b ){ // 00 = 2 devices + if( diff > i || + ((*id & 1) && diff != i) ){ + b = 1; // now 1 + next_diff = i; // next pass 0 + } + } + } + w1_bit_io( b ); // write bit + *id >>= 1; + if( b ) // store bit + *id |= 0x80; + i--; + }while( --j ); + id++; // next byte + }while( i ); + return next_diff; // to continue search +} + + +void w1_command( uint8_t command, uint8_t *id ) +{ + uint8_t i; + w1_reset(); + if( id ){ + w1_byte_wr( MATCH_ROM ); // to a single device + i = 8; + do{ + w1_byte_wr( *id ); + id++; + }while( --i ); + }else{ + w1_byte_wr( SKIP_ROM ); // to all devices + } + w1_byte_wr( command ); +} + + + +void start_meas( void ){ + if( W1_IN & 1<< W1_PIN ){ + w1_command( CONVERT_T, NULL ); + W1_OUT |= 1<< W1_PIN; + W1_DDR |= 1<< W1_PIN; // parasite power on + + }else{ + //uputsnl( "Short Circuit !" ); + } +} + + +void read_meas( void ) { + uint8_t id[8], diff; + uint8_t i; + uint16_t temp; + uint8_t s = 0; + for( diff = SEARCH_FIRST; diff != LAST_DEVICE; ){ + diff = w1_rom_search( diff, id ); + + if( diff == PRESENCE_ERR ){ + break; + } + if( diff == DATA_ERR ){ + break; + } + if( id[0] == 0x28 || id[0] == 0x10 ){ // temperature sensor + for( i = 0; i < 8; i++ ){ + ids[s][i] = id[i]; + } + w1_byte_wr( READ ); // read command + temp = w1_byte_rd(); // low byte + temp |= (uint16_t)w1_byte_rd() << 8; // high byte + if( id[0] == 0x10 ) // 9 -> 12 bit + temp <<= 3; + temps[s] = temp; + } + s++; + } + } + + diff --git a/atmega32u4/cooler/1wire.h b/atmega32u4/cooler/1wire.h new file mode 100644 index 0000000..3c98645 --- /dev/null +++ b/atmega32u4/cooler/1wire.h @@ -0,0 +1,32 @@ +#ifndef _1wire_h_ +#define _1wire_h_ +#define MATCH_ROM 0x55 +#define SKIP_ROM 0xCC +#define SEARCH_ROM 0xF0 + +#define CONVERT_T 0x44 // DS1820 commands +#define READ 0xBE +#define WRITE 0x4E +#define EE_WRITE 0x48 +#define EE_RECALL 0xB8 + +#define SEARCH_FIRST 0xFF // start new search +#define PRESENCE_ERR 0xFF +#define DATA_ERR 0xFE +#define LAST_DEVICE 0x00 // last device found +// 0x01 ... 0x40: continue searching + + +uint8_t w1_reset(void); + +uint8_t w1_byte_wr( uint8_t b ); +uint8_t w1_byte_rd( void ); + +uint8_t w1_rom_search( uint8_t diff, uint8_t *id ); + +void w1_command( uint8_t command, uint8_t *id ); + +void start_meas( void ); +void read_meas( void ); + +#endif diff --git a/atmega32u4/cooler/Makefile b/atmega32u4/cooler/Makefile index 0d19434..40cd5d9 100644 --- a/atmega32u4/cooler/Makefile +++ b/atmega32u4/cooler/Makefile @@ -9,7 +9,8 @@ MCU = atmega32u4 FORMAT = ihex TARGET = cooler SRC = $(TARGET).c usb/usb_serial.c lcdlib/lcd-color-graphic.c lcdlib/font.c \ - lcdlib/Fonts/digits_24px.c lcdlib/Fonts/digits_32px.c lcdlib/Fonts/font_proportional_16px.c lcdlib/Fonts/font_proportional_8px.c lcdlib/Fonts/symbols_16px.c + lcdlib/Fonts/digits_24px.c lcdlib/Fonts/digits_32px.c lcdlib/Fonts/font_proportional_16px.c lcdlib/Fonts/font_proportional_8px.c lcdlib/Fonts/symbols_16px.c \ + 1wire.c ASRC = OPT = 2 @@ -130,7 +131,7 @@ ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) # Default target. -all: build size +all: build build: elf hex eep diff --git a/atmega32u4/cooler/cooler.c b/atmega32u4/cooler/cooler.c index 2a8bb79..bb2d26c 100644 --- a/atmega32u4/cooler/cooler.c +++ b/atmega32u4/cooler/cooler.c @@ -1,14 +1,6 @@ // #define F_CPU 8000000UL -#include -#include -#include -#include - -#include "usb/usb_serial.h" -#include "lcdlib/lcd-color-graphic.h" -#include "lcdlib/font.h" - +#include "main.h" #define YELLOW_ON() PORTB &= ~0x1 #define YELLOW_OFF() PORTB |= 0x1 @@ -31,7 +23,11 @@ uint8_t buffer[BUFFERSIZE]; uint8_t recvpointer = 0; int16_t values[2][STORAGESIZE]; uint16_t valuepointer[2] = {0}; -uint16_t lastvalues[2] = {0,0}; +int16_t lastvalues[2] = {0,0}; +volatile uint8_t second, lastsecond; +uint8_t ids[NUMSENSORS][8]; +uint16_t temps[NUMSENSORS] = {-255}; +int16_t set_value = 12*16; #define MINX 19 #define MAXX 318 @@ -50,7 +46,9 @@ void draw(void) { y >>= 4; y = 190-y; lcd_set_pixel_col_xy(c+1,y,background); + lcd_set_pixel_col_xy(c+1,y+1,background); lcd_set_pixel_col_xy(c,y,foreground); + lcd_set_pixel_col_xy(c,y+1,foreground); p++; if(p>= STORAGESIZE) p = 0; }while(++c= '0' && c<='9') return c-'0'; @@ -192,6 +205,7 @@ __attribute__((naked)) int main(void) { init(); usb_init(); lcd_init(); + timer_init(); lcd_command_1(LCD_MIRROR, LCD_BGR | LCD_FLIP_XY); lcd_command(LCD_ON); @@ -202,12 +216,9 @@ __attribute__((naked)) int main(void) { lcd_set_foreground(0x1f,0x3f,0x1f); lcd_set_font(FONT_PROP_16,DOUBLE_HEIGHT|SPACING); lcd_putstr_xy_P(PSTR("Cooling Control"),26,30); - - - - pushvalue(0,(17*16+5)); - pushvalue(1,-(8*16+3)); printAxes(); + + pushvalue(1,set_value); while (1) { @@ -218,16 +229,24 @@ __attribute__((naked)) int main(void) { recvpointer = 0; } - if(n == 10 && recvpointer >= 8 && buffer[0] == 'S' && buffer[1] == 'E' && buffer[2] == 'T') { //SETnsvvv - uint16_t t = hex2int(buffer[5])*256 + hex2int(buffer[6])*16 + hex2int(buffer[7]); + if(n == 10 && recvpointer >= 8 && buffer[0] == 'S' && buffer[1] == 'E' && buffer[2] == 'T') { //SETsvv.v + uint16_t t = hex2int(buffer[5])*160 + hex2int(buffer[6])*16 + hex2int(buffer[8]); if (buffer[4] == '-') t = t*-1; - pushvalue(hex2int(buffer[3]),t); + set_value = t; + pushvalue(1,t); } if(n==10) { usb_serial_write(buffer,recvpointer); recvpointer = 0; } } - } + if(second != lastsecond) { + usb_serial_putchar(0x10); + read_meas(); + start_meas(); + pushvalue(0,temps[0]); + lastsecond = second; + } + } } \ No newline at end of file diff --git a/atmega32u4/cooler/main.h b/atmega32u4/cooler/main.h new file mode 100644 index 0000000..caf3c2a --- /dev/null +++ b/atmega32u4/cooler/main.h @@ -0,0 +1,29 @@ +#ifndef _main_h_ +#define _main_h_ + +#include +#include +#include +#include +#include + +#include "usb/usb_serial.h" +#include "lcdlib/lcd-color-graphic.h" +#include "lcdlib/font.h" +#include "1wire.h" + + +#ifndef W1_PIN +#define W1_PIN PE6 +#define W1_IN PINE +#define W1_OUT PORTE +#define W1_DDR DDRE +#endif + + +#define NUMSENSORS 2 +extern uint8_t ids[NUMSENSORS][8]; +extern uint16_t temps[NUMSENSORS]; + + +#endif \ No newline at end of file diff --git a/atmega32u4/cooler/pinout.txt b/atmega32u4/cooler/pinout.txt new file mode 100644 index 0000000..d6f05c7 --- /dev/null +++ b/atmega32u4/cooler/pinout.txt @@ -0,0 +1,11 @@ +7 1-wire +8 Reset +9 CS +10 D/C +16 SDI +15 SCK + +A0 TDI +A1 TDO +A2 TMS +A3 TCK -- 2.43.0