Advertisement
halfordC

Untitled

Jul 5th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.43 KB | None | 0 0
  1. /*
  2.  * ADCTestMux.c
  3.  *
  4.  * This is a MicroController program to read 44 separate knobs, through 5 multiplexers and 1 16 channel ADC.
  5.  * The 16 channel ADC is internal, and the 5 8-channel Multiplexers are external.
  6.  * Created: 6/26/2019 6:01:49 PM
  7.  * Author : Hal
  8.  */
  9.  
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include "OLEDLib.h"
  13.  
  14. volatile uint8_t internalMuxSwitch = 0; //this number should go from 0 to 8.
  15. volatile uint8_t externalMuxSwitch = 0; //this number should go from 0 to 7.
  16. uint8_t adcMuxWrite = 0;
  17.  
  18. char* adcTestArray = "ADC Value is: xxxx  ";
  19.  
  20. volatile uint16_t outVolume[8]; // these will end up being from -70 to 10, but for now, they will be 10 bit values. (straight ADC Reads)
  21. uint16_t prevOutVolume[8];
  22. //do these need to be volatile?
  23. uint16_t pitch[8];
  24. uint16_t eTime[8];
  25. uint16_t eLevel[8];
  26. uint16_t trackVolume[8];
  27. volatile uint16_t gpKnob0;
  28. volatile uint16_t gpKnob1;
  29. volatile uint16_t gpKnob2;
  30. volatile uint16_t gpKnob3;
  31.  
  32.  
  33.  
  34. volatile uint8_t averageCounter = 0; //goes from 0 to 63;
  35. volatile uint16_t averageSum = 0;
  36.  
  37. //mux pins are 13, 12, and 11. that way we can see the onboard light flash
  38. //which are PB7, 6, and 5.
  39. void startADCConversion()
  40. {
  41.    
  42.     adcMuxWrite = (internalMuxSwitch+64); //(interlanMuxSwitch|64) seems to yeild the same results
  43.     ADMUX = adcMuxWrite; //this should write the value of the internal ADC while keeping the ref0 bit high.
  44.     //only for ports 0 through 7 though, port 8 seems to have a different set of bits it needs set.
  45.    
  46.     ADCSRA |= (1 << ADSC); //this moves the read instruction bit to the ADC Register.
  47. }
  48.  
  49. void initAdc()
  50. {
  51.     DDRB = 0B11100000; //init pins B7, 6, and 5 as select pins on the external mux.
  52.    
  53.     ADMUX = (1 << REFS0);
  54.     ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 <<ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  55.     ADCSRB = (1 << MUX5);
  56.     DIDR0 = (1<<ADC0D); // we should set this register to all 1s, so there is no digital input triggering.
  57.    
  58.     startADCConversion();
  59. }
  60.  
  61. void numPrinter(char* charArray,uint8_t startingPos, uint8_t numCharacters, uint16_t inputNumber)
  62. {
  63.     //this needs to go in the OLED Library.
  64.     uint8_t onesPlace = 0;
  65.     uint8_t tensPlace = 0;
  66.     uint8_t hunderedsPlace = 0;
  67.     uint8_t thousandsPlace = 0;
  68.     uint8_t tenThousandsPlace = 0;
  69.    
  70.     switch(numCharacters)
  71.     {
  72.         case 0:
  73.         break;
  74.        
  75.         case 1:
  76.         onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
  77.         charArray[startingPos] = onesPlace;
  78.         break;
  79.        
  80.         case 2:
  81.         onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
  82.         tensPlace = (inputNumber/10)+48;
  83.         charArray[(startingPos+1)] = onesPlace;
  84.         charArray[startingPos] = tensPlace;
  85.         break;
  86.        
  87.         case 3:
  88.         onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
  89.         tensPlace = ((inputNumber%100)/10)+48;
  90.         hunderedsPlace = (inputNumber/100)+48;
  91.         charArray[(startingPos+2)] = onesPlace;
  92.         charArray[(startingPos+1)] = tensPlace;
  93.         charArray[startingPos] = hunderedsPlace;
  94.         break;
  95.        
  96.         case 4:
  97.         onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
  98.         tensPlace = ((inputNumber%100)/10)+48;
  99.         hunderedsPlace = ((inputNumber%1000)/100)+48;
  100.         thousandsPlace = (inputNumber/1000)+48;
  101.         charArray[(startingPos+3)] = onesPlace;
  102.         charArray[(startingPos+2)] = tensPlace;
  103.         charArray[(startingPos+1)] = hunderedsPlace;
  104.         charArray[startingPos] = thousandsPlace;
  105.         break;
  106.        
  107.         case 5:
  108.         onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
  109.         tensPlace = ((inputNumber%100)/10)+48;
  110.         hunderedsPlace = ((inputNumber%1000)/100)+48;
  111.         thousandsPlace = ((inputNumber%10000)/1000)+48;
  112.         tenThousandsPlace = (inputNumber/10000)+48;
  113.         charArray[(startingPos+4)] = onesPlace;
  114.         charArray[(startingPos+3)] = tensPlace;
  115.         charArray[(startingPos+2)] = hunderedsPlace;
  116.         charArray[(startingPos+1)] = thousandsPlace;
  117.         charArray[startingPos] = tenThousandsPlace;
  118.         break;
  119.        
  120.         //if your number is higher than 16 bit, sorry, no can do.
  121.         default:
  122.         break;
  123.    
  124.     }
  125.  
  126.  
  127. }
  128.  
  129.  
  130. ISR(ADC_vect)
  131. {
  132.     //we need to increment the count, but also increment the step of the multiplexer.
  133.     startADCConversion();
  134.    
  135.     switch(internalMuxSwitch)
  136.     {//I don't think we can let our mux switch just overflow anymore. maybe we can use a modulo operator for checking the internal switch?
  137.         case 0:
  138.         outVolume[externalMuxSwitch] = ADC + outVolume[externalMuxSwitch];
  139.         externalMuxSwitch++;
  140.         if(externalMuxSwitch>7)
  141.         {
  142.             externalMuxSwitch=0;
  143.             internalMuxSwitch++;
  144.         }
  145.         PORTB = (externalMuxSwitch << 5);
  146.         break;
  147.  
  148.         case 1:
  149.         pitch[externalMuxSwitch] = ADC + pitch[externalMuxSwitch];
  150.         externalMuxSwitch++;
  151.         if(externalMuxSwitch>7)
  152.         {
  153.             externalMuxSwitch=0;
  154.             internalMuxSwitch++;
  155.         }
  156.         PORTB = (externalMuxSwitch << 5);
  157.         break;
  158.  
  159.         case 2:
  160.         eTime[externalMuxSwitch] = ADC + eTime[externalMuxSwitch];
  161.         externalMuxSwitch++;
  162.         if(externalMuxSwitch>7)
  163.         {
  164.             externalMuxSwitch=0;
  165.             internalMuxSwitch++;
  166.         }
  167.         PORTB = (externalMuxSwitch << 5);
  168.         break;
  169.  
  170.         case 3:
  171.         eLevel[externalMuxSwitch] = ADC + eLevel[externalMuxSwitch];
  172.         externalMuxSwitch++;
  173.         if(externalMuxSwitch>7)
  174.         {
  175.             externalMuxSwitch=0;
  176.             internalMuxSwitch++;
  177.         }
  178.         PORTB = (externalMuxSwitch << 5);
  179.         break;
  180.  
  181.         case 4:
  182.         trackVolume[externalMuxSwitch] = ADC + trackVolume[externalMuxSwitch];
  183.         externalMuxSwitch++;
  184.        
  185.         if(externalMuxSwitch>7)
  186.         {
  187.             externalMuxSwitch=0;
  188.             internalMuxSwitch++;
  189.         }
  190.         PORTB = (externalMuxSwitch << 5);
  191.         break;
  192.  
  193.         case 5:
  194.         gpKnob0 = ADC + gpKnob0;
  195.         internalMuxSwitch++;
  196.         break;
  197.  
  198.         case 6:
  199.         gpKnob1 = ADC + gpKnob1;
  200.         internalMuxSwitch++;
  201.         break;
  202.  
  203.         case 7:
  204.         gpKnob2 = ADC + gpKnob2;
  205.         internalMuxSwitch++;
  206.         break;
  207.  
  208.         case 8:
  209.         gpKnob3 = ADC + gpKnob3;
  210.         internalMuxSwitch++;
  211.         break;
  212.  
  213.         default:
  214.         break;
  215.  
  216.     }
  217.    
  218.     if(internalMuxSwitch>8)
  219.     {
  220.         internalMuxSwitch = 0;
  221.         externalMuxSwitch = 0;
  222.         averageCounter++;
  223.     }
  224. }
  225.  
  226. int main(void)
  227. {
  228.     /* Replace with your application code */
  229.     initScreen();
  230.     initAdc();
  231.     sei();
  232.    
  233.     while (1)
  234.     {
  235.    
  236.         if(averageCounter==31){
  237.             for(int i = 0; i<8; i++){
  238.             outVolume[i] = (outVolume[i] / 32);
  239.              pitch[i] = (pitch[i] /32);
  240.              eTime[i] = (eTime[i] /32);
  241.              eLevel[i] = (eLevel[i] /32);
  242.              trackVolume[i] = (trackVolume[i] /32);
  243.             }
  244.             gpKnob0 = gpKnob0 / 32;
  245.             gpKnob1 = gpKnob1 / 32;
  246.             gpKnob2 = gpKnob2 / 32;
  247.             gpKnob3 = gpKnob3 / 32;
  248.            
  249.            
  250.         numPrinter(adcTestArray,14, 4, pitch[0]);
  251.         outputS(adcTestArray, 1);
  252.         averageCounter = 0;
  253.         }
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement