Hello all,
I have a Teensy 3.2 and I am using the Arduino IDE to program it. I am trying to generate 8 pulses with the frequency of 40 kHz. I have written 3 different codes and none of them is working quite the way I wanted. The duty cycle of the pulses are not always constant, indicating an inaccuracy with the delay in the code. There seems to be an issue with delays not being consistent and changing within the 8 pulses and between the 8 pulses. This inaccuracy is different between the 3 different codes, but exists in all of them. Changing the processor speed between 16 MHz and 120 MHz does not appear to change the problem (except in code 3 where NOP is dependent on it). Is there a way to make the cycles more consistent at 40 kHz? Do I need to use an external timer or oscillator? The two images below display the two problems. The first image shows the differences in time delays and output frequency. The second image shows a recurring issue where the square wave is not in fact a square, and doesn't seem to be caused by software problems. I couldn't figure out how to display them so I had to link to where I uploaded them.
Image 1:
https://ibb.co/jggDMd
Image 2:
https://ibb.co/jqxngd
Code 1:
Code 2:
Code 3:
I have a Teensy 3.2 and I am using the Arduino IDE to program it. I am trying to generate 8 pulses with the frequency of 40 kHz. I have written 3 different codes and none of them is working quite the way I wanted. The duty cycle of the pulses are not always constant, indicating an inaccuracy with the delay in the code. There seems to be an issue with delays not being consistent and changing within the 8 pulses and between the 8 pulses. This inaccuracy is different between the 3 different codes, but exists in all of them. Changing the processor speed between 16 MHz and 120 MHz does not appear to change the problem (except in code 3 where NOP is dependent on it). Is there a way to make the cycles more consistent at 40 kHz? Do I need to use an external timer or oscillator? The two images below display the two problems. The first image shows the differences in time delays and output frequency. The second image shows a recurring issue where the square wave is not in fact a square, and doesn't seem to be caused by software problems. I couldn't figure out how to display them so I had to link to where I uploaded them.
Image 1:
https://ibb.co/jggDMd
Image 2:
https://ibb.co/jqxngd
Code 1:
Code:
const uint8_t pin1 = 5;
const uint8_t pin2 = 6;
void setup() {
analogWriteFrequency(pin1, 40000);
analogWriteFrequency(pin2, 40000);
}
void loop() {
analogWrite(pin1,128); // for a 50:50 square wave
delayMicroseconds(188);
analogWrite(pin1,0);
delay(1000);
}
Code:
const uint8_t pin1 = 5;
const uint8_t pin2 = 6;
int count;
void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
}
void loop() {
PWMSignal();
delay(1000);
}
void PWMSignal(){
for(int i =0; i<=8; i++){
analogWrite(pin1, 255);
analogWrite(pin2, 0);
delayMicroseconds(10);
analogWrite(pin1, 0);
analogWrite(pin2, 255);
delayMicroseconds(10);
analogWrite(pin2, 0);
}
}
Code 3:
Code:
const uint8_t pin1 = 5;
const uint8_t pin2 = 6;
const int N = 50;
#define NOP __asm__ __volatile__ ("nop\n\t")
void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
}
void loop() {
PWMSignal();
delay(100);
}
void PWMSignal(){
for(int i =0; i<=8; i++){
analogWrite(pin1, 255);
analogWrite(pin2, 0);
delayNOP();
analogWrite(pin1, 0);
analogWrite(pin2, 255);
delayNOP();
analogWrite(pin2, 0);
}
}
void delayNOP(){
for(int j =0; j<=N; j++){
NOP;
}
}