Skip to main content
Copy edited (e.g. ref. <https://www.arduino.cc/en/Main/ArduinoBoardMicro>).
Source Link

Arduino Uno vs Arduino Micro Timers ->timers → pins

Where todo I find the pins relative to timers of each individual Arduino Microcontroller.microcontroller?

As iI like Arduino's iI already have various samples of them. The one iI mostly use is the unoUno and microMicro for now.. i'm. I'm planning to get also get a megaMega and others. Most simple things work on all devices, but if you start with complex code to fasten up things, every device is a little different. A problem iI could not solve for days now is the relation between pins and timers. I

I cannot find any information about the arduino microArduino Micro. I also don't know exactly what the proper keywords are that iI need to find the relation between timers and pins.

Example: A PWM Driverdriver multiplexing

As iI for now just got some stuff from my local dealer iI have no access to shifregistersshift registers, pwmPWM drivers and other proper integrated circuits.

So iI used the arduinoArduino as a pwmPWM driver for 8 rgbRGB or 24  (3*8) white ledsLEDs. The code works on Arduino Uno. But as the Arduino Micro has no Timer2, TCCR2B i, I cannot use the same code on the arduino microArduino Micro.

Even if iI understand the code (wrote alot ofI wrote a lot in other languages...), some of the Atmel chip specific parameters are not clear.

This function sets the prescaler of the timer.:

void setPwmFrequency(int pin, int divisor){ 

  byte mode;
  if(pin==5||pin==6||pin==9||pin==10){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 64:mode=0x03;break;
      case 256:mode=0x04;break;
      case 1024:mode=0x05;break;
      default:return;
    }
    if(pin==5||pin==6){
      TCCR0B=TCCR0B&0b11111000|mode;
    } else{
      TCCR1B=TCCR1B&0b11111000|mode;
    }
  }else if(pin==3||pin==11){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 32:mode=0x03;break;
      case 64:mode=0x04;break;
      case 128:mode=0x05;break;
      case 256:mode=0x06;break;
      case 1024:mode=0x7;break;
      default:return;
    }
    TCCR2B=TCCR2B&0b11111000|mode;
  }
}

First of all i, I probably don't even need this function as soon iI know what frequency iI want, and so iI probably need only 2two lines to set my 3three (RGB) pins.

//Arduino Uno timer setup for pin 9, 10, and 11
TCCR1B=TCCR1B&0b11111000|0x01; //pin Pin 9,10
TCCR2B=TCCR2B&0b11111000|0x01; //pin Pin 11
  1. Correct, iI need to set TCCR1B only one time?

  2. What dodoes the 0b11110000b11111000 refer to?

TIMSK2=1<<TOIE2;TIMSK2 = 1<<TOIE2;
  1. whatWhat does this do?

While the above 3three questions are not so important until it works, iI need to understand how iI can convert those timers to work with Arduino Micro, but also later with other Arduino devicesboards.

PinDo pin 9,10 10,12 and 12 have the same timer  ?

Also if iI understand correctly the Arduino microMicro has:

  1. aA higher PWM range 1024 vs 255 (UNO)??
  2. aA much higher frequency aviableavailable in on timer4Timer4 only for pin 13

Full working (Onon Arduino UNOUno) code.

unsigned char Prescaler=0;
unsigned char CurrentLED=0;
unsigned char LEDValues[8][3];
unsigned char ports[8]={
  0b00000100,
  0b00001000,
  0b00010000,
  0b00100000,
  0b01000000,
  0b10000000,
  0b00000001,
  0b00010000
};//PINS 2,3,4,5,6,7,8,12 

#define PrescalerOverflowValue 4 

ISR(TIMER2_OVF_vect){
  if(Prescaler<PrescalerOverflowValue){
    Prescaler++;
  }else {
    Prescaler=0;
    Multiplex();
  }
} 

void Multiplex(void){    
  PORTD&=0b00000011;
  PORTB&=0b11101110;
  analogWrite(9,255-LEDValues[CurrentLED][0]);  
  analogWrite(10,255-LEDValues[CurrentLED][1]);
  analogWrite(11,255-LEDValues[CurrentLED][2]);
  CurrentLED<6?(PORTD|=ports[CurrentLED]):(PORTB|=ports[CurrentLED]);
  CurrentLED++;
  if(CurrentLED>7)CurrentLED=0;  
}
void setPwmFrequency(int pin, int divisor){
  byte mode;
  if(pin==5||pin==6||pin==9||pin==10){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 64:mode=0x03;break;
      case 256:mode=0x04;break;
      case 1024:mode=0x05;break;
      default:return;
    }
    if(pin==5||pin==6){
      TCCR0B=TCCR0B&0b11111000|mode;
    }else{
      TCCR1B=TCCR1B&0b11111000|mode;
    }
  }else if(pin==3||pin==11){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 32:mode=0x03;break;
      case 64:mode=0x04;break;
      case 128:mode=0x05;break;
      case 256:mode=0x06;break;
      case 1024:mode=0x7;break;
      default:return;
    }
    TCCR2B=TCCR2B&0b11111000|mode;
  }
} 

