4.2 Transmit the Results
To visualize the ADC results using the Data Visualizer plug-in, the user must first send the results through USART. The USART BAUD register must be used to configure the baud rate. It is presented in the figure below.
The following code must be used.
USART1.BAUD = (uint16_t)USART1_BAUD_RATE(115200);
The USART1_BAUD_RATE macro is used to compute the corresponding value for the BAUD register. It is presented below.
#define USART1_BAUD_RATE(BAUD_RATE) (((float)F_CPU*64/(16 *(float)BAUD_RATE))+0.5)
The USART transmission can be enabled using the CTRLB register, presented below.
The following code must be used.
USART1.CTRLB = USART_TXEN_bm; /* Enable TX */
The character size can be configured using the CTRLC register.
The following code must be used.
USART1.CTRLC = USART_CHSIZE_8BIT_gc; /* Configure character size: 8 bit */
To transmit one byte through USART, the user must check if the USART buffer is ready to transmit data and then write the data to the TXDATAL register.
For the data to be correctly interpreted by the Data Visualizer, it must follow a specific format. The ADC result of 12-bit resolution will be provided using a 16-bit format (the sign bit is extended), and the two bytes of the result will be transmitted as described below.
The frame start token (START_TOKEN
) and the frame end token
(END_TOKEN
) macros are defined as described below.
#define START_TOKEN 0x03 /* Start Frame Token */
#define END_TOKEN 0xFC /* End Frame Token */
Inside the infinite loop, the ADC result will be read, it will be transmitted through USART using a specific format, and the conversion will be started again.
The following code must be added to the main infinite loop to transmit the ADC result through USART:
while (1)
{
/* Start the ADC conversion */
ADC0_start();
/* Read the ADC result */
adcVal = ADC0_read();
/* Transmit the ADC result to be plotted using Data Visualizer */
USART1_Write(START_TOKEN);
USART1_Write(adcVal & 0x00FF);
USART1_Write(adcVal >> 8);
USART1_Write(END_TOKEN);
}