• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need a hand understanding this code

Status
Not open for further replies.

wejos

Member
this is actually from nigel's tutorial that i have chopped chopped so i could learn line by line. for 16f84.

Code:
		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
			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	PORTA
SER_TRIS	Equ	TRISA
SER_IN	Equ	0x00
LED_PORT	Equ	PORTB
LED_TRIS	Equ	TRISB

		org	0x0000


Initialise	clrf	count
		clrf	PORTA
		clrf	PORTB
	        BSF     STATUS, RP0           ;select bank 1	
		clrf	LED_TRIS	      ;make LED_Port all outputs
	        BCF     STATUS, RP0           ;select bank 0
		call	SER_INIT	      ;initialise serial port
Loop		call	Rcv_RS232
		movwf	LED_PORT	      ;transfer byte to LED's


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


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   0x0C
            MOVWF   Delay_Count
Start_Wait  NOP
            DECFSZ  Delay_Count , f
            GOTO    Start_Wait
            RETURN

Bit_Delay   MOVLW   0x18
            MOVWF   Delay_Count
Bit_Wait    NOP
            DECFSZ  Delay_Count , f
            GOTO    Bit_Wait
            RETURN




1.) from the original RA7 or "SER_IN Equ 0x07", since im using 16f84 which has 5 bits only for port A, i changed it to RA0 or SER_IN Equ 0x00". you think this is okay?

2.) this part of the code i dont know when they are triggered, since its not a subroutine, and appeared to me that its never been called from the rest of the code. I know thou that there must be some kind of delay due to RS232 so its important. anyone kindly point out to me where this part comes in?

Code:
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	0xC7
		movwf	counta
		movlw	0x01
		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

yes i removed the XMIT_RS232 subroutine.

and i was not able to make it work in my breadboard. i know i missing something here. ty for your help.
 
Last edited:

Nigel Goodwin

Super Moderator
Most Helpful Member
2.) this part of the code i dont know when they are triggered, since its not a subroutine, and appeared to me that its never been called from the rest of the code. I know thou that there must be some kind of delay due to RS232 so its important. anyone kindly point out to me where this part comes in?
It's a subroutine, in fact a number of them - but it might not be used in that particular tutorial - I generally included some time delay routines in the template I used for the tutorials, as most use them.
 

wejos

Member
It's a subroutine, in fact a number of them - but it might not be used in that particular tutorial - I generally included some time delay routines in the template I used for the tutorials, as most use them.
thanks nigel glad you said that.



one final thing you think the code below would work?

can i use XT when i burned them since in the config there's _HS_OSC?

should i check power on also?

should i check also watchdog given this code?



Code:
LIST	p=16F84		;tell assembler what chip we are using
	include "P16F84.inc"		;include the defaults for the chip
	ERRORLEVEL	0,	-302	;suppress bank selection messages
	__CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _HS_OSC ;sets the configuration settings (oscillator type etc.) or RC_OSC if thats what your using



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
			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	PORTA
SER_TRIS	Equ	TRISA
SER_IN	Equ	0x00
LED_PORT	Equ	PORTB
LED_TRIS	Equ	TRISB

		org	0x0000


Initialise	clrf	count
		clrf	PORTA
		clrf	PORTB
	        BSF     STATUS, RP0           ;select bank 1	
		clrf	LED_TRIS	      ;make LED_Port all outputs
	        BCF     STATUS, RP0           ;select bank 0
		call	SER_INIT	      ;initialise serial port
Loop		call	Rcv_RS232
		movwf	LED_PORT	      ;transfer byte to LED's


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


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   0x0C
            MOVWF   Delay_Count
Start_Wait  NOP
            DECFSZ  Delay_Count , f
            GOTO    Start_Wait
            RETURN

Bit_Delay   MOVLW   0x18
            MOVWF   Delay_Count
Bit_Wait    NOP
            DECFSZ  Delay_Count , f
            GOTO    Bit_Wait
            RETURN

end
 
Last edited:

wejos

Member
is this the UART software?

guys is this the UART software? responsible for waiting for the start bit, data, and the stop bit.

Code:
Start_Delay MOVLW   0x0C
            MOVWF   Delay_Count
Start_Wait  NOP
            DECFSZ  Delay_Count , f
            GOTO    Start_Wait
            RETURN

Bit_Delay   MOVLW   0x18
            MOVWF   Delay_Count
Bit_Wait    NOP
            DECFSZ  Delay_Count , f
            GOTO    Bit_Wait
            RETURN
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top