void setup(void){ 

  pinMode(2,OUTPUT);//1
  pinMode(3,OUTPUT);//2
  pinMode(4,OUTPUT);//3
  pinMode(5,OUTPUT);//4
  pinMode(6,OUTPUT);//5
  pinMode(7,OUTPUT);//6
  pinMode(8,OUTPUT);//7
  pinMode(12,OUTPUT);//8
  pinMode(9,OUTPUT);//red
  pinMode(10,OUTPUT);//green
  pinMode(11,OUTPUT);//blue
  setPwmFrequency(9,8);
  setPwmFrequency(10,8);    
  setPwmFrequency(11,8);
  TIMSK2=1<<TOIE2;
} 

void loop(void){
  for(int i=0;i<8;i++){
    //LEDValues[i][0]; //red Red
    //LEDValues[i][1]; //green Green
    //LEDValues[i][2]; //blue Blue
  }
}

Arduino Uno vs Arduino Micro Timers -> pins

Where to find the pins relative to timers of each individual Arduino Microcontroller.

As i like Arduino's i already have various of them. The one i mostly use is the uno and micro for now.. i'm planning to get also a mega and others. Most simple things work on all devices but if you start with complex code to fasten up things every device is a little different. A problem i could not solve for days now is the relation between pins and timers. I cannot find any information about the arduino micro. I also don't know exactly what the proper keywords are that i need to find the relation between timers and pins.

Example: A PWM Driver multiplexing

As i for now just got some stuff from my local dealer i have no access to shifregisters, pwm drivers and other proper integrated circuits.

So i used the arduino as a pwm driver for 8 rgb or 24(3*8) white leds. The code works on Arduino Uno. But as the Arduino Micro has no Timer2 TCCR2B i cannot use the same code on the arduino micro.

Even if i understand the code (wrote alot of other languages...), some of the Atmel chip specific parameters are not clear.

This function sets the prescaler of the timer.

void setPwmFrequency(int pin, int divisor){
 byte mode;
 if(pin==5||pin==6||pin==9||pin==10){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 64:mode=0x03;break;
   case 256:mode=0x04;break;
   case 1024:mode=0x05;break;
   default:return;
  }
  if(pin==5||pin==6){
   TCCR0B=TCCR0B&0b11111000|mode;
  }else{
   TCCR1B=TCCR1B&0b11111000|mode;
  }
 }else if(pin==3||pin==11){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 32:mode=0x03;break;
   case 64:mode=0x04;break;
   case 128:mode=0x05;break;
   case 256:mode=0x06;break;
   case 1024:mode=0x7;break;
   default:return;
  }
  TCCR2B=TCCR2B&0b11111000|mode;
 }
}

First of all i probably don't even need this function as soon i know what frequency i want and so i probably need only 2 lines to set my 3(RGB) pins.

//Arduino Uno timer setup for pin 9,10,11
TCCR1B=TCCR1B&0b11111000|0x01; //pin 9,10
TCCR2B=TCCR2B&0b11111000|0x01; //pin 11
  1. Correct, i need to set TCCR1B only one time?

  2. What do the 0b1111000 refer to?

TIMSK2=1<<TOIE2;
  1. what does this ?

While the above 3 questions are not so important until it works, i need to understand how i can convert those timers to work with Arduino Micro, but also later with other Arduino devices.

Pin 9,10,12 have the same timer  ?

Also if i understand correctly the Arduino micro has:

  1. a higher PWM range 1024 vs 255 (UNO)??
  2. a much higher frequency aviable in on timer4 only for pin 13

Full working (On Arduino UNO) code.

unsigned char Prescaler=0;
unsigned char CurrentLED=0;
unsigned char LEDValues[8][3];
unsigned char ports[8]={
 0b00000100,
 0b00001000,
 0b00010000,
 0b00100000,
 0b01000000,
 0b10000000,
 0b00000001,
 0b00010000
};//PINS 2,3,4,5,6,7,8,12
#define PrescalerOverflowValue 4
ISR(TIMER2_OVF_vect){
 if(Prescaler<PrescalerOverflowValue){
  Prescaler++;
 }else {
  Prescaler=0;
  Multiplex();
 }
}
void Multiplex(void){    
 PORTD&=0b00000011;
 PORTB&=0b11101110;
 analogWrite(9,255-LEDValues[CurrentLED][0]);  
 analogWrite(10,255-LEDValues[CurrentLED][1]);
 analogWrite(11,255-LEDValues[CurrentLED][2]);
 CurrentLED<6?(PORTD|=ports[CurrentLED]):(PORTB|=ports[CurrentLED]);
 CurrentLED++;
 if(CurrentLED>7)CurrentLED=0;  
}
void setPwmFrequency(int pin, int divisor){
 byte mode;
 if(pin==5||pin==6||pin==9||pin==10){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 64:mode=0x03;break;
   case 256:mode=0x04;break;
   case 1024:mode=0x05;break;
   default:return;
  }
  if(pin==5||pin==6){
   TCCR0B=TCCR0B&0b11111000|mode;
  }else{
   TCCR1B=TCCR1B&0b11111000|mode;
  }
 }else if(pin==3||pin==11){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 32:mode=0x03;break;
   case 64:mode=0x04;break;
   case 128:mode=0x05;break;
   case 256:mode=0x06;break;
   case 1024:mode=0x7;break;
   default:return;
  }
  TCCR2B=TCCR2B&0b11111000|mode;
 }
}
void setup(void){
 pinMode(2,OUTPUT);//1
 pinMode(3,OUTPUT);//2
 pinMode(4,OUTPUT);//3
 pinMode(5,OUTPUT);//4
 pinMode(6,OUTPUT);//5
 pinMode(7,OUTPUT);//6
 pinMode(8,OUTPUT);//7
 pinMode(12,OUTPUT);//8
 pinMode(9,OUTPUT);//red
 pinMode(10,OUTPUT);//green
 pinMode(11,OUTPUT);//blue
 setPwmFrequency(9,8);
 setPwmFrequency(10,8);    
 setPwmFrequency(11,8);
 TIMSK2=1<<TOIE2;
}
void loop(void){
 for(int i=0;i<8;i++){
  //LEDValues[i][0];//red
  //LEDValues[i][1];//green
  //LEDValues[i][2];//blue
 }
}

Arduino Uno vs Arduino Micro timers → pins

Where do I find the pins relative to timers of each individual Arduino microcontroller?

As I like Arduino's I already have various samples of them. The one I mostly use is the Uno and Micro for now... I'm planning to also get a Mega and others. Most simple things work on all devices, but if you start with complex code to fasten up things, every device is a little different. A problem I could not solve for days now is the relation between pins and timers.

I cannot find any information about the Arduino Micro. I also don't know exactly what the proper keywords are that I need to find the relation between timers and pins.

Example: A PWM driver multiplexing

As I for now just got some stuff from my local dealer I have no access to shift registers, PWM drivers and other proper integrated circuits.

So I used the Arduino as a PWM driver for 8 RGB or 24  (3*8) white LEDs. The code works on Arduino Uno. But as the Arduino Micro has no Timer2, TCCR2B, I cannot use the same code on the Arduino Micro.

Even if I understand the code (I wrote a lot in other languages...), some of the Atmel chip specific parameters are not clear.

This function sets the prescaler of the timer:

void setPwmFrequency(int pin, int divisor){ 

  byte mode;
  if(pin==5||pin==6||pin==9||pin==10){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 64:mode=0x03;break;
      case 256:mode=0x04;break;
      case 1024:mode=0x05;break;
      default:return;
    }
    if(pin==5||pin==6){
      TCCR0B=TCCR0B&0b11111000|mode;
    } else{
      TCCR1B=TCCR1B&0b11111000|mode;
    }
  }else if(pin==3||pin==11){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 32:mode=0x03;break;
      case 64:mode=0x04;break;
      case 128:mode=0x05;break;
      case 256:mode=0x06;break;
      case 1024:mode=0x7;break;
      default:return;
    }
    TCCR2B=TCCR2B&0b11111000|mode;
  }
}

First of all, I probably don't even need this function as soon I know what frequency I want, and so I probably need only two lines to set my three (RGB) pins.

//Arduino Uno timer setup for pin 9, 10, and 11
TCCR1B=TCCR1B&0b11111000|0x01; // Pin 9,10
TCCR2B=TCCR2B&0b11111000|0x01; // Pin 11
  1. Correct, I need to set TCCR1B only one time?

  2. What does the 0b11111000 refer to?

TIMSK2 = 1<<TOIE2;
  1. What does this do?

While the above three questions are not so important until it works, I need to understand how I can convert those timers to work with Arduino Micro, but also later with other Arduino boards.

Do pin 9, 10, and 12 have the same timer?

Also if I understand correctly the Arduino Micro has:

  1. A higher PWM range 1024 vs 255 (UNO)??
  2. A much higher frequency available in on Timer4 only for pin 13

Full working (on Arduino Uno) code.

unsigned char Prescaler=0;
unsigned char CurrentLED=0;
unsigned char LEDValues[8][3];
unsigned char ports[8]={
  0b00000100,
  0b00001000,
  0b00010000,
  0b00100000,
  0b01000000,
  0b10000000,
  0b00000001,
  0b00010000
};//PINS 2,3,4,5,6,7,8,12 

#define PrescalerOverflowValue 4 

ISR(TIMER2_OVF_vect){
  if(Prescaler<PrescalerOverflowValue){
    Prescaler++;
  }else {
    Prescaler=0;
    Multiplex();
  }
} 

void Multiplex(void){
  PORTD&=0b00000011;
  PORTB&=0b11101110;
  analogWrite(9,255-LEDValues[CurrentLED][0]);
  analogWrite(10,255-LEDValues[CurrentLED][1]);
  analogWrite(11,255-LEDValues[CurrentLED][2]);
  CurrentLED<6?(PORTD|=ports[CurrentLED]):(PORTB|=ports[CurrentLED]);
  CurrentLED++;
  if(CurrentLED>7)CurrentLED=0;
}
void setPwmFrequency(int pin, int divisor){
  byte mode;
  if(pin==5||pin==6||pin==9||pin==10){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 64:mode=0x03;break;
      case 256:mode=0x04;break;
      case 1024:mode=0x05;break;
      default:return;
    }
    if(pin==5||pin==6){
      TCCR0B=TCCR0B&0b11111000|mode;
    }else{
      TCCR1B=TCCR1B&0b11111000|mode;
    }
  }else if(pin==3||pin==11){
    switch(divisor){
      case 1:mode=0x01;break;
      case 8:mode=0x02;break;
      case 32:mode=0x03;break;
      case 64:mode=0x04;break;
      case 128:mode=0x05;break;
      case 256:mode=0x06;break;
      case 1024:mode=0x7;break;
      default:return;
    }
    TCCR2B=TCCR2B&0b11111000|mode;
  }
} 

void setup(void){ 

  pinMode(2,OUTPUT);//1
  pinMode(3,OUTPUT);//2
  pinMode(4,OUTPUT);//3
  pinMode(5,OUTPUT);//4
  pinMode(6,OUTPUT);//5
  pinMode(7,OUTPUT);//6
  pinMode(8,OUTPUT);//7
  pinMode(12,OUTPUT);//8
  pinMode(9,OUTPUT);//red
  pinMode(10,OUTPUT);//green
  pinMode(11,OUTPUT);//blue
  setPwmFrequency(9,8);
  setPwmFrequency(10,8);
  setPwmFrequency(11,8);
  TIMSK2=1<<TOIE2;
} 

void loop(void){
  for(int i=0;i<8;i++){
    //LEDValues[i][0]; // Red
    //LEDValues[i][1]; // Green
    //LEDValues[i][2]; // Blue
  }
}
edited tags
Source Link
cocco
  • 431
  • 5
  • 7
  • 16

So is there somewhere a table describing the differences between all Arduino timers?

Full working (On Arduino UNO) code.

Full working (On Arduino UNO) code.

So is there somewhere a table describing the differences between all Arduino timers?

Full working (On Arduino UNO) code.

Source Link
cocco
  • 431
  • 5
  • 7
  • 16

Arduino Uno vs Arduino Micro Timers -> pins

Where to find the pins relative to timers of each individual Arduino Microcontroller.

As i like Arduino's i already have various of them. The one i mostly use is the uno and micro for now.. i'm planning to get also a mega and others. Most simple things work on all devices but if you start with complex code to fasten up things every device is a little different. A problem i could not solve for days now is the relation between pins and timers. I cannot find any information about the arduino micro. I also don't know exactly what the proper keywords are that i need to find the relation between timers and pins.

Example: A PWM Driver multiplexing

As i for now just got some stuff from my local dealer i have no access to shifregisters, pwm drivers and other proper integrated circuits.

So i used the arduino as a pwm driver for 8 rgb or 24(3*8) white leds. The code works on Arduino Uno. But as the Arduino Micro has no Timer2 TCCR2B i cannot use the same code on the arduino micro.

Even if i understand the code (wrote alot of other languages...), some of the Atmel chip specific parameters are not clear.

This function sets the prescaler of the timer.

void setPwmFrequency(int pin, int divisor){
 byte mode;
 if(pin==5||pin==6||pin==9||pin==10){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 64:mode=0x03;break;
   case 256:mode=0x04;break;
   case 1024:mode=0x05;break;
   default:return;
  }
  if(pin==5||pin==6){
   TCCR0B=TCCR0B&0b11111000|mode;
  }else{
   TCCR1B=TCCR1B&0b11111000|mode;
  }
 }else if(pin==3||pin==11){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 32:mode=0x03;break;
   case 64:mode=0x04;break;
   case 128:mode=0x05;break;
   case 256:mode=0x06;break;
   case 1024:mode=0x7;break;
   default:return;
  }
  TCCR2B=TCCR2B&0b11111000|mode;
 }
}

First of all i probably don't even need this function as soon i know what frequency i want and so i probably need only 2 lines to set my 3(RGB) pins.

//Arduino Uno timer setup for pin 9,10,11
TCCR1B=TCCR1B&0b11111000|0x01; //pin 9,10
TCCR2B=TCCR2B&0b11111000|0x01; //pin 11
  1. Correct, i need to set TCCR1B only one time?

  2. What do the 0b1111000 refer to?

In the setup function there is also this line:

TIMSK2=1<<TOIE2;
  1. what does this ?

While the above 3 questions are not so important until it works, i need to understand how i can convert those timers to work with Arduino Micro, but also later with other Arduino devices.


On Arduino Micro

There is no timer 2

Pin 9,10,12 have the same timer ?

??????=??????&0b11111000|0x01;

Also if i understand correctly the Arduino micro has:

  1. a higher PWM range 1024 vs 255 (UNO)??
  2. a much higher frequency aviable in on timer4 only for pin 13

Full working (On Arduino UNO) code.

unsigned char Prescaler=0;
unsigned char CurrentLED=0;
unsigned char LEDValues[8][3];
unsigned char ports[8]={
 0b00000100,
 0b00001000,
 0b00010000,
 0b00100000,
 0b01000000,
 0b10000000,
 0b00000001,
 0b00010000
};//PINS 2,3,4,5,6,7,8,12
#define PrescalerOverflowValue 4
ISR(TIMER2_OVF_vect){
 if(Prescaler<PrescalerOverflowValue){
  Prescaler++;
 }else {
  Prescaler=0;
  Multiplex();
 }
}
void Multiplex(void){    
 PORTD&=0b00000011;
 PORTB&=0b11101110;
 analogWrite(9,255-LEDValues[CurrentLED][0]);  
 analogWrite(10,255-LEDValues[CurrentLED][1]);
 analogWrite(11,255-LEDValues[CurrentLED][2]);
 CurrentLED<6?(PORTD|=ports[CurrentLED]):(PORTB|=ports[CurrentLED]);
 CurrentLED++;
 if(CurrentLED>7)CurrentLED=0;  
}
void setPwmFrequency(int pin, int divisor){
 byte mode;
 if(pin==5||pin==6||pin==9||pin==10){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 64:mode=0x03;break;
   case 256:mode=0x04;break;
   case 1024:mode=0x05;break;
   default:return;
  }
  if(pin==5||pin==6){
   TCCR0B=TCCR0B&0b11111000|mode;
  }else{
   TCCR1B=TCCR1B&0b11111000|mode;
  }
 }else if(pin==3||pin==11){
  switch(divisor){
   case 1:mode=0x01;break;
   case 8:mode=0x02;break;
   case 32:mode=0x03;break;
   case 64:mode=0x04;break;
   case 128:mode=0x05;break;
   case 256:mode=0x06;break;
   case 1024:mode=0x7;break;
   default:return;
  }
  TCCR2B=TCCR2B&0b11111000|mode;
 }
}
void setup(void){
 pinMode(2,OUTPUT);//1
 pinMode(3,OUTPUT);//2
 pinMode(4,OUTPUT);//3
 pinMode(5,OUTPUT);//4
 pinMode(6,OUTPUT);//5
 pinMode(7,OUTPUT);//6
 pinMode(8,OUTPUT);//7
 pinMode(12,OUTPUT);//8
 pinMode(9,OUTPUT);//red
 pinMode(10,OUTPUT);//green
 pinMode(11,OUTPUT);//blue
 setPwmFrequency(9,8);
 setPwmFrequency(10,8);    
 setPwmFrequency(11,8);
 TIMSK2=1<<TOIE2;
}
void loop(void){
 for(int i=0;i<8;i++){
  //LEDValues[i][0];//red
  //LEDValues[i][1];//green
  //LEDValues[i][2];//blue
 }
}