📄 uart.s
字号:
.module uart.c
.area text(rom, con, rel)
; uartNum -> R16
.even
_uartInit::
; #define _UART_C_
;
; #define BAUD 38400
; #define CRYSTAL 11059200
; #define BAUD_SETTING (unsigned int)((unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1)
; #define BAUD_H (unsigned char)(BAUD_SETTING>>8)
; #define BAUD_L (unsigned char)(BAUD_SETTING)
;
; #include "includes.h"
; extern uint8 isrCount;
;
; void uartInit(uint8 uartNum)
; {
; if(1==uartNum)
cpi R16,1
brne L3
; {
; UCSR1B = 0x00; //disable while setting baud rate
clr R2
sts 154,R2
; UCSR1A = 0x00;
sts 155,R2
; UCSR1C = 0x06;
ldi R24,6
sts 157,R24
; UBRR1L = BAUD_L; //set baud rate lo
ldi R24,17
sts 153,R24
; UBRR1H = BAUD_H; //set baud rate hi
sts 152,R2
; UCSR1B = 0x18;
ldi R24,24
sts 154,R24
; ///共有3个中断源, 接受, 发送, 寄存器空, 对应此寄存器的高3位
; }
xjmp L4
L3:
; else
; {
; UCSR0B = 0x00; //disable while setting baud rate
clr R2
out 0xa,R2
; UCSR0A = 0x00;
out 0xb,R2
; UCSR0C = 0x06; //set frame format, 8 bits, 2stop]
ldi R24,6
sts 149,R24
; UBRR0H = BAUD_H;
sts 144,R2
; UBRR0L = BAUD_L;
ldi R24,17
out 0x9,R24
; UCSR0B = 0x18; //enable receive and transmite
ldi R24,24
out 0xa,R24
;
; }
L4:
L2:
.dbline 0 ; func end
ret
; dat -> R18
; num -> R16
.even
_putChar::
; }
;
; void putChar(uint8 num, uint8 dat)
; {
; ENTER_CRITICAL();
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
; if(1==num)
cpi R16,1
brne L6
; {
; UDR1 = dat;
sts 156,R18
L8:
L9:
; while(!(UCSR1A&BIT(TXC1))); //发送寄存器空
lds R2,155
sbrs R2,6
rjmp L8
; UCSR1A|=BIT(TXC1);
lds R24,155
ori R24,64
sts 155,R24
; }
xjmp L7
L6:
; else
; {
; UDR0 = dat;
out 0xc,R18
L11:
L12:
; while(!(UCSR0A&BIT(TXC0)));
sbis 0xb,6
rjmp L11
; UCSR0A|=BIT(TXC0);
sbi 0xb,6
; }
L7:
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
L5:
.dbline 0 ; func end
ret
; str -> R10,R11
; num -> R12
.even
_putStr::
xcall push_gset4x
movw R10,R18
mov R12,R16
; }
;
; void putStr(uint8 num, uint8 * str)
; {
; ENTER_CRITICAL();
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
xjmp L16
L15:
movw R30,R10
ldd R18,z+0
mov R16,R12
xcall _putChar
movw R24,R10
adiw R24,1
movw R10,R24
L16:
; while(*str)
movw R30,R10
ldd R2,z+0
tst R2
brne L15
; {
; putChar(num, *str);
; str++;
; }
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
L14:
xcall pop_gset4x
.dbline 0 ; func end
ret
; num -> R12
; port -> R10
.even
_putNum::
xcall push_gset4x
mov R12,R18
mov R10,R16
; }
;
; void putNum(uint8 port, uint8 num)
; {
; if((num>>4)<='9')
ldi R24,57
mov R25,R12
swap R25
andi R25,#0x0F
cp R24,R25
brlo L19
; {
; putChar(port, (num>>4)+'0');
mov R18,R12
swap R18
andi R18,#0x0F
subi R18,208 ; addi 48
mov R16,R10
xcall _putChar
; }
xjmp L20
L19:
; else
; {
; putChar(port, (num>>4)+'A');
mov R18,R12
swap R18
andi R18,#0x0F
subi R18,191 ; addi 65
mov R16,R10
xcall _putChar
; }
L20:
; if((num&0x0f)<='9')
ldi R24,57
mov R25,R12
andi R25,15
cp R24,R25
brlo L21
; {
; putChar(port, (num&0x0f)+'0');
mov R18,R12
andi R18,15
subi R18,208 ; addi 48
mov R16,R10
xcall _putChar
; }
xjmp L22
L21:
; else
; {
; putChar(port, (num&0x0f)+'A');
mov R18,R12
andi R18,15
subi R18,191 ; addi 65
mov R16,R10
xcall _putChar
; }
L22:
L18:
xcall pop_gset4x
.dbline 0 ; func end
ret
; temp -> R10
; num -> R16
.even
_getChar::
xcall push_gset3x
; }
;
; uint8 getChar(uint8 num)
; {
; uint8 temp=0;
clr R10
; ENTER_CRITICAL();
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
; if(1==num)
cpi R16,1
brne L24
; {
; if(UCSR1A&BIT(RXC1)) //no data
lds R2,155
sbrs R2,7
rjmp L25
; {
; temp=UDR1;
lds R10,156
; UCSR1A|=BIT(RXC1);
lds R24,155
ori R24,128
sts 155,R24
; }
; }
xjmp L25
L24:
; else
; {
; if(UCSR0A&BIT(RXC0)) //no data
sbis 0xb,7
rjmp L28
; {
; temp=UDR0;
in R10,0xc
; UCSR0A|=BIT(RXC0);
sbi 0xb,7
; }
L28:
; }
L25:
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
; return(temp);
mov R16,R10
L23:
xcall pop_gset3x
.dbline 0 ; func end
ret
.area vector(rom, abs)
.org 120
jmp _uart1_rx_isr
.area text(rom, con, rel)
.even
_uart1_rx_isr::
st -y,R0
in R0,0x3f
st -y,R0
; }
;
;
; ////////////////////////////////////////////////
; //接受完成中断
; ////////////////////////////////////////////////
; #pragma interrupt_handler uart1_rx_isr:31
; void uart1_rx_isr(void)
; {
; ENTER_CRITICAL();//uart has received a character in UDR
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
L30:
ld R0,y+
out 0x3f,R0
ld R0,y+
.dbline 0 ; func end
reti
.area vector(rom, abs)
.org 124
jmp _uart1_udre_isr
.area text(rom, con, rel)
.even
_uart1_udre_isr::
st -y,R0
in R0,0x3f
st -y,R0
; }
; ///////////////////////////////////////////////
; //发送寄存器空中断
; ///////////////////////////////////////////////
; #pragma interrupt_handler uart1_udre_isr:32
; void uart1_udre_isr(void)
; {
; ENTER_CRITICAL();;//character transferred to shift register so UDR is now empty
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
L31:
ld R0,y+
out 0x3f,R0
ld R0,y+
.dbline 0 ; func end
reti
.area vector(rom, abs)
.org 128
jmp _uart1_tx_isr
.area text(rom, con, rel)
.even
_uart1_tx_isr::
st -y,R0
in R0,0x3f
st -y,R0
; }
; //////////////////////////////////////////////
; //发送完成中断
; //////////////////////////////////////////////
; #pragma interrupt_handler uart1_tx_isr:33
; void uart1_tx_isr(void)
; {
; ENTER_CRITICAL();;//character has been transmitted
st -y,r16
in r16,0x3F
cli
push r16
ld r16,y+
; EXIT_CRITICAL();
st -y,r16
pop r16
out 0x3F,r16
ld r16,y+
L32:
ld R0,y+
out 0x3f,R0
ld R0,y+
.dbline 0 ; func end
reti
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -