--- /dev/null
+/************************************************************************/\r
+/* */\r
+/* Access Dallas 1-Wire Devices */\r
+/* */\r
+/* Author: Peter Dannegger */\r
+/* danni@specs.de */\r
+/* */\r
+/************************************************************************/\r
+#include "main.h"\r
+\r
+\r
+\r
+uint8_t w1_reset(void)\r
+{\r
+ uint8_t err;\r
+\r
+ W1_OUT &= ~(1<<W1_PIN);\r
+ W1_DDR |= 1<<W1_PIN;\r
+ _delay_us(480);\r
+ cli();\r
+ W1_DDR &= ~(1<<W1_PIN);\r
+ _delay_us(100);\r
+ err = W1_IN & (1<<W1_PIN); // no presence detect\r
+ sei();\r
+ _delay_us(480);\r
+ if( (W1_IN & (1<<W1_PIN)) == 0 ) // short circuit\r
+ err = 1;\r
+ return err;\r
+}\r
+\r
+\r
+uint8_t w1_bit_io( uint8_t b )\r
+{\r
+ cli();\r
+ W1_DDR |= 1<<W1_PIN;\r
+ _delay_us(1.5);\r
+ if( b )\r
+ W1_DDR &= ~(1<<W1_PIN);\r
+ _delay_us(10);\r
+ if( (W1_IN & (1<<W1_PIN)) == 0 )\r
+ b = 0;\r
+ _delay_us(60);\r
+ W1_DDR &= ~(1<<W1_PIN);\r
+ sei();\r
+ return b;\r
+}\r
+\r
+\r
+uint8_t w1_byte_wr( uint8_t b )\r
+{\r
+ uint8_t i = 8, j;\r
+ do{\r
+ j = w1_bit_io( b & 1 );\r
+ b >>= 1;\r
+ if( j )\r
+ b |= 0x80;\r
+ }while( --i );\r
+ return b;\r
+}\r
+\r
+\r
+uint8_t w1_byte_rd( void )\r
+{\r
+ return w1_byte_wr( 0xFF );\r
+}\r
+\r
+\r
+uint8_t w1_rom_search( uint8_t diff, uint8_t *id )\r
+{\r
+ uint8_t i, j, next_diff;\r
+ uint8_t b;\r
+\r
+ if( w1_reset() )\r
+ return PRESENCE_ERR; // error, no device found\r
+ w1_byte_wr( SEARCH_ROM ); // ROM search command\r
+ next_diff = LAST_DEVICE; // unchanged on last device\r
+ i = 8 * 8; // 8 bytes\r
+ do{\r
+ j = 8; // 8 bits\r
+ do{\r
+ b = w1_bit_io( 1 ); // read bit\r
+ if( w1_bit_io( 1 ) ){ // read complement bit\r
+ if( b ) // 11\r
+ return DATA_ERR; // data error\r
+ }else{\r
+ if( !b ){ // 00 = 2 devices\r
+ if( diff > i ||\r
+ ((*id & 1) && diff != i) ){\r
+ b = 1; // now 1\r
+ next_diff = i; // next pass 0\r
+ }\r
+ }\r
+ }\r
+ w1_bit_io( b ); // write bit\r
+ *id >>= 1;\r
+ if( b ) // store bit\r
+ *id |= 0x80;\r
+ i--;\r
+ }while( --j );\r
+ id++; // next byte\r
+ }while( i );\r
+ return next_diff; // to continue search\r
+}\r
+\r
+\r
+void w1_command( uint8_t command, uint8_t *id )\r
+{\r
+ uint8_t i;\r
+ w1_reset();\r
+ if( id ){\r
+ w1_byte_wr( MATCH_ROM ); // to a single device\r
+ i = 8;\r
+ do{\r
+ w1_byte_wr( *id );\r
+ id++;\r
+ }while( --i );\r
+ }else{\r
+ w1_byte_wr( SKIP_ROM ); // to all devices\r
+ }\r
+ w1_byte_wr( command );\r
+}\r
+\r
+\r
+\r
+void start_meas( void ){\r
+ if( W1_IN & 1<< W1_PIN ){\r
+ w1_command( CONVERT_T, NULL );\r
+ W1_OUT |= 1<< W1_PIN;\r
+ W1_DDR |= 1<< W1_PIN; // parasite power on\r
+\r
+ }else{\r
+ //uputsnl( "Short Circuit !" );\r
+ }\r
+}\r
+\r
+\r
+void read_meas( void ) {\r
+ uint8_t id[8], diff;\r
+ uint8_t i;\r
+ uint16_t temp;\r
+ uint8_t s = 0;\r
+ for( diff = SEARCH_FIRST; diff != LAST_DEVICE; ){\r
+ diff = w1_rom_search( diff, id );\r
+\r
+ if( diff == PRESENCE_ERR ){\r
+ break;\r
+ }\r
+ if( diff == DATA_ERR ){\r
+ break;\r
+ }\r
+ if( id[0] == 0x28 || id[0] == 0x10 ){ // temperature sensor\r
+ for( i = 0; i < 8; i++ ){\r
+ ids[s][i] = id[i];\r
+ }\r
+ w1_byte_wr( READ ); // read command\r
+ temp = w1_byte_rd(); // low byte\r
+ temp |= (uint16_t)w1_byte_rd() << 8; // high byte\r
+ if( id[0] == 0x10 ) // 9 -> 12 bit\r
+ temp <<= 3;\r
+ temps[s] = temp;\r
+ }\r
+ s++; \r
+ }\r
+ }\r
+\r
+\r
--- /dev/null
+#ifndef _1wire_h_\r
+#define _1wire_h_\r
+#define MATCH_ROM 0x55\r
+#define SKIP_ROM 0xCC\r
+#define SEARCH_ROM 0xF0\r
+\r
+#define CONVERT_T 0x44 // DS1820 commands\r
+#define READ 0xBE\r
+#define WRITE 0x4E\r
+#define EE_WRITE 0x48\r
+#define EE_RECALL 0xB8\r
+\r
+#define SEARCH_FIRST 0xFF // start new search\r
+#define PRESENCE_ERR 0xFF\r
+#define DATA_ERR 0xFE\r
+#define LAST_DEVICE 0x00 // last device found\r
+// 0x01 ... 0x40: continue searching\r
+\r
+\r
+uint8_t w1_reset(void);\r
+\r
+uint8_t w1_byte_wr( uint8_t b );\r
+uint8_t w1_byte_rd( void );\r
+\r
+uint8_t w1_rom_search( uint8_t diff, uint8_t *id );\r
+\r
+void w1_command( uint8_t command, uint8_t *id );\r
+\r
+void start_meas( void );\r
+void read_meas( void );\r
+\r
+#endif\r
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
# Default target.
-all: build size
+all: build
build: elf hex eep
// #define F_CPU 8000000UL
-#include <avr/interrupt.h>
-#include <util/delay.h>
-#include <avr/io.h>
-#include <stdlib.h>
-
-#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
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
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<MAXX);
YELLOW_OFF();
GREEN_OFF();
}
-
+
+void timer_init(void) {
+ TCCR0A = (1<<WGM01); //CTC
+ TCCR0B = (4<<CS00); // /256
+ OCR0A = 167;
+ TIMSK0 = (1<<OCIE0A);
+ }
+
+ISR(TIMER0_COMPA_vect ) {
+ static uint8_t cnt = 0;
+ cnt++;
+ if(cnt == 167) {
+ cnt = 0;
+ second++;
+ }
+ }
uint8_t hex2int(char c) {
if(c >= '0' && c<='9') return c-'0';
init();
usb_init();
lcd_init();
+ timer_init();
lcd_command_1(LCD_MIRROR, LCD_BGR | LCD_FLIP_XY);
lcd_command(LCD_ON);
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) {
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
--- /dev/null
+#ifndef _main_h_
+#define _main_h_
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <util/delay.h>
+
+#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
--- /dev/null
+7 1-wire
+8 Reset
+9 CS
+10 D/C
+16 SDI
+15 SCK
+
+A0 TDI
+A1 TDO
+A2 TMS
+A3 TCK