Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I'm using Arduino YUN PWM pins 9, 10, 11, and 13, and have it set up so all are using the same PWM frequency.

Everything is working fine, but now I NEED TWO MORE PWMs.

I have PWM pins 3, 5, 6 available.

My existing code is below, does anybody know how I can add the two needed PWM outputs using any of pins 3, 5, or 6 using the same frequency as the rest of the PWMs??

#define pwm_13 OCR4A
#define pwm_11 OCR1C
#define pwm_10 OCR1B
#define pwm_9  OCR1A

#define PWM_4A_PIN  13 
#define PWM_1C_PIN  11
#define PWM_1B_PIN  10
#define PWM_1A_PIN  9

void Timer0init()
{
  TIFR0 =0x00;
  TCCR0A = 0;// set entire TCCR0A register to 0
  TCCR0B = 0;// same for TCCR0B
  TCNT0  = 0;//initialize counter value to 0
  OCR0A = 125; 
  TCCR0A |= (1 << WGM01);
  TCCR0B |= 0x03; //0x02; // (1 << CS01) | (1 << CS00);   
  TIMSK0 |= (1 << OCIE0A); 
}

void initADC()
{
  ADCSRA &=0xF8; //clear prescaler bits
  ADCSRA |=0x05;  
}

void initPWM()
{
 TCCR1B = TCCR1B & 0b11111000 | 0x05;
 TCCR4B = TCCR1B & 0b11110000 | 0x0B;

 analogWrite(PWM_4A_PIN,128);
 analogWrite(PWM_1C_PIN,128);
 analogWrite(PWM_1B_PIN,128);
 analogWrite(PWM_1A_PIN,128); 

 TCCR0A &= ~(_BV(COM0A1) | _BV(COM0A0));
 TCCR1A |= _BV(COM1C1);
}

void setup()
{
  Timer0init();
  initADC();
  initPWM();
  ...
}

void loop()
{
  pwm_13=v1++;
  pwm_11=v2++;
  pwm_10=v3++;
  pwm_9 =v4++;
}
share|improve this question
1  
What is the point mixing low-level AVR API (direct manipulation of registers like TCCR1B) with Arduino API (analogWrite()). Why not use just Arduino (or just AVR)? –  jfpoilpret Oct 18 '14 at 16:37
    
I got this code from someone who fixed the problem I had by doing it this way. I'm not arduino expert, so since it worked, I left it like that. According to him, in order to get all the PWM pins working off the same frequency I had to use the register/API mix. Using the API set things to the default, then the register writes allowed customization of the needed bits to set the output pins and speed to the desired values (I was told). So you have any idea how to adapt the code to use two additional PWM pins? –  user4448 Oct 18 '14 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.