4.3 Add RTC, ADC, and SLPCTRL Functionality in Application Code

Once the RTC, CPUINIT, and SLPCTRL modules have been added and reconfigured using Atmel | START, the application code needs to be updated in Atmel Studio.

Todo: Update the code in Atmel Studio to make use of these module drivers just added through Atmel | START. Specifically, update the main.c and the driver_isr.c file.
  1. Update the main.c file:
    1. Include the sleep header file at the top of the file:
      
          #include <avr/sleep.h>
      
    2. Enable the Sleep Standby mode by adding the following piece of code in the main function after initialization:
      
          //Set sleep mode to STANDBY mode
          set_sleep_mode(SLEEP_MODE_STANDBY);
          sleep_enable();
      
    3. Set the device to Sleep mode by replacing the following piece of code in the while-loop of the main function:
        
          //Enter into sleep mode
          sleep_cpu();
      
      The complete code of the main.c file should look like:
      Figure 4-9. main.c Code

  2. Edit the driver_isrc.c file by double-clicking and open it in the Solution Explorer in Atmel Studio:
    1. Start ADC conversion when RTC interrupt is triggered by adding the following line of code in the RTC interrupt routine:
        
          /* RTC Overflow Interrupt handling */
          ADC_0_start_conversion(10); //start ADC conversion on channel 10
      
    2. Manually add the ADC result ready interrupt routine as:
      
      ISR(ADC0_RESRDY_vect)
      {
          /* ADC result ready Interrupt handling: start USART transmission */
          USART_0_write(ADC_0_get_conversion_result()); //USART write ADC result
          while(!(USART0.STATUS & USART_TXCIF_bm)); //wait for USART TX complete
          USART0.STATUS = USART_TXCIF_bm; //Clear TXCIF flag
      	
          /* The interrupt flag has to be cleared manually */
          ADC0.INTFLAGS = ADC_RESRDY_bm;
      }
      

      In the ADC result ready interrupt routine, the ADC result ready interrupt will trigger for the ADC result to be sent over USART.

      The complete code of driver_isr.c looks like:
      Figure 4-10. driver_isr.c Code

Program the device by clicking Debug → Start without Debugging on the top menu window or by using the shortcut Ctrl+Alt+F5.
Info: Start Without Debugging will build the project and program the device as long as there are no build errors.