Pages: [1]   Go Down
Author Topic: Help with ADC triggered by timer interrupt  (Read 3257 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
#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 smiley

Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 109
ArduiYES!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think Iḿ missing a statement like
Code:
sysclk_enable_peripheral_clock(USART_SERIAL_ID);

for the ADC.

 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply. Besides that statement, I've added a few more code which was missing. The ADC is now working. Thanks you very much for the help.
Later on, I'll post a updated version of the code in the main post of this thread smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@rtmetz92 :
1. What other lines of code did you add?
2. For me the code works fine with TC0 but not with TC1 or TC2. Did you try with the other timers as well?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@rtmetz92:

Could we have an update please? I think many others in the forum would find this useful, including myself.
Logged

Laguna Hills, CA 92653
Offline Offline
Full Member
***
Karma: 0
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

rtmetz92 never posted his fixes and working code.

Does anybody else have it, please?

Thanks, Gary
Logged

Cheers, Gary
Due for controlling Electric car charging.
Nissan LEAF - Mini Quick Charge (mQC)

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another call for anyone to post working code for this issue...
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you seen this post:
http://forum.arduino.cc/index.php?topic=205096.0
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Magician! Very helpful post smiley
Logged

Pages: [1]   Go Up
Jump to: