]> Devi Nivas Git - gt-ece4180-lab02.git/commitdiff
Sine Wave Code
authorAdvaith Menon <83835839+advaithm582@users.noreply.github.com>
Mon, 15 Sep 2025 19:08:43 +0000 (15:08 -0400)
committerAdvaith Menon <83835839+advaithm582@users.noreply.github.com>
Mon, 15 Sep 2025 19:08:43 +0000 (15:08 -0400)
* Add sine.py for generating sine wave samples for DAC
* Update ino code to use samples

03-I2CDAC/03-I2CDAC.ino
03-I2CDAC/sine.py [new file with mode: 0755]

index 74f966a4e2fe85eb6097ef342c3d3f2633e66ad8..7b028e183c3e0981b8c5df61c50432c72f63cede 100644 (file)
@@ -1,6 +1,67 @@
+#define PIN_SDA 6
+#define PIN_SCL 7
+#define DAC_RESOLUTION 12
+#define I2C_ADDR 0x60
 
+#include <stdint.h>
+#include <Arduino.h>
+#include <Wire.h>
+//#include <Adafruit_MCP4725.h>
+
+
+const uint16_t lut[512] = 
+{2048, 2073, 2098, 2123, 2148, 2173, 2198, 2223, 2248, 2273, 2298, 2323, 2348, 
+2373, 2398, 2422, 2447, 2472, 2496, 2521, 2545, 2569, 2594, 2618, 2642, 2666, 
+2690, 2714, 2737, 2761, 2785, 2808, 2831, 2854, 2877, 2900, 2923, 2946, 2968, 
+2991, 3013, 3035, 3057, 3079, 3100, 3122, 3143, 3164, 3185, 3206, 3227, 3247, 
+3267, 3288, 3307, 3327, 3347, 3366, 3385, 3404, 3423, 3441, 3460, 3478, 3496, 
+3513, 3531, 3548, 3565, 3582, 3598, 3615, 3631, 3646, 3662, 3677, 3692, 3707, 
+3722, 3736, 3750, 3764, 3778, 3791, 3804, 3817, 3829, 3842, 3854, 3865, 3877, 
+3888, 3899, 3909, 3920, 3930, 3940, 3949, 3958, 3967, 3976, 3984, 3992, 4000, 
+4007, 4014, 4021, 4028, 4034, 4040, 4046, 4051, 4056, 4061, 4065, 4069, 4073, 
+4077, 4080, 4083, 4086, 4088, 4090, 4092, 4093, 4094, 4095, 4095, 4095, 4095, 
+4095, 4094, 4093, 4092, 4090, 4088, 4086, 4083, 4080, 4077, 4073, 4069, 4065, 
+4061, 4056, 4051, 4046, 4040, 4034, 4028, 4021, 4014, 4007, 4000, 3992, 3984, 
+3976, 3967, 3958, 3949, 3940, 3930, 3920, 3909, 3899, 3888, 3877, 3865, 3854, 
+3842, 3829, 3817, 3804, 3791, 3778, 3764, 3750, 3736, 3722, 3707, 3692, 3677, 
+3662, 3646, 3631, 3615, 3598, 3582, 3565, 3548, 3531, 3513, 3496, 3478, 3460, 
+3441, 3423, 3404, 3385, 3366, 3347, 3327, 3307, 3288, 3267, 3247, 3227, 3206, 
+3185, 3164, 3143, 3122, 3100, 3079, 3057, 3035, 3013, 2991, 2968, 2946, 2923, 
+2900, 2877, 2854, 2831, 2808, 2785, 2761, 2737, 2714, 2690, 2666, 2642, 2618, 
+2594, 2569, 2545, 2521, 2496, 2472, 2447, 2422, 2398, 2373, 2348, 2323, 2298, 
+2273, 2248, 2223, 2198, 2173, 2148, 2123, 2098, 2073, 2048, 2022, 1997, 1972, 
+1947, 1922, 1897, 1872, 1847, 1822, 1797, 1772, 1747, 1722, 1697, 1673, 1648, 
+1623, 1599, 1574, 1550, 1526, 1501, 1477, 1453, 1429, 1405, 1381, 1358, 1334, 
+1310, 1287, 1264, 1241, 1218, 1195, 1172, 1149, 1127, 1104, 1082, 1060, 1038, 
+1016, 995, 973, 952, 931, 910, 889, 868, 848, 828, 807, 788, 768, 748, 729, 
+710, 691, 672, 654, 635, 617, 599, 582, 564, 547, 530, 513, 497, 480, 464, 449, 
+433, 418, 403, 388, 373, 359, 345, 331, 317, 304, 291, 278, 266, 253, 241, 230, 
+218, 207, 196, 186, 175, 165, 155, 146, 137, 128, 119, 111, 103, 95, 88, 81, 
+74, 67, 61, 55, 49, 44, 39, 34, 30, 26, 22, 18, 15, 12, 9, 7, 5, 3, 2, 1, 0, 0, 
+0, 0, 0, 1, 2, 3, 5, 7, 9, 12, 15, 18, 22, 26, 30, 34, 39, 44, 49, 55, 61, 67, 
+74, 81, 88, 95, 103, 111, 119, 128, 137, 146, 155, 165, 175, 186, 196, 207, 
+218, 230, 241, 253, 266, 278, 291, 304, 317, 331, 345, 359, 373, 388, 403, 418, 
+433, 449, 464, 480, 497, 513, 530, 547, 564, 582, 599, 617, 635, 654, 672, 691, 
+710, 729, 748, 768, 788, 807, 828, 848, 868, 889, 910, 931, 952, 973, 995, 
+1016, 1038, 1060, 1082, 1104, 1127, 1149, 1172, 1195, 1218, 1241, 1264, 1287, 
+1310, 1334, 1358, 1381, 1405, 1429, 1453, 1477, 1501, 1526, 1550, 1574, 1599, 
+1623, 1648, 1673, 1697, 1722, 1747, 1772, 1797, 1822, 1847, 1872, 1897, 1922, 
+1947, 1972, 1997, 2022};
+
+
+const uint16_t DAC_MAX = (1 << (DAC_RESOLUTION)) - 1;
+uint16_t curr_val;
 void setup() {
+    /* set up I2C pins */
+    Wire.begin(PIN_SDA, PIN_SCL);
+    curr_val = 0;
 }
 
 void loop() {
+    Wire.beginTransmission(I2C_ADDR);
+    Wire.write(64);
+    Wire.write((lut[curr_val]) >> 4);
+    Wire.write((lut[curr_val] & 15) << 5);
+    Wire.endTransmission();
+    curr_val = (curr_val + 1) & 511;
 }
diff --git a/03-I2CDAC/sine.py b/03-I2CDAC/sine.py
new file mode 100755 (executable)
index 0000000..6c9c5b1
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+
+import math as m
+import sys
+from functools import partial
+
+def generate_samples(num):
+    # generates num samples
+    step = (2 * m.pi) / num;
+
+    i = 0
+    samp = []
+    while i < 2 * m.pi:
+        samp.append(m.sin(i));
+        i += step;
+    return samp
+
+def dacify(dacbits, x):
+    x = int((1 + x) * 2 ** (dacbits - 1));
+    if (x == 2 ** dacbits): x -= 1;
+    return x;
+
+def main(argv):
+    if len(argv) < 2:
+        print("usage: [DAC bits] [values]");
+        return;
+
+    vals = int(argv[2]);
+    dacbits = int(argv[1]);
+
+    print(tuple(
+        map(partial(dacify, dacbits), generate_samples(vals))));
+
+if __name__ == "__main__":
+    main(sys.argv);
+