Simulation of PWM using timer
I'm currently trying to output a PWM signal from an Arduino Mega using register TCCR4A & TCCR4B manipulation. I'm able to have the attached motor spinning but I strongly doubt it's receiving the correct frequency and duty cycle. What's the best way to simulate the Arduino output? I read there are different software available, but none of them seem able to simulate what I want. I do not own an oscilloscope, but I'd like to debug the code anyway. Thanks!
EDIT: for those asking for the code...
Although this wasn't the main aim of the question, here is it.
#include <avr/interrupt.h>
#include <avr/io.h>
void setup() {
pinMode(6, OUTPUT);
TCCR4A = 0x00;
TCCR4B = 0x00;
//Mode to 14: WGM43:0 = 1110, fast PWM, page 145 datasheet
//Prescaler: 8, CS42:0 = 010
//TOP value: ICR4 = 9999
//
//Should yield to:
//frequency: f = 16e6/8/10000 = 200 Hz
//Pulse lenght: every clock is Pl = 16e6/8 = 2MHz --> 1 pulse = 0.5 us
//To have 1ms on time, should set OCR4A = 2000, 2ms on time --> OCR4A = 4000
TCCR4A = (1 << COM4A1) | (0 << COM4A0) | (1 << COM4B1) | (0 << COM4B0) | (1 << COM4C1) | (0 << COM4C0) | (1 << WGM41) | (0 << WGM40);
TCCR4B = (0 << ICNC4) | (0 << ICES4) | (1 << WGM43) | (1 << WGM42) | (0 << CS42) | (1 << CS41) | (0 << CS40);
ICR4 = 9999;
OCR4A = 2000;
delay(5000);
}
void loop() {
OCR4A = 3000; //1.5 ms on time
delay(1500);
OCR4A = 2000; //1.0 ms on time
delay(1500);
}
EDIT: for those asking for the code...
Although this wasn't the main aim of the question, here is it.
#include <avr/interrupt.h>
#include <avr/io.h>
void setup() {
pinMode(6, OUTPUT);
TCCR4A = 0x00;
TCCR4B = 0x00;
//Mode to 14: WGM43:0 = 1110, fast PWM, page 145 datasheet
//Prescaler: 8, CS42:0 = 010
//TOP value: ICR4 = 9999
//
//Should yield to:
//frequency: f = 16e6/8/10000 = 200 Hz
//Pulse lenght: every clock is Pl = 16e6/8 = 2MHz --> 1 pulse = 0.5 us
//To have 1ms on time, should set OCR4A = 2000, 2ms on time --> OCR4A = 4000
TCCR4A = (1 << COM4A1) | (0 << COM4A0) | (1 << COM4B1) | (0 << COM4B0) | (1 << COM4C1) | (0 << COM4C0) | (1 << WGM41) | (0 << WGM40);
TCCR4B = (0 << ICNC4) | (0 << ICES4) | (1 << WGM43) | (1 << WGM42) | (0 << CS42) | (1 << CS41) | (0 << CS40);
ICR4 = 9999;
OCR4A = 2000;
delay(5000);
}
void loop() {
OCR4A = 3000; //1.5 ms on time
delay(1500);
OCR4A = 2000; //1.0 ms on time
delay(1500);
}
Комментарии
Отправить комментарий