I need help… i have the coding as shown below, but its not working as what i expected… base on what i know, the coding below should provide me a 2MHz frecuency at output pin 9 and 1kHz at ouput pin 8… but when i test it with oscilloscope i only obtain 2kHz at output pin 9… what should i do to obtain 2MHz at output pin 9?? how can i change/modified the code?? many2 thankz…
#include <util/delay.h>
int rog = 8;
void setup() {
pinMode(9, OUTPUT);
pinMode(rog, OUTPUT);
TCCR1A = 0;
TCCR1B = 0;
TCCR1A |= _BV(WGM11) | _BV(COM1A1);
TCCR1B |= _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);
ICR1= 7;
OCR1A = 3;
}
void loop(){
delayrog();
}
void delayrog(){
while (1)
{
digitalWrite(rog, HIGH);
_delay_ms(0.52);
digitalWrite(rog, LOW);
_delay_ms(0.52);
}
}
haneys:
digitalWrite(rog, HIGH);
_delay_ms(0.52);
digitalWrite(rog, LOW);
_delay_ms(0.52);
A - The 0.52 value is a floating point value. I don't think the Arduino language speaks "floating point" in this case. I'm fairly sure only Integer values are allowed here.
B - Try the loop without any delay and see how fast it’ll actually go without anything hindering it. It won’t do 2Mhz. If I remember right, my MegaADK only does 65Khz in an empty pin toggling loop.
C - Even if the Arduino was capable of 2Mhz output using this code, it wouldn’t, because you’ve told it to delay .52 MILLIseconds, meaning you would actually get a frequency of a bit less than 961Hz ( 1 second / .00104 seconds = 961.xxxx).
D - Even if the value was .52 MICROseconds, which is what I suspect you wanted, that value would give you an output frequency of a bit less than 961KHz.
So, the question is, how did you arrive at this 0.52 value?
I think he is trying to use _delay_ms(0.5) for the 1kHz (which should work as it has a resolution of 0.1ms) - NOTE: I dont know why you chose 0.52??
And then timer1’s Fast PWM to generate of 2MHz wave by making the timer toggle OC1A (Arduino digital 9) on match.
Are you trying to make a 3bit PWM on pin 9, or are you just trying to make a square wave? There is a much easier way to achieve the latter.