]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
Update for cooler
authorJan Michel <j.michel@gsi.de>
Mon, 5 Jan 2015 16:47:48 +0000 (17:47 +0100)
committerJan Michel <j.michel@gsi.de>
Mon, 5 Jan 2015 16:47:48 +0000 (17:47 +0100)
atmega32u4/cooler/1wire.c [new file with mode: 0644]
atmega32u4/cooler/1wire.h [new file with mode: 0644]
atmega32u4/cooler/Makefile
atmega32u4/cooler/cooler.c
atmega32u4/cooler/main.h [new file with mode: 0644]
atmega32u4/cooler/pinout.txt [new file with mode: 0644]

diff --git a/atmega32u4/cooler/1wire.c b/atmega32u4/cooler/1wire.c
new file mode 100644 (file)
index 0000000..5d23cdb
--- /dev/null
@@ -0,0 +1,166 @@
+/************************************************************************/\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
diff --git a/atmega32u4/cooler/1wire.h b/atmega32u4/cooler/1wire.h
new file mode 100644 (file)
index 0000000..3c98645
--- /dev/null
@@ -0,0 +1,32 @@
+#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
index 0d194344da2dc795f0ae780e03f43e99ab1672de..40cd5d9a338049a2982b858bd279224938cb029e 100644 (file)
@@ -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
 
index 2a8bb79f2e09fcbb3a308e96fb800f9fdfea4a1c..bb2d26cc7633e11cd505b22c7bbbac36e9d4934b 100644 (file)
@@ -1,14 +1,6 @@
 // #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
@@ -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<MAXX);
@@ -72,7 +70,22 @@ void init(void) {
   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';
@@ -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 (file)
index 0000000..caf3c2a
--- /dev/null
@@ -0,0 +1,29 @@
+#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
diff --git a/atmega32u4/cooler/pinout.txt b/atmega32u4/cooler/pinout.txt
new file mode 100644 (file)
index 0000000..d6f05c7
--- /dev/null
@@ -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