This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SAM D21 Clock System

Hello again!

I have doubts about how to modify the working frequency of the SAMD21 microcontroller, I made this poor program attempt:

#include <stdint.h>
#include <stdbool.h>
#include "samd21.h"                     // Device header
#include "GPIO.h"

//Definiciones
#define LED_BUILTIN 8
#define TEMP1MSEG 8000

//FUNCIONES GLOBALES
void SystemBegin(void);
void Delay_ms(int x);
void DelaySystick(void);

int main(void)
{
    //Inicializa el sistema SAMD21
    SystemBegin();
    
    //Configuramos el Pin PB08 como salida
    GPIOPinMode(GPIO_B,LED_BUILTIN,OUTPUT_TOGGLE);
    /* PROGRAMA PRINCIPAL */
    while(true)
    {
        GPIOPinToggle(GPIO_B,LED_BUILTIN);
        Delay_ms(500);
    }
}

//FUNCIONES QUE MODFICAN EL SISTEMA Y EL LOS RETARDOS
void SystemBegin(void)
{
  //Inicia el sistema
    SystemInit();
    
    SYSCTRL->OSC8M.bit.PRESC=0x00;           //no hay preescalador (es 8 en el reajuste)
    SYSCTRL->OSC8M.reg=SYSCTRL_OSC8M_ENABLE; //Habilita la fuente
    GCLK->GENDIV.bit.ID=0x00;                //Seleccionamos el Generador 0
    GCLK->GENDIV.bit.DIV=0;                  //Sin preescaldor
    GCLK->GENCTRL.bit.ID=0x00;               //Seleccionamos el Generador 0
    GCLK->GENCTRL.reg=GCLK_GENCTRL_SRC_OSC8M;//Seleccionamos como fuente de oscilador interno la de 8MHz
    GCLK->GENCTRL.bit.GENEN=1;               //Habilitamos el generador
}    

void Delay_ms(int x)
{
    int i;
    
    for(i=0;i<x;i++)
    {
        DelaySystick();
    }
}

void DelaySystick(void)
{
    SysTick->CTRL=0x00;
    SysTick->LOAD=TEMP1MSEG;
    SysTick->CTRL=0x05;
    while( (SysTick->CTRL & (1<<16)) == 0);
    
    SysTick->CTRL=0x00;
}

But I think what caused the microcontroller to lock up or something, was that it changed in the Xtal(MHz) option to 1MHz

My question is whether modifying the frequency within the program does not affect what I put into the Xtal(MHz) function?