From 9d751dc020b43f32b1cd7e5216f4d8de0208dda0 Mon Sep 17 00:00:00 2001 From: Vineet K Date: Mon, 15 Apr 2024 22:38:55 -0400 Subject: [PATCH] investigate why variable delay is not working --- main.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index 9f99ce1..461a970 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include #define F_CPU 16000000UL +#define __DELAY_BACKWARD_COMPATIBLE__ /* to use variables with _delay_u/ms */ #include #define P_RX 0 @@ -16,29 +17,29 @@ #define P_SPKR 2 unsigned short freqs[] = { - 100, - 200, - 250, - 300, - 350, - 400, - 500, - 600, - 700, - 750, - 1000, - 1250, - 1500, - 1750, - 2000, - 3000, - 4000, - 5000, - 6000, - 7000, - 8000 + 1 / 100 * 1e6 / 2, + 1 / 200 * 1e6 / 2, + 1 / 250 * 1e6 / 2, + 1 / 300 * 1e6 / 2, + 1 / 350 * 1e6 / 2, + 1 / 400 * 1e6 / 2, + 1 / 500 * 1e6 / 2, + 1 / 600 * 1e6 / 2, + 1 / 700 * 1e6 / 2, + 1 / 750 * 1e6 / 2, + 1 / 1000 * 1e6 / 2, + 1 / 1250 * 1e6 / 2, + 1 / 1500 * 1e6 / 2, + 1 / 1750 * 1e6 / 2, + 1 / 2000 * 1e6 / 2, + 1 / 3000 * 1e6 / 2, + 1 / 4000 * 1e6 / 2, + 1 / 5000 * 1e6 / 2, + 1 / 6000 * 1e6 / 2, + 1 / 7000 * 1e6 / 2, + 1 / 8000 * 1e6 / 2 }; -unsigned char note = 0; +unsigned char note = 8; unsigned char buttonpressed = 1; int @@ -57,8 +58,10 @@ main(void) //for (;;); for (unsigned char n = 0;; n = (n + 1) % 2) { - if (!buttonpressed) continue; - _delay_us(71.43); + if (!buttonpressed && n == 0) continue; + _delay_us(freqs[note]); +// _delay_us(714.2857); +// _delay_us(70); PORTD ^= 1 << P_SPKR; } }