2.3.1 Transmit ADC Samples

Todo: Add code to transmit ADC samples over USART when new results are ready.
  1. Open main.c and locate the empty while(1) loop.
  2. Check if the ADC has a new conversion result ready, and transmit new samples in a data streamer packet. Do this by adding the following code to the empty while(1) loop:
    if(adc_result_is_ready())
    {
    	adc_t.adc_sample = adc_get_sample();
    	adc_t.adc_result = adc_get_result();
    
    	adc_t.adc_average_result = adc_t.adc_result;
    
    	transmit_to_DV();
    }
    Info: adc_result_is_ready(), adc_get_sample() and adc_get_result() are defined in adc.c. transmit_to_DV() and the adc_t data type are defined in data_streamer.c and data_streamer.h, respectively:
    • adc_result_is_ready() returns the ADC’s result ready flag. The result ready flag is bit 0 in ADC0.INTFLAGS. This bit is set when a new result is ready from the ADC. The bit is automatically cleared when ADC0.RESULT is read.
    • adc_get_sample() returns a single ADC conversion stored in the SAMPLE register
    • adc_get_result() returns the ADC result stored in the RESULT register
    • transmit_to_DV() is implemented in data_streamer.c. This function will transmit a data streamer packet over USART.
    • adc_t is a struct variable. The structure stores the conversion values to be sent over USART.
  3. Verify that the solution builds with no errors by selecting Build → Build Solution from the Microchip Studio top menu bar or pressing the F7 key.
  4. Study the implementation of transmit_to_DV().
    Info: The packet structure follows the Data Streamer protocol. This protocol allows Data Visualizer to receive and interpret streamed multi-byte data as well as data originating from multiple sources.

    The packet is initialized in transmit_to_DV(). The packet structure or stream format is defined in Figure 2-3:

    Figure 2-3. Assignment 1: Stream Format

    The data stream Stream Format is processed in the same order as the variable group specifies. All data must be given as little-endian values, meaning that the lowest byte must be sent first. Additionally, a wrapper consisting of one byte before and one byte after the data stream variables must be added. This wrapper is used by the interpreter to synchronize to the data stream. The Start byte can be of an arbitrary value, but the end byte must be the inverse of the Start byte.

Result: Code to transmit ADC samples for Single mode has been added to main.c.