SAMD21 Counting random pulses

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

 

My apologies for a rather newbie question, but I am trying to convert an industrial sensor design using an end of life NEC / Renesas chip to something that's actually still made. For prototyping purposes I'm using an Arduino Zero board with the SAMD21G18A before designing my own hardware. My problem is that I cannot configure a working 16 bit counter that I can read periodically. The pulses are completely random so the current design just counts them over a specific time period (30 or 10 seconds) and then produces an average counts per minute. ARM based processors are still new to me so I'd appreciate any help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With SAM D counting pulses is very easy and rather nice. 

 

Steps for a very good pulse counting :- 

1.) Configure EIC on a pin and enable events for that particular pin.

2.) Configure timer and enable event inputs and set the event action as to increment the count register.

3.) Connect the EIC output event with the timer with event system.

 

This way whenever a pulse strikes, the count register is automatically incremented without any CPU intervention. I think you can even do this while you are in STANDBY sleep.

 

Personally if I am buying Atmel ARM M0+ parts, it will be for the event system and low power ofcourse.

 

This method may sound complex, but it is not. Try it out, let me know how it goes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually that does make sense, I appreciate the advice. I'll give it a shot and see what I can come up with.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to do the same thing: I have the following code but it does not work

 

 

void enableEIC(void)

{

if (EIC->CTRL.bit.ENABLE == 0)

{

  // Enable GCLK for IEC (External Interrupt Controller)

  GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_EIC));

 

  // Enable EIC

  EIC->CTRL.bit.ENABLE = 1;

  while (EIC->STATUS.bit.SYNCBUSY == 1) { }

}

}

 

 

void setupStepEvent(void)

{

//we will set up the EIC to generate an even on rising edge of step pin

//make sure EIC is setup

enableEIC();

 

 

// Assign step pin to EIC

// Step pin is PA11, EXTINT11

pinPeripheral(PIN_STEP_INPUT, PIO_EXTINT);

 

//***** setup EIC ******

EIC->EVCTRL.bit.EXTINTEO11=1; //enable event for EXTINT11

//setup up external interurpt 11 to be rising edge triggered

EIC->CONFIG[1].reg |= EIC_CONFIG_SENSE3(EIC_CONFIG_SENSE3_HIGH_Val);

 

//diable actually generating an interrupt, we only want event triggered

EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT11;

 

//**** setup the event system ***

// Enable GCLK for EVSYS channel 0

GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_EVSYS_CHANNEL_0));

while (GCLK->STATUS.bit.SYNCBUSY);

EVSYS->CHANNEL.reg=EVSYS_CHANNEL_CHANNEL(0)

| EVSYS_CHANNEL_EDGSEL_RISING_EDGE

| EVSYS_CHANNEL_EVGEN(EVSYS_ID_GEN_EIC_EXTINT_11)

| EVSYS_CHANNEL_PATH_ASYNCHRONOUS;

 

EVSYS->USER.reg = EVSYS_USER_CHANNEL(1)

| EVSYS_USER_USER(EVSYS_ID_USER_TC4_EVU);

 

//**** setup the Timer counter ******

// Enable GCLK for TC4 and TC5 (timer counter input clock)

GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5));

while (GCLK->STATUS.bit.SYNCBUSY);

 

TC4->COUNT32.CTRLA.reg = TC_CTRLA_SWRST;  //reset TC4

WAIT_TC32_REGS_SYNC(TC4)

 

TC4->COUNT32.CTRLA.reg = TC_CTRLA_MODE_COUNT32    // Set Timer counter Mode to 32 bits

| TC_CTRLA_WAVEGEN_MFRQ  //normal counting mode (not using waveforms)

| TC_CTRLA_PRESCALER_DIV1; //count each pulse

WAIT_TC32_REGS_SYNC(TC4)

 

TC4->COUNT32.CTRLBCLR.reg=0xFF; //clear all values.

WAIT_TC32_REGS_SYNC(TC4)

 

TC4->COUNT32.EVCTRL.reg=TC_EVCTRL_TCEI | TC_EVCTRL_EVACT_COUNT; //enable event input and count

WAIT_TC32_REGS_SYNC(TC4)

 

TC4->COUNT32.COUNT.reg=0;

WAIT_TC32_REGS_SYNC(TC4)

 

// Enable TC

TC4->COUNT32.CTRLA.reg |= TC_CTRLA_ENABLE;

WAIT_TC32_REGS_SYNC(TC4)

}