I have a project that tests circuit frequency,and i should compile timer2 with this frequency to output a pwm signal. The Problem is how can I synchronize this pwm signal and voltage running of this circuit? Should I use the timer2 Interrupt ?
xtasy1998:
I have a project that tests circuit frequency,
What do you mean by "tests circuit frequency"?
xtasy1998:
and i should compile timer2 with this frequency to output a pwm signal.
Do you want to output a specific frequency of square wave?
xtasy1998:
The Problem is how can I synchronize this pwm signal and voltage running of this circuit? Should I use the timer2 Interrupt ?
What are you trying to synchronize? So far you have mentioned one signal... a frequency... that you are generating.
johnwasser:
What do you mean by "tests circuit frequency"?Do you want to output a specific frequency of square wave?
What are you trying to synchronize? So far you have mentioned one signal... a frequency... that you are generating.
yes, i need to output a specific frequecy of square wave but use timer2 to do it , and the frequency is measured from a electric circuit.As the frequency of the electric circuit changes, the frequency of the square wave also changes. and this square should synchronous to signal(voltage,time) of this electric circuit.
unsigned long Htime;
unsigned long Ltime;
unsigned long Ttime;
unsigned long f;
int prescaler;
void setup() {
// put your setup code here, to run once:
TCNT2 = 0;
TCCR2A = 0;
TCCR2B = 0;
Serial.begin(9600);
pinMode(5, INPUT);
pinMode(3,OUTPUT);
pinMode(11,OUTPUT);
TCCR2A |= (1 << WGM20);
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
}
void loop() {
// put your main code here, to run repeatedly:
Htime = pulseIn(5, HIGH);
Ltime = pulseIn(5, LOW);
Ttime = Htime + Ltime;
f = 1000000 / Ttime;
Serial.println(f);
prescaler = (16000000 / 1024 / f) - 1;
OCR2A = prescaler;
OCR2B = prescaler * 0.5;
}
this is what i have written. I don‘t know why output of square wave didn't work and how could i synchronize two signals.
xtasy1998:
this is what i have written. I don‘t know why output of square wave didn't work and how could i synchronize two signals.
You're pretty close.
You have the Waveform Generation Mode set wrong. It looks like you want Mode 7: Fast PWM where 'TOP' is stored in OCR2A.
You did not set the Compare Output Mode for OCR2B so the output pin won't do anything.
Have you checked the limits of the timer to see if you can get the frequency you want? With the prescale set to 1024 your clock is running at 15.625 kHz. I think the highest frequency will be half that: 7812.5 Hz. Since the timer can count up to 256 clocks your frequency can go down to about 61 Hz. Is that the range you wanted?