I wanted to run the following code as something similar to Tone or pwm. However for some reason that I am unable to seek it doesn't work.
#define WAIT 3200UL
void setup() {
DDRB |= B00000001; // set pin8 as output
}
void loop() {
unsigned long t;
while (1) {
PORTB &= B11111110; // clear pin 8
for (t = 0 ; t < WAIT; t++);
PORTB |= B00000001; // set pin 8
for (t = 0; t < WAIT; t++);
// PORTB &= B11111110;
// delayMicroseconds(WAIT);
// PORTB |= B00000001;
// delayMicroseconds(WAIT);
}
}
the code is pretty simple, just toggling a pin. If I use the commented section, it works fine but as is, it doesn't work.
I removed the "while(1)" statement since it was unnecessary for now, it started to work improperly, (changing the WAIT didn't have any effect and I'm not sure I'm getting a full swing).
Pin 8 is connected to an LED + R to ground and a piezzo in parallel to them. the LED seems to be steady on yet the buzzer makes a low noise. I measured a voltage of 3.8v DC and arround 9v AC on the pin, other pins I ran the code for were similar.
As another attempt I put a Serial.print in the beginning of the while(1) but it printed out only once.
Also looked at the assembly code generated for a clue to find out what does delayMicroseconds() do but couldn't figure out what's wrong.
Appreciate your help.