2.4.2 Generated Code Overview
To generate the code designed with MCC, click the Generate button. Then, to continue the application development, close the MCC. The MCC generated files can be seen in the Projects tab, under the created project.
The generated files are included in the main source file and the system is
initialized. The generated code for main.c
is presented below.
Code Listing 1 – Main Generated File
#include "mcc_generated_files/mcc.h" /* Main application */ int main(void) { /* Initializes MCU, drivers and middleware */ SYSTEM_Initialize(); /* Replace with your application code */ while (1) { } } /** End of File */
The SYSTEM_Initialize
function is used to initialize the system and all
the peripheral modules, as configured earlier using MCC: The system clock, the ADC, the
USART, and the VREF.
The generated files provide ADC and USART functions that will help the user
to implement the algorithm. The functions of interest for this application are
ADC0_GetDiffConversion
and USART1_Write
. Their
implementation in the MCC generated files is presented in the code listings below.
Code Listing 2 – ADC Function Used to Obtain the Differential Conversion Result
diff_adc_result_t ADC0_GetDiffConversion(adc_0_channel_t channel, adc_0_muxneg_channel_t channel1) { diff_adc_result_t res; ADC0_StartDiffConversion(channel, channel1); while (!ADC0_IsConversionDone()); res = ADC0_GetConversionResult(); ADC0.INTFLAGS |= ADC_RESRDY_bm; return res; }
Code Listing 3 – USART Function Used to Transmit the ADC Result
void USART1_Write(const uint8_t data)
{
while (!(USART1.STATUS & USART_DREIF_bm)) ;
USART1.TXDATAL = data;
}