Code for DACC does not work

Moderator: nferre

Igor_Gl
Posts: 1
Joined: Fri Aug 07, 2015 11:07 am

Code for DACC does not work

Fri Aug 07, 2015 11:45 am

Hello. I am working with ATSAM4SD32C which was mounted at SAM4S-EK2 board. I wrote the code for work with DACC, wich make ramp on first channel of DAC. This is my code:

Code: Select all

#include "sam.h"

int dout = 300;

int main(void)
{	
	/* Initialize the SAM system */
    SystemInit();

	__enable_irq ();
	
	// --------------- Enable Interrupts in NVIC ----------------------------
	NVIC_EnableIRQ(DACC_IRQn);
	
	//------------------- Disable Watchdog Timer ---------------------------
	WDT -> WDT_MR = WDT_MR_WDDIS; // disable Watchdog Timer
	//----------------------------------------------------------------------

	//----------------- Switch to fast RC oscillator ----------------------
	// Enable Fast RC oscillator but DO NOT switch to RC now
	PMC-> CKGR_MOR |= (CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCRCEN);

	// Wait the Fast RC to stabilize
	while (!(PMC -> PMC_SR & PMC_SR_MOSCRCS)){};

	// Change Fast RC oscillator frequency
	PMC-> CKGR_MOR = (PMC-> CKGR_MOR & ~CKGR_MOR_MOSCRCF_Msk) | CKGR_MOR_KEY_PASSWD |  CKGR_MOR_MOSCRCF_4_MHz;

	// Wait the Fast RC to stabilize
	while (!(PMC-> PMC_SR & PMC_SR_MOSCRCS)){};

	// Switch to Fast RC
	PMC-> CKGR_MOR = (PMC -> CKGR_MOR & ~CKGR_MOR_MOSCSEL) | CKGR_MOR_KEY_PASSWD;
	//---------------------------------------------------------------------------------------------------------------

	//-------------------------- Switch to Main Clock ---------------------------------------------------------------
	// Switch to Main Clock
	PMC -> PMC_MCKR |= PMC_MCKR_CSS_MAIN_CLK;

	// Wait the Fast RC to stabilize
	while (!(PMC-> PMC_SR & PMC_SR_MOSCRCS)){};
	//---------------------------------------------------------------------------------------------------------------

	//-------------------------- PLLA enable ------------------------------------------------------------------------

	// Number Wait States Required to Access the Embedded Flash Memory (page 366 and 1183 datasheet)
	EFC0 -> EEFC_FMR = (EFC0 -> EEFC_FMR & ~EEFC_FMR_FWS_Msk) | EEFC_FMR_FWS(6);

	// Always stop PLL first!
	PMC-> CKGR_PLLAR = CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(0);

	// Adjustment of the Multiplier and Divider
	PMC-> CKGR_PLLAR = CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(24) | CKGR_PLLAR_DIVA(1) | CKGR_PLLAR_PLLACOUNT(100) | PMC_MCKR_PLLADIV2;

	// Wait the PLL Lock
	while (!(PMC-> PMC_SR & PMC_SR_LOCKA)){};

	// Switch to PLLA Clock
	PMC -> PMC_MCKR = (PMC -> PMC_MCKR & ~PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_PLLA_CLK;

	// Wait Master Clock is Ready
	while(!(PMC -> PMC_SR & PMC_SR_MCKRDY)){};
	
	// --------- Enables the corresponding peripheral clock	----------------
	// ------------------------ Enable PIOA --------------------------------
	PMC -> PMC_PCER0 = (1 << ID_PIOA);
	
	// ---------------------- Enable DACC clock ----------------------------
	PMC -> PMC_PCER0 = (1 << ID_DACC);
	PMC -> PMC_PCER0 = (1 << ID_ADC);

	// --------- Enable corresponding pins ---------------------------------
	PIOA -> PIO_PER = PIO_PER_P19 | PIO_PER_P20;
	
	// ---- Enable output corresponding pins in PORTA-----------------------
	PIOA -> PIO_OER = PIO_OER_P19|PIO_OER_P20;

	// --------------------- DACC Software Reset ---------------------------
	DACC -> DACC_CR = DACC_CR_SWRST;
	
	// ----------------------- DACC Mode Register --------------------------
	DACC -> DACC_MR = DACC_MR_WORD_HALF | DACC_MR_USER_SEL_CHANNEL1 | DACC_MR_REFRESH(1);

	// ----------------------- Channel 1 Enable ----------------------------
	DACC -> DACC_CHER = DACC_CHER_CH1;

	// ------------------- End of conversion interrupt ---------------------
	DACC -> DACC_IER = DACC_IER_TXRDY;

// --------------------------- This is my ramp!!!!!!!!!!!

   while (1) 
         {
	 if(dout == 4000)
	{dout = 0;}
	 else
	{dout = (dout + 100);}
         }
	return 0;
}

void DACC_Handler( void )
{ 
	volatile int IRQ_sense = 0;
	IRQ_sense = DACC -> DACC_ISR;
	DACC -> DACC_CDR = DACC_CDR_DATA(dout);
	return;
}
So, there is one problem... it does't work. But when I write these lines:

Code: Select all

if(dout == 4000)
	{dout = 0;}
	else
	{dout = (dout + 100);}
in "void DACC_Handler( void )" function, it works very well. I can't understand why does it happen?

P.S.
Forgive me my english :roll:

Return to “SAM9-based”

Who is online

Users browsing this forum: No registered users and 3 guests