-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_macro.asm
89 lines (79 loc) · 2.2 KB
/
uart_macro.asm
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;------------------------------------------------------------------------------
; Ìàêðîñû äëÿ ðàáîòû ñ UART
;
;
;
; (C) 2017-2020 Vitaliy Zinoviev
; https://github.com/nf-zvv/IVC_Tracer
;------------------------------------------------------------------------------
;USART INIT
.MACRO USART_INIT
.equ XTAL = F_CPU
.equ baudrate = 19200
.equ bauddivider = XTAL/(16*baudrate)-1
#if defined(__ATmega168__) || defined(__ATmega328P__) || defined(__ATmega1284P__)
ldi r16,low(bauddivider)
OutReg UBRR0L,r16
ldi r16,high(bauddivider)
OutReg UBRR0H,r16
ldi r16,0
OutReg UCSR0A,r16
ldi r16,(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(0<<TXCIE0)|(0<<UDRIE0) ; Ïðåðûâàíèå íà ïðèåì ðàçðåøåíî, ïðèåì-ïåðåäà÷à ðàçðåøåíû
OutReg UCSR0B,r16
; Ôîðìàò êàäðà - 8 áèò äàííûõ, 1 ñòîï-áèò
ldi r16,(0<<USBS0)|(3<<UCSZ00)
OutReg UCSR0C,r16
#elif defined(__ATmega8__) || defined(__ATmega16A__) || defined(__ATmega16__)
ldi r16,low(bauddivider)
OutReg UBRRL,r16
ldi r16,high(bauddivider)
OutReg UBRRH,r16
ldi r16,0
OutReg UCSRA,r16
ldi r16,(1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<TXCIE)|(0<<UDRIE) ; Ïðåðûâàíèå íà ïðèåì ðàçðåøåíî, ïðèåì-ïåðåäà÷à ðàçðåøåíû
OutReg UCSRB,r16
; Ôîðìàò êàäðà - 8 áèò äàííûõ, 1 ñòîï-áèò
; Áåç óñòàíîâêè áèòà URSEL ïðèåìî-ïåðåäàò÷èê íå ðàáîòàë
; URSEL âñåãäà ðàâåí 1. Ñëóæèò äëÿ âûáîðà ðåãèñòðà UCSRC, èíà÷å UBRRH.
ldi r16,(1<<URSEL)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)
OutReg UCSRC,r16
#else
#error "Unsupported part:" __PART_NAME__
#endif // part specific code
.ENDM
; Ñáðîñèòü ôëàã
; @0 - èìÿ ÿ÷åéêè ïàìÿòè
; @1 - èìÿ ôëàãà
.MACRO CLFL
lds r19,@0
andi r19,~(1 << @1)
sts @0,r19
.ENDM
; Óñòàíîâèòü ôëàã
; @0 - èìÿ ÿ÷åéêè ïàìÿòè
; @1 - èìÿ ôëàãà
.MACRO STFL
lds r19,@0
ori r19,(1 << @1)
sts @0,r19
.ENDM
; Ïåðåéòè, åñëè ôëàã óñòàíîâëåí
; Branch if Flag is Set
; @0 - èìÿ ÿ÷åéêè ïàìÿòè
; @1 - èìÿ ôëàãà
; @2 - ìåòêà
.MACRO BRFS
lds r19,@0
sbrc r19,@1
rjmp @2
.ENDM
; Ïåðåéòè, åñëè ôëàã î÷èùåí
; Branch if Flag is Clear
; @0 - èìÿ ÿ÷åéêè ïàìÿòè
; @1 - èìÿ ôëàãà
; @2 - ìåòêà
.MACRO BRFC
lds r19,@0
sbrs r19,@1
rjmp @2
.ENDM