aboutsummaryrefslogtreecommitdiffstats
path: root/softserial.c
diff options
context:
space:
mode:
Diffstat (limited to 'softserial.c')
-rw-r--r--softserial.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/softserial.c b/softserial.c
new file mode 100644
index 0000000..cbec672
--- /dev/null
+++ b/softserial.c
@@ -0,0 +1,33 @@
+
+#define baud 9600
+#define serial_bit_delay 1000000/baud
+#define SERIAL_LOW() PORTB &= ~_BV(PB4)
+#define SERIAL_HIGH() PORTB |= _BV(PB4)
+
+// write out a byte as software emulated Uart
+void serial_putchar(uint8_t byte)
+{
+ uint8_t mask;
+ SERIAL_LOW(); // signal start bit
+ _delay_us(serial_bit_delay);
+ for (mask = 0x01; mask; mask <<= 1) {
+ if (byte & mask) { // choose bit
+ SERIAL_HIGH(); // send 1
+ } else {
+ SERIAL_LOW(); // send 0
+ }
+ _delay_us(serial_bit_delay);
+ }
+ SERIAL_HIGH(); //signal end bit
+ _delay_us(serial_bit_delay);
+}
+
+void serial_puts(const char* str)
+{
+ while (*str) {
+ serial_putchar((uint8_t)*str);
+ ++str;
+ }
+ serial_putchar('\r');
+ serial_putchar('\n');
+}