ATtiny85 Template Code
Code snippets for the ATtiny85
Main Page
Files
File List
Globals
All
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
uart_send.c
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*
2
* UART interface implementation for ATmega
3
*---------------------------------------------------------------------------*
4
* 01-Apr-2014 ShaneG
5
*
6
* Half-duplex 8N1 serial UART in software.
7
*
8
* 1%/2% Tx/Rx timing error for 115.2kbps@8Mhz
9
* 2%/1% Tx/Rx timing error for 230.4kbps@8Mhz
10
*
11
* Uses only one pin on the AVR for both Tx and Rx:
12
*
13
* D1
14
* AVR ----+--|>|-----+----- Tx
15
* | 10K $ R1
16
* +--------(/^\)--- Rx
17
* NPN E C
18
*--------------------------------------------------------------------------*/
19
#include <avr/io.h>
20
#include <avr/interrupt.h>
21
#include "../hardware.h"
22
#include "
uart_defs.h
"
23
#include "
softuart.h
"
24
25
// Only if enabled
26
#ifdef UART_ENABLED
27
30
void
uartInit
() {
31
// Set as input and disable pullup
32
DDRB &= ~(1 <<
UART_RX
);
33
PORTB &= ~(1 <<
UART_RX
);
34
#ifdef UART_TWOPIN
35
// Set up TX pin
36
DDRB |= (1 <<
UART_TX
);
37
PORTB |= (1 <<
UART_TX
);
38
# ifdef UART_INTERRUPT
39
// Enable pin change interrupts
40
PCMSK |= (1 <<
UART_RX
);
41
GIMSK |= (1 << PCIE);
42
# endif
/* UART_INTERRUPT */
43
#endif
/* UART_TWOPIN */
44
}
45
52
void
uartSend
(
char
ch) {
53
// Set to output state and bring high
54
PORTB |= (1 <<
UART_TX
);
55
#ifdef UART_ONEPIN
56
DDRB |= (1 <<
UART_TX
);
57
#endif
58
cli();
59
asm
volatile
(
60
" cbi %[uart_port], %[uart_pin] \n\t"
// start bit
61
" in r0, %[uart_port] \n\t"
62
" ldi r30, 3 \n\t"
// stop bit + idle state
63
" ldi r28, %[txdelay] \n\t"
64
"TxLoop: \n\t"
65
// 8 cycle loop + delay - total = 7 + 3*r22
66
" mov r29, r28 \n\t"
67
"TxDelay: \n\t"
68
// delay (3 cycle * delayCount) - 1
69
" dec r29 \n\t"
70
" brne TxDelay \n\t"
71
" bst %[ch], 0 \n\t"
72
" bld r0, %[uart_pin] \n\t"
73
" lsr r30 \n\t"
74
" ror %[ch] \n\t"
75
" out %[uart_port], r0 \n\t"
76
" brne TxLoop \n\t"
77
:
78
: [uart_port]
"I"
(_SFR_IO_ADDR(PORTB)),
79
[uart_pin]
"I"
(
UART_TX
),
80
[txdelay]
"I"
(TXDELAY),
81
[ch]
"r"
(ch)
82
:
"r0"
,
"r28"
,
"r29"
,
"r30"
);
83
sei();
84
#ifdef UART_ONEPIN
85
// Change back to idle state
86
DDRB &= ~(1 <<
UART_TX
);
87
PORTB &= ~(1 <<
UART_TX
);
88
#endif
89
}
90
91
#endif
/* UART_ENABLED */
92
uartInit
void uartInit()
Definition:
uart_send.c:30
UART_TX
#define UART_TX
Definition:
hardware.h:58
UART_RX
#define UART_RX
Definition:
hardware.h:65
uart_defs.h
softuart.h
uartSend
void uartSend(char ch)
Definition:
uart_send.c:52
shared
uart_send.c
Generated on Wed Jun 4 2014 17:16:48 for ATtiny85 Template Code by
1.8.6