2.4.3 Implement the Desired Algorithm
This subsection will describe how to implement the algorithm in the main file, using the generated files. The functions needed to test the application using the Data Visualizer will also be provided.
MPLAB Data Visualizer provides simple, one-way communication between the programmed embedded device and the computer. The embedded application must control when and how to transmit data. For this example, the data will be packed as described in Figure 2-22.
The transmitted data must be framed by start and end tokens. They are inverse/one’s complement of each other. The Data Visualizer synchronizes on framing tokens and payload size. In multibyte variables, the lower bytes must be sent first.
Code Listing 4 – Reading and Transmitting the ADC Result
int main(void) { diff_adc_result_t adcVal_12b; /* Initializes MCU, drivers and middleware */ SYSTEM_Initialize(); while (1) { adcVal_12b = ADC0_GetDiffConversion(ADC_MUXPOS_AIN3_gc, ADC_MUXNEG_AIN4_gc); USART1_Write(START_TOKEN); USART1_Write(adcVal_12b & 0x00FF); USART1_Write(adcVal_12b >> 8); USART1_Write(END_TOKEN); } }
START_TOKEN
and END_TOKEN
are user defined macros
that must be also defined in the main.c
file to be used in the main
function, as presented
below.#define START_TOKEN 0x03 /* Start Frame Token */ #define END_TOKEN 0xFC /* End Frame Token */