hi need help in getting pwm code to work on leonardo
hi guys,
i found disappointment leonardo has no pwm higher 500hz or so. when bought it, thought had pwm pins have docs "libs" handle them far search there no one.
guys question how can make pwm timers , register without screwing up usb connection. tried the code bellow. compiles fine after uploading, lost usb connection , leonardo not recognized. pressed upload , after compiling finished pressed reset on board new empty sketch uploaded , have leonardo recognized again:
http://harizanov.com/2013/04/crazy-high-frequency-pwm-with-atmega32u4/
code:
#define timer4_resolution 1023ul
#define pll_freq 48000000ul
unsigned long pwmperiod = 0;
isr(timer4_ovf_vect) {}
void enable_intr(){
timsk4 = _bv(toie4);
}
void disable_intr(){
timsk4 = 0;
}
void initialize(unsigned long freq) {
/* init internal pll */
pllfrq = _bv(pdiv2);
pllcsr = _bv(plle);
while(!(pllcsr & _bv(plock)));
pllfrq |= _bv(plltm0); /* pck 48mhz */
tccr4a = (1<<pwm4a);
tccr4e = (1<<enhc4);
tccr4d = (1<<wgm40); //set phase , frequency correct mode
tccr4c = 0;
setperiod(freq);
}
void setpwmduty(unsigned int duty) {
unsigned long dutycycle = pwmperiod;
dutycycle *= duty;
dutycycle >>= 9;
tc4h = (dutycycle) >> 8;
ocr4a = (dutycycle) & 255;
}
void start() {
tccr4a |= _bv(com4a1);
}
void stop() {
tccr4a &= ~(_bv(com4a1));
}
void setperiod(unsigned long freq) {
unsigned long cycles = pll_freq / 2 / freq;
unsigned char clockselectbits = 0;
if (cycles < timer4_resolution) {
clockselectbits = _bv(cs40);
pwmperiod = cycles;
} else
if (cycles < timer4_resolution * 2) {
clockselectbits = _bv(cs41);
pwmperiod = cycles / 2;
} else
if (cycles < timer4_resolution * 4) {
clockselectbits = _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 4;
} else
if (cycles < timer4_resolution * {
clockselectbits = _bv(cs42);
pwmperiod = cycles / 8;
} else
if (cycles < timer4_resolution * 16) {
clockselectbits = _bv(cs42) | _bv(cs40);
pwmperiod = cycles / 16;
} else
if (cycles < timer4_resolution * 32) {
clockselectbits = _bv(cs42) | _bv(cs41);
pwmperiod = cycles / 32;
} else
if (cycles < timer4_resolution * 64) {
clockselectbits = _bv(cs42) | _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 64;
} else
if (cycles < timer4_resolution * 128) {
clockselectbits = _bv(cs43);
pwmperiod = cycles / 128;
} else
if (cycles < timer4_resolution * 256) {
clockselectbits = _bv(cs43) | _bv(cs40);
pwmperiod = cycles / 256;
} else
if (cycles < timer4_resolution * 512) {
clockselectbits = _bv(cs43) | _bv(cs41);
pwmperiod = cycles / 512;
} else
if (cycles < timer4_resolution * 1024) {
clockselectbits = _bv(cs43) | _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 1024;
} else
if (cycles < timer4_resolution * 2048) {
clockselectbits = _bv(cs43) | _bv(cs42);
pwmperiod = cycles / 2048;
} else
if (cycles < timer4_resolution * 4096) {
clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs40);
pwmperiod = cycles / 4096;
} else
if (cycles < timer4_resolution * 8192) {
clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41);
pwmperiod = cycles / 8192;
} else
if (cycles < timer4_resolution * 16384) { clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41) | _bv(cs40); pwmperiod = cycles / 16384; } /*else clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41) | _bv(cs40); pwmperiod = timer4_resolution - 1; */ tccr4b = clockselectbits; tc4h = pwmperiod >> 8;
ocr4c = pwmperiod;
}
void setup() {
pinmode(13, output);
digitalwrite(13,low);
initialize(123456); // frequency generate in hz
setpwmduty(512); // duty cycle 0-1024, 1024 100%, 512 50%
start();
}
void loop(){
}
any appreciated. started arduino gentle me smiley-wink.
timi
i found disappointment leonardo has no pwm higher 500hz or so. when bought it, thought had pwm pins have docs "libs" handle them far search there no one.
guys question how can make pwm timers , register without screwing up usb connection. tried the code bellow. compiles fine after uploading, lost usb connection , leonardo not recognized. pressed upload , after compiling finished pressed reset on board new empty sketch uploaded , have leonardo recognized again:
http://harizanov.com/2013/04/crazy-high-frequency-pwm-with-atmega32u4/
code:
#define timer4_resolution 1023ul
#define pll_freq 48000000ul
unsigned long pwmperiod = 0;
isr(timer4_ovf_vect) {}
void enable_intr(){
timsk4 = _bv(toie4);
}
void disable_intr(){
timsk4 = 0;
}
void initialize(unsigned long freq) {
/* init internal pll */
pllfrq = _bv(pdiv2);
pllcsr = _bv(plle);
while(!(pllcsr & _bv(plock)));
pllfrq |= _bv(plltm0); /* pck 48mhz */
tccr4a = (1<<pwm4a);
tccr4e = (1<<enhc4);
tccr4d = (1<<wgm40); //set phase , frequency correct mode
tccr4c = 0;
setperiod(freq);
}
void setpwmduty(unsigned int duty) {
unsigned long dutycycle = pwmperiod;
dutycycle *= duty;
dutycycle >>= 9;
tc4h = (dutycycle) >> 8;
ocr4a = (dutycycle) & 255;
}
void start() {
tccr4a |= _bv(com4a1);
}
void stop() {
tccr4a &= ~(_bv(com4a1));
}
void setperiod(unsigned long freq) {
unsigned long cycles = pll_freq / 2 / freq;
unsigned char clockselectbits = 0;
if (cycles < timer4_resolution) {
clockselectbits = _bv(cs40);
pwmperiod = cycles;
} else
if (cycles < timer4_resolution * 2) {
clockselectbits = _bv(cs41);
pwmperiod = cycles / 2;
} else
if (cycles < timer4_resolution * 4) {
clockselectbits = _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 4;
} else
if (cycles < timer4_resolution * {
clockselectbits = _bv(cs42);
pwmperiod = cycles / 8;
} else
if (cycles < timer4_resolution * 16) {
clockselectbits = _bv(cs42) | _bv(cs40);
pwmperiod = cycles / 16;
} else
if (cycles < timer4_resolution * 32) {
clockselectbits = _bv(cs42) | _bv(cs41);
pwmperiod = cycles / 32;
} else
if (cycles < timer4_resolution * 64) {
clockselectbits = _bv(cs42) | _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 64;
} else
if (cycles < timer4_resolution * 128) {
clockselectbits = _bv(cs43);
pwmperiod = cycles / 128;
} else
if (cycles < timer4_resolution * 256) {
clockselectbits = _bv(cs43) | _bv(cs40);
pwmperiod = cycles / 256;
} else
if (cycles < timer4_resolution * 512) {
clockselectbits = _bv(cs43) | _bv(cs41);
pwmperiod = cycles / 512;
} else
if (cycles < timer4_resolution * 1024) {
clockselectbits = _bv(cs43) | _bv(cs41) | _bv(cs40);
pwmperiod = cycles / 1024;
} else
if (cycles < timer4_resolution * 2048) {
clockselectbits = _bv(cs43) | _bv(cs42);
pwmperiod = cycles / 2048;
} else
if (cycles < timer4_resolution * 4096) {
clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs40);
pwmperiod = cycles / 4096;
} else
if (cycles < timer4_resolution * 8192) {
clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41);
pwmperiod = cycles / 8192;
} else
if (cycles < timer4_resolution * 16384) { clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41) | _bv(cs40); pwmperiod = cycles / 16384; } /*else clockselectbits = _bv(cs43) | _bv(cs42) | _bv(cs41) | _bv(cs40); pwmperiod = timer4_resolution - 1; */ tccr4b = clockselectbits; tc4h = pwmperiod >> 8;
ocr4c = pwmperiod;
}
void setup() {
pinmode(13, output);
digitalwrite(13,low);
initialize(123456); // frequency generate in hz
setpwmduty(512); // duty cycle 0-1024, 1024 100%, 512 50%
start();
}
void loop(){
}
any appreciated. started arduino gentle me smiley-wink.
timi
well, first thing put put code in code tags avoid crap being displayed
if (cycles < timer4_resolution * {
if (cycles < timer4_resolution * {
Arduino Forum > Using Arduino > Programming Questions > hi need help in getting pwm code to work on leonardo
arduino
Comments
Post a Comment