Trying to use TimeB in single shot mode

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


Hi All,

I am trying to use TimerB of ATmega1608 in Single shot mode. The event to trigger this single shot mode comes from internal Analog comparator. I first initialized the Analog comparator and saw its output on pin 1 of MCU and it works as expected. Now, this output from anlaog comparator technically should trigger timerB in shingle shot mode, but its not doing right now. I am attaching my timer B snippet, please can someone tell me, is it configured correctly? If needed I can also attach my Event system configuration and Analog comparator configuration.

This topic has a solution.
Last Edited: Mon. Aug 8, 2022 - 08:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to configure the event system to route the AC output to TCB. Try this:

 

	EVSYS.CHANNEL0 = EVSYS_GENERATOR_AC0_OUT_gc;
	EVSYS.USERTCB1 = EVSYS_CHANNEL_CHANNEL0_gc;

 

One more thing, when you are writing CTRLA to enable the timer, you are overwriting the prescaler you wrote earlier.

Last Edited: Wed. Aug 3, 2022 - 08:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


balisong42 wrote:

You need to configure the event system to route the AC output to TCB. Try this:

 

	EVSYS.CHANNEL0 = EVSYS_GENERATOR_AC0_OUT_gc;
	EVSYS.USERTCB1 = EVSYS_CHANNEL_CHANNEL0_gc;

 

Yes, I did that exactly, but still does not work i.e. does not give single shot output from TimerB.

For your reference

 

 

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

Have you configured the pulse output pin as an output so you can see it (I'm assuming you need to do so, perhaps not).  Make sure you are looking at the proper pin for this output (easy to get confused).

 

Also a one-time pulse is very hard to spot until your scope is properly adjusted....so at first pump out a rapid steady stream of these pulses so you can get you setup monitor ready.  Then you can do one pulse a minute.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Have you configured the pulse output pin as an output so you can see it (I'm assuming you need to do so, perhaps not).  Make sure you are looking at the proper pin for this output (easy to get confused).

 

Also a one-time pulse is very hard to spot until your scope is properly adjusted....so at first pump out a rapid steady stream of these pulses so you can get you setup monitor ready.  Then you can do one pulse a minute.

Yes I configured PA3 pin as o/p. If technically, my single shot works for first event it receives, it should work other similar events, right?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I have tested a minimal code on ATtiny402 and it runs smoothly.

 

/*----------------------------------------------------------------------------------------
 The positive edge of the AC output triggers the TCB and a 100ms pulse will be generated.
 MCU: ATtiny402
 PA7: input (voltage from potentiometer)
 PA6: output (LED)
-------------------------------------------------------------------------------------------*/
#include <avr/io.h>

int main(void)
{
  _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm);	// Main Clock 1.25 MHz

  PORTA.PIN7CTRL = 0x04;                                // disable digital input buffer
  PORTA.DIRSET = PIN6_bm;                               // PA6 as output

  VREF.CTRLA = VREF_DAC0REFSEL_1V1_gc;			// voltage reference 1.1V

  AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc | AC_MUXNEG_VREF_gc; // AINP0 to IN+, 1.1V to IN-
  AC0.CTRLA = AC_HYSMODE_50mV_gc | AC_ENABLE_bm;        // select hysteresis, enable AC

  EVSYS.ASYNCCH0 = EVSYS_ASYNCCH0_AC0_OUT_gc;           // Asynchronous Channel 0, AC OUT as event generator
  EVSYS.ASYNCUSER0 = EVSYS_ASYNCUSER0_ASYNCCH0_gc;      // TCB as event user, Asynchronous Channel 0

  TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc;                   // TCB clock source CLK_PER/DIV2 (625 kHz)
  TCB0.CCMP = 0xF424;                                   // TCB TOP value for a 100ms output pulse
  TCB0.EVCTRL = TCB_CAPTEI_bm;                          // enable Capture Event Input
  TCB0.CTRLB = TCB_CCMPEN_bm | TCB_CNTMODE_SINGLE_gc;   // enable TCB WO Output (PA6), select Single-Shot mode
  TCB0.CTRLA |= TCB_ENABLE_bm;                          // enable TCB

  while(1)
  {

  }
}

 

The truth is more important than the facts.

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

poftamunk wrote:

I have tested a minimal code on ATtiny402 and it runs smoothly.

 

/*----------------------------------------------------------------------------------------
 The positive edge of the AC output triggers the TCB and a 100ms pulse will be generated.
 MCU: ATtiny402
 PA7: input (voltage from potentiometer)
 PA6: output (LED)
-------------------------------------------------------------------------------------------*/
#include <avr/io.h>

int main(void)
{
  _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm);	// Main Clock 1.25 MHz

  PORTA.PIN7CTRL = 0x04;                                // disable digital input buffer
  PORTA.DIRSET = PIN6_bm;                               // PA6 as output

  VREF.CTRLA = VREF_DAC0REFSEL_1V1_gc;			// voltage reference 1.1V

  AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc | AC_MUXNEG_VREF_gc; // AINP0 to IN+, 1.1V to IN-
  AC0.CTRLA = AC_HYSMODE_50mV_gc | AC_ENABLE_bm;        // select hysteresis, enable AC

  EVSYS.ASYNCCH0 = EVSYS_ASYNCCH0_AC0_OUT_gc;           // Asynchronous Channel 0, AC OUT as event generator
  EVSYS.ASYNCUSER0 = EVSYS_ASYNCUSER0_ASYNCCH0_gc;      // TCB as event user, Asynchronous Channel 0

  TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc;                   // TCB clock source CLK_PER/DIV2 (625 kHz)
  TCB0.CCMP = 0xF424;                                   // TCB TOP value for a 100ms output pulse
  TCB0.EVCTRL = TCB_CAPTEI_bm;                          // enable Capture Event Input
  TCB0.CTRLB = TCB_CCMPEN_bm | TCB_CNTMODE_SINGLE_gc;   // enable TCB WO Output (PA6), select Single-Shot mode
  TCB0.CTRLA |= TCB_ENABLE_bm;                          // enable TCB

  while(1)
  {

  }
}

 

I was not ORing the CTRLA register with the enable bit and Frequency prescalar. Thank you.