📄 myuart.s
字号:
.module myuart.c
.area text(rom, con, rel)
.dbfile F:\全物理仿真\mega128\uart_0\myuart.c
.dbfunc e uart0_init _uart0_init fV
.even
_uart0_init::
.dbline -1
.dbline 8
; #include"includes.h"
;
; /*USART initiate*/
; U8 rx_flag; //接收数据标志
; U8 rx_buff; //接收数据缓存区
;
; void uart0_init(void)
; {
.dbline 9
; UBRR0H = BAUD_H;
clr R2
sts 144,R2
.dbline 10
; UBRR0L = BAUD_L; //Set baud rate
ldi R24,35
out 0x9,R24
.dbline 11
; UCSR0C = 0x0E; //Set frame format: 8data, 2stop bit
ldi R24,14
sts 149,R24
.dbline 12
; UCSR0B = ((1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0)); //Enable Receiver and Transmitter
ldi R24,152
out 0xa,R24
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 72
jmp _uart0_rx_isr
.area text(rom, con, rel)
.dbfile F:\全物理仿真\mega128\uart_0\myuart.c
.dbfunc e uart0_rx_isr _uart0_rx_isr fV
.even
_uart0_rx_isr::
st -y,R2
in R2,0x3f
st -y,R2
.dbline -1
.dbline 20
; //1:接受中断
; //0:关闭中断
; }
;
; //接收中断
; #pragma interrupt_handler uart0_rx_isr:19
; void uart0_rx_isr(void)
; {
.dbline 22
; //uart has received a character in UDR
; PORTA=UDR0; //接收数据并输出到PA
in R2,0xc
out 0x1b,R2
.dbline -2
L2:
ld R2,y+
out 0x3f,R2
ld R2,y+
.dbline 0 ; func end
reti
.dbend
.area vector(rom, abs)
.org 80
jmp _uart0_tx_isr
.area text(rom, con, rel)
.dbfile F:\全物理仿真\mega128\uart_0\myuart.c
.dbfunc e uart0_tx_isr _uart0_tx_isr fV
.even
_uart0_tx_isr::
.dbline -1
.dbline 28
; }
;
; //发送中断
; #pragma interrupt_handler uart0_tx_isr:21
; void uart0_tx_isr(void)
; {
.dbline -2
L3:
.dbline 0 ; func end
reti
.dbend
.area vector(rom, abs)
.org 76
jmp _uart0_data_isr
.area text(rom, con, rel)
.dbfile F:\全物理仿真\mega128\uart_0\myuart.c
.dbfunc e uart0_data_isr _uart0_data_isr fV
.even
_uart0_data_isr::
.dbline -1
.dbline 34
; }
;
; //USART0 数据寄存器空中断
; #pragma interrupt_handler uart0_data_isr:20
; void uart0_data_isr(void)
; {
.dbline -2
L4:
.dbline 0 ; func end
reti
.dbend
.dbfunc e uart0_putchar _uart0_putchar fV
; c -> R16
.even
_uart0_putchar::
.dbline -1
.dbline 43
; }
;
; //call this routine to initialize all peripherals
;
;
;
; //从RS232发送一个字节
; void uart0_putchar(char c)
; {
L6:
.dbline 44
L7:
.dbline 44
; while(!(UCSR0A & 0x20));
sbis 0xb,5
rjmp L6
.dbline 45
; UDR0 = c;
out 0xc,R16
.dbline -2
L5:
.dbline 0 ; func end
ret
.dbsym r c 16 c
.dbend
.dbfunc e uart0_putstr _uart0_putstr fV
; str -> R20,R21
.even
_uart0_putstr::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 50
; }
;
; //输出一个字符串
; void uart0_putstr(char* str)
; {
xjmp L11
L10:
.dbline 52
.dbline 53
movw R30,R20
ldd R16,z+0
xcall _uart0_putchar
.dbline 54
subi R20,255 ; offset = 1
sbci R21,255
.dbline 55
L11:
.dbline 51
; while(*str)
movw R30,R20
ldd R2,z+0
tst R2
brne L10
.dbline -2
L9:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r str 20 pc
.dbend
.dbfunc e uart0_getchar _uart0_getchar fI
; res -> R16
; status -> R18
.even
_uart0_getchar::
.dbline -1
.dbline 60
; {
; uart0_putchar(*str);
; str++;
; }
; }
;
; //从RS232接收一个字节
; int uart0_getchar(void)
; {
.dbline 62
; unsigned char status,res;
; if(!(UCSR0A & 0x80)) return -1; //no data to be received
sbic 0xb,7
rjmp L14
.dbline 62
ldi R16,-1
ldi R17,-1
xjmp L13
L14:
.dbline 63
; status = UCSR0A;
in R18,0xb
.dbline 64
; res = UDR0;
in R16,0xc
.dbline 65
; if (status & 0x1c) return -1; // If error, return -1
mov R24,R18
andi R24,28
breq L16
.dbline 65
ldi R16,-1
ldi R17,-1
xjmp L13
L16:
.dbline 66
; return res;
clr R17
.dbline -2
L13:
.dbline 0 ; func end
ret
.dbsym r res 16 c
.dbsym r status 18 c
.dbend
.dbfunc e uart0_waitchar _uart0_waitchar fc
; c -> R20,R21
.even
_uart0_waitchar::
xcall push_gset1
.dbline -1
.dbline 70
; }
;
; char uart0_waitchar(void)
; {
L19:
.dbline 72
L20:
.dbline 72
; int c;
; while((c=uart0_getchar())==-1);
xcall _uart0_getchar
movw R20,R16
cpi R20,255
ldi R30,255
cpc R21,R30
breq L19
.dbline 73
; return (char)c;
.dbline -2
L18:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r c 20 I
.dbend
.area bss(ram, con, rel)
.dbfile F:\全物理仿真\mega128\uart_0\myuart.c
_rx_buff::
.blkb 1
.dbsym e rx_buff _rx_buff c
_rx_flag::
.blkb 1
.dbsym e rx_flag _rx_flag c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -