Trammell Hudson avatar Trammell Hudson committed 863be8c

Convert to a cosine filter and use the timer for phase detection

Comments (0)

Files changed (3)

 use warnings;
 use strict;
 
-print "static const uint8_t sin_table[] = {\n";
 my $pi = 2.0 * atan2(1,0);
 my $bits = 5;
 my $count = 128;
-my $scale = 1 << ($bits - 1);
+#my $scale = 1 << ($bits - 1);
+my $scale = 128;
+
+print "static const int8_t sin_table[] = {\n";
 
 for my $t (0..$count-1)
 {
 	$i = -$scale		if $i < -$scale;
 	$i =  $scale - 1	if $i >= $scale;
 
-	printf "\t%d, // t=%d -> %.4f\n", $i + $scale, $t, $s;
+	printf "\t%d, // t=%d -> %.4f\n", $i, $t, $s;
 }
 
 print "};\n";
 
 static uint8_t phase;
 
-// 833 Hz / 31.25 Hz == 27 cycles per bit
-static const uint16_t bit_width = 27 * 128;
 
+static void
+output_bit(
+	uint8_t bit
+)
+{
+	for (uint8_t cycle = 0 ; cycle < 16 ; cycle++)
+	{
+		// If we are outputting a zero, swap the phase
+		if (cycle == 8 && bit == 0)
+			phase += 64;
 
-static void output_constant(void)
-{
-	for (uint16_t i = 0 ; i < bit_width ; i++)
-	{
-		const uint8_t s = sin_table[phase++ & 127];
-		uint8_t spin = 32;
-		while(spin--)
-			PORTB = s;
+		for (uint8_t i = 0 ; i < 128 ; i++)
+		{
+			int16_t s = sin_table[(i + phase) & 127];
+			int16_t c = 128;
+
+			// if we are outputting a zero,
+			// multiple the scale of this cycle
+			// by the cosine of the cycle.
+			if (bit == 0)
+				c = sin_table[64 + cycle * 4 + i / 16];
+
+			// Multiply by the cosine and add a DC offset
+			// to get an unsigned value
+			uint16_t u = (s * c) + 128 * 128;
+
+			// Convert to an unsigned 5 bit value with dc offset
+			PORTB = u >> 11;
+
+			OCR1A += 250; // 16 MHz / 128 / 250 == 500 Hz
+			TIFR1 |= 1 << OCF1A; // clear the OCF1A timer
+			
+			while ((TIFR1 & (1 << OCF1A)) == 0)
+				;
+		}
 	}
 }
 
-
+#if 0
 static void output_bit(unsigned bit)
 {
 	// 1 bits go out with no change in phase or amplitude
-	if (bit)
+	if (1 || bit)
 	{
-		output_constant();
+		output_bit_constant();
 		return;
 	}
 
 			PORTB = s;
 	}
 }
+#endif
 
 
 static void output_word(uint16_t word)
 	// Port B has 6 bits of output on pins 8-13
 	DDRB = 0x3F;
 
+	// Timer1 at clk/1
+	TCCR1B = 1;
+
 	// Clock is 16 MHz
 	// 512 Hz sine wave is 32768 instructions per cycle
 	// 6 bits of output == 64 steps from 0 to 1
-static const uint8_t sin_table[] = {
-	16, // t=0 -> 0.0000
-	16, // t=1 -> 0.0491
-	17, // t=2 -> 0.0980
+static const int8_t sin_table[] = {
+	0, // t=0 -> 0.0000
+	6, // t=1 -> 0.0491
+	12, // t=2 -> 0.0980
 	18, // t=3 -> 0.1467
-	19, // t=4 -> 0.1951
-	19, // t=5 -> 0.2430
-	20, // t=6 -> 0.2903
-	21, // t=7 -> 0.3369
-	22, // t=8 -> 0.3827
-	22, // t=9 -> 0.4276
-	23, // t=10 -> 0.4714
-	24, // t=11 -> 0.5141
-	24, // t=12 -> 0.5556
-	25, // t=13 -> 0.5957
-	26, // t=14 -> 0.6344
-	26, // t=15 -> 0.6716
-	27, // t=16 -> 0.7071
-	27, // t=17 -> 0.7410
-	28, // t=18 -> 0.7730
-	28, // t=19 -> 0.8032
-	29, // t=20 -> 0.8315
-	29, // t=21 -> 0.8577
-	30, // t=22 -> 0.8819
-	30, // t=23 -> 0.9040
-	30, // t=24 -> 0.9239
-	31, // t=25 -> 0.9415
-	31, // t=26 -> 0.9569
-	31, // t=27 -> 0.9700
-	31, // t=28 -> 0.9808
-	31, // t=29 -> 0.9892
-	31, // t=30 -> 0.9952
-	31, // t=31 -> 0.9988
-	31, // t=32 -> 1.0000
-	31, // t=33 -> 0.9988
-	31, // t=34 -> 0.9952
-	31, // t=35 -> 0.9892
-	31, // t=36 -> 0.9808
-	31, // t=37 -> 0.9700
-	31, // t=38 -> 0.9569
-	31, // t=39 -> 0.9415
-	30, // t=40 -> 0.9239
-	30, // t=41 -> 0.9040
-	30, // t=42 -> 0.8819
-	29, // t=43 -> 0.8577
-	29, // t=44 -> 0.8315
-	28, // t=45 -> 0.8032
-	28, // t=46 -> 0.7730
-	27, // t=47 -> 0.7410
-	27, // t=48 -> 0.7071
-	26, // t=49 -> 0.6716
-	26, // t=50 -> 0.6344
-	25, // t=51 -> 0.5957
-	24, // t=52 -> 0.5556
-	24, // t=53 -> 0.5141
-	23, // t=54 -> 0.4714
-	22, // t=55 -> 0.4276
-	22, // t=56 -> 0.3827
-	21, // t=57 -> 0.3369
-	20, // t=58 -> 0.2903
-	19, // t=59 -> 0.2430
-	19, // t=60 -> 0.1951
+	24, // t=4 -> 0.1951
+	31, // t=5 -> 0.2430
+	37, // t=6 -> 0.2903
+	43, // t=7 -> 0.3369
+	48, // t=8 -> 0.3827
+	54, // t=9 -> 0.4276
+	60, // t=10 -> 0.4714
+	65, // t=11 -> 0.5141
+	71, // t=12 -> 0.5556
+	76, // t=13 -> 0.5957
+	81, // t=14 -> 0.6344
+	85, // t=15 -> 0.6716
+	90, // t=16 -> 0.7071
+	94, // t=17 -> 0.7410
+	98, // t=18 -> 0.7730
+	102, // t=19 -> 0.8032
+	106, // t=20 -> 0.8315
+	109, // t=21 -> 0.8577
+	112, // t=22 -> 0.8819
+	115, // t=23 -> 0.9040
+	118, // t=24 -> 0.9239
+	120, // t=25 -> 0.9415
+	122, // t=26 -> 0.9569
+	124, // t=27 -> 0.9700
+	125, // t=28 -> 0.9808
+	126, // t=29 -> 0.9892
+	127, // t=30 -> 0.9952
+	127, // t=31 -> 0.9988
+	127, // t=32 -> 1.0000
+	127, // t=33 -> 0.9988
+	127, // t=34 -> 0.9952
+	126, // t=35 -> 0.9892
+	125, // t=36 -> 0.9808
+	124, // t=37 -> 0.9700
+	122, // t=38 -> 0.9569
+	120, // t=39 -> 0.9415
+	118, // t=40 -> 0.9239
+	115, // t=41 -> 0.9040
+	112, // t=42 -> 0.8819
+	109, // t=43 -> 0.8577
+	106, // t=44 -> 0.8315
+	102, // t=45 -> 0.8032
+	98, // t=46 -> 0.7730
+	94, // t=47 -> 0.7410
+	90, // t=48 -> 0.7071
+	85, // t=49 -> 0.6716
+	81, // t=50 -> 0.6344
+	76, // t=51 -> 0.5957
+	71, // t=52 -> 0.5556
+	65, // t=53 -> 0.5141
+	60, // t=54 -> 0.4714
+	54, // t=55 -> 0.4276
+	48, // t=56 -> 0.3827
+	43, // t=57 -> 0.3369
+	37, // t=58 -> 0.2903
+	31, // t=59 -> 0.2430
+	24, // t=60 -> 0.1951
 	18, // t=61 -> 0.1467
-	17, // t=62 -> 0.0980
-	16, // t=63 -> 0.0491
-	16, // t=64 -> 0.0000
-	15, // t=65 -> -0.0491
-	14, // t=66 -> -0.0980
-	13, // t=67 -> -0.1467
-	12, // t=68 -> -0.1951
-	12, // t=69 -> -0.2430
-	11, // t=70 -> -0.2903
-	10, // t=71 -> -0.3369
-	9, // t=72 -> -0.3827
-	9, // t=73 -> -0.4276
-	8, // t=74 -> -0.4714
-	7, // t=75 -> -0.5141
-	7, // t=76 -> -0.5556
-	6, // t=77 -> -0.5957
-	5, // t=78 -> -0.6344
-	5, // t=79 -> -0.6716
-	4, // t=80 -> -0.7071
-	4, // t=81 -> -0.7410
-	3, // t=82 -> -0.7730
-	3, // t=83 -> -0.8032
-	2, // t=84 -> -0.8315
-	2, // t=85 -> -0.8577
-	1, // t=86 -> -0.8819
-	1, // t=87 -> -0.9040
-	1, // t=88 -> -0.9239
-	0, // t=89 -> -0.9415
-	0, // t=90 -> -0.9569
-	0, // t=91 -> -0.9700
-	0, // t=92 -> -0.9808
-	0, // t=93 -> -0.9892
-	0, // t=94 -> -0.9952
-	0, // t=95 -> -0.9988
-	0, // t=96 -> -1.0000
-	0, // t=97 -> -0.9988
-	0, // t=98 -> -0.9952
-	0, // t=99 -> -0.9892
-	0, // t=100 -> -0.9808
-	0, // t=101 -> -0.9700
-	0, // t=102 -> -0.9569
-	0, // t=103 -> -0.9415
-	1, // t=104 -> -0.9239
-	1, // t=105 -> -0.9040
-	1, // t=106 -> -0.8819
-	2, // t=107 -> -0.8577
-	2, // t=108 -> -0.8315
-	3, // t=109 -> -0.8032
-	3, // t=110 -> -0.7730
-	4, // t=111 -> -0.7410
-	4, // t=112 -> -0.7071
-	5, // t=113 -> -0.6716
-	5, // t=114 -> -0.6344
-	6, // t=115 -> -0.5957
-	7, // t=116 -> -0.5556
-	7, // t=117 -> -0.5141
-	8, // t=118 -> -0.4714
-	9, // t=119 -> -0.4276
-	9, // t=120 -> -0.3827
-	10, // t=121 -> -0.3369
-	11, // t=122 -> -0.2903
-	12, // t=123 -> -0.2430
-	12, // t=124 -> -0.1951
-	13, // t=125 -> -0.1467
-	14, // t=126 -> -0.0980
-	15, // t=127 -> -0.0491
+	12, // t=62 -> 0.0980
+	6, // t=63 -> 0.0491
+	0, // t=64 -> 0.0000
+	-6, // t=65 -> -0.0491
+	-12, // t=66 -> -0.0980
+	-18, // t=67 -> -0.1467
+	-24, // t=68 -> -0.1951
+	-31, // t=69 -> -0.2430
+	-37, // t=70 -> -0.2903
+	-43, // t=71 -> -0.3369
+	-48, // t=72 -> -0.3827
+	-54, // t=73 -> -0.4276
+	-60, // t=74 -> -0.4714
+	-65, // t=75 -> -0.5141
+	-71, // t=76 -> -0.5556
+	-76, // t=77 -> -0.5957
+	-81, // t=78 -> -0.6344
+	-85, // t=79 -> -0.6716
+	-90, // t=80 -> -0.7071
+	-94, // t=81 -> -0.7410
+	-98, // t=82 -> -0.7730
+	-102, // t=83 -> -0.8032
+	-106, // t=84 -> -0.8315
+	-109, // t=85 -> -0.8577
+	-112, // t=86 -> -0.8819
+	-115, // t=87 -> -0.9040
+	-118, // t=88 -> -0.9239
+	-120, // t=89 -> -0.9415
+	-122, // t=90 -> -0.9569
+	-124, // t=91 -> -0.9700
+	-125, // t=92 -> -0.9808
+	-126, // t=93 -> -0.9892
+	-127, // t=94 -> -0.9952
+	-127, // t=95 -> -0.9988
+	-128, // t=96 -> -1.0000
+	-127, // t=97 -> -0.9988
+	-127, // t=98 -> -0.9952
+	-126, // t=99 -> -0.9892
+	-125, // t=100 -> -0.9808
+	-124, // t=101 -> -0.9700
+	-122, // t=102 -> -0.9569
+	-120, // t=103 -> -0.9415
+	-118, // t=104 -> -0.9239
+	-115, // t=105 -> -0.9040
+	-112, // t=106 -> -0.8819
+	-109, // t=107 -> -0.8577
+	-106, // t=108 -> -0.8315
+	-102, // t=109 -> -0.8032
+	-98, // t=110 -> -0.7730
+	-94, // t=111 -> -0.7410
+	-90, // t=112 -> -0.7071
+	-85, // t=113 -> -0.6716
+	-81, // t=114 -> -0.6344
+	-76, // t=115 -> -0.5957
+	-71, // t=116 -> -0.5556
+	-65, // t=117 -> -0.5141
+	-60, // t=118 -> -0.4714
+	-54, // t=119 -> -0.4276
+	-48, // t=120 -> -0.3827
+	-43, // t=121 -> -0.3369
+	-37, // t=122 -> -0.2903
+	-31, // t=123 -> -0.2430
+	-24, // t=124 -> -0.1951
+	-18, // t=125 -> -0.1467
+	-12, // t=126 -> -0.0980
+	-6, // t=127 -> -0.0491
 };
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.