Hi all,
I want to have phase shift of 90 degree between the pins (D5,D6), (D10& D11). I am using code attached where I am generating 30Hz frequency with 50% duty. I am having the code. Help me with your ideas.
Please, never ever post code as a picture, I for one am not going to type in something.
Also please state your problem clearly.
This makes little sense to me. What exactly are you trying to do?
You might want to look at this How to get the best out of this forum before you proceed any further.
30Hz is not very fast. For your 90 degree phase shift you could run a timer at 120Hz and, in its ISR, manipulate the pins as required.
I’m also a bit confused by your question, but I’d start by generating a 120Hz ticker, dividing it by 4 (=30Hz, with nice square quadrants), and working from there.
Hi @Grumpy_Mike
I want to generate a phase of 90 degree between two pins D5&D6 at 30Hz frequency, which means there should be a phase difference of 90degree between both PWM's.
Is it possible to do so?
Hi @6v6gt
Can you tell me the way to do so.
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// Pins D5 and D6 - 30 Hz
TCCR0B = 0b00000101; // x1024
TCCR0A = 0b00000001; // phase correct
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000101; // x1024 phase correct
TCCR2B = 0b00000111; // x1024
TCCR2A = 0b00000001; // phase correct
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(5, 128);
analogWrite(6, 127);
analogWrite(10, 128);
analogWrite(11, 127);
}
Yes, it is.
Now post your code correctly and we can all see what it does.
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// Pins D5 and D6 - 30 Hz
TCCR0B = 0b00000101; // x1024
TCCR0A = 0b00000001; // phase correct
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000101; // x1024 phase correct
TCCR2B = 0b00000111; // x1024
TCCR2A = 0b00000001; // phase correct
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(5, 128);
analogWrite(6, 127);
analogWrite(10, 128);
analogWrite(11, 127);
}
Please try to tell me in a proper so that I can make changes
<CODE/>
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// Pins D5 and D6 - 30 Hz
TCCR0B = 0b00000101; // x1024
TCCR0A = 0b00000001; // phase correct
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000101; // x1024 phase correct
TCCR2B = 0b00000111; // x1024
TCCR2A = 0b00000001; // phase correct
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(5, 128);
analogWrite(6, 127);
analogWrite(10, 128);
analogWrite(11, 127);
}
You’re posting the same/similar code over and over…
What’s the period (ms or us) of 30Hz…
Now, i’d you want 90degrees, that offset would be a quarter of that 30Hz period.
How’s a good time to practice your coding, or paying someone to do it for you,
Is this an exercise that you have been give where you have to use the pins and timers you have indicated to achieve this phase shifting which is also respected by analogWrite() ?
It is not the simplest way of a achieving your stated goal as has been pointed out.
Would it help to think of "phase shift" as just "time delay" ... ?
The design and coding of a sequencer could help.
@paulpaulson
Can you please help me with some more details
This will do it.
ISR(TIMER1_COMPA_vect){
static int times = 0;
times = (times + 1) & 3 ;
if(times == 0) { digitalWrite(5, HIGH); digitalWrite(10, HIGH);}
if(times == 1) { digitalWrite(6, HIGH); digitalWrite(11, HIGH); }
if(times == 2) { digitalWrite(5, LOW); digitalWrite(10, LOW); }
if(times == 3) { digitalWrite(6, LOW); digitalWrite(11, LOW);}
}
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
cli();//stop interrupts
//set timer1 values
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 120 Hz increments
OCR1A = 130;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
}
void loop() {
// put your main code here, to run repeatedly:
}
@charanpatel_k is doing is replying to each request.
@charanpatel_k as everybody can see your posts, you only need to respond once in reply to many requests with the same question.
Tom...
@Grumpy_Mike
Thank you Guys
Mike beat me to it, but this is my attempt.
First work out which pins need to change state at each 90degree step.
Then:
- make first change of pins
- add delay equivalent to 90 degrees
- make second change of pins
- add delay equivalent to 90 degrees
- make third change of pins
- add delay equivalent to 90 degrees
- make fourth change of pins
- add delay equivalent to 90 degrees
- back to beginning
unsigned int frequency = 30; // desired frequency in Hz
unsigned int period = 1000000 / frequency; // period of signal in microseconds
unsigned int quarterPeriod = period / 4; // 90 degrees phase shift
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(5, HIGH);
digitalWrite(10, LOW);
delayMicroseconds(quarterPeriod);
digitalWrite(6, HIGH);
digitalWrite(11, LOW);
delayMicroseconds(quarterPeriod);
digitalWrite(5, LOW);
digitalWrite(10, HIGH);
delayMicroseconds(quarterPeriod);
digitalWrite(6, LOW);
digitalWrite(11, HIGH);
delayMicroseconds(quarterPeriod);
}
This is pretty accurate, but the delay() function takes about four and a half microseconds, so could become appreciable if the frequency needed to be higher.
This could be improved by using direct port manipulation, and even more if all 4 pins were on the same port.
charanpatel_k,
It looks as though Mike and I interpreted your requirements in different ways.
This is what Mike's output looks like:
Which did you mean?
Precisely. This is why I asked for a better description of the problem and was met with the same gobbledygook as the original description.