int main(void) { //Initial Serial, ADC & LCD Display: USARTInit(52); lcd_init(); ADCinit(); pwm_init(); char adc_output[6][4]; int adc_value[6]; // Variable to hold ADC result while(1) { for(int i=0;i<6;i++){ adc_value[i] = ADCread(i); itoa(adc_value[i],adc_output[i],10); USARTWriteString(adc_output[i]); if(i!=5){USARTWriteChar(';');} } //Sending a new line: USARTWriteChar('\r'); USARTWriteChar('\n'); //Set PWM: pwm(adc_value[5]); //Show data on the lcd display show(adc_output); //Delay: _delay_ms(500); } }
void TC5_Handler (void) { __prevData = __newData; __newData = ADCread(); if ((__prevData < MIDPOINT) && (__newData >= MIDPOINT)) { __newSlope = __newData - __prevData; if (abs(__newSlope - __maxSlope) < __slopeTolerance) { __slope[__arrayIndex] = __newSlope; __timer[__arrayIndex] = __time; __time = 0; if (__arrayIndex == 0) { __noMatch = 0; __arrayIndex++; } else if ((abs(__timer[0] - __timer[__arrayIndex]) < __timerTolerance) && (abs(__slope[0] - __newSlope) < __slopeTolerance)) { __totalTimer = 0; for (uint8_t i = 0; i < __arrayIndex; i++) { __totalTimer += __timer[i]; } __period = __totalTimer; __timer[0] = __timer[__arrayIndex]; __slope[0] = __slope[__arrayIndex]; __arrayIndex = 1; __noMatch = 0; } else { __arrayIndex++; if (__arrayIndex > ARRAY_DEPTH - 1) { __arrayIndex = 0; __noMatch = 0; __maxSlope = 0; } } } else if (__newSlope > __maxSlope) { __maxSlope = __newSlope; __time = 0; __noMatch = 0; __arrayIndex = 0; } else { __noMatch++; if (__noMatch > ARRAY_DEPTH - 1) { __arrayIndex = 0; __noMatch = 0; __maxSlope = 0; } } } if (__clippingPin > 0) { if (__newData == BOTTOMPOINT || __newData == TOPPOINT) { digitalWrite(__clippingPin, HIGH); __clipping = true; } } __time++; // Incremented at sampleRate __amplitudeTimer++; // Incremented at sampleRate __newMaxAmplitude = abs(MIDPOINT - __newData); if (__newMaxAmplitude > __maxAmplitude) { __maxAmplitude = __newMaxAmplitude; } if (__amplitudeTimer >= TIMER_TIMEOUT) { __amplitudeTimer = 0; __checkMaxAmp = __maxAmplitude; __maxAmplitude = 0; } TC5->COUNT16.INTFLAG.bit.MC0 = 1; // Clear interrupt }