// Scale the next wave sample and place in centre
newsample = (newsample / 2) + 64;
// generate the BTc bit,
// does the BTc wave need to react UP or DOWN to follow it?
if(newsample > lastbtc)
{
newbit = 1; // BTc must move UP
dist = (256 - lastbtc); // calc total distance to charge
dist = (dist / 4); // BTc4, only charge 1/4 distance
lastbtc = (lastbtc + dist); // where it charges up to
goto done;
}
else
{
newbit = 0; // BTc must move DOWN
dist = lastbtc; // calc total distance to charge
dist = (dist / 4); // BTc4, only charge 1/4 distance
lastbtc = (lastbtc - dist); // where it charges down to
goto done;
}
// Scale the next wave sample and place in centre
newsample = (newsample / 2) + 64;
// generate a "1" (high) outcome
dist = (256 - lastbtc); // calc total distance to charge
dist = (dist / 4); // BTc4, only charge 1/4 distance
highbtc = (lastbtc + dist); // where it charges up to
// generate a "0" (low) outcome
dist = lastbtc; // calc total distance to charge
dist = (dist / 4); // BTc4, only charge 1/4 distance
lowbtc = (lastbtc - dist); // where it charges down to
// calc distance from high outcome (up or down) to new sample
if(highbtc > newsample) disthigh = (highbtc - newsample);
else disthigh = (newsample - highbtc);
// calc distance from low outcome (up or down) to new sample
if(lowbtc > newsample) distlow = (lowbtc - newsample);
else distlow = (newsample - lowbtc);
// see which outcome is closest to new sample and generate the bit
if(disthigh > distlow) // low is closest
{
newbit=0;
lastbtc = lowbtc;
}
else // else high is closest
{
newbit=1;
lastbtc = highbtc;
}
goto done;
interrupt_routine: clrc ; clear carry ready to use it rlf data ; left shift data, puts next bit in carry btfss carry ; test carry and set output pin to match bcf output_pin ; =carry low, so set pin low btfsc carry ; bsf output_pin ; =carry high, so set pin high decfsc bit_count ; count 1 more bit was played goto interrupt_end ; more bits remaining ; have played all 8 bits so load another byte call table ; returns with next byte in W reg movwf data ; put byte in data reg movlw d'8' ; also reset count, to play 8 more bits movwf bit_count ; interrupt_end: