]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
value and address transmission to FPGA and change in timing; fix of some bugs; 115...
authorAdrian Weber <adrian.a.weber@exp2.physik.uni-giessen.de>
Tue, 9 Feb 2021 12:53:12 +0000 (13:53 +0100)
committerAdrian Weber <adrian.a.weber@exp2.physik.uni-giessen.de>
Tue, 9 Feb 2021 12:53:12 +0000 (13:53 +0100)
atmega32u4/CbmRich_sensoring/main.c

index c71fc0e38bd4a59379ddfa7c8ec2ce7682e1a5d3..6bf7167f48f4e48287a37d4b5b6dab2145b25869 100644 (file)
 
 #define FIRMWARE_VERSION 0x001
 
-#define DEBUG_USB_OUT
+//#define DEBUG_USB_OUT
 
 // BEGIN define 1-wire parameters
-#define MAX_OW_DEV_COUNT_PER_PIN 10
-#define NUM_1W_PINS 3
+#define MAX_OW_DEV_COUNT_PER_PIN 17
+#define NUM_1W_PINS 9
+
+typedef enum {
+       DS18B20 = 0,
+       DS28E17
+} OW_DEV_CODE_t;
 
 uint8_t scratch_pad[__SCR_LENGTH];
 uint8_t dev_count[NUM_1W_PINS];
-uint8_t dev_addr[NUM_1W_PINS][MAX_OW_DEV_COUNT_PER_PIN][8];
+uint8_t dev_addr[NUM_1W_PINS][MAX_OW_DEV_COUNT_PER_PIN][8]; // UID
+uint8_t dev_int_addr[NUM_1W_PINS][MAX_OW_DEV_COUNT_PER_PIN]; // int. Address of Device Type
 uint8_t DS18B20_scratch_pad[__SCR_LENGTH];
 
 // END define 1-wire parameters
@@ -34,11 +40,11 @@ uint8_t DS18B20_scratch_pad[__SCR_LENGTH];
 uint8_t  cur_meas_line = 0;
 uint8_t  cur_meas_devNbr = 0;
 uint8_t  settings_changed;
-uint16_t time;
+volatile uint16_t time;
 
 uint8_t rxcnt = 0, txpoint = 0;
 uint8_t rxbuf[11];
-uint8_t txbuf[12];
+uint8_t txbuf[22];
 
 uint8_t tx_test_buf[100];
 
@@ -46,35 +52,9 @@ uint8_t tx_test_buf[100];
 int search_devices(owu_struct_t []);
 int init_OW_measurements(owu_struct_t []);
 int OW_measurements(owu_struct_t []);
+void send_Address(uint8_t line , uint8_t devNbr, uint8_t I2C_add);
+void send_Values(uint8_t line , uint8_t devNbr, uint8_t valType, uint16_t value);
 
-/***
- *  Data:   XuuGcRvvvv
- *          
- *          X    - command   (write: W, read: R, answer: A etc.)
- *          uu   - Controllernumber (Hex value)
- *          G    - Groupnumber  (to talk to all channels, that belong together, in one command.)
- *          c    - channelnumber in the group (Hex value)
- *          R    - register (Hex value)
- *          vvvv - 16 Bit value 
- * 
- *          All in all 10 characters
- *          
- * 
- *          close with a "\n"
- *          e.g. "RF2012FE51\n"
- * 
- ***/
-
-/***
- *  Registers  |  description
- *  ------------------------------  
- *     0       |  DCDC On/Off -> not avail. for  <= Rev1_2
- *     1       |  DCDC set voltage adjustment resistors
- *     2       |  Voltage V_in
- *     3       |  Current C_in
- *     4       |  Temperature
- *     5       |  [15:4] Firmware; [3:2] reserved; [1] Switch ; [0] LED
- ***/
 
 void getdata(uint8_t buf) {
   
@@ -90,7 +70,7 @@ ISR(USART1_RX_vect) {
 ISR(USART1_UDRE_vect) {
   if(txbuf[txpoint] != 0)
     UDR1 = txbuf[txpoint++];
-  if(txpoint > 11 || txbuf[txpoint] == 0) {  
+  if(txpoint > 22 || txbuf[txpoint] == 0) {  
     txpoint = 0;
     UCSR1B &= ~(1<< UDRIE1); //deactivate Transmit
     }
@@ -134,9 +114,9 @@ int main(void) {
   DDRF  = 0b01110011;
   //--------------------------------------------------------//
   
-  //Timer0 at 30 Hz overflow for ADC control
+  //Timer0 at 30 Hz overflow for Interrupt control
   TCCR0B = (5 << CS00);
-  TIMSK0 = (1 << TOIE0); //Overflow interrupt`
+  TIMSK0 = (1 << TOIE0); //Overflow interrupt
 
   
   //Init USART    
@@ -144,7 +124,7 @@ int main(void) {
   //UCSR1A = (0 << U2X1);  // Single Speed Mode
   UCSR1B = (1 << RXCIE1) | (0 << TXCIE1) | (0 << RXEN1) | (1 << TXEN1);
   UCSR1C = (3 << UCSZ10); //8 Bit
-  UBRR1  = 0x10; // 38k4 (SSM) //0x33; // 38k4  //0x10; //57600
+  UBRR1  = 0x08; //BaudRate: 115.2k (-3.5%)  //0x10; BaudRate: 57.6k (2.1%)
   _delay_ms(10);
   UCSR1B |= (1 << RXEN1);
   
@@ -175,19 +155,34 @@ int main(void) {
   //PORTB &= ~(1<<PB0);
 
   // Start actual firmware behaviour
+  _delay_ms(5000);
   search_devices(ow_Q);
   init_OW_measurements(ow_Q);
 
+  uint16_t lasttime = 0;
   // here start first scan of all Temp devices that are found
   //TODO
   while(1) {
+    // delay a measurement run by a specific time
+    if (cur_meas_line == 0 && cur_meas_devNbr == 0){
+      cli(); // Deactivate Interrupt for solid calculation
+      if((time % 30) == 0 && (time != lasttime)) {
+        sei();
+        OW_measurements(ow_Q);
+      }
+      sei();
+    } else {
+      OW_measurements(ow_Q);
+    }
 
-    OW_measurements(ow_Q);
+    cli();
+    if((time != lasttime) && ((time % 100) == 0)) {
+      PORTB ^= (1<<PB0); // Toggle LED
+    }
+    sei();
 
-    PORTB |= (1<<PB0);
-    _delay_ms(2000);
-    PORTB &= ~(1<<PB0);
-    _delay_ms(2000);
+    lasttime = time;
   }
 }
 
@@ -209,26 +204,48 @@ void delay_us(uint32_t us)
  **/
 int search_devices(owu_struct_t ow_Q[]){
   // 1-wire search of boards / addresses > Later in separate function
+
+  // init main Arrays
   for (uint8_t i = 0; i < NUM_1W_PINS; ++i){
     dev_count[i] = 0;
+    for (uint8_t j = 0; j < MAX_OW_DEV_COUNT_PER_PIN; ++j){
+      dev_int_addr[i][j] = 0;
+      for (uint8_t k = 0; k < 8; ++k){
+        dev_addr[i][j][k] = 0;
+      }
+    }
   }
 
+  // search
+  uint8_t dev_sum[] = {0,0};
   for (uint8_t ow_line = 0; ow_line < NUM_1W_PINS; ++ow_line){
     while(owu_search(&ow_Q[ow_line], dev_addr[ow_line][dev_count[ow_line]])) {
       dev_count[ow_line]++;
+      uint8_t I2C_add = 0x00;
+      if (dev_addr[ow_line][dev_count[ow_line]-1][0] == 0x28 ) { // DS18B20
+        dev_int_addr[ow_line][dev_count[ow_line]-1] = dev_sum[0];
+        dev_sum[0]++;
+      } else if (dev_addr[ow_line][dev_count[ow_line]-1][0] == 0x19 ){ //DS28E17
+        dev_int_addr[ow_line][dev_count[ow_line]-1] = dev_sum[1];
+        dev_sum[1]++;
+      } else {
+        //Error: Action to be defined.
+      }      
+
+      send_Address(ow_line, dev_count[ow_line]-1, I2C_add);
  
       #ifdef DEBUG_USB_OUT
-        int nbytes = sprintf((char*) tx_test_buf, "Devices found on line %d :  %d\r\n", ow_line, dev_count[ow_line]); 
+        int nbytes = sprintf((char*) tx_test_buf, "Devices found on line %d : %d\n", ow_line, dev_count[ow_line]); 
         usb_serial_write(tx_test_buf,nbytes);
-        
-        nbytes = sprintf((char*) tx_test_buf, "\t Address:  %x %x %x %x %x %x %x %x\r\n", dev_addr[ow_line][dev_count[ow_line]-1][0],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][1],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][2],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][3],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][4],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][5],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][6],
-                                                                                  dev_addr[ow_line][dev_count[ow_line]-1][7]); 
+
+        nbytes = sprintf((char*) tx_test_buf, "Address:  %02x%02x%02x%02x%02x%02x%02x%02x\n", dev_addr[ow_line][dev_count[ow_line]-1][0],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][1],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][2],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][3],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][4],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][5],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][6],
+                                                                                              dev_addr[ow_line][dev_count[ow_line]-1][7]); 
         usb_serial_write(tx_test_buf,nbytes);
       #endif
 
@@ -242,6 +259,10 @@ int search_devices(owu_struct_t ow_Q[]){
   return 0;
 }
 
+
+/**
+ *  Start the conversion of all Sensors for the first time to have a measured value in first loop
+ **/
 int init_OW_measurements(owu_struct_t ow_Q[]){
  
   for (uint8_t ow_line = 0; ow_line < NUM_1W_PINS; ++ow_line){
@@ -278,23 +299,28 @@ int init_OW_measurements(owu_struct_t ow_Q[]){
 }
 
 
+/**
+ *  Get the measurment value from the globally selected sensor and init a new conversion
+ **/
 int OW_measurements(owu_struct_t ow_Q[]){
   // start measurement on device  
   if (cur_meas_devNbr < dev_count[cur_meas_line]) {
-    int nbytes2 = sprintf((char*) tx_test_buf, "Line:  %d  |  Device %d \n", cur_meas_line, cur_meas_devNbr);
-         usb_serial_write(tx_test_buf,nbytes2);
-
+    #ifdef DEBUG_USB_OUT
+      int nbytes2 = sprintf((char*) tx_test_buf, "Line:  %d  |  Device: %d \n", cur_meas_line, cur_meas_devNbr);
+           usb_serial_write(tx_test_buf,nbytes2);
+    #endif  
     if (dev_addr[cur_meas_line][cur_meas_devNbr][0] == 0x28) { // Device is DS18B20
       ds_read_temp_only(&ow_Q[cur_meas_line], dev_addr[cur_meas_line][cur_meas_devNbr], DS18B20_scratch_pad);
+      send_Values(cur_meas_line,cur_meas_devNbr,'T',ds_get_raw(DS18B20_scratch_pad));
       #ifdef DEBUG_USB_OUT
         int nbytes = sprintf((char*) tx_test_buf, "UID:  %x%x%x%x%x%x%x%x \n", dev_addr[cur_meas_line][cur_meas_devNbr][0],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][1],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][2],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][3],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][4],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][5],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][6],
-                                                                                                 dev_addr[cur_meas_line][cur_meas_devNbr][7]); 
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][1],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][2],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][3],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][4],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][5],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][6],
+                                                                               dev_addr[cur_meas_line][cur_meas_devNbr][7]); 
         usb_serial_write(tx_test_buf,nbytes);
 
         nbytes = sprintf((char*) tx_test_buf, "Temperatur: %d \n", (int16_t) ds_get_raw(DS18B20_scratch_pad));
@@ -305,7 +331,10 @@ int OW_measurements(owu_struct_t ow_Q[]){
       ds_convert_device(&ow_Q[cur_meas_line], dev_addr[cur_meas_line][cur_meas_devNbr]);
 
     } else if (dev_addr[cur_meas_line][cur_meas_devNbr][0] == 0x19) { // Device is DS28E17
-
+      send_Values(cur_meas_line,cur_meas_devNbr,'T',0x0000);
+      send_Values(cur_meas_line,cur_meas_devNbr,'X',0x0000);
+      send_Values(cur_meas_line,cur_meas_devNbr,'Y',0x0000);
+      send_Values(cur_meas_line,cur_meas_devNbr,'Z',0x0000);
     } else {
       // error: Action to be defined
     }
@@ -320,4 +349,43 @@ int OW_measurements(owu_struct_t ow_Q[]){
   }
 
   return 0;
+}
+
+void send_Address(uint8_t line , uint8_t devNbr, uint8_t I2C_add){
+  // Definition of Address message:
+  // A<FF><II><xxxxxxxxxxxx><CC>\n
+  // A : char for Address message
+  // FF: 8-bit Family code (hex)
+  // II: 8-bit internal Address for Register mapping (hex)
+  // xx: 48-bit UID
+  // CC: I2C Address (maybe not needed)
+
+  uint8_t nbytes = sprintf((char*) txbuf, "A%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",dev_addr[line][devNbr][0],
+                                                                                      dev_int_addr[line][devNbr],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][1],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][2],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][3],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][4],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][5],
+                                                                                      dev_addr[cur_meas_line][cur_meas_devNbr][6],
+                                                                                      I2C_add);
+  usb_serial_write(txbuf,nbytes);
+  UCSR1B |= (1<< UDRIE1);
+}
+
+void send_Values(uint8_t line , uint8_t devNbr, uint8_t valType, uint16_t value){
+  // Definition of Value message:
+  // V<FF><II><T><VAL>\n
+  // V : char for Value message
+  // FF: 8-bit Family code (hex)
+  // II: 8-bit internal Address for Register mapping (hex)
+  // T:  char for data type (T,X,Y,Z)
+  // VAL: value of measurement in hex
+
+  uint8_t nbytes = sprintf((char*) txbuf, "V%02x%02x%c%x\n",dev_addr[line][devNbr][0],
+                                                     dev_int_addr[line][devNbr],
+                                                     (char) valType,
+                                                     value);
+  usb_serial_write(txbuf,nbytes);
+  UCSR1B |= (1<< UDRIE1);
 }
\ No newline at end of file