Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me code EUART pic16f883 asm

Status
Not open for further replies.

kidteam

Junior Member level 3
Junior Member level 3
Joined
Dec 22, 2010
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,420
hi all
i'm try code asm with pic16f883
code transmit
Code:
#include p16f883.inc
__config _CONFIG1, _FOSC_XT& _WDTE_OFF& _PWRTE_OFF& _MCLRE_OFF& _CP_OFF& _CPD_OFF& _BOR_OFF& _IESO_OFF& _FCMEN_OFF& _LVP_OFF& _DEBUG_OFF
__config _CONFIG2, _BOR21V& _WRT_OFF
;thach anh 3.686400 MHz
		c1			equ			0x20
		c2			equ			0x21
		c3			equ			0x22
		org			0x0000
		goto		start
start
		banksel		ANSEL
		clrf		ANSEL
		clrf		ANSELH
		banksel		TXSTA
		bsf			TXSTA,		TXEN
		bcf			TXSTA,		SYNC
		banksel		RCSTA
		bsf			RCSTA,		RCEN
		bsf			RCSTA,		SPEN
		banksel		SPBRG
		clrf		SPBRGH
		movlw		d'5'
		movwf		SPBRG
main
		banksel		PIR1
		btfss		PIR1,		TXIF
		goto		$-1
		banksel		TXREG
		movlw		'K'
		movwf		TXREG
		call		delay
		goto		main
;--------------------------------------------------------------
delay
		banksel		0
		movlw		100
		movwf		c1
loop2
		movlw		50
		movwf		c2
loop1
		movlw		3
		movwf		c3
		decfsz		c3,			1
		goto		$-1
		decfsz		c2,			1
		goto		loop1
		decfsz		c1,			1
		goto		loop2
		return
;==============================================================
				
end
is work fine
can i help me code receive.
Please help me.
 

Hi,

You are very close to getting it right.

Have made 2 changes to your code.

Selected the BRGH bit in your TX setup and changed your SPBRG to vaule 25 to transmit at 9600, 8 ,1, none with 4 mhz crystal in simulation.

Your code comments about a 3.686mhz crystal, are you actually using that ?
Much simpler to use the internal oscillator if you know how to set it up ?

Also changed the RADIX in your delay routine see comment and the Assembler Help page on Radix.

Code:
	  	LIST P=16F883,r=hex     ;Set the radix as hex or decimal as needed, defualt is HEX

	#include p16f883.inc
	__config _CONFIG1, _FOSC_XT& _WDTE_OFF& _PWRTE_OFF& _MCLRE_OFF& _CP_OFF& _CPD_OFF& _BOR_OFF& _IESO_OFF& _FCMEN_OFF& _LVP_OFF& _DEBUG_OFF
	__config _CONFIG2, _BOR21V& _WRT_OFF
;thach anh 3.686400 MHz
c1			equ			0x20
c2			equ			0x21
c3			equ			0x22

		org			0x0000
		goto		start
start
		banksel		ANSEL
		clrf		ANSEL
		clrf		ANSELH
		banksel		TXSTA
		bsf			TXSTA,		TXEN
		bcf			TXSTA,		SYNC
		BSF			TXSTA,		BRGH
		banksel		RCSTA
		bsf			RCSTA,		RCEN
		bsf			RCSTA,		SPEN
		banksel		SPBRG
		clrf		SPBRGH
		movlw		d'25'
		movwf		SPBRG
main
		banksel		PIR1
		btfss		PIR1,		TXIF
		goto		$-1
		banksel		TXREG
		movlw		'K'
		movwf		TXREG
		call		delay
		goto		main
;--------------------------------------------------------------
delay
		banksel		0
		movlw		.100			;100 is declared in decimal. or use RADIX in LIST

		movwf		c1
loop2
		movlw		.50				;50 is declared in decimal.
		movwf		c2
loop1
		movlw		.3				;3 is declared in decimal.
		movwf		c3
		decfsz		c3,			1
		goto		$-1
		decfsz		c2,			1
		goto		loop1
		decfsz		c1,			1
		goto		loop2
		return
;==============================================================
				
		end
 

Attachments

  • 2013-01-01 17_13_12-UNTITLED - ISIS Professional (Animating).jpg
    2013-01-01 17_13_12-UNTITLED - ISIS Professional (Animating).jpg
    42.2 KB · Views: 77

my original code is work. TX work fine, how to receive?
thanks
New Bitmap Image.JPG
**broken link removed**
 

Hi,

Should be something like this

Code:
rx_rs232 
		banksel		PIR1
		btfss   PIR1,  RCIF 	    ;check for received data
     	goto    rx_rs232
        movf  	RCREG,W
 

Can you help me complete example. Wait receive and echo back. Thanks.
 

Can you help me complete example. Wait receive and echo back. Thanks.

Hi,

That bit of Receive code does work ok, but when I tried it with your code it would not work ??
Unable to quickly see the error so have used all my old usart code instead.

As you can see it should be easier for you to use.

I have enclosed pics of how VT is set up to echo sent data.

In this case VT receives your K and waits for any character to be returned before sending the next K.

You can see in screenshot5 how you can do things the other way around; only when I send ' T ' from VT will the pic return the temperature.
 

Attachments

  • rs232.rar
    213.3 KB · Views: 114
Thanks.
i change your code is
Code:
	  	LIST P=16F883,r=hex     ;Set the radix as hex or decimal as needed, defualt is HEX

	#include p16f883.inc
	__config _CONFIG1, _FOSC_XT& _WDTE_OFF& _PWRTE_OFF& _MCLRE_OFF& _CP_OFF& _CPD_OFF& _BOR_OFF& _IESO_OFF& _FCMEN_OFF& _LVP_OFF& _DEBUG_OFF
	__config _CONFIG2, _BOR21V& _WRT_OFF
;thach anh 3.686400 MHz
c1			equ			0x20
c2			equ			0x21
c3			equ			0x22

		org			0x0000
		goto		start



start
		banksel 	TRISC
		movlw 		 b'11000000' 		;RC6=TX RC7=RX  TRISC must be set to 1 for each bit
		movwf		TRISC

		banksel		ANSEL
		clrf		ANSEL
		clrf		ANSELH

		banksel 0


		call	ser_init				; set up Usart 9600,8,1,none at with 4 Mhz osc		


main_loop
		movlw	'K'						; send out K
		call	tx_rs232

      	call    rx_rs232				; wait for any returned character
        
		goto	main_loop					; once a character received , loop again



			
	
tx_rs232	btfss   PIR1,  TXIF 	    ; xmit buffer empty?
     		goto	tx_rs232            ; no, wait
     		movwf   TXREG		  	    ; now send
  			return

rx_rs232 	btfss   PIR1,  RCIF 	    ;check for received data
     		goto    rx_rs232
            movf  	RCREG,W
            return


ser_init	movlw   d'5'               ; 9600 baud @ 4 Mhz Fosc  8 bit Async	
			;ban dau 25 sua lai thanh 5
			banksel SPBRG	
     		movwf   SPBRG				; BRGH=1	
     		movlw   b'00100000'
			;ban dau 00100100 sua thanh 00100000
     		movwf   TXSTA               ;enable Async Transmission, set brgh
			banksel 0
     		movlw   b'10010000'
     		movwf   RCSTA               ;enable Async Reception
           	return	



;--------------------------------------------------------------
delay
		banksel		0
		movlw		.100			;100 is declared in decimal. or use RADIX in LIST

		movwf		c1
loop2
		movlw		.50				;50 is declared in decimal.
		movwf		c2
loop1
		movlw		.3				;3 is declared in decimal.
		movwf		c3
		decfsz		c3,			1
		goto		$-1
		decfsz		c2,			1
		goto		loop1
		decfsz		c1,			1
		goto		loop2
		return
;==============================================================

	END
then code is work fine but i don't where is error in my code. :)
thanks.
update.....
If i init spbrg before txsta and rcsta, code work but i don't understand why?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top