]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
ADCuC: fixed AVR code to get proper output (incl. all channels)
authorPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Thu, 13 Jul 2017 11:42:55 +0000 (13:42 +0200)
committerPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Thu, 13 Jul 2017 11:42:55 +0000 (13:42 +0200)
atmega32u4/adcuc/main.c

index 6822b56c1ee1faeb48e2fca29febfd8397f908d3..0fb80c9d9f55addfa39664180218ee9888a5b4f6 100644 (file)
@@ -90,6 +90,7 @@ void send_answer_hex(uint8_t chan, uint16_t v) {
   txbuf[7]='\n';
   txbuf[8] = 0;
   usb_serial_write(txbuf,8);
+  usb_serial_flush_output();
   }
     
     
@@ -115,8 +116,8 @@ 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;
+void original_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) {
+  uint16_t result = (get_value_1 << 8) | get_value_2;
   send_answer_hex(channel,result);  
 }
   
@@ -133,10 +134,10 @@ void do_channel(uint8_t channel) {
     else 
       ADC2_select();
        SPDR  = 0b11000011 + ((channel & 0b00000011) << 4); // MSB=Byte1
-       while(SPDR & ((1<<SPIF) ==0));
+       while ((SPSR & (1<<SPIF)) == 0);
                get_value_1 = SPDR;
        SPDR  = 0b11101011; // LSB=Byte2
-       while(SPDR & ((1<<SPIF) ==0));
+       while ((SPSR & (1<<SPIF)) == 0);
                get_value_2 = SPDR;
     if(channel<4) 
       ADC1_unselect(); 
@@ -151,18 +152,18 @@ void read_channel(uint8_t channel) {
       ADC1_select();
     else
       ADC2_select();
-               SPDR  = 0b10000000; // MSB=Byte1
-       while(SPDR & ((1<<SPIF) ==0));
+               SPDR  = 0b00000000; // MSB=Byte1
+       while ((SPSR & (1<<SPIF)) == 0);
                get_value_1 = SPDR;
-       SPDR  = 0b00000001; // LSB=Byte2
-       while(SPDR & ((1<<SPIF) ==0));
+       SPDR  = 0b00000000; // LSB=Byte2
+       while ((SPSR & (1<<SPIF)) == 0);
                get_value_2 = SPDR;
     if(channel<4)
       ADC1_unselect();
     else
       ADC2_unselect();
 
-    orignal_value(channel, get_value_1, get_value_2);
+    original_value(channel, get_value_1, get_value_2);
 }      
 
 
@@ -196,13 +197,13 @@ __attribute__((naked)) int main(void) {
   PORTC = 0b11000000; 
   DDRC  = 0b00000000;
 
-  PORTE = 0b01000100;
+  PORTE = 0b01000000;
   DDRE  = 0b00000000;
   
   //limit[3]  |= eeprom_read_byte((uint8_t*)0x26);  
   
   //Timer0 at ~488 Hz overflow for ADC control and buttons
-  TCCR0B = (1 << CS01) | (1 << CS00);
+  TCCR0B = 4 << CS00; //(1 << CS01) | (1 << CS00);
   TIMSK0 = (1 << TOIE0); //Overflow interrupt`
 
   
@@ -215,18 +216,37 @@ __attribute__((naked)) int main(void) {
   sei();  
   
   uint16_t lasttime = 0;
+  uint16_t step = 0;
 
   while(1) {
     int n = usb_serial_getchar();
-    if (n >= 0) {
+    while (n >= 0) {
       getdata(n);
+      n = usb_serial_getchar();
       }
       
     if(time != lasttime) {
-      switch(time) {
-        case 1: do_channel(1); break;
-        case 2: read_channel(1); break;
-        case 487: time = 0; break;
+      switch(step++) {
+        case 10: do_channel(0); break;
+        case 20: read_channel(0); break;
+        case 30: do_channel(1); break;
+        case 40: read_channel(1); break;
+        case 50: do_channel(2); break;
+        case 60: read_channel(2); break;
+        case 70: do_channel(3); break;
+        case 80: read_channel(3); break;
+        case 90: do_channel(4); break;
+        case 100: read_channel(4); break;
+        case 110: do_channel(5); break;
+        case 120: read_channel(5); break;
+        case 130: do_channel(6); break;
+        case 140: read_channel(6); break;
+        case 150: do_channel(7); break;
+        case 160: read_channel(7); break;
+        }
+      if (step >= 487/2) {
+        //time = 0;
+        step = 0;
         }
       }