|
| 1 | +#include "application.h" |
| 2 | + |
| 3 | +void ADCC_SetChannel(adcc_channel_t channel) { |
| 4 | + // select the A/D channel |
| 5 | + ADPCH = channel; |
| 6 | +} |
| 7 | + |
| 8 | +void ApplicationTask (void) { |
| 9 | + // change the ADCC modes based on button press |
| 10 | + switch(count) { |
| 11 | + case BASIC_MODE: |
| 12 | + if(!basicModeInit) { |
| 13 | + // stop the timer and configure it to 10ms for ADC auto conversion |
| 14 | + TMR0_StopTimer(); |
| 15 | + TMR0_Reload(TIMER0_10ms); |
| 16 | + // turn off both LEDs |
| 17 | + LED_D3_SetLow(); |
| 18 | + LED_D2_SetLow(); |
| 19 | + // initialize ADC in basic mode with potentiometer as input |
| 20 | + ADCC_Initialize_BasicMode(); |
| 21 | + ADCC_SetChannel(POT_DATA); |
| 22 | + // start Timer which in turn starts ADC conversion |
| 23 | + TMR0_StartTimer(); |
| 24 | + basicModeInit = true; |
| 25 | + } else { |
| 26 | + // if ADC conversion is ready, print the data on serial port |
| 27 | + if(adcReadyFlag) { |
| 28 | + adcReadyFlag = false; |
| 29 | + printf ("Basic Mode - ADRES=%d \r\n", ADRES); |
| 30 | + } |
| 31 | + } |
| 32 | + break; |
| 33 | + case AVG_MODE: |
| 34 | + if(!avgModeInit) { |
| 35 | + // stop the timer |
| 36 | + TMR0_StopTimer(); |
| 37 | + // turn on LED 1 |
| 38 | + LED_D3_SetLow(); |
| 39 | + LED_D2_SetHigh(); |
| 40 | + // initialize ADC in average mode with potentiometer as input |
| 41 | + ADCC_Initialize_AverageMode(); |
| 42 | + ADCC_SetChannel(POT_DATA); |
| 43 | + // start Timer which in turn starts ADC conversion |
| 44 | + TMR0_StartTimer(); |
| 45 | + avgModeInit = true; |
| 46 | + basicModeInit = false; |
| 47 | + } else { |
| 48 | + // if ADC value is within threshold limits, print the actual value |
| 49 | + if(adcReadyFlag) { |
| 50 | + adcReadyFlag = false; |
| 51 | + printf ("AVG Mode - ADFLT=%d \r\n", ADFLTR); |
| 52 | + } |
| 53 | + // check if upper threshold is crossed |
| 54 | + if(ADCC_HasErrorCrossedUpperThreshold()) { |
| 55 | + printf ("AVG Mode - ADFLT=%d - SP=%d - UT=%d - UT Crossed\r\n", ADFLTR,ADSTPT,ADUTH); |
| 56 | + // check if lower threshold is crossed |
| 57 | + } else if(ADCC_HasErrorCrossedLowerThreshold()) { |
| 58 | + printf ("AVG Mode - ADFLT=%d - SP=%d - LT=%d - LT Crossed\r\n", ADFLTR,ADSTPT,ADLTH); |
| 59 | + } |
| 60 | + } |
| 61 | + break; |
| 62 | + case BURST_AVG_MODE: |
| 63 | + if(!burstAvgModeInit) { |
| 64 | + // stop the timer |
| 65 | + TMR0_StopTimer(); |
| 66 | + // turn on LED 2 |
| 67 | + LED_D3_SetHigh(); |
| 68 | + LED_D2_SetLow(); |
| 69 | + // initialize ADC in burst average mode with potentiometer as input |
| 70 | + ADCC_Initialize_BurstAverageMode(); |
| 71 | + ADCC_SetChannel(POT_DATA); |
| 72 | + // start Timer which in turn starts ADC conversion |
| 73 | + TMR0_StartTimer(); |
| 74 | + burstAvgModeInit = true; |
| 75 | + avgModeInit = false; |
| 76 | + } else { |
| 77 | + // if ADC value is within threshold limits, print the actual value |
| 78 | + if(adcReadyFlag) { |
| 79 | + adcReadyFlag = false; |
| 80 | + printf ("Burst AVG Mode - ADFLT=%d \r\n", ADFLTR); |
| 81 | + } |
| 82 | + // check if upper threshold is crossed |
| 83 | + if(ADCC_HasErrorCrossedUpperThreshold()) { |
| 84 | + printf ("Burst AVG Mode - ADFLT=%d - SP=%d - UT=%d - UT Crossed\r\n", ADFLTR,ADSTPT,ADUTH); |
| 85 | + // check if lower threshold is crossed |
| 86 | + } else if(ADCC_HasErrorCrossedLowerThreshold()) { |
| 87 | + printf ("Burst AVG Mode - ADFLT=%d - SP=%d - LT=%d - LT Crossed\r\n", ADFLTR,ADSTPT,ADLTH); |
| 88 | + } |
| 89 | + } |
| 90 | + break; |
| 91 | + case LPF_MODE: |
| 92 | + if(!lpfModeInit) { |
| 93 | + // stop the timer and configure it to 1ms for ADC auto conversion |
| 94 | + TMR0_StopTimer(); |
| 95 | + TMR0_Reload(TIMER0_1ms); |
| 96 | + // turn on both LEDs |
| 97 | + LED_D3_SetHigh(); |
| 98 | + LED_D2_SetHigh(); |
| 99 | + // initialize ADC in low pass filter mode with DC offset having noise as input |
| 100 | + ADCC_Initialize_LowPassFilterMode(); |
| 101 | + ADCC_SetChannel(FG_DATA); |
| 102 | + // start Timer which in turn starts ADC conversion |
| 103 | + TMR0_StartTimer(); |
| 104 | + lpfModeInit = true; |
| 105 | + burstAvgModeInit = false; |
| 106 | + } else { |
| 107 | + // send the low pass filter value to the data visualizer |
| 108 | + if(adcReadyFlag) { |
| 109 | + adcReadyFlag = false; |
| 110 | + EUSART1_Write(0x03); |
| 111 | + EUSART1_Write(ADRESL); // Visualizer reads low byte first |
| 112 | + EUSART1_Write(ADRESH); // Then reads the high byte// These are commands sent to the Data Visualizer, 0x03 = Start |
| 113 | + EUSART1_Write(ADFLTRL); // Visualizer reads low byte first |
| 114 | + EUSART1_Write(ADFLTRH); // Then reads the high byte |
| 115 | + EUSART1_Write(0xFC); // Stop command |
| 116 | + } |
| 117 | + } |
| 118 | + break; |
| 119 | + default: |
| 120 | + count = BASIC_MODE; |
| 121 | + lpfModeInit = false; |
| 122 | + break; |
| 123 | + } |
| 124 | +} |
0 commit comments