SAM4S ADC

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

jma
Posts: 7
Joined: Wed Oct 22, 2014 4:04 am

SAM4S ADC

Wed Oct 22, 2014 4:21 am

Hello,
I am a complete noob to the SAM4S, having mostly worked with PICs in the past. I am trying to get the ADC working on a SAM4S Xplained Pro board, using the code from ADC Quick Start guide with a few tweaks that I found in an online search but am not having any luck.  This is my code:

Code: Select all

/**
 * \file
 *
 * \brief Empty user application template
 *
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * This is a bare minimum user application template.
 *
 * For documentation of the board, go \ref group_common_boards "here" for a link
 * to the board-specific documentation.
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# Minimal main function that starts with a call to board_init()
 * -# Basic usage of on-board LED and button
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
#include <asf.h>
#include <pwm.h>
#include <adc.h>
#include <dacc.h>

#define ADC_CLOCK   6400000

void adc_setup(void);
void ADC_IrqHandler(void);

volatile uint32_t adc_val = -1;
volatile int adcReady = 0;

int main (void)
{	
	uint32_t dac_val;
	
	board_init();
	
	// Insert application code here, after the board has been initialized.
	sysclk_init();
	
	// ADC input on PB1/AD5 pin 5
	adc_setup(); 
	uint32_t adcResult;
	
	while (1) {	
		// read ADC value
        adcReady = 0;
        adc_start(ADC);
        while(!adcReady) {
	        /* Wait for IRQ handler to set resReady */
        }
		adcResult = adc_val;
	}
}

void adc_setup(void)
{
	//sysclk_enable_peripheral_clock(ID_ADC);	
	pmc_enable_periph_clk(ID_ADC);	
	adc_init(ADC, sysclk_get_main_hz(), ADC_CLOCK, 8);		// Initialize the  ADC with the specified ADC clock and startup time
	adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1);	// Settling time = 17 periods of ADCClock
	adc_set_resolution(ADC, ADC_MR_LOWRES_BITS_12);			// 12-bit resolution
	adc_enable_channel(ADC, ADC_CHANNEL_5);					// AD5/PB1???
	adc_enable_interrupt(ADC, ADC_IER_DRDY);				// Data Ready Interrupt Enable
	adc_configure_trigger(ADC, ADC_TRIG_SW, 0);				// Free run mode
	adc_disable_channel_differential_input(ADC, ADC_CHANNEL_5);   // ??
	
	NVIC_EnableIRQ(ADC_IRQn);
}

void ADC_IrqHandler(void)
{
	// Check the ADC conversion status
	//if ((adc_get_status(ADC) & ADC_ISR_DRDY) == ADC_ISR_DRDY)
	if (adc_get_status(ADC) & ADC_ISR_DRDY)
	{
		// Get latest digital data value from ADC and can be used by application
		adc_val = adc_get_latest_value(ADC);
		adcReady = 1;
	}
} 
If I put a breakpoint at line 64 (adcResult = adc_val;) it never gets there, so I assume that ADC_IrqHandler is never reached. Any ideas?

Thanks in advance!

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests