Continue to Site

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.

  • 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.

UART is working,but why is blinking ?

Status
Not open for further replies.

PIC2PIC

New Member
I made a code for communicating PIC 16f877 to PIC 16f877.
Both PICs are transmitter ans receiver. One side takes a byte from PORTB and sends that byte to other PIC2 and PIC2 shows that byte on PORTB (there are LED).
Also PIC2 takes a byte from PORTA and sends that byte to PORTA on PIC1 and PIC1 shows that byte on PORTB (there are LED).
I see that it works except PORTB and PORTA are blinking all the time with frequency about 1-2 Hz (I mean LED i put on these ports) .

What is the problem ?

Down there is the code for PIC1. Code for PIC2 is the same except it takes data from PORTA and shows received byte on PORTB



;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;---------------------------------------------------------------------------
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
errorlevel -302, -207
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

org 00h
goto start

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;ISR
;---------------------------------------------------------------------------
org 04h
banksel PIE1
bcf PIE1,TXIE
bcf PIE1,RCIE
bcf INTCON,PEIE
bcf INTCON,GIE

;---------------------------------------------------------------------------
banksel TXSTA
bcf TXSTA,TXEN
banksel RCSTA
bcf RCSTA,CREN

;---------------------------------------------------------------------------
banksel RCREG
movf RCREG,W
movwf PORTA

;---------------------------------------------------------------------------
movf RCREG,W
movf RCREG,W

;---------------------------------------------------------------------------
banksel TXSTA
bsf TXSTA,TXEN
banksel RCSTA
bsf RCSTA,CREN

;---------------------------------------------------------------------------
banksel PIE1
bcf PIE1,TXIE
bsf PIE1,RCIE
movlw b'11000000'
movwf INTCON

return
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

start
radix dec
;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;---------------------------------------------------------------------------
cblock 20h
brojac,brojac1,podatak,aktiviraj_transmisiju
endc
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;---------------------------------------------------------------------------


banksel TRISB

movlw 0ffh
movwf TRISB
movwf TRISC
movlw 00h
movwf TRISA


;---------------------------------------------------------------------------

movlw b'00000110' ; all analog pins = digital
movwf ADCON1

banksel PIE1
movlw b'00100000'
movwf PIE1
movlw b'11000000'
movwf INTCON

;---------------------------------------------------------------------------
banksel SPBRG
movlw 0x0C
movwf SPBRG

movlw b'00100100'
movwf TXSTA

banksel RCSTA
movlw b'10010000'
movwf RCSTA
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;---------------------------------------------------------------------------
banksel PIE1
bcf PIE1,TXIE
bsf PIE1,RCIE
movlw b'11000000'
movwf INTCON


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;---------------------------------------------------------------------------
Main

call Trans

call Pauza

goto Main



;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;Subrutine
;---------------------------------------------------------------------------
Trans
banksel INTCON
bcf INTCON,GIE
banksel PORTB
movf PORTB,W
movwf TXREG

banksel TXSTA
WtHere btfss TXSTA,TRMT
goto WtHere

banksel INTCON
bsf INTCON,GIE
nop


return

;---------------------------------------------------------------------------
Pauza
movlw 75
movwf brojac1
mala_pauza_1
movlw 100
movwf brojac
mala_pauza
decfsz brojac,f
goto mala_pauza
decfsz brojac1,f
goto mala_pauza_1

return
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------




end
 
Interrupts exit with retfie not return. Good assembly practice means ample comments.

PS please use code tags (Go Advanced)
 
Last edited:
You also need to save the context in the ISR. See section 14.12 of the data sheet.

Mike.
 
I already done saving working register and status register, but it is still blinking.
RETFIE is the same as RETURN except it set GIE, the one I set in code.

What is the problem ? I don't know what to do. Any suggestion ?
 
Here is the new code with comments:
I made a code for communicating PIC 16f877 to PIC 16f877.
Both PICs are transmitter ans receiver. One side takes a byte from PORTB and sends that byte to other PIC2 and PIC2 shows that byte on PORTB (there are LED).
Also PIC2 takes a byte from PORTA and sends that byte to PIC1 and PIC1 shows that byte on PORTA (there are LED).
I see that it works except PORTB and PORTA are blinking all the time with frequency about 1-2 Hz (I mean LED i put on these ports) .

What is the problem ?

Down there is the code for PIC1. Code for PIC2 is the same except it takes data from PORTA and shows received byte on PORTB:

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;
;---------------------------------------------------------------------------
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
errorlevel -302, -207
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

org 00h
goto start

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;ISR
;---------------------------------------------------------------------------
org 04h
;Disable interrupts
banksel PIE1
bcf PIE1,TXIE
bcf PIE1,RCIE
bcf INTCON,PEIE
bcf INTCON,GIE

;---------------------------------------------------------------------------
;Disable reception and transmition
banksel TXSTA
bcf TXSTA,TXEN
banksel RCSTA
bcf RCSTA,CREN

;---------------------------------------------------------------------------
;save working and status register
banksel W_Save
movwf W_Save
movf STATUS,W
clrf STATUS
movwf STATUS_Save

;---------------------------------------------------------------------------
;accept data and put it on PORTA
banksel RCREG
movf RCREG,W
movwf PORTA

;---------------------------------------------------------------------------
;clear buffer
movf RCREG,W
movf RCREG,W

;---------------------------------------------------------------------------
;restore working i status register
banksel W_Save
movf STATUS_Save,W
movwf STATUS
movf W_Save,W

;---------------------------------------------------------------------------
;enable transmision and reception
banksel TXSTA
bsf TXSTA,TXEN
banksel RCSTA
bsf RCSTA,CREN

;---------------------------------------------------------------------------
;Enable interrupts
banksel PIE1
bcf PIE1,TXIE
bsf PIE1,RCIE
bsf INTCON,PEIE
bsf INTCON,GIE

return
;
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

start
radix dec
;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;
;---------------------------------------------------------------------------
cblock 20h
brojac,brojac1,podatak,aktiviraj_transmisiju,STATUS_Save,W_Save
endc
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;Initialization USART and setting input and output
;---------------------------------------------------------------------------


banksel TRISB

movlw 0ffh
movwf TRISB
movwf TRISC
movlw 00h
movwf TRISA


;---------------------------------------------------------------------------

movlw b'00000110' ; all analog pins = digital
movwf ADCON1

banksel PIE1
movlw b'00100000'
movwf PIE1
movlw b'11000000'
movwf INTCON

;---------------------------------------------------------------------------
banksel SPBRG
movlw 0x0C
movwf SPBRG

movlw b'00100100'
movwf TXSTA

banksel RCSTA
movlw b'10010000'
movwf RCSTA
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------

;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;Setting interupts
;---------------------------------------------------------------------------
banksel PIE1
bcf PIE1,TXIE
bsf PIE1,RCIE
movlw b'11000000'
movwf INTCON


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;
;---------------------------------------------------------------------------
Main

call Trans ;call transmit data

call Pauza ;Some time to wait

goto Main



;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;Subroutines
;---------------------------------------------------------------------------
;transmit data
Trans
banksel INTCON
bcf INTCON,GIE
banksel PORTB
movf PORTB,W
movwf TXREG

banksel TXSTA
WtHere btfss TXSTA,TRMT
goto WtHere

banksel INTCON
bsf INTCON,GIE
nop


return

;---------------------------------------------------------------------------
;Some time to wait
Pauza
movlw 75
movwf brojac1
mala_pauza_1
movlw 100
movwf brojac
mala_pauza
decfsz brojac,f
goto mala_pauza
decfsz brojac1,f
goto mala_pauza_1

return
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------




end
 
You are far more likely to get help if you use code tags as then people don't have to reformat your code. Type [code] before it and [/code] after it. You can edit your previous posts and insert the code tags but you will probably have to reinsert your code as the formatting will have been lost.

Mike.
 
Pommie thank you , but:

I am sorry, my english is not so good, so I don't understand you. What is code tags ? I thought it is comment in the code. And what is it mean "people have to reformat my code" ?
 
Hi PIC2PIC,

You should use the proper way for saving the values of the registers right after entering ISR. And restore them back right before exiting ISR (retfie).

https://www.electro-tech-online.com/custompdfs/2009/09/30292c.pdf
Page 130

Otherwise there's no point of storing. From your code STATUS register is screwed up as well as the bank before exiting ISR. And why don't you use 'retfie' as designed, instead of doing extra works?


Thanks
 
@bananasiong
Thank you for answering. But as you can see I preserve values of STATUS and WORKING registers . Also, it is true that I can use 'retfie' but still it will not solve the problem. Why is it blinking? Maybe something resets POTRB and PORTA or maybe TRISB and TRISB.
 
Without saving them your uC will restart sometimes (I've experienced the same frustration a while ago!) Which explains the flashing here is an example of context saving during an interrupt!


Code:
;*** Interrupt Service Routine ***

ISR 
	MOVWF	W_TEMP						; 	
	SWAPF 	STATUS,W   					; Context Saving
	MOVWF 	STATUS_TEMP					; 

;*** Code Insert ***

	

;*** End of Insert ***

Exit
	SWAPF 	STATUS_TEMP,W				; Restore Status and W	
	MOVWF 	STATUS			                ;
	SWAPF 	W_TEMP,F					;
	SWAPF 	W_TEMP,W					;
	RETFIE							;

;*** End of ISR ***
 
@bananasiong
Thank you for answering. But as you can see I preserve values of STATUS and WORKING registers . Also, it is true that I can use 'retfie' but still it will not solve the problem. Why is it blinking? Maybe something resets POTRB and PORTA or maybe TRISB and TRISB.
Alright let's argue :D
Code:
;restore working i status register  [COLOR=Red]<-restore should be done RIGHT BEFORE returning from ISR![/COLOR]
        banksel    W_Save
        movf     STATUS_Save,W
        movwf     STATUS
        movf     W_Save,W
 
;---------------------------------------------------------------------------        
;enable transmision and reception
        banksel    TXSTA [COLOR=Red]<-STATUS register is changed before returning from ISR[/COLOR]
        bsf        TXSTA,TXEN
        banksel    RCSTA [COLOR=Red]<-STATUS register is changed before returning from ISR[/COLOR]
        bsf     RCSTA,CREN
 
;---------------------------------------------------------------------------
;Enable interrupts    
        banksel PIE1 [COLOR=Red]<-STATUS register is changed before returning from ISR[/COLOR]
        bcf     PIE1,TXIE
        bsf     PIE1,RCIE
        bsf        INTCON,PEIE
        bsf     INTCON,GIE
 
        return [COLOR=Red]<-so you want to return from ISR with bank 1 i guess[/COLOR]

Don't you agree?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top