Skip to content

Commit

Permalink
switch to frequency correct mode and update bpm register values
Browse files Browse the repository at this point in the history
  • Loading branch information
rabidaudio committed Oct 8, 2022
1 parent 7aa7a69 commit b9eb0ae
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 129 deletions.
5 changes: 3 additions & 2 deletions clock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ States:
- Restart subdivisions
- Restore default settings

- Rate CV input
- Less jittery timer
- Get working on bare ATMega328p
- Faster speeds with negative subdivisions
- Rate CV input
- New knob system
- Get working on bare ATMega328p

https://en.wikipedia.org/wiki/Swing_(jazz_performance_style)
https://github.com/adafruit/TinyWireM/blob/master/USI_TWI_Master.h
258 changes: 132 additions & 126 deletions clock/src/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,135 +6,137 @@
#define DEFAULT_SUBDIVISIONS 2
#define DEFAULT_SWING 0

// Lookup table for OCR1A register values for BPMs from 15 to 500.
// Lookup table for OCR1A register values for BPMs from 8 to 1200.
// This is to avoid floating point math.
// Calculations:
// https://docs.google.com/spreadsheets/d/1pTg9IQDEw8LUGN85Lwp80kAzATN1I9yxx0m5xDrEWBg/edit#gid=0
const PROGMEM uint16_t BPM_LOOKUP[] = {
62499, 58592, 55146, 52082, 49341, 46874, 44641, 42612, 40759, 39061,
37499, 36056, 34721, 33481, 32326, 31249, 30240, 29295, 28408, 27572,
26784, 26040, 25336, 24670, 24037, 23436, 22864, 22320, 21801, 21305,
20832, 20379, 19945, 19530, 19131, 18749, 18381, 18027, 17687, 17360,
17044, 16740, 16446, 16162, 15888, 15624, 15367, 15119, 14879, 14647,
14422, 14203, 13991, 13785, 13585, 13391, 13203, 13019, 12841, 12667,
12499, 12334, 12174, 12018, 11866, 11717, 11573, 11431, 11294, 11159,
11028, 10900, 10774, 10652, 10532, 10415, 10301, 10189, 10079, 9972,
9867, 9764, 9663, 9565, 9468, 9374, 9281, 9190, 9100, 9013,
8927, 8843, 8760, 8679, 8599, 8521, 8444, 8369, 8295, 8222,
8151, 8080, 8011, 7943, 7877, 7811, 7746, 7683, 7620, 7559,
7499, 7439, 7380, 7323, 7266, 7210, 7155, 7101, 7047, 6995,
6943, 6892, 6842, 6792, 6743, 6695, 6647, 6601, 6554, 6509,
6464, 6420, 6376, 6333, 6290, 6249, 6207, 6166, 6126, 6086,
6047, 6008, 5970, 5932, 5895, 5858, 5821, 5786, 5750, 5715,
5680, 5646, 5612, 5579, 5546, 5513, 5481, 5449, 5418, 5386,
5356, 5325, 5295, 5265, 5236, 5207, 5178, 5150, 5121, 5094,
5066, 5039, 5012, 4985, 4959, 4933, 4907, 4881, 4856, 4831,
4806, 4782, 4757, 4733, 4710, 4686, 4663, 4640, 4617, 4594,
4572, 4549, 4527, 4506, 4484, 4463, 4442, 4421, 4400, 4379,
4359, 4339, 4319, 4299, 4279, 4260, 4241, 4221, 4203, 4184,
4165, 4147, 4128, 4110, 4092, 4075, 4057, 4039, 4022, 4005,
3988, 3971, 3954, 3938, 3921, 3905, 3889, 3872, 3857, 3841,
3825, 3809, 3794, 3779, 3764, 3749, 3734, 3719, 3704, 3689,
3675, 3661, 3646, 3632, 3618, 3604, 3590, 3577, 3563, 3550,
3536, 3523, 3510, 3497, 3484, 3471, 3458, 3445, 3433, 3420,
3408, 3395, 3383, 3371, 3359, 3347, 3335, 3323, 3311, 3300,
3288, 3276, 3265, 3254, 3242, 3231, 3220, 3209, 3198, 3187,
3176, 3166, 3155, 3144, 3134, 3124, 3113, 3103, 3093, 3082,
3072, 3062, 3052, 3042, 3032, 3023, 3013, 3003, 2994, 2984,
2975, 2965, 2956, 2947, 2937, 2928, 2919, 2910, 2901, 2892,
2883, 2874, 2865, 2857, 2848, 2839, 2831, 2822, 2814, 2805,
2797, 2789, 2780, 2772, 2764, 2756, 2748, 2740, 2732, 2724,
2716, 2708, 2700, 2692, 2685, 2677, 2669, 2662, 2654, 2647,
2639, 2632, 2625, 2617, 2610, 2603, 2595, 2588, 2581, 2574,
2567, 2560, 2553, 2546, 2539, 2532, 2525, 2519, 2512, 2505,
2499, 2492, 2485, 2479, 2472, 2466, 2459, 2453, 2446, 2440,
2434, 2427, 2421, 2415, 2409, 2402, 2396, 2390, 2384, 2378,
2372, 2366, 2360, 2354, 2348, 2342, 2336, 2331, 2325, 2319,
2313, 2308, 2302, 2296, 2291, 2285, 2280, 2274, 2268, 2263,
2258, 2252, 2247, 2241, 2236, 2231, 2225, 2220, 2215, 2210,
2204, 2199, 2194, 2189, 2184, 2179, 2174, 2169, 2164, 2159,
2154, 2149, 2144, 2139, 2134, 2129, 2124, 2120, 2115, 2110,
2105, 2101, 2096, 2091, 2086, 2082, 2077, 2073, 2068, 2063,
2059, 2054, 2050, 2045, 2041, 2037, 2032, 2028, 2023, 2019,
2015, 2010, 2006, 2002, 1997, 1993, 1989, 1985, 1981, 1976,
1972, 1968, 1964, 1960, 1956, 1952, 1948, 1944, 1939, 1935,
1931, 1928, 1924, 1920, 1916, 1912, 1908, 1904, 1900, 1896,
1892, 1889, 1885, 1881, 1877, 1874, 1870, 1866, 1862, 1859,
1855, 1851, 1848, 1844, 1840, 1837, 1833, 1830, 1826, 1822,
1819, 1815, 1812, 1808, 1805, 1801, 1798, 1794, 1791, 1788,
1784, 1781, 1777, 1774, 1771, 1767, 1764, 1761, 1757, 1754,
1751, 1748, 1744, 1741, 1738, 1735, 1731, 1728, 1725, 1722,
1719, 1716, 1712, 1709, 1706, 1703, 1700, 1697, 1694, 1691,
1688, 1685, 1682, 1679, 1676, 1673, 1670, 1667, 1664, 1661,
1658, 1655, 1652, 1649, 1646, 1643, 1640, 1637, 1635, 1632,
1629, 1626, 1623, 1620, 1618, 1615, 1612, 1609, 1607, 1604,
1601, 1598, 1596, 1593, 1590, 1587, 1585, 1582, 1579, 1577,
1574, 1571, 1569, 1566, 1564, 1561, 1558, 1556, 1553, 1551,
1548, 1546, 1543, 1540, 1538, 1535, 1533, 1530, 1528, 1525,
1523, 1520, 1518, 1515, 1513, 1511, 1508, 1506, 1503, 1501,
1499, 1496, 1494, 1491, 1489, 1487, 1484, 1482, 1480, 1477,
1475, 1473, 1470, 1468, 1466, 1463, 1461, 1459, 1457, 1454,
1452, 1450, 1447, 1445, 1443, 1441, 1439, 1436, 1434, 1432,
1430, 1428, 1425, 1423, 1421, 1419, 1417, 1415, 1413, 1410,
1408, 1406, 1404, 1402, 1400, 1398, 1396, 1394, 1392, 1389,
1387, 1385, 1383, 1381, 1379, 1377, 1375, 1373, 1371, 1369,
1367, 1365, 1363, 1361, 1359, 1357, 1355, 1353, 1351, 1349,
1347, 1345, 1344, 1342, 1340, 1338, 1336, 1334, 1332, 1330,
1328, 1326, 1325, 1323, 1321, 1319, 1317, 1315, 1313, 1312,
1310, 1308, 1306, 1304, 1302, 1301, 1299, 1297, 1295, 1293,
1292, 1290, 1288, 1286, 1285, 1283, 1281, 1279, 1277, 1276,
1274, 1272, 1271, 1269, 1267, 1265, 1264, 1262, 1260, 1259,
1257, 1255, 1254, 1252, 1250, 1249, 1247, 1245, 1244, 1242,
1240, 1239, 1237, 1235, 1234, 1232, 1230, 1229, 1227, 1226,
1224, 1222, 1221, 1219, 1218, 1216, 1214, 1213, 1211, 1210,
1208, 1207, 1205, 1204, 1202, 1200, 1199, 1197, 1196, 1194,
1193, 1191, 1190, 1188, 1187, 1185, 1184, 1182, 1181, 1179,
1178, 1176, 1175, 1173, 1172, 1170, 1169, 1167, 1166, 1165,
1163, 1162, 1160, 1159, 1157, 1156, 1154, 1153, 1152, 1150,
1149, 1147, 1146, 1145, 1143, 1142, 1140, 1139, 1138, 1136,
1135, 1133, 1132, 1131, 1129, 1128, 1127, 1125, 1124, 1123,
1121, 1120, 1119, 1117, 1116, 1115, 1113, 1112, 1111, 1109,
1108, 1107, 1105, 1104, 1103, 1101, 1100, 1099, 1098, 1096,
1095, 1094, 1092, 1091, 1090, 1089, 1087, 1086, 1085, 1084,
1082, 1081, 1080, 1079, 1077, 1076, 1075, 1074, 1072, 1071,
// 1070, 1069, 1067, 1066, 1065, 1064, 1063, 1061, 1060, 1059,
// 1058, 1057, 1055, 1054, 1053, 1052, 1051, 1050, 1048, 1047,
// 1046, 1045, 1044, 1042, 1041, 1040, 1039, 1038, 1037, 1036,
// 1034, 1033, 1032, 1031, 1030, 1029, 1028, 1026, 1025, 1024,
// 1023, 1022, 1021, 1020, 1019, 1018, 1016, 1015, 1014, 1013,
// 1012, 1011, 1010, 1009, 1008, 1007, 1005, 1004, 1003, 1002,
// 1001, 1000, 999, 998, 997, 996, 995, 994, 993, 992,
// 991, 990, 988, 987, 986, 985, 984, 983, 982, 981,
// 980, 979, 978, 977, 976, 975, 974, 973, 972, 971,
// 970, 969, 968, 967, 966, 965, 964, 963, 962, 961,
// 960, 959, 958, 957, 956, 955, 954, 953, 952, 951,
// 950, 949, 948, 947, 946, 945, 945, 944, 943, 942,
// 941, 940, 939, 938, 937, 936, 935, 934, 933, 932,
// 931, 930, 929, 929, 928, 927, 926, 925, 924, 923,
// 922, 921, 920, 919, 919, 918, 917, 916, 915, 914,
// 913, 912, 911, 910, 910, 909, 908, 907, 906, 905,
// 904, 903, 903, 902, 901, 900, 899, 898, 897, 896,
// 896, 895, 894, 893, 892, 891, 891, 890, 889, 888,
// 887, 886, 885, 885, 884, 883, 882, 881, 880, 880,
// 879, 878, 877, 876, 875, 875, 874, 873, 872, 871,
// 871, 870, 869, 868, 867, 867, 866, 865, 864, 863,
// 863, 862, 861, 860, 859, 859, 858, 857, 856, 855,
// 855, 854, 853, 852, 852, 851, 850, 849, 848, 848,
// 847, 846, 845, 845, 844, 843, 842, 842, 841, 840,
// 839, 839, 838, 837, 836, 836, 835, 834, 833, 833,
// 832, 831, 830, 830, 829, 828, 827, 827, 826, 825,
// 824, 824, 823, 822, 822, 821, 820, 819, 819, 818,
// 817, 817, 816, 815, 814, 814, 813, 812, 812, 811,
// 810, 809, 809, 808, 807, 807, 806, 805, 805, 804,
// 803, 803, 802, 801, 800, 800, 799, 798, 798, 797,
// 796, 796, 795, 794, 794, 793, 792, 792, 791, 790,
// 790, 789, 788, 788, 787, 786, 786, 785, 784, 784,
// 783, 782, 782, 781, 780, 780,
/*117187, 93750, 78125, 66964,*/ 58593, 52083, 46875, 42613, 39062, 36057,
33482, 31250, 29296, 27573, 26041, 24671, 23437, 22321, 21306, 20380,
19531, 18750, 18028, 17361, 16741, 16163, 15625, 15120, 14648, 14204,
13786, 13392, 13020, 12668, 12335, 12019, 11718, 11432, 11160, 10901,
10653, 10416, 10190, 9973, 9765, 9566, 9375, 9191, 9014, 8844,
8680, 8522, 8370, 8223, 8081, 7944, 7812, 7684, 7560, 7440,
7324, 7211, 7102, 6996, 6893, 6793, 6696, 6602, 6510, 6421,
6334, 6250, 6167, 6087, 6009, 5933, 5859, 5787, 5716, 5647,
5580, 5514, 5450, 5387, 5326, 5266, 5208, 5151, 5095, 5040,
4986, 4934, 4882, 4832, 4783, 4734, 4687, 4641, 4595, 4550,
4507, 4464, 4422, 4380, 4340, 4300, 4261, 4222, 4185, 4148,
4111, 4076, 4040, 4006, 3972, 3939, 3906, 3873, 3842, 3810,
3780, 3750, 3720, 3690, 3662, 3633, 3605, 3578, 3551, 3524,
3498, 3472, 3446, 3421, 3396, 3372, 3348, 3324, 3301, 3277,
3255, 3232, 3210, 3188, 3167, 3145, 3125, 3104, 3083, 3063,
3043, 3024, 3004, 2985, 2966, 2948, 2929, 2911, 2893, 2875,
2858, 2840, 2823, 2806, 2790, 2773, 2757, 2741, 2725, 2709,
2693, 2678, 2663, 2648, 2633, 2618, 2604, 2589, 2575, 2561,
2547, 2533, 2520, 2506, 2493, 2480, 2467, 2454, 2441, 2428,
2416, 2403, 2391, 2379, 2367, 2355, 2343, 2332, 2320, 2309,
2297, 2286, 2275, 2264, 2253, 2242, 2232, 2221, 2211, 2200,
2190, 2180, 2170, 2160, 2150, 2140, 2130, 2121, 2111, 2102,
2092, 2083, 2074, 2064, 2055, 2046, 2038, 2029, 2020, 2011,
2003, 1994, 1986, 1977, 1969, 1961, 1953, 1945, 1936, 1929,
1921, 1913, 1905, 1897, 1890, 1882, 1875, 1867, 1860, 1852,
1845, 1838, 1831, 1823, 1816, 1809, 1802, 1795, 1789, 1782,
1775, 1768, 1762, 1755, 1749, 1742, 1736, 1729, 1723, 1717,
1710, 1704, 1698, 1692, 1686, 1680, 1674, 1668, 1662, 1656,
1650, 1644, 1638, 1633, 1627, 1621, 1616, 1610, 1605, 1599,
1594, 1588, 1583, 1578, 1572, 1567, 1562, 1557, 1552, 1547,
1541, 1536, 1531, 1526, 1521, 1516, 1512, 1507, 1502, 1497,
1492, 1488, 1483, 1478, 1474, 1469, 1464, 1460, 1455, 1451,
1446, 1442, 1437, 1433, 1429, 1424, 1420, 1416, 1411, 1407,
1403, 1399, 1395, 1390, 1386, 1382, 1378, 1374, 1370, 1366,
1362, 1358, 1354, 1350, 1346, 1343, 1339, 1335, 1331, 1327,
1324, 1320, 1316, 1313, 1309, 1305, 1302, 1298, 1294, 1291,
1287, 1284, 1280, 1277, 1273, 1270, 1266, 1263, 1260, 1256,
1253, 1250, 1246, 1243, 1240, 1236, 1233, 1230, 1227, 1223,
1220, 1217, 1214, 1211, 1208, 1205, 1201, 1198, 1195, 1192,
1189, 1186, 1183, 1180, 1177, 1174, 1171, 1168, 1166, 1163,
1160, 1157, 1154, 1151, 1148, 1146, 1143, 1140, 1137, 1134,
1132, 1129, 1126, 1124, 1121, 1118, 1116, 1113, 1110, 1108,
1105, 1102, 1100, 1097, 1095, 1092, 1090, 1087, 1085, 1082,
1080, 1077, 1075, 1072, 1070, 1067, 1065, 1062, 1060, 1058,
1055, 1053, 1051, 1048, 1046, 1043, 1041, 1039, 1037, 1034,
1032, 1030, 1027, 1025, 1023, 1021, 1019, 1016, 1014, 1012,
1010, 1008, 1005, 1003, 1001, 999, 997, 995, 993, 991,
988, 986, 984, 982, 980, 978, 976, 974, 972, 970,
968, 966, 964, 962, 960, 958, 956, 954, 952, 950,
948, 946, 945, 943, 941, 939, 937, 935, 933, 931,
930, 928, 926, 924, 922, 920, 919, 917, 915, 913,
911, 910, 908, 906, 904, 903, 901, 899, 897, 896,
894, 892, 891, 889, 887, 886, 884, 882, 881, 879,
877, 876, 874, 872, 871, 869, 868, 866, 864, 863,
861, 860, 858, 856, 855, 853, 852, 850, 849, 847,
846, 844, 843, 841, 840, 838, 837, 835, 834, 832,
831, 829, 828, 826, 825, 823, 822, 820, 819, 818,
816, 815, 813, 812, 810, 809, 808, 806, 805, 804,
802, 801, 799, 798, 797, 795, 794, 793, 791, 790,
789, 787, 786, 785, 783, 782, 781, 779, 778, 777,
776, 774, 773, 772, 770, 769, 768, 767, 765, 764,
763, 762, 760, 759, 758, 757, 756, 754, 753, 752,
751, 750, 748, 747, 746, 745, 744, 742, 741, 740,
739, 738, 737, 735, 734, 733, 732, 731, 730, 729,
727, 726, 725, 724, 723, 722, 721, 720, 718, 717,
716, 715, 714, 713, 712, 711, 710, 709, 708, 707,
705, 704, 703, 702, 701, 700, 699, 698, 697, 696,
695, 694, 693, 692, 691, 690, 689, 688, 687, 686,
685, 684, 683, 682, 681, 680, 679, 678, 677, 676,
675, 674, 673, 672, 671, 670, 669, 668, 667, 666,
665, 664, 663, 663, 662, 661, 660, 659, 658, 657,
656, 655, 654, 653, 652, 651, 651, 650, 649, 648,
647, 646, 645, 644, 643, 643, 642, 641, 640, 639,
638, 637, 636, 636, 635, 634, 633, 632, 631, 630,
630, 629, 628, 627, 626, 625, 625, 624, 623, 622,
621, 620, 620, 619, 618, 617, 616, 615, 615, 614,
613, 612, 611, 611, 610, 609, 608, 607, 607, 606,
605, 604, 604, 603, 602, 601, 600, 600, 599, 598,
597, 597, 596, 595, 594, 594, 593, 592, 591, 591,
590, 589, 588, 588, 587, 586, 585, 585, 584, 583,
583, 582, 581, 580, 580, 579, 578, 577, 577, 576,
575, 575, 574, 573, 573, 572, 571, 570, 570, 569,
568, 568, 567, 566, 566, 565, 564, 564, 563, 562,
562, 561, 560, 560, 559, 558, 558, 557, 556, 556,
555, 554, 554, 553, 552, 552, 551, 550, 550, 549,
548, 548, 547, 546, 546, 545, 545, 544, 543, 543,
542, 541, 541, 540, 540, 539, 538, 538, 537, 536,
536, 535, 535, 534, 533, 533, 532, 532, 531, 530,
530, 529, 529, 528, 527, 527, 526, 526, 525, 524,
524, 523, 523, 522, 521, 521, 520, 520, 519, 519,
518, 517, 517, 516, 516, 515, 515, 514, 513, 513,
512, 512, 511, 511, 510, 510, 509, 508, 508, 507,
507, 506, 506, 505, 505, 504, 504, 503, 502, 502,
501, 501, 500, 500, 499, 499, 498, 498, 497, 497,
496, 496, 495, 494, 494, 493, 493, 492, 492, 491,
491, 490, 490, 489, 489, 488, 488, 487, 487, 486,
486, 485, 485, 484, 484, 483, 483, 482, 482, 481,
481, 480, 480, 479, 479, 478, 478, 477, 477, 476,
476, 475, 475, 474, 474, 473, 473, 473, 472, 472,
471, 471, 470, 470, 469, 469, 468, 468, 467, 467,
466, 466, 465, 465, 465, 464, 464, 463, 463, 462,
462, 461, 461, 460, 460, 460, 459, 459, 458, 458,
457, 457, 456, 456, 455, 455, 455, 454, 454, 453,
453, 452, 452, 452, 451, 451, 450, 450, 449, 449,
448, 448, 448, 447, 447, 446, 446, 446, 445, 445,
444, 444, 443, 443, 443, 442, 442, 441, 441, 440,
440, 440, 439, 439, 438, 438, 438, 437, 437, 436,
436, 436, 435, 435, 434, 434, 434, 433, 433, 432,
432, 432, 431, 431, 430, 430, 430, 429, 429, 428,
428, 428, 427, 427, 426, 426, 426, 425, 425, 424,
424, 424, 423, 423, 423, 422, 422, 421, 421, 421,
420, 420, 420, 419, 419, 418, 418, 418, 417, 417,
417, 416, 416, 415, 415, 415, 414, 414, 414, 413,
413, 412, 412, 412, 411, 411, 411, 410, 410, 410,
409, 409, 409, 408, 408, 407, 407, 407, 406, 406,
406, 405, 405, 405, 404, 404, 404, 403, 403, 403,
402, 402, 402, 401, 401, 400, 400, 400, 399, 399,
399, 398, 398, 398, 397, 397, 397, 396, 396, 396,
395, 395, 395, 394, 394, 394, 393, 393, 393, 392,
392, 392, 391, 391, 391, 390, 390
};

uint16_t counterValue(uint16_t bpm)
{
return pgm_read_word_near(BPM_LOOKUP + bpm - 15);
bpm = constrain(bpm, 8, 1200);
return pgm_read_word_near(BPM_LOOKUP + bpm - 8);
}

// Core clock source. Async using Timer1.
Expand Down Expand Up @@ -171,8 +173,8 @@ class Timer1
_evenTick = baseValue - offset;

OCR1A = _isEven ? _evenTick : _oddTick;
if (TCNT1 > OCR1A)
TCNT1 = 0; // reset timer
// if (TCNT1 > OCR1A)
// TCNT1 = 0; // reset timer
}

public:
Expand All @@ -192,10 +194,14 @@ class Timer1
// 120 BPM = 2 Hz = (16000000/((7811+1)*1024))
OCR1A = counterValue(_bpm);
OCR1B = 313; // ~20ms
// CTC
TCCR1B |= (1 << WGM12);

//TCCR1B |= (1 << WGM12); // CTC
// Phase and frequency correct PWM
TCCR1A |= (0 << WGM11) | (1 << WGM10);
TCCR1B |= (1 << WGM13) | (0 << WGM12);

// Pre-scaler 1024
TCCR1B |= (1 << CS12) | (1 << CS10);
TCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
// Output Compare Match A Interrupt Enable
TIMSK1 |= (1 << OCIE1A) | (1 << OCIE1B);
interrupts();
Expand Down
2 changes: 1 addition & 1 deletion clock/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void loop()
else
{
uint16_t bpm = Timer.getBPM() + knobMotion;
bpm = constrain(bpm, (uint16_t) 15, (uint16_t) 287); // TODO: increase
bpm = constrain(bpm, (uint16_t) 8, (uint16_t) 1200); // TODO: increase
Timer.setBPM(bpm);
// uint8_t flags = (tapTempo.isActive() ? 0x08 : 0) | (Timer.clockOn() ? 0x01 : 0x00) | (Timer.subdivisionOn() ? 0x02 : 0x00);
display.displayNumber(bpm);
Expand Down

0 comments on commit b9eb0ae

Please sign in to comment.