使用串行端口

时间:2015-12-25 10:14:29

标签: assembly serial-port mplab pic18

我想编写一个程序来从dipswitch读取一个数字并使用rs232协议(串行端口)传输该数字。这个程序是用汇编语言编写的。我使用PORTB作为输入端口,它连接到dipswitch以获取应该传输的数字。我在proteus中模拟了这个协议,但它没有显示任何结果。什么错了?

PROTEUS SIMULATION -

Screenshot

这是代码:

#include<p18f2550.inc>

; CONFIG1H
 config FOSC =HS      ; Oscillator Selection bits (HS oscillator (HS))

; CONFIG2H
 config WDT = OFF        ; Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
 config WDTPS = 32768    ; Watchdog Timer Postscale Select bits (1:32768)


ORG 0X00
GOTO MAIN

MAIN:
MOVLW B'00100000' ;enable transmit and choose low baud
MOVWF TXSTA ;write to reg
MOVLW D'15' ;9600 bps
MOVWF SPBRG ;write to reg
BCF TRISC, TX ;make tx pin and output pin
BSF RCSTA, SPEN ;enable the serial port
SETF TRISB ;portb defined as input

OVER:
MOVFF PORTB,W ;move portb to wreg
CALL TRANS


TRANS:
S1
BTFSS PIR1, TXIF ;wait until last bit is gone
BRA S1 ;stay in loop
MOVWF TXREG ;load th value to e transmitted
CALL OVER

END

1 个答案:

答案 0 :(得分:0)

在没有CALL的情况下多次使用RETURN会导致堆栈溢出,并且会在消息建议时导致设备重置,除非STVREN寄存器中的CONFIG4L位设置为0

未经测试,请尝试使用GOTO代替CALL s。

#include<p18f2550.inc>

; CONFIG1H
 config FOSC =HS      ; Oscillator Selection bits (HS oscillator (HS))

; CONFIG2H
 config WDT = OFF        ; Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
 config WDTPS = 32768    ; Watchdog Timer Postscale Select bits (1:32768)


ORG 0X00
GOTO MAIN

MAIN:
MOVLW B'00100000' ;enable transmit and choose low baud
MOVWF TXSTA ;write to reg
MOVLW D'15' ;9600 bps
MOVWF SPBRG ;write to reg
BCF TRISC, TX ;make tx pin and output pin
BSF RCSTA, SPEN ;enable the serial port
SETF TRISB ;portb defined as input

OVER:
MOVFF PORTB,W ;move portb to wreg
GOTO TRANS ; **change "CALL" to "GOTO" here**


TRANS:
S1
BTFSS PIR1, TXIF ;wait until last bit is gone
BRA S1 ;stay in loop
MOVWF TXREG ;load th value to e transmitted
GOTO OVER ; **change "CALL" to "GOTO" here**

END