Creating Clock on Adafruit Grand Central M4 Express

Hi I am trying to get a 12 Mhz clock out of the Board in the title. I am trying to follow this post but I am not getting anything from my program. Here is my code, I am a bit lost so perhaps you guys can help me see what I am doing wrong. I am using pin 34 because the PA19 thing they use in their example is mapped to 34 on grand central (I think ) but otherwise I have no preference on pin.

void setup()
{
    pinMode(35, OUTPUT);
    // Activate timer TC3
    // CLK_TC3_APB
    MCLK->APBBMASK.reg |= MCLK_APBBMASK_TC3;

    // Set up the generic clock (GCLK7)
    GCLK->GENCTRL[7].reg =
        // Divide the 48MHz clock source by divisor 1: 48MHz/1 = 48MHz
        GCLK_GENCTRL_DIV(4) |
        // Set the duty cycle to 50/50 HIGH/LOW
        GCLK_GENCTRL_IDC |
        // Enable GCLK7
        GCLK_GENCTRL_GENEN |
        // Select 48MHz DFLL clock source
        GCLK_GENCTRL_SRC_DFLL;
        // Select 100MHz DPLL clock source
        //GCLK_GENCTRL_SRC_DPLL1;
        // Select 120MHz DPLL clock source
        // GCLK_GENCTRL_SRC_DPLL0;
    // Wait for synchronization
    while (GCLK->SYNCBUSY.bit.GENCTRL7);

    // for PCHCTRL numbers have a look at Table 14-9. PCHCTRLm Mapping page168ff
    // http://ww1.microchip.com/downloads/en/DeviceDoc/60001507C.pdf#page=169&zoom=page-width,-8,696
    GCLK->PCHCTRL[26].reg =
        // Enable the TC3 peripheral channel
        GCLK_PCHCTRL_CHEN |
        // Connect generic clock 7 to TC3
        GCLK_PCHCTRL_GEN_GCLK7;

    // Enable the peripheral multiplexer on pin D9
    PORT->Group[g_APinDescription[34].ulPort].
        PINCFG[g_APinDescription[34].ulPin].bit.PMUXEN = 1;

    // Set the D9 (PORT_PA19) peripheral multiplexer to
    // peripheral (odd port number) E(6): TC3, Channel 1
    // check if you need even or odd PMUX!!!
    // http://forum.arduino.cc/index.php?topic=589655.msg4064311#msg4064311
    PORT->Group[g_APinDescription[34].ulPort].
        PMUX[g_APinDescription[34].ulPin >> 1].reg |= PORT_PMUX_PMUXE(4);

    TC3->COUNT8.CTRLA.reg =
        // Set prescaler to 1, 48MHz/1 = 48MHz
        // Set prescaler to 1, 120MHz/1 = 120MHz
        // TC_CTRLA_PRESCALER_DIV1 |
        // Set prescaler to 8, 48MHz/8 = 6MHz <-- Not This FOUR 48/FOUR = 12Mhz
        TC_CTRLA_PRESCALER_DIV4 |
        // Set the reset/reload to trigger on prescaler clock
        TC_CTRLA_PRESCSYNC_PRESC;

    // Set-up TC3 timer for Normal Frequency Generation (NFRQ)
    // TC3->COUNT8.WAVE.reg = TC_WAVE_WAVEGEN_NFRQ;
    // Set-up TC3 timer for Match Frequency Generation (MFRQ)
    TC3->COUNT8.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
    // Wait for synchronization
    // while (TC3->COUNT8.SYNCBUSY.bit.WAVE)

    // Set-up the PER (period) register 50Hz PWM
    TC3->COUNT8.PER.reg = 000;
    // Wait for synchronization
    while (TC3->COUNT8.SYNCBUSY.bit.PER);

    // // Set-up the CC (counter compare), channel 1 register for 50% duty-cycle
    TC3->COUNT8.CC[1].reg = 0;
    // // Wait for synchronization
    while (TC3->COUNT8.SYNCBUSY.bit.CC1);

    // Enable timer TC3
    TC3->COUNT8.CTRLA.bit.ENABLE = 1;
    // Wait for synchronization
    while (TC3->COUNT8.SYNCBUSY.bit.ENABLE);
}

void loop() {
  
}```
// Adafruit Metro M4 Only: Set-up digital pin D7 to output 50Hz, single slope PWM with a 50% duty cycle
void setup() 
{
  pinMode(34, OUTPUT);
  // Set up the generic clock (GCLK7) to clock timer TCC0 
  GCLK->GENCTRL[7].reg = GCLK_GENCTRL_DIV(1) |       // Divide the 48MHz clock source by divisor 1: 48MHz/1 = 48MHz
                         GCLK_GENCTRL_IDC |          // Set the duty cycle to 50/50 HIGH/LOW
                         GCLK_GENCTRL_GENEN |        // Enable GCLK7
                         //GCLK_GENCTRL_SRC_DFLL;      // Select 48MHz DFLL clock source
                         //GCLK_GENCTRL_SRC_DPLL1;     // Select 100MHz DPLL clock source
                         GCLK_GENCTRL_SRC_DPLL0;     // Select 120MHz DPLL clock source
  while (GCLK->SYNCBUSY.bit.GENCTRL7);               // Wait for synchronization  

  GCLK->PCHCTRL[25].reg = GCLK_PCHCTRL_CHEN |        // Enable the TCC0 peripheral channel
                          GCLK_PCHCTRL_GEN_GCLK7;    // Connect generic clock 7 to TCC0

  // Enable the peripheral multiplexer on pin D7
  PORT->Group[g_APinDescription[18].ulPort].PINCFG[g_APinDescription[18].ulPin].bit.PMUXEN = 1;
  
  // Set the D7 (PORT_PB12) peripheral multiplexer to peripheral (even port number) E(6): TCC0, Channel 0
  PORT->Group[g_APinDescription[18].ulPort].PMUX[g_APinDescription[18].ulPin >> 1].reg |= PORT_PMUX_PMUXE(6);
  
  TCC0->CTRLA.reg = TC_CTRLA_PRESCALER_DIV2 |        // Set prescaler to 8, 48MHz/8 = 6MHz 120Mhz/8 = 15Mhz
                    TC_CTRLA_PRESCSYNC_PRESC;        // Set the reset/reload to trigger on prescaler clock                 

  TCC0->WAVE.reg = TC_WAVE_WAVEGEN_NPWM;             // Set-up TCC0 timer for Normal (single slope) PWM mode (NPWM)
  while (TCC0->SYNCBUSY.bit.WAVE)                    // Wait for synchronization

  TCC0->PER.reg = 40;                            // Set-up the PER (period) register 50Hz PWM
  while (TCC0->SYNCBUSY.bit.PER);                    // Wait for synchronization
  
  TCC0->CC[0].reg = 20;                           // Set-up the CC (counter compare), channel 0 register for 50% duty-cycle
  while (TCC0->SYNCBUSY.bit.CC0);                    // Wait for synchronization

  TCC0->CTRLA.bit.ENABLE = 1;                        // Enable timer TCC0
  while (TCC0->SYNCBUSY.bit.ENABLE);                 // Wait for synchronization
}

void loop() {
    //digitalWrite(34,HIGH);
  }

This is where I am now , however the clock is not going all the way to 0

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.