Author Topic: Analog Comparatos AC Atmel Sam D21 - Bare Code  (Read 9004 times)

0 Members and 1 Guest are viewing this topic.

Offline zat

  • Newbie
  • Posts: 5
  • Country: tv
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #25 on: January 12, 2018, 03:43:52 am »
Can anyone give a minimum working project for AC settings bare metal (Atmel Studio 7)?
Tried a lot of options, nothing works. There may be a problem in initializing system timers,
I can not figure it out .. As soon as the signal is supplied to the comparator,
everything hangs. Clock speed 48 MHz
Please help.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #26 on: January 12, 2018, 03:49:11 am »
As soon as the signal is supplied to the comparator, everything hangs.
Hangs how exactly?

My best guess is you had enabled interrupts, but have not actually declared an interrupt handler.

I can provide an example, but only tomorrow.

But the cod from my earlier post is 100% working.
Alex
 

Offline zat

  • Newbie
  • Posts: 5
  • Country: tv
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #27 on: January 12, 2018, 05:08:33 am »
Thanks, ataradov.
3 days I try to start the comparator and it does not work.
The interrogation mode is continuous, I try to apply a voltage of 3.3 V to pin  PA04(for the test).
In interrupting (by raising), I increase the counter by 1,
and in the main cycle I light the LED for 200 ms.
Does not work.
« Last Edit: January 12, 2018, 05:12:29 am by zat »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #28 on: January 12, 2018, 05:11:35 am »
Can you post your code you have so far?
Alex
 

Offline zat

  • Newbie
  • Posts: 5
  • Country: tv
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #29 on: January 12, 2018, 05:30:38 am »

Code: [Select]
//----------------------------------------
void AC_Init(void) {
  PORT->Group[0].PMUX[4/2].reg |= PORT_PMUX_PMUXE_B ;
  PORT->Group[0].PINCFG[4].reg |=  PORT_PINCFG_PMUXEN | PORT_PINCFG_INEN ; 

/*Only for test */
PORT->Group[0].PMUX[12/2].reg |= PORT_PMUX_PMUXE_H ;
PORT->Group[0].PINCFG[12].reg |= PORT_PINCFG_PMUXEN ;

REG_AC_CTRLA = AC_CTRLA_SWRST ;
while( REG_AC_STATUSA & AC_CTRLA_SWRST) ;

REG_PM_APBCMASK |= PM_APBCMASK_AC ;

REG_GCLK_CLKCTRL = (
GCLK_CLKCTRL_ID_AC_DIG |
GCLK_CLKCTRL_GEN_GCLK0 | //48 MHz
GCLK_CLKCTRL_CLKEN
);
while( REG_GCLK_STATUS &  GCLK_STATUS_SYNCBUSY) ;

REG_GCLK_CLKCTRL = (
GCLK_CLKCTRL_ID_AC_ANA |
GCLK_CLKCTRL_GEN_GCLK0 | //48 MGz or need max 64 kHz ?
GCLK_CLKCTRL_CLKEN
);
while( REG_GCLK_STATUS &  GCLK_STATUS_SYNCBUSY) ;

REG_AC_CTRLA = (
AC_CTRLA_RUNSTDBY(1)
);

REG_AC_COMPCTRL0 |= (
(0 << AC_COMPCTRL_SINGLE_Pos) |
AC_COMPCTRL_HYST |
AC_COMPCTRL_SPEED_HIGH |
AC_COMPCTRL_INTSEL_RISING |
AC_COMPCTRL_MUXNEG_VSCALE |
AC_COMPCTRL_MUXPOS_PIN0 |
// AC_COMPCTRL_OUT_SYNC |
AC_COMPCTRL_OUT_OFF |
AC_COMPCTRL_FLEN(AC_COMPCTRL_FLEN_OFF_Val) |
AC_COMPCTRL_ENABLE
);
while( REG_AC_STATUSB & AC_STATUSB_SYNCBUSY) ;

REG_AC_SCALER0 = AC_SCALER_VALUE(32);   //~1,6 V
while( REG_AC_STATUSB & AC_STATUSB_SYNCBUSY) ;

REG_AC_INTENSET = AC_INTENSET_COMP0 ;
NVIC_EnableIRQ(AC_IRQn);

REG_AC_CTRLA |=  AC_CTRLA_ENABLE;
while (REG_AC_STATUSB & AC_STATUSB_SYNCBUSY);
}

//------------------------------------------------------
void AC_Handler (void)
{
ac_pulse_count +=1 ;
}

//---------------------------------------------------
int main(void)
{
__disable_irq();
SetupMainClock_48M();
AC_Init();
__enable_irq();

    while (1)
    {
        if (ac_pulse_count != 0) {
            REG_PORT_OUTCLR1 = LED0 ;   //on
            _delay_ms(200);
            REG_PORT_OUTSET1 = LED0 ;   //off
            ac_pulse_count = 0;
        }
    } // end while
} // end main

« Last Edit: January 12, 2018, 05:37:33 am by zat »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #30 on: January 12, 2018, 05:41:46 am »
You don't loop in the main(), what do you expect to happen?

EDIT: Well, now you do.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #31 on: January 12, 2018, 05:53:15 am »
I don't see anything obviously wrong with the code. I'll make a complete demo project tomorrow.
Alex
 
The following users thanked this post: zat

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #32 on: January 12, 2018, 09:20:09 pm »
Here is a complete project.

Your original code does not clear the interrupt flag, so it is constantly called. There may be something else wrong as well, I have not checked.

Also make sure to declare  ac_pulse_count volatile.
Alex
 
The following users thanked this post: dino7, zat

Offline zat

  • Newbie
  • Posts: 5
  • Country: tv
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #33 on: January 13, 2018, 01:30:22 am »
Your original code does not clear the interrupt flag, so it is constantly called. There may be something else wrong as well, I have not checked.
Thanks again, Alex.
Thanks to the provided project, I was able to find the problem.
Indeed, it's all about resetting the interrupt flag.
But why is it necessary for the comparator to do this?
I have SPI and UART, and there are no such problems.
They also use the interrupt, but it is not necessary to reset the flag.
Code: [Select]
void AC_Handler (void)
{
    REG_AC_INTFLAG = AC_INTFLAG_COMP0;
    ac_pulse_count +=1 ;
}
Quote
Also make sure to declare  ac_pulse_count volatile.
Yes, it's present.
Code: [Select]
/* AC */
uint16_t volatile ac_pulse_count = 0 ;
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #34 on: January 13, 2018, 01:40:08 am »
But why is it necessary for the comparator to do this?
It is necessary for all peripherals to know when you have actually handled the interrupt.

They also use the interrupt, but it is not necessary to reset the flag.
It is necessary, but there flags are reset by reading the data from the DATA register. If you exit from UART RX interrupt without reading the DATA register, you will get exact same result.

The exact way flags are cleared depends on the peripheral and described in the datasheet.

Also, a bit of debugging helps with finding bugs like this. Debuggers are extremely cheap nowadays.
Alex
 

Offline zat

  • Newbie
  • Posts: 5
  • Country: tv
Re: Analog Comparatos AC Atmel Sam D21 - Bare Code
« Reply #35 on: January 13, 2018, 02:45:39 am »
I checked with debugger , I saw that the interrupt was working. But for 3 days of comparing different results, I probably just did not pay attention. Most likely this has already affected from fatigue. |O
Sometimes need to rest  :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf