gnea / grbl

An open source, embedded, high performance g-code-parser and CNC milling controller written in optimized C that will run on a straight Arduino
https://github.com/gnea/grbl/wiki
Other
4.08k stars 1.61k forks source link

D11 changing PWM frequency #981

Open Mungbeanz opened 3 years ago

Mungbeanz commented 3 years ago

Hi,

Had a really good look around but am a bit confused at how to change the PWM frequency on D11. I plan on using my spare arduino nano on eleks mana se breakout board to control a NUBM08 driven by a BlackBuck 8m 8A driver. The driver accepts PWM inputs with 4Khz modulation.

D11 should be on timer 2. TCCR2B Table of possibilities

//TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR2B = TCCR2B & B11111000 | B00000010; // set timer 2 divisor to 8 for PWM frequency of 3921.16 Hz //TCCR2B = TCCR2B & B11111000 | B00000011; // set timer 2 divisor to 32 for PWM frequency of 980.39 Hz TCCR2B = TCCR2B & B11111000 | B00000100; // set timer 2 divisor to 64 for PWM frequency of 490.20 Hz (The DEFAULT) //TCCR2B = TCCR2B & B11111000 | B00000101; // set timer 2 divisor to 128 for PWM frequency of 245.10 Hz //TCCR2B = TCCR2B & B11111000 | B00000110; // set timer 2 divisor to 256 for PWM frequency of 122.55 Hz //TCCR2B = TCCR2B & B11111000 | B00000111; // set timer 2 divisor to 1024 for PWM frequency of 30.64 Hz

In the CPU_MAP.H file I can only see

define SPINDLE_TCCRA_REGISTER TCCR2A

define SPINDLE_TCCRB_REGISTER TCCR2B

define SPINDLE_OCR_REGISTER OCR2A

define SPINDLE_COMB_BIT COM2A1

// Prescaled, 8-bit Fast PWM mode.
#define SPINDLE_TCCRA_INIT_MASK   ((1<<WGM20) | (1<<WGM21))  // Configures fast PWM mode.
// #define SPINDLE_TCCRB_INIT_MASK   (1<<CS20)               // Disable prescaler -> 62.5kHz
// #define SPINDLE_TCCRB_INIT_MASK   (1<<CS21)               // 1/8 prescaler -> 7.8kHz (Used in v0.9)
// #define SPINDLE_TCCRB_INIT_MASK   ((1<<CS21) | (1<<CS20)) // 1/32 prescaler -> 1.96kHz
#define SPINDLE_TCCRB_INIT_MASK      (1<<CS22)               // 1/64 prescaler -> 0.98kHz (J-tech laser)

Some advice I came across said that GRBL can be compiled to use 3.92KHZ PWM. It mentioned setting disabling fast PWM and then prescaler to 8, then setting the times for the phase correct PWM.

From this I take it comment out #define SPINDLE_TCCRA_INIT_MASK ((1<<WGM20) | (1<<WGM21)) // Configures fast PWM mode.

enable this: // #define SPINDLE_TCCRB_INIT_MASK (1<<CS21) // 1/8 prescaler -> 7.8kHz (Used in v0.9)

Am I correct in my thinking? this should give me a PWM frequency of 3.92?

Mungbeanz commented 3 years ago

Ordering a basic oscilloscope so will test this out myself and report back in 4-6 weeks

quotelawrence commented 3 years ago

I hope you can add some testing information