Pages

Sunday, August 7, 2016

Programming Arduino Zero, MKR1000, or any SAMD21 Arduino via Registers

In this video we look at how to program registers on Arduino's based on the SAMD21 MCU, like the Zero and MKR1000. To do this we will leverage the Atmel ASF data structures. This is a great way to access capabilities in the SAMD21 that are not covered by Arduino libraries. The link to the ASF documentation is http://asf.atmel.com/docs/3.16.0/samd21/html/annotated.html


Arduino code from video:
bool tog = false; //variable to toggle digital output
int cDiv = 1; //varible to hold clock divider
int count = 0; //variable to track when to switch clock freq

void setup() {
  PM->CPUSEL.reg = PM_CPUSEL_CPUDIV(1); //Sets CPU frequency: power management struct, CPUSEL union, register variable
  pinMode(3, OUTPUT); //set D3 to output
}

void loop() {
  if(tog) tog = false; //if tog is true turn it false
  else tog =true;
  digitalWrite(3,tog); //set digital output
  count++;  
  if(count > 6) { //check if it is time to change clock frequency
    if(cDiv == 1) cDiv = 4;
    else cDiv = 1;
    PM->CPUSEL.reg = PM_CPUSEL_CPUDIV(cDiv); //PM_CPUSEL_CPUDIV_DIV4_Val
    count = 0;
  }
}

No comments:

Post a Comment