Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ADCTestMux.c
- *
- * This is a MicroController program to read 44 separate knobs, through 5 multiplexers and 1 16 channel ADC.
- * The 16 channel ADC is internal, and the 5 8-channel Multiplexers are external.
- * Created: 6/26/2019 6:01:49 PM
- * Author : Hal
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include "OLEDLib.h"
- volatile uint8_t internalMuxSwitch = 0; //this number should go from 0 to 8.
- volatile uint8_t externalMuxSwitch = 0; //this number should go from 0 to 7.
- uint8_t adcMuxWrite = 0;
- char* adcTestArray = "ADC Value is: xxxx ";
- 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)
- uint16_t prevOutVolume[8];
- //do these need to be volatile?
- uint16_t pitch[8];
- uint16_t eTime[8];
- uint16_t eLevel[8];
- uint16_t trackVolume[8];
- volatile uint16_t gpKnob0;
- volatile uint16_t gpKnob1;
- volatile uint16_t gpKnob2;
- volatile uint16_t gpKnob3;
- volatile uint8_t averageCounter = 0; //goes from 0 to 63;
- volatile uint16_t averageSum = 0;
- //mux pins are 13, 12, and 11. that way we can see the onboard light flash
- //which are PB7, 6, and 5.
- void startADCConversion()
- {
- adcMuxWrite = (internalMuxSwitch+64); //(interlanMuxSwitch|64) seems to yeild the same results
- ADMUX = adcMuxWrite; //this should write the value of the internal ADC while keeping the ref0 bit high.
- //only for ports 0 through 7 though, port 8 seems to have a different set of bits it needs set.
- ADCSRA |= (1 << ADSC); //this moves the read instruction bit to the ADC Register.
- }
- void initAdc()
- {
- DDRB = 0B11100000; //init pins B7, 6, and 5 as select pins on the external mux.
- ADMUX = (1 << REFS0);
- ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 <<ADPS2) | (1 << ADPS1) | (1 << ADPS0);
- ADCSRB = (1 << MUX5);
- DIDR0 = (1<<ADC0D); // we should set this register to all 1s, so there is no digital input triggering.
- startADCConversion();
- }
- void numPrinter(char* charArray,uint8_t startingPos, uint8_t numCharacters, uint16_t inputNumber)
- {
- //this needs to go in the OLED Library.
- uint8_t onesPlace = 0;
- uint8_t tensPlace = 0;
- uint8_t hunderedsPlace = 0;
- uint8_t thousandsPlace = 0;
- uint8_t tenThousandsPlace = 0;
- switch(numCharacters)
- {
- case 0:
- break;
- case 1:
- onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
- charArray[startingPos] = onesPlace;
- break;
- case 2:
- onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
- tensPlace = (inputNumber/10)+48;
- charArray[(startingPos+1)] = onesPlace;
- charArray[startingPos] = tensPlace;
- break;
- case 3:
- onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
- tensPlace = ((inputNumber%100)/10)+48;
- hunderedsPlace = (inputNumber/100)+48;
- charArray[(startingPos+2)] = onesPlace;
- charArray[(startingPos+1)] = tensPlace;
- charArray[startingPos] = hunderedsPlace;
- break;
- case 4:
- onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
- tensPlace = ((inputNumber%100)/10)+48;
- hunderedsPlace = ((inputNumber%1000)/100)+48;
- thousandsPlace = (inputNumber/1000)+48;
- charArray[(startingPos+3)] = onesPlace;
- charArray[(startingPos+2)] = tensPlace;
- charArray[(startingPos+1)] = hunderedsPlace;
- charArray[startingPos] = thousandsPlace;
- break;
- case 5:
- onesPlace = (inputNumber%10)+48; //this should be a value between 1 and 10.
- tensPlace = ((inputNumber%100)/10)+48;
- hunderedsPlace = ((inputNumber%1000)/100)+48;
- thousandsPlace = ((inputNumber%10000)/1000)+48;
- tenThousandsPlace = (inputNumber/10000)+48;
- charArray[(startingPos+4)] = onesPlace;
- charArray[(startingPos+3)] = tensPlace;
- charArray[(startingPos+2)] = hunderedsPlace;
- charArray[(startingPos+1)] = thousandsPlace;
- charArray[startingPos] = tenThousandsPlace;
- break;
- //if your number is higher than 16 bit, sorry, no can do.
- default:
- break;
- }
- }
- ISR(ADC_vect)
- {
- //we need to increment the count, but also increment the step of the multiplexer.
- startADCConversion();
- switch(internalMuxSwitch)
- {//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?
- case 0:
- outVolume[externalMuxSwitch] = ADC + outVolume[externalMuxSwitch];
- externalMuxSwitch++;
- if(externalMuxSwitch>7)
- {
- externalMuxSwitch=0;
- internalMuxSwitch++;
- }
- PORTB = (externalMuxSwitch << 5);
- break;
- case 1:
- pitch[externalMuxSwitch] = ADC + pitch[externalMuxSwitch];
- externalMuxSwitch++;
- if(externalMuxSwitch>7)
- {
- externalMuxSwitch=0;
- internalMuxSwitch++;
- }
- PORTB = (externalMuxSwitch << 5);
- break;
- case 2:
- eTime[externalMuxSwitch] = ADC + eTime[externalMuxSwitch];
- externalMuxSwitch++;
- if(externalMuxSwitch>7)
- {
- externalMuxSwitch=0;
- internalMuxSwitch++;
- }
- PORTB = (externalMuxSwitch << 5);
- break;
- case 3:
- eLevel[externalMuxSwitch] = ADC + eLevel[externalMuxSwitch];
- externalMuxSwitch++;
- if(externalMuxSwitch>7)
- {
- externalMuxSwitch=0;
- internalMuxSwitch++;
- }
- PORTB = (externalMuxSwitch << 5);
- break;
- case 4:
- trackVolume[externalMuxSwitch] = ADC + trackVolume[externalMuxSwitch];
- externalMuxSwitch++;
- if(externalMuxSwitch>7)
- {
- externalMuxSwitch=0;
- internalMuxSwitch++;
- }
- PORTB = (externalMuxSwitch << 5);
- break;
- case 5:
- gpKnob0 = ADC + gpKnob0;
- internalMuxSwitch++;
- break;
- case 6:
- gpKnob1 = ADC + gpKnob1;
- internalMuxSwitch++;
- break;
- case 7:
- gpKnob2 = ADC + gpKnob2;
- internalMuxSwitch++;
- break;
- case 8:
- gpKnob3 = ADC + gpKnob3;
- internalMuxSwitch++;
- break;
- default:
- break;
- }
- if(internalMuxSwitch>8)
- {
- internalMuxSwitch = 0;
- externalMuxSwitch = 0;
- averageCounter++;
- }
- }
- int main(void)
- {
- /* Replace with your application code */
- initScreen();
- initAdc();
- sei();
- while (1)
- {
- if(averageCounter==31){
- for(int i = 0; i<8; i++){
- outVolume[i] = (outVolume[i] / 32);
- pitch[i] = (pitch[i] /32);
- eTime[i] = (eTime[i] /32);
- eLevel[i] = (eLevel[i] /32);
- trackVolume[i] = (trackVolume[i] /32);
- }
- gpKnob0 = gpKnob0 / 32;
- gpKnob1 = gpKnob1 / 32;
- gpKnob2 = gpKnob2 / 32;
- gpKnob3 = gpKnob3 / 32;
- numPrinter(adcTestArray,14, 4, pitch[0]);
- outputS(adcTestArray, 1);
- averageCounter = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement