📄 tut7_2a.asm
字号:
;Tutorial 7_2a
;Serial routines - send string to PC
;Nigel Goodwin 2003
LIST p=16F876 ;tell assembler what chip we are using
include "P16F876.inc" ;include the defaults for the chip
ERRORLEVEL 0, -302 ;suppress bank selection messages
__config 0x393A ;sets the configuration settings (oscillator type etc.)
cblock 0x20 ;start of general purpose registers
count ;used in looping routines
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
templcd ;temp store for 4 bit mode
templcd2
lcdtmp
Xmit_Byte ;holds byte to xmit
Rcv_Byte ;holds received byte
Bit_Cntr ;bit counter for RS232
Delay_Count ;delay loop counter
endc
SER_PORT Equ PORTC
SER_TRIS Equ TRISC
SER_IN Equ 0x07
SER_OUT Equ 0x06
org 0x0000
BANKSEL ADCON1
movlw 0x06
movwf ADCON1
BANKSEL PORTA ;turn analogue inputs off
Initialise clrf count
clrf PORTA
clrf PORTB
call SER_INIT ;initialise serial port
call Long_Delay
movlw 0x00 ;select first string
call Print_String
Done goto Done ;loop for ever
Print_String movwf count1 ;save string number (0 is first string)
clrf count ;set counter register to zero
NextLine movf count1, w
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
goto NextChar ;if so we've reached required string
movlw 0x07
movwf PCLATH
movf count, w ;put counter value in W
call Strings ;get a character from the text table
clrf PCLATH
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
decf count1, f
incf count, f
goto NextLine
NextChar ;display selected string
movlw 0x07 ;set PCLATH for strings in last page of memory
movwf PCLATH
movf count, w ;put counter value in W
call Strings ;get a character from the text table
clrf PCLATH ;reset PCLATH
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
return ;exit from routine
call XMIT_RS232
incf count, f
goto NextChar
;Serial routines
SER_INIT
BSF STATUS, RP0 ;select bank 1
BCF SER_TRIS, SER_OUT ;set B6 as an output
BSF SER_TRIS, SER_IN ;set B7 as an input
BCF STATUS, RP0 ;select bank 0
BSF SER_PORT, SER_OUT ;set SER_OUT high
RETURN
XMIT_RS232 MOVWF Xmit_Byte ;move W to Xmit_Byte
MOVLW 0x08 ;set 8 bits out
MOVWF Bit_Cntr
BCF SER_PORT, SER_OUT
CALL Bit_Delay
Ser_Loop RRF Xmit_Byte , f ;send one bit
BTFSS STATUS , C
BCF SER_PORT, SER_OUT
BTFSC STATUS , C
BSF SER_PORT, SER_OUT
CALL Bit_Delay
DECFSZ Bit_Cntr , f ;test if all done
GOTO Ser_Loop
BSF SER_PORT, SER_OUT
CALL Bit_Delay
RETURN
Rcv_RS232 BTFSC SER_PORT, SER_IN ;wait for start bit
GOTO Rcv_RS232
CALL Start_Delay ;do half bit time delay
BTFSC SER_PORT, SER_IN ;check still in start bit
GOTO Rcv_RS232
MOVLW 0x08 ;set up to read 8 bits
MOVWF Bit_Cntr
CLRF Rcv_Byte
Next_RcvBit CALL Bit_Delay
BTFSS SER_PORT, SER_IN
BCF STATUS , C
BTFSC SER_PORT, SER_IN
BSF STATUS , C
RRF Rcv_Byte , f
DECFSZ Bit_Cntr , f ;test if all done
GOTO Next_RcvBit
CALL Bit_Delay
MOVF Rcv_Byte, W
RETURN
Start_Delay MOVLW 0x3D
MOVWF Delay_Count
Start_Wait NOP
DECFSZ Delay_Count , f
GOTO Start_Wait
RETURN
Bit_Delay MOVLW 0x7A
MOVWF Delay_Count
Bit_Wait NOP
DECFSZ Delay_Count , f
GOTO Bit_Wait
RETURN
;End of serial routines
;Delay routines
Long_Delay
call Delay255
call Delay255
call Delay255
call Delay255
return
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay10 movlw d'10' ;delay 10mS
goto d0
Delay1 movlw d'1' ;delay 1mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xE7
movwf counta
movlw 0x04
movwf countb
Delay_0 decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
;end of Delay routines
org 0x0700 ;set to last page of memory
Strings addwf PCL , f
retlw 'W'
retlw 'i'
retlw 'n'
retlw 'P'
retlw 'i'
retlw 'c'
retlw 'P'
retlw 'r'
retlw 'o'
retlw 'g'
retlw 0x0A
retlw 0x0D
retlw 'T'
retlw 'u'
retlw 't'
retlw 'o'
retlw 'r'
retlw 'i'
retlw 'a'
retlw 'l'
retlw 's'
retlw 0x0A
retlw 0x0D
retlw 0x00
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -