4 Adding Application Logic to the Project
To develop and run the application, follow these steps:
- Add the required macro and variables outside the
main()
function in themain.c
file.#define ADC_VREF (5.0f) uint16_t adc_count[3]; volatile bool transferDone = false; float inp_voltage[3];
- Add a DMAC callback function outside
the
main()
function in themain.c
file.void DMAC_Callback(DMAC_TRANSFER_EVENT status, uintptr_t context) { if(status == DMAC_TRANSFER_EVENT_COMPLETE) { // Transfer is completed. transferDone = true; } }
Figure 4-1. Adding Macro and DMAC Callback Function and Variables - Add the
printf
statements, DMAC callback register function, DMAC channel transfer function, ADC enable function, and the Timer Start function inside themain()
function as shown below:printf("\n\r---------------------------------------------------------"); printf("\n\r ADC Sequencer Demo "); printf("\n\r---------------------------------------------------------\n\r"); TC0_TimerStart(); ADC0_Enable(); DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, DMAC_Callback, 0); DMAC_ChannelTransfer(DMAC_CHANNEL_0, (const void *)&ADC0_REGS->ADC_RESULT, &adc_count, sizeof(adc_count));
- Inside the while loop, add the
application logic as shown
below:
if(transferDone == true) { transferDone = false; for(int i = 0; i<3;i++) { /* The below formula is used to find the voltage for the corresponding ADC value. Input_Volt = (ADC_count * Ref_Volt)/(2^No.of Bits); Where Ref_Volt = 5V and No. of Bits = 12*/ inp_voltage[i] = (float)adc_count[i] * ADC_VREF / 4095U; } printf("\rADC0 Count = 0x%x, ADC0 Voltage = %d.%02d V, " "ADC1 Count = 0x%x, ADC1 Voltage = %d.%02d V, " "ADC2 Count = 0x%x, ADC2 Voltage = %d.%02d V ", adc_count[0], (int)inp_voltage[0], (int)((inp_voltage[0] - (int)inp_voltage[0])*100.0), adc_count[1], (int)inp_voltage[1], (int)((inp_voltage[1] - (int)inp_voltage[1])*100.0), adc_count[2], (int)inp_voltage[2], (int)((inp_voltage[2] - (int)inp_voltage[2])*100.0)); DMAC_ChannelTransfer(DMAC_CHANNEL_0, (const void *)&ADC0_REGS->ADC_RESULT, &adc_count, sizeof(adc_count)); }
Note: The formula used inprintf
statements is to print the integer and the decimal values. In the expression %d.%02d, %d represents the integer value and .%02d represents the decimal value rounded to two places.Figure 4-2. Adding Application Logic to main.c
Note: The example code shown above is in the main routine inside while (1). This example code can be run in Standby mode or other sub-routine, where the CPU intervention and CPU load reduction can be observed.