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.
- 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>
- Add ADC and USART functionality to the
application, by adding the following piece of code in the
while(1)
-loop in themain
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 - Go to the implementation of ADC_0_start_conversion() by
- Hover over ADC_0_start_conversion().
- Right-click → Goto ImplementationInfo: A menu, showing the different locations, will appear.
- 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.
- View the complete list of ADC driver functions by clicking the arrow, shown in Figure 3-13.Info: The ADC driver functions marked with 2-5 in Figure 3-13 are all used by this training.
- Go to the implementation of USART_0_write() by
- Hover over USART_0_write() in the main.c file.
- Right-click → Goto ImplementationInfo: A menu, showing the different locations, will appear.
- 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.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.