Hi,
I have implemented some code in order to try to start the ADC conversion after a timer interruption (TIOA). However, I don't have any success as it is not working.
I can see the timer is counting, but there is no ADC conversion (the adc interrupt handler doesn't come into action).
The code I have so far, is represented below. I would be very grateful if someone could give me some tips on what I'm doing wrong
#include <asf.h>
#include <stdio_serial.h>
#define USART_SERIAL USART0
#define USART_SERIAL_ID ID_USART0
#define USART_SERIAL_ISR_HANDLER USART0_Handler
#define USART_SERIAL_PIO PINS_USART_PIO
#define USART_SERIAL_TYPE PINS_USART_TYPE
#define USART_SERIAL_PINS PINS_USART_PINS
#define USART_SERIAL_MASK PINS_USART_MASK
#define USART_SERIAL_BAUDRATE 115200
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT
#define USART_IRQn USART0_IRQn
/* A reference setting for UART */
/** UART Interface */
#define CONF_UART CONSOLE_UART
/** Baudrate setting */
#define CONF_UART_BAUDRATE 115200
/** Parity setting */
#define CONF_UART_PARITY UART_MR_PAR_NO
/** Stop bits setting */
#define CONF_UART_STOP_BITS US_MR_NBSTOP_1_BIT
uint32_t received_byte;
void comm_conf(){
const sam_usart_opt_t usart_console_settings = {
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
const usart_serial_options_t uart_serial_options = {
.baudrate = CONF_UART_BAUDRATE,
.paritytype = CONF_UART_PARITY
};
/* Configure console UART. */
sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
stdio_serial_init(CONF_UART, &uart_serial_options);
usart_init_rs232(USART_SERIAL, &usart_console_settings, sysclk_get_main_hz());
usart_enable_tx(USART_SERIAL);
usart_enable_rx(USART_SERIAL);
usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY);
NVIC_EnableIRQ(USART_IRQn);}
void setup_timer(){
uint32_t ul_div = 0;
uint32_t ul_tc_clks = 0;
uint32_t ul_sysclk = sysclk_get_cpu_hz();
/* Enable peripheral clock. */
pmc_enable_periph_clk(ID_TC0);
/* TIOA configuration */
gpio_configure_pin(PIN_TC0_TIOA0, PIN_TC0_TIOA0_FLAGS);
/* Configure TC for a 1Hz frequency and trigger on RC compare. */
tc_find_mck_divisor(1, ul_sysclk, &ul_div, &ul_tc_clks, ul_sysclk);
tc_init(TC0, 0, ul_tc_clks | TC_CMR_CPCTRG | TC_CMR_WAVE |
TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET);
TC0->TC_CHANNEL[0].TC_RA = (ul_sysclk / ul_div) / 2;
TC0->TC_CHANNEL[0].TC_RC = (ul_sysclk / ul_div) / 1;
/* Start the Timer. */
tc_start(TC0, 0);
}
int setup_adc(){
adc_init(ADC, sysclk_get_main_hz(), 1000000, ADC_STARTUP_NORM);
adc_configure_trigger(ADC, ADC_TRIG_TIO_CH_0, ADC_MR_FREERUN_ON);
adc_configure_power_save(ADC, ADC_MR_SLEEP_NORMAL, ADC_MR_FWUP_ON);
adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1);
adc_enable_anch(ADC);
adc_set_channel_input_gain(ADC, ADC_CHANNEL_0, ADC_GAINVALUE_0);
adc_set_resolution(ADC, ADC_12_BITS);
adc_enable_channel(ADC, ADC_CHANNEL_0);
adc_enable_interrupt(ADC, ADC_IER_DRDY);
adc_check(ADC, sysclk_get_main_hz());
}
void ADC_IrqHandler(void){
// Check the ADC conversion status
if ((adc_get_status(ADC) & ADC_ISR_DRDY) == ADC_ISR_DRDY)
{
// Get latest digital data value from ADC and can be used by application
uint32_t result = adc_get_latest_value(ADC);
printf("adc measured: %d\n", result);
}
}
int main (void){
sysclk_init();
board_init();
comm_conf();
setup_timer();
setup_adc();
adc_start(ADC);
while(1){
}return 0;}
Thanks in advance