📄 uart.s
字号:
.module uart.c
.area text(rom, con, rel)
.dbfile G:\CS5532\firmware\uart.c
.dbfunc e uart_init _uart_init fV
.even
_uart_init::
.dbline -1
.dbline 47
; /****************************************Copyright (c)**************************************************
; **
; **
; **
; ** 文 件 名: uart.c
; ** 最后修改日期: 2007-10-8
; ** 描 述: rs232通讯函数
; ** 版 本: V1.0
; ** 主 控 芯 片:M16 晶振频率:7.37MHZ, 波特率19200
; ** IDE:ICCAVR 6.31
; **********************************************************************************************************/
;
; #define MCU_M16
; #define MCLK737
;
;
; #ifdef MCU_M8
; #include <iom8v.h>
; #endif
;
; #ifdef MCU_M16
; #include <iom16v.h>
; #endif
;
; #include <macros.h>
; #include "DEFINE.H"
; #include "UART.H"
; #include "COMMAND.H"
;
;
; uint08 uart_tx_buf[UART_BUF_SIZE]; //发送缓冲
; uint08 uart_tx_wr_ptr,uart_tx_rd_ptr;
; uint08 uart_rx_counter,uart_tx_counter;
; uint08 uart_rx_buf[UART_BUF_SIZE]; //接收缓冲,相当于消息体
; uint08 seq_number; //顺序数
; uint08 check_sum; //和校验
; uint08 rx_stu_mac; //接收状态机
; uint08 msg_end_flag; //消息体结束标志
; uint16 msg_size; //消息尺寸
;
;
; /**********************************************************************
; functionName:void uart_init(void)
; description:uart初始化
; **********************************************************************/
; void uart_init(void)
; {
.dbline 49
; //IO初始化
; SET_BIT(PORTD,PD0);
sbi 0x12,0
.dbline 50
; SET_BIT(PORTD,PD1);
sbi 0x12,1
.dbline 51
; SET_BIT(DDRD,PD1);
sbi 0x11,1
.dbline 52
; CLR_BIT(DDRD,PD0);
cbi 0x11,0
.dbline 55
;
; //寄存器初始化
; UCSRB = 0x00;
clr R2
out 0xa,R2
.dbline 56
; UCSRA = 0x00;
out 0xb,R2
.dbline 57
; UCSRC = 0x86;
ldi R24,134
out 0x20,R24
.dbline 58
; UBRRL = BAUD19200; //设置拨特率
ldi R24,23
out 0x9,R24
.dbline 59
; UBRRH = 0x00; //set baud rate hi
out 0x20,R2
.dbline 60
; UCSRB = 0x98; //使用中断
ldi R24,152
out 0xa,R24
.dbline 63
;
; //变量初始化
; uart_tx_wr_ptr=uart_tx_rd_ptr=0;
sts _uart_tx_rd_ptr,R2
sts _uart_tx_wr_ptr,R2
.dbline 64
; uart_rx_counter=uart_tx_counter=0;
sts _uart_tx_counter,R2
sts _uart_rx_counter,R2
.dbline 65
; msg_end_flag=0; //表示没有接收到数据
sts _msg_end_flag,R2
.dbline 66
; rx_stu_mac=ST_START;
ldi R24,240
sts _rx_stu_mac,R24
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 44
jmp _uart0_rx_isr
.area text(rom, con, rel)
.dbfile G:\CS5532\firmware\uart.c
.dbfunc e uart0_rx_isr _uart0_rx_isr fV
; uart_data -> R16
.even
_uart0_rx_isr::
st -y,R2
st -y,R3
st -y,R4
st -y,R5
st -y,R16
st -y,R18
st -y,R19
st -y,R24
st -y,R25
st -y,R30
st -y,R31
in R2,0x3f
st -y,R2
.dbline -1
.dbline 78
; }
;
;
;
;
; /**********************************************************************
; functionName:void uart0_rx_isr(void)
; description:串口接受中断函数
; **********************************************************************/
; #pragma interrupt_handler uart0_rx_isr:12
; void uart0_rx_isr(void)
; {
.dbline 80
; uint08 uart_data;
; uart_data=UDR;
in R16,0xc
.dbline 81
; switch(rx_stu_mac)
lds R18,_rx_stu_mac
clr R19
cpi R18,240
ldi R30,0
cpc R19,R30
breq L6
cpi R18,241
ldi R30,0
cpc R19,R30
breq L9
cpi R18,242
ldi R30,0
cpc R19,R30
breq L10
cpi R18,243
ldi R30,0
cpc R19,R30
brne X1
xjmp L11
X1:
cpi R18,244
ldi R30,0
cpc R19,R30
brne X2
xjmp L12
X2:
cpi R18,245
ldi R30,0
cpc R19,R30
brne X3
xjmp L15
X3:
cpi R18,246
ldi R30,0
cpc R19,R30
brne X4
xjmp L18
X4:
xjmp L3
X0:
.dbline 82
; {
L6:
.dbline 84
; case ST_START:
; {
.dbline 85
; if(uart_data==MESSAGE_START)
cpi R16,27
breq X5
xjmp L4
X5:
.dbline 86
; {
.dbline 87
; rx_stu_mac = ST_GET_SEQ_NUM;
ldi R24,241
sts _rx_stu_mac,R24
.dbline 88
; check_sum = MESSAGE_START;
ldi R24,27
sts _check_sum,R24
.dbline 89
; }
.dbline 90
; break;
xjmp L4
L9:
.dbline 93
; }
; case ST_GET_SEQ_NUM:
; {
.dbline 94
; seq_number = uart_data;
sts _seq_number,R16
.dbline 95
; check_sum ^= uart_data;
lds R2,_check_sum
eor R2,R16
sts _check_sum,R2
.dbline 96
; rx_stu_mac = ST_MSG_SIZE_H;
ldi R24,242
sts _rx_stu_mac,R24
.dbline 97
; break;
xjmp L4
L10:
.dbline 100
; }
; case ST_MSG_SIZE_H:
; {
.dbline 101
; msg_size = uart_data;
mov R2,R16
clr R3
sts _msg_size+1,R3
sts _msg_size,R2
.dbline 102
; msg_size <<=8;
mov R3,R2
clr R2
sts _msg_size+1,R3
sts _msg_size,R2
.dbline 103
; check_sum ^= uart_data;
lds R2,_check_sum
eor R2,R16
sts _check_sum,R2
.dbline 104
; rx_stu_mac = ST_MSG_SIZE_L;
ldi R24,243
sts _rx_stu_mac,R24
.dbline 105
; break;
xjmp L4
L11:
.dbline 108
; }
; case ST_MSG_SIZE_L:
; {
.dbline 109
; msg_size |= uart_data;
mov R2,R16
clr R3
lds R4,_msg_size
lds R5,_msg_size+1
or R4,R2
or R5,R3
sts _msg_size+1,R5
sts _msg_size,R4
.dbline 110
; check_sum ^= uart_data;
lds R2,_check_sum
eor R2,R16
sts _check_sum,R2
.dbline 111
; rx_stu_mac = ST_GET_TOKEN;
ldi R24,244
sts _rx_stu_mac,R24
.dbline 112
; break;
xjmp L4
L12:
.dbline 115
; }
; case ST_GET_TOKEN:
; {
.dbline 116
; if(uart_data==TOKEN)
cpi R16,14
brne L13
.dbline 117
; {
.dbline 118
; check_sum ^= uart_data;
lds R2,_check_sum
eor R2,R16
sts _check_sum,R2
.dbline 119
; rx_stu_mac = ST_GET_DATA;
ldi R24,245
sts _rx_stu_mac,R24
.dbline 120
; uart_rx_counter=0;
clr R2
sts _uart_rx_counter,R2
.dbline 121
; }
xjmp L4
L13:
.dbline 123
; else
; {
.dbline 124
; rx_stu_mac = ST_START;
ldi R24,240
sts _rx_stu_mac,R24
.dbline 125
; }
.dbline 126
; break;
xjmp L4
L15:
.dbline 129
; }
; case ST_GET_DATA:
; {
.dbline 130
; uart_rx_buf[uart_rx_counter++]=uart_data;
lds R2,_uart_rx_counter
clr R3
mov R24,R2
subi R24,255 ; addi 1
sts _uart_rx_counter,R24
ldi R24,<_uart_rx_buf
ldi R25,>_uart_rx_buf
mov R30,R2
clr R31
add R30,R24
adc R31,R25
std z+0,R16
.dbline 131
; check_sum ^= uart_data;
lds R2,_check_sum
eor R2,R16
sts _check_sum,R2
.dbline 132
; if(uart_rx_counter==msg_size)
lds R2,_msg_size
lds R3,_msg_size+1
lds R4,_uart_rx_counter
clr R5
cp R4,R2
cpc R5,R3
brne L4
.dbline 133
; {
.dbline 134
; rx_stu_mac = ST_GET_CHECK;
ldi R24,246
sts _rx_stu_mac,R24
.dbline 135
; }
.dbline 136
; break;
xjmp L4
L18:
.dbline 139
; }
; case ST_GET_CHECK:
; {
.dbline 140
; if(uart_data == check_sum)
lds R2,_check_sum
cp R16,R2
brne L19
.dbline 141
; {
.dbline 142
; msg_end_flag=1;
ldi R24,1
sts _msg_end_flag,R24
.dbline 143
; }
L19:
.dbline 144
; rx_stu_mac = ST_START;
ldi R24,240
sts _rx_stu_mac,R24
.dbline 145
; break;
xjmp L4
L3:
.dbline 148
.dbline 149
ldi R24,240
sts _rx_stu_mac,R24
.dbline 150
L4:
.dbline -2
L2:
ld R2,y+
out 0x3f,R2
ld R31,y+
ld R30,y+
ld R25,y+
ld R24,y+
ld R19,y+
ld R18,y+
ld R16,y+
ld R5,y+
ld R4,y+
ld R3,y+
ld R2,y+
.dbline 0 ; func end
reti
.dbsym r uart_data 16 c
.dbend
.dbfunc e rx_pkg_process _rx_pkg_process fV
; cmd -> R20
; i -> R20,R21
.even
_rx_pkg_process::
xcall push_gset1
.dbline -1
.dbline 840
; }
; default:
; {
; rx_stu_mac=ST_START;
; break;
; }
; }
; }
;
;
;
;
; /*void packageProcess(uint08 seqNum)
; {
; uint08 cmd;
; uint08 tmp=0;
; uint08 tmp2=0;
; uint08 tmp3=0;
; uint08 mode;
; uint16 block_size;
; uint16 i;
; uint16 tmp16;
; uint16 num_bytes=0;
; uint32 polling_address=0;
; uint32 start_address = address;
; cmd = msg_buffer[0];
;
; //命令CMD_SIGN_ON
; if(cmd==CMD_SIGN_ON)
; {
; num_bytes = 11;
; msg_buffer[0] = CMD_SIGN_ON;
; msg_buffer[1] = STATUS_CMD_OK;
; msg_buffer[2] = 8;
; memcpy(msg_buffer+3,"STK500_2",8);
; }
; //命令CMD_SET_PARAMETER
; else if(cmd==CMD_SET_PARAMETER)
; {
; switch(msg_buffer[1])
; {
; case PARAM_SCK_DURATION:
;
; if((msg_buffer[2]) >= 1 ) clockSpeed=SPI_SPEED_2MHZ;
; if((msg_buffer[2]) >= 2 ) clockSpeed=SPI_SPEED_1MHZ;
; if((msg_buffer[2]) >= 4 ) clockSpeed=SPI_SPEED_500KHZ;
; if((msg_buffer[2]) >= 8 ) clockSpeed=SPI_SPEED_250KHZ;
; if((msg_buffer[2]) >= 16 ) clockSpeed=SPI_SPEED_125KHZ;
; if((msg_buffer[2]) >= 32 ) clockSpeed=SPI_SPEED_62KHZ;
; if(EEPROMread(EEP_SCK_DURATION)!=clockSpeed)
; EEPROMwrite(EEP_SCK_DURATION,clockSpeed);
; //spi_set_speed(clock_speed); //因为我这里不用硬件SPI
; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -