Arduion Zero (SAMD21G18) Timed Interrupt

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello All,

 

I'm trying to get a timed interrupt to work since i want to control 2 stepper motors. My first question would be if it is possible to define the functions inside a class so that the motor class itself can configure the timers and also handle the interrupts itself. I don't care if it is the way to do it but i'm just curious if its possible this way? 

 

I have the following code setup but the code stays stuck at the SYNCBUSY bits for the timer.

 

// Set The CPU To Run At Maximum = 48 MHz & Wait For Sync Status Idle
  PM->CPUSEL.reg = PM_CPUSEL_CPUDIV_DIV1;
  while (GCLK->STATUS.bit.SYNCBUSY) { };

  // Set The Global Clock: Improve Duty Cycle, Enable And Clock Selection
  GCLK->GENCTRL.reg |= GCLK_GENCTRL_IDC;
  GCLK->GENCTRL.reg |= GCLK_GENDIV_DIV(3);
  GCLK->GENCTRL.reg |= GCLK_GENCTRL_GENEN;
  GCLK->GENCTRL.reg |= GCLK_GENCTRL_SRC_DFLL48M;
  GCLK->GENCTRL.reg |= GCLK_GENCTRL_ID(4);
  while (GCLK->STATUS.bit.SYNCBUSY) { };

  // Feed Clock To Timer, Select Clock 4 & Enable Clock
  GCLK->CLKCTRL.reg |= GCLK_CLKCTRL_ID_TCC0_TCC1;
  GCLK->CLKCTRL.reg |= GCLK_CLKCTRL_GEN_GCLK4;
  GCLK->CLKCTRL.reg |= GCLK_CLKCTRL_CLKEN;
  while (GCLK->STATUS.bit.SYNCBUSY) { };

  // Configure The Timed Interrupt For MotorControl
  PM->APBCMASK.reg |= PM_APBCMASK_TCC1;
  TCC1->WAVE.reg = TCC_WAVE_WAVEGEN_NFRQ;
  TCC1->PER.reg = 100;
  while (TCC1->SYNCBUSY.bit.PER) { };
  TCC1->CC[0].reg = 5208;
  while (TCC1->SYNCBUSY.bit.CC0) { };
  TCC1->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1024;
  TCC1->CTRLA.reg |= TCC_CTRLA_PRESCSYNC_PRESC;
  TCC1->INTFLAG.reg |= TCC_INTFLAG_MC0 | TCC_INTFLAG_OVF;
  TCC1->CTRLA.reg |= TCC_CTRLA_ENABLE;
  TCC1->INTENSET.reg |= TCC_INTENSET_MC0 | TCC_INTENSET_OVF;
  while (TCC1->SYNCBUSY.bit.ENABLE) { };

 

I have no idea why it is stuck there, can someone help me with this? 

 

Also, i'm open to suggestions for a better approach on how to control 2 stepper motors. I also saw something about a PWM signal but since i need to control the stepper motor with 2 wires i need 1 PWM signal phase shifted to the other. does anyone here know how to do this? 

 

I'm curious how all this is possible and if you have a better suggestion on how to do it it would be greatly appreciated.

 

Thanks in advance! 

 

Kind regards,

 

 

Bob