-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_uart_tx.c
55 lines (42 loc) · 841 Bytes
/
sw_uart_tx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <avr/io.h>
#include <stdio.h>
#include <stdint.h>
#include <util/delay.h>
#include "sw_uart_tx.h"
/*
Quick and dirty implementation of a basic software UART (TX only)
(c) 2022 by kittennbfive
AGPLv3+ and NO WARRANTY!
*/
#define BITDELAY_US 26 //this needs to be an integer! 26us is the lowest value for a standard baudrate (38400)
#define UART_LOW UART_PORT&=~(1<<UART_OUT)
#define UART_HIGH UART_PORT|=(1<<UART_OUT)
#define UART_DELAY _delay_us(BITDELAY_US)
void sw_uart_tx_init(void)
{
UART_DDR|=(1<<UART_OUT);
UART_HIGH; //idle high
UART_DELAY;
}
int sw_uart_putchar(char c, FILE *stream)
{
(void)stream;
//startbit
UART_LOW;
UART_DELAY;
//data
uint8_t i;
for(i=0; i<8; i++)
{
if(c&(1<<0))
UART_HIGH;
else
UART_LOW;
UART_DELAY;
c>>=1;
}
//stopbit
UART_HIGH;
UART_DELAY;
return 0;
}