Trammell Hudson avatar Trammell Hudson committed 7e01cb0

Timer based encoder and cosine filtering works

Comments (0)

Files changed (1)

 #include "sin-table.h"
 #include "varicode.h"
 
-static const char msg[] = "ny3u KD4ISF QSO cq 0123456789\n";
+static char msg[] = "ny3u KD4ISF QSO cq 0123456789\n";
 
 static uint8_t phase;
 
 
 static void
 output_bit(
-	uint8_t bit
+	const uint8_t bit
 )
 {
 	for (uint8_t cycle = 0 ; cycle < 16 ; cycle++)
 		for (uint8_t i = 0 ; i < 128 ; i++)
 		{
 			int16_t s = sin_table[(i + phase) & 127];
-			int16_t c = 128;
+			uint16_t u;
 
 			// 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];
+			{
+				int16_t c = cycle < 8
+					? sin_table[32 + cycle * 4 + i / 32]
+					: sin_table[96 - cycle * 4 - i / 32];
 
-			// Multiply by the cosine and add a DC offset
-			// to get an unsigned value
-			uint16_t u = (s * c) + 128 * 128;
+				// Multiply by the cosine and add a DC offset
+				// to get an unsigned value
+				u = (s * c) + 128 * 128;
+			} else {
+				// Just output it straight
+				u = (s + 128) * 128;
+			}
 
 			// Convert to an unsigned 5 bit value with dc offset
 			PORTB = u >> 11;
 
+			// Output this for 500 Hz / 128 clock ticks
 			OCR1A += 250; // 16 MHz / 128 / 250 == 500 Hz
 			TIFR1 |= 1 << OCF1A; // clear the OCF1A timer
 			
 	}
 }
 
-#if 0
-static void output_bit(unsigned bit)
-{
-	// 1 bits go out with no change in phase or amplitude
-	if (1 || bit)
-	{
-		output_bit_constant();
-		return;
-	}
-
-	// 0 bits ramp down to zero amplitude and then back up
-	for (int16_t i = 0 ; i < 1024 ; i++)
-	{
-		int16_t s = (uint16_t) sin_table[phase++ & 127];
-		// Shift it to the center of the waveform
-		s -= 16;
-		// scale it linearly
-		s = (s * (1024 - i)) >> 10;
-		// Shift it back to the DC offset
-		s += 16;
-		uint8_t spin = 28;
-		while(spin--)
-			PORTB = s;
-	}
-	
-	// Shift the phase
-	phase = (phase + 64) & 127;
-
-	// And ramp up down to zero amplitude and then back up
-	for (int16_t i = 0 ; i < 1024 ; i++)
-	{
-		int16_t s = (uint16_t) sin_table[phase++ & 127];
-		// Shift it to the center of the waveform
-		s -= 16;
-		// scale it linearly
-		s = (s * i) >> 10;
-		// Shift it back to the DC offset
-		s += 16;
-
-		uint8_t spin = 28;
-		while(spin--)
-			PORTB = s;
-	}
-
-	// And now constant amplitude to round it out
-	for (int16_t i = 0 ; i < bit_width - 1024 * 2 ; i++)
-	{
-		uint8_t s = sin_table[phase++ & 127];
-		uint8_t spin = 32;
-		while(spin--)
-			PORTB = s;
-	}
-}
-#endif
-
 
 static void output_word(uint16_t word)
 {
 	while (word)
 	{
-		unsigned bit = word & 0x8000;
+		uint8_t bit = (word & 0x8000) ? 1 : 0;
 		word <<= 1;
 
 		output_bit(bit);
+		//output_bit(TCNT1 & 1);
 	}
 
 	// Mark the end ofthe word with two zeros
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.