3.3 Add ADC and USART Functionality to Application Code

Once the ADC and USART functional drivers have been added using Atmel | START, developing the application code can be started.

Todo: Add code to the application which performs ADC conversion and sends the ADC result via USART to a terminal.
  1. Include functions for adding delays to the application code by adding the following line of code at the beginning of main.c.
    #include <util/delay.h>
  2. Add ADC and USART functionality to the application, by adding the following piece of code in the while(1)-loop in the main function.
    
        ADC_0_start_conversion(10); //Start ADC conversion on channel 10
        while(!ADC_0_is_conversion_done()); //wait for ADC conversion is done
        USART_0_write(ADC_0_get_conversion_result()); //USART write ADC result
        while(!(USART0.STATUS & USART_TXCIF_bm)); //wait for USART TX complete
        _delay_ms(500); // delay to easier observe changes to ADC input in terminal
    Info: main.c should look similar to the code in Figure 3-12
    Figure 3-12. main.c Code
  3. Go to the implementation of ADC_0_start_conversion() by
    1. Hover over ADC_0_start_conversion().
    2. Right-click → Goto Implementation
      Info: A menu, showing the different locations, will appear.
    3. Jump to where the function is implemented by selecting the first option from the pop-up window.
      Info: The implementation of ADC_0_start_conversion(), which is located in adc_basic.c, should now be visible in the Atmel Studio editor window.
  4. View the complete list of ADC driver functions by clicking the arrow, shown in Figure 3-13.
    Figure 3-13. ADC Driver Function List

    Info: The ADC driver functions marked with 2-5 in Figure 3-13 are all used by this training.
  5. Go to the implementation of USART_0_write() by
    1. Hover over USART_0_write() in the main.c file.
    2. Right-click → Goto Implementation
      Info: A menu, showing the different locations, will appear.
    3. Jump to where the function is implemented by selecting the first option from the pop-up window.
      Info: The implementation of USART_0_write(), which is located in usart_basic.c should now be visible in the Atmel Studio editor window, as shown in Figure 3-14.
      Figure 3-14. USART Driver Function List
      Info: USART_0_write is the only USART driver function used in this training.
Result: Adding ADC and USART functionality to the application code is completed.