Hello. I have a Mega based board that I am using for a CNC mill. Trying to add a laser on and need to switch the default PWM pin in the code to another one that uses 5V instead of the 24V of the power supply.
The code (grbl fork) used port information instead of just specifying D5 or something like that. Could someone check and see if this code is correct to control D5 for PWM? It doesn't seem to be working and I don't know where else to find the information I need. Thank you!
// PWM on D5:
// Set Timer up to use TIMER3A which is attached to Digital Pin 5 - Ramps Servo 2
#define SPINDLE_PWM_MAX_VALUE 255.0 // Translates to about 1.9 kHz PWM frequency at 1/8 prescaler
#ifndef SPINDLE_PWM_MIN_VALUE
#define SPINDLE_PWM_MIN_VALUE 1 // Must be greater than zero.
#endif
#define SPINDLE_PWM_OFF_VALUE 0
#define SPINDLE_PWM_RANGE (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE)
//Control Digital Pin 6 which is Servo 2 signal pin on Ramps 1.4 board
#define SPINDLE_TCCRA_REGISTER TCCR3A
#define SPINDLE_TCCRB_REGISTER TCCR3B
#define SPINDLE_OCR_REGISTER OCR3A
#define SPINDLE_COMB_BIT COM3A1
// 1/8 Prescaler, 8-bit Fast PWM mode
#define SPINDLE_TCCRA_INIT_MASK (1<<WGM41)
#define SPINDLE_TCCRB_INIT_MASK ((1<<WGM42) | (1<<WGM43) | (1<<CS41))
#define SPINDLE_OCRA_REGISTER ICR3 // 8-bit Fast PWM mode requires top reset value stored here.
#define SPINDLE_OCRA_TOP_VALUE 0xFF // PWM counter reset value. Should be the same as PWM_MAX_VALUE in hex.
// Define spindle output pins.
#define SPINDLE_PWM_DDR DDRE
#define SPINDLE_PWM_PORT PORTE
#define SPINDLE_PWM_BIT 3 // MEGA2560 Digital Pin 5
type or paste code here
Each PWM pin can only produce a 5V signal on a 5V controller. The 24V are applied to the power driver circuit, that's where you have to apply a different voltage. Else you need another driver for your 5V motor.
If you want to use a different pin for PWM then you also have to change at least the OCR to the new channel. Eventually you also have to change the timer and control bits related to PWM generation for that channel.
Yes. OC3A (Output Compare pin A for timer 3) is the one connected to Pin 5. If you want to use a different PWM pin, you have to pick one that is not used for anything else. Obvious choices are OC3B (Pin 2) and OC3C (Pin 3). If 2, 3, and 5 are not available you have to switch to a different timer, like Timer4 (Pins 6, 7, or 8), or Timer5 (Pins 46, 45, or 44) if they aren't in use.
Another option is to connect to Pin 5 rather than connecting to whatever output Pin 5 is switching. It looks like Pin 5 is used for the SPINDLE output. Instead of connecting your laser to the SPINDLE output, connect it to Pin 5.
This code that i have posted is an attempt to switch from another pin to pin 5 but it doesn't seem to be working. Which is why I was hoping someone could check it for me to make sure it was correct.
It obviously is not correct. But without knowledge of the previously used pin and the usage of timers and pins in the omitted code - before and after - it's impossible to tell what else has to be changed.
It looks like the default SPINDLE PWM pin for the ATmega2560 is Pin 7 (OC4B, PH4). Why did you feel the need to move it?
// Start of PWM & Stepper Enabled Spindle
#ifdef VARIABLE_SPINDLE
// Advanced Configuration Below You should not need to touch these variables
// Set Timer up to use TIMER4B which is attached to Digital Pin 7
#define PWM_MAX_VALUE 65535.0
#define TCCRA_REGISTER TCCR4A
#define TCCRB_REGISTER TCCR4B
#define OCR_REGISTER OCR4B
#define COMB_BIT COM4B1
#define WAVE0_REGISTER WGM40
#define WAVE1_REGISTER WGM41
#define WAVE2_REGISTER WGM42
#define WAVE3_REGISTER WGM43
#define SPINDLE_PWM_DDR DDRH
#define SPINDLE_PWM_PORT PORTH
#define SPINDLE_PWM_BIT 4 // MEGA2560 Digital Pin 7
#endif // End of VARIABLE_SPINDLE
On the board i use, pins 7, 8, 9, 10 are all 24V. Pin 6 doesn't work, pin 11's timer is mapped to another function. 5 was one of the only ones left to me that was 5V