Skip to content

Instantly share code, notes, and snippets.

@bigjosh
Created January 2, 2017 21:51
Show Gist options
  • Save bigjosh/a7c68768100b8e638968b0c599eaf3d6 to your computer and use it in GitHub Desktop.
Save bigjosh/a7c68768100b8e638968b0c599eaf3d6 to your computer and use it in GitHub Desktop.
Output a byte on a pin as serial data. Good for debugging, especially if your scope as serial decode.
// Print as a serial bit stream on PB2 - Each bit is 17us, just about 38400 baud - LSB first - for debugging.
void sprint( uint8_t x ) {
DDRB |= _BV(2);
PORTB |= _BV(2); // Idle on serial port
_delay_us(22);
PORTB &= ~_BV(PORTB2); // start bit
_delay_us(22);
for(uint8_t b=8;b>0;b--) {
if (x&0b00000001) {
PORTB |= _BV(PORTB2); // start bit
} else {
PORTB &= ~_BV(PORTB2); // stop bit
//_delay_us(1);
_delay_us(3);
}
_delay_us(15);
x>>=1;
}
PORTB |= _BV(PORTB2); // stop bit
_delay_us(26*3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment