Mega2650 - No PWM output from OC1C (Digital Pin 13)

Hello everyone,
I am using a Arduino Mega2560 to control a 3 phase brushless DC motor via the 3 PWM outputs available from Timer1. Two of the outputs (OC1A/B, digital Pins 11/12) are providing the correct 20KHz output, however I am not getting any output from OC1C (Digital Pin 13, defined in my code as PWM), it just sits at 0 volts.

I am aware that Digital Pin 13 is shared with the output from OC0A, section 19 of the data sheet (Output Compare Modulator) indicates that the output on Pin 13 is determined by the relevant COMnx pins. Unless all my Arduino are broken there must be a problem with my configuration of the device however I cannot see it.
I have stripped my code down to the bare essentials as shown below and removed all connections other than the USB cable thus eliminating any other potential issues and still it does not work, can anyone see where I have gone wrong?

#include <avr/wdt.h>    //Include for Watchdog Timer

#define PWM_SPARE 11  
#define DCM_PWM 12    
#define PWM 13        

void setup() {
TCCR0A = TCCR0A & 0x3F;           //Disconnect Timer OCOA output from Digital Pin 13 (PB7)
TCCR1A = (TCCR1A & 0x00) | 0xAA;  //Ensure TCCR1A is clear then Set for Phase Correct PWM, TOP = ICRn, output = A, B & C (1010 1010)
TCCR1B = (TCCR1B & 0x00) | 0x11;  //Clear TCCR1B then set for PWM, no Prescaling 0001 0001 - DO NOT CHANGE bit 7:3
TCCR1C = (TCCR1C & 0x00);         //Ensure TRRC1C is clear, TCCR1C not used, just belt & braces code
ICR1 = 0x0190;                    //Use IRCn to Set TOP for a 20KHz PWM Frequency  = 400 ($0190)

Serial.begin(19200);

digitalWrite(PWM_SPARE, LOW); //No PWM - Output in Tristate condition
digitalWrite(DCM_PWM, LOW);   //No PWM - Output in Tristate condition
digitalWrite(PWM, LOW);       //No PWM - Output in Tristate condition

pinMode(PWM_SPARE, OUTPUT);
pinMode(DCM_PWM, OUTPUT);
pinMode(PWM, OUTPUT);

analogWrite(PWM, 100);        //25% Modulation depth
analogWrite(DCM_PWM, 100);    //25% Modulation depth
analogWrite(PWM_SPARE, 100);  //25% Modulation Depth

WDTO_2S;  //2 Second Time out (Watchdog Timer enabled at power up)
}

void loop() {
wdt_reset();
}

Stephen

1 Like

That pin is controlled also by OC0A, the arduino runtime only knows about that one. So the call

analogWrite (PWM, 100)

Simply enables timer0 to drive that pin, throwing away all your setup for timer1...

1 Like

Thanks for replying.
If that is the case then it is extremely annoying and one has to wonder why it has not been resolved.
To be clear what do you mean by the Ardunio runtime, do you means the IDE or the compiler? If the complier then presumably using a different C compiler may work, but are there any other known ways of getting the Timer1C (OC1C) output from device pin 26/Arduino Digital Pin13?

Regards,
Stephen

1 Like

Problem solved: :slight_smile:
The line TIMER1C , // PB 7 ** 13 ** PWM13 was missing from the pins_arduino.h file in the variants\mega folder of the install directory.

Open that folder, i.e....
C:\Program Files (x86)\Ardunio\hardware\ardunio\avr\varients\mega.

Delete or rename the pins_ardunio.h file, and replace it with the attached file (you may have to edit folder permissions to do this).
Now, provided you have disabled the TIMER0A output (OCOA) by setting COM0A1 & COM0A0 bits of the TCCR0A register to 0 and set either one or both of the COM1C1 & COM1C0 bits of the TCCR1A register to 1 (as applicable) you can now get a PWM output from TIMER1C (OC1C) on pin26 of the ATmega2560/Digital pin 13 of the Arduino Mega2560 board.

Thanks to MarkT for his reply which made me think there may be a problem with one of the files the IDE uses.

pins_arduino.h (13.8 KB)

1 Like