]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
Add configuration channels fo ADCuc
authorOle Artz <ole.artz@t-online.de>
Fri, 23 Jun 2017 10:02:49 +0000 (12:02 +0200)
committerOle Artz <ole.artz@t-online.de>
Fri, 23 Jun 2017 10:02:49 +0000 (12:02 +0200)
atmega32u4/adcuc/main.c

index f6139ab4824bf6c37be8ca223e155d237282aa92..6822b56c1ee1faeb48e2fca29febfd8397f908d3 100644 (file)
 //Icc?         -- Firmware Info
 
 
-#define LED2_ON()  PORTB |= (1<<PB1)
-#define LED2_OFF() PORTB &= ~(1<<PB1)
+#define LED1_ON()  PORTF |= (1<<PF5)
+#define LED1_OFF() PORTF &= ~(1<<PF5)
+#define LED2_ON()  PORTF |= (1<<PF6)
+#define LED2_OFF() PORTF &= ~(1<<PF6)
+
+#define ADC1_select()   PORTB &= ~(1<<PB7)
+#define ADC1_unselect() PORTB |= (1<<PB7)
+#define ADC2_select()   PORTB &= ~(1<<PB0)
+#define ADC2_unselect() PORTB |= (1<<PB0)
+
+#define IO1_set()   PORTD |=  (1<<PD4)
+#define IO1_unset() PORTD &= ~(1<<PD4)
+#define IO2_set()   PORTD |=  (1<<PD6)
+#define IO2_unset() PORTD &= ~(1<<PD6)
+#define IO3_set()   PORTD |=  (1<<PD7)
+#define IO3_unset() PORTD &= ~(1<<PD7)
+#define IO4_set()   PORTB |=  (1<<PB5)
+#define IO4_unset() PORTB &= ~(1<<PB5)
+#define IO5_set()   PORTB |=  (1<<PB6)
+#define IO5_unset() PORTB &= ~(1<<PB6)
+#define IO6_set()   PORTC |=  (1<<PC6)
+#define IO6_unset() PORTC &= ~(1<<PC6)
+#define IO7_set()   PORTC |=  (1<<PC7)
+#define IO7_unset() PORTC &= ~(1<<PC7)
+#define IO8_set()   PORTE |=  (1<<PE2)
+#define IO8_unset() PORTE &= ~(1<<PE2)
 
 volatile uint16_t time = 0;
 
-uint8_t rxcnt = 0, txpoint = 0;
+uint8_t rxcnt = 0;
 uint8_t rxbuf[7];
-uint8_t txbuf[7];
-  
-void uart_puts(const uint8_t *s ){
-  while (*s) 
-    usb_serial_putchar(*s++);
-  } 
+uint8_t txbuf[16];
+
+
 
 uint8_t nib_to_hex(uint16_t in, uint8_t nib) {
   uint8_t t = (in >> (nib*4)) & 0xF;
   if (t <= 9) {return t + 0x30;}
   return t - 10 + 0x61;
   }
+
 uint8_t hex_to_int(uint8_t h) { //assumes valid number
   if (h < 0x40) return h-0x30;
   if (h < 0x50) return h-0x41+10;
                 return h-0x61+10;
   }
-void sub2(uint8_t* c1, uint8_t* c2) {
-  uint8_t b = hex_to_int(*c1)*16 + hex_to_int(*c2);
-  b -= 4;
-  *c1 = nib_to_hex(b,1);
-  *c2 = nib_to_hex(b,0);
-  }
 
-void send_answer_buf(uint8_t* b) {
-  UCSR1B |= (1<< UDRIE1);
-  }
-  
-void send_answer_hex(uint16_t v) {
-  txbuf[0]='A';
-  txbuf[1]=nib_to_hex(v,2);
-  txbuf[2]=nib_to_hex(v,1);
-  txbuf[3]=nib_to_hex(v,0);
-  txbuf[4]='\n';
-  txbuf[5] = 0;
-  send_answer_buf(txbuf);
+void send_answer_hex(uint8_t chan, uint16_t v) {
+  txbuf[0]='A';//ADC
+  txbuf[1]=nib_to_hex(chan,0);
+  txbuf[2]='-';//temperature or modi
+  txbuf[3]=nib_to_hex(v,3);
+  txbuf[4]=nib_to_hex(v,2);
+  txbuf[5]=nib_to_hex(v,1);
+  txbuf[6]=nib_to_hex(v,0);
+  txbuf[7]='\n';
+  txbuf[8] = 0;
+  usb_serial_write(txbuf,8);
   }
     
     
@@ -102,21 +115,64 @@ void getdata(uint8_t buf) {
   if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; }  
   } 
 
+void orignal_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) {
+  uint16_t result = (get_value_2 << 8) | get_value_1;
+  send_answer_hex(channel,result);  
+}
+  
 void update_eeprom(void) {
   //etc...
   // eeprom_update_byte((uint8_t*)0x28,adc_enable);
   }
 
-  
-  
+void do_channel(uint8_t channel) {
+    uint8_t get_value_1;
+    uint8_t get_value_2;
+    if(channel<4) 
+      ADC1_select(); 
+    else 
+      ADC2_select();
+       SPDR  = 0b11000011 + ((channel & 0b00000011) << 4); // MSB=Byte1
+       while(SPDR & ((1<<SPIF) ==0));
+               get_value_1 = SPDR;
+       SPDR  = 0b11101011; // LSB=Byte2
+       while(SPDR & ((1<<SPIF) ==0));
+               get_value_2 = SPDR;
+    if(channel<4) 
+      ADC1_unselect(); 
+    else 
+      ADC2_unselect();
+       }  
+
+void read_channel(uint8_t channel) {
+    uint8_t get_value_1;
+    uint8_t get_value_2;
+    if(channel<4)
+      ADC1_select();
+    else
+      ADC2_select();
+               SPDR  = 0b10000000; // MSB=Byte1
+       while(SPDR & ((1<<SPIF) ==0));
+               get_value_1 = SPDR;
+       SPDR  = 0b00000001; // LSB=Byte2
+       while(SPDR & ((1<<SPIF) ==0));
+               get_value_2 = SPDR;
+    if(channel<4)
+      ADC1_unselect();
+    else
+      ADC2_unselect();
+
+    orignal_value(channel, get_value_1, get_value_2);
+}      
+
+
 __attribute__((naked)) int main(void) {
   
   CLKPR = (1 << CLKPCE); // prescaler 2 = 8 MHz
   CLKPR = (1 << CLKPS0); // prescaler 2
   usb_init(); 
-  
 
-  
+
  // Configure ports
  //LED:     PF6, PF5
  //Switch:  PF4, PF7
@@ -137,7 +193,7 @@ __attribute__((naked)) int main(void) {
   PORTF = 0b11010011;
   DDRF  = 0b01100000;
 
-  PORTC = 0b11000000;
+  PORTC = 0b11000000; 
   DDRC  = 0b00000000;
 
   PORTE = 0b01000100;
@@ -166,6 +222,14 @@ __attribute__((naked)) int main(void) {
       getdata(n);
       }
       
+    if(time != lasttime) {
+      switch(time) {
+        case 1: do_channel(1); break;
+        case 2: read_channel(1); break;
+        case 487: time = 0; break;
+        }
+      }
+      
 //  update_eeprom();
     lasttime = time;
     }