Commits

Trammell Hudson committed 8ce537b

Better times and ramp down

Comments (0)

Files changed (1)

 
 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(unsigned bit)
+static void output_constant(void)
 {
-	// Phase shift on zero-bits
-	if (!bit)
-		phase = (phase + 64) & 127;
-
-	// 833 Hz / 31.25 Hz == 27 cycles per bit
-	const uint16_t bit_width = 27 * 128;
-
 	for (uint16_t i = 0 ; i < bit_width ; i++)
 	{
-#if 1
 		const uint8_t s = sin_table[phase++ & 127];
-#else
-		const uint8_t s = bit ? 0x3F : 0x00;
-#endif
-
 		uint8_t spin = 32;
 		while(spin--)
 			PORTB = s;
 }
 
 
+static void output_bit(unsigned bit)
+{
+	// 1 bits go out with no change in phase or amplitude
+	if (bit)
+	{
+		output_constant();
+		return;
+	}
+
+	// 0 bits ramp down to zero amplitude and then back up
+	for (int16_t i = 0 ; i < bit_width/2 ; 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 * ((bit_width/2) - i)) >> 11;
+		// 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 < bit_width/2 ; 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) >> 11;
+		// Shift it back to the DC offset
+		s += 16;
+
+		uint8_t spin = 28;
+		while(spin--)
+			PORTB = s;
+	}
+}
+
+
 static void output_word(uint16_t word)
 {
 	while (word)
 	{
-		unsigned bit = word & 0x80;
+		unsigned bit = word & 0x8000;
 		word <<= 1;
 
 		output_bit(bit);