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.

What can reset PORTx ?

Status
Not open for further replies.

PIC2PIC

New Member
I made a code for communicating PIC1 to PIC2 .Both PICs are 16f877.The code uses UART. Once it receives data it send it to PORTA (in the PIC1) and PORTB (in the PIC2). Program is working, but something makes PORTs to turn off and on all the time and it is not supposed to do that. I guess something reset PORTs .

What can reset PORT ? It is not WDT, POR, BOR. I don't know what to do anymore . Any suggetions.
 
Here is Interrupt service routine. The job of ISR is to take a data from RCREG when it gets there and put it on PORT (PORTA or PORTB depending which PIC). There is also code for transmitter I will post later

Let's examine ISR first. Is it something wrong with it.


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;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

;---------------------------------------------------------------------------
;clean buffer
movf RCREG,W
movf RCREG,W

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

;---------------------------------------------------------------------------
;enable reception and transmition
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
;
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------
 
hi pic2pic,
Is the code the same as the code in your other thread Post number 5.??

If yes, I'll run thru it for you.:)
 
hi pic2pic.

The code from Post #5 runs OK in transmission and receive, BUT
the bcf RCSTA,CREN clears the RCREG receive buffer, so you never read the character in the RCREG!!!!

If you remove these two lines, the RCREG data is shown OK in PORTA.:)
Code:
;---------------------------------------------------------------------------  
;Disable reception and transmition  
	banksel	TXSTA
	bcf	TXSTA,TXEN
	;banksel	RCSTA
	;bcf	RCSTA,CREN

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

EDIT:
Image shows received numer '9' in PORTA.
 

Attachments

  • AAesp01.gif
    AAesp01.gif
    15 KB · Views: 185
Last edited:
@ericgibbs

Thank you for answering. I changed code for both PIC as you say, but it is still the same, it is still blinking both PORTA and PORTB . Are you shore that clearing bit CREN will clear the RCREG buffer ? I did not find that in the data sheet for 16f877.
 
Also I forget to say that I receive data that I send from one PIC to other but it is just blinking the problem.
 
Also I forget to say that I receive data that I send from one PIC to other but it is just blinking the problem.

I ran your code on the Oshonsoft simulator and RCEN clears the contents of the RCREG.
I did not see any blinking on the PORTA.

I used the code from Post #5 of your other thread. Please post the full code and I will try on on the sim.
 
@ericgibbs

Thank you for answering. I changed code for both PIC as you say, but it is still the same, it is still blinking both PORTA and PORTB . Are you shore that clearing bit CREN will clear the RCREG buffer ? I did not find that in the data sheet for 16f877.

hi,
Unzip this file, view using Windows Media player.

I sent a '9' to the PIC via the UART, you can see the '9' in the RCREG.
I single stepped the program and at CREN, the RCREG is cleared.
 

Attachments

  • pic2p.zip
    769.2 KB · Views: 110
Last edited:
Thank you all for your help.
I changed the code as you suggest me. Still is blinking. MCLR is high.
The new code is down (I try to comment it):
;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;
;---------------------------------------------------------------------------
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
;---------------------------------------------------------------------------
;save working and status register
movwf W_Save
swapf STATUS,W
movwf STATUS_Save

;banksel W_Save
;movwf W_Save
;movf STATUS,W
;clrf STATUS
;movwf STATUS_Save

;---------------------------------------------------------------------------
;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

;---------------------------------------------------------------------------
;take data and put it on PORTB
banksel RCREG
movf RCREG,W
movwf PORTB

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

;---------------------------------------------------------------------------
;enable reception and transmision
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

;---------------------------------------------------------------------------
;restore working and status register

banksel W_Save
swapf STATUS_Save,W ; Restore Status and W
movwf STATUS ;
swapf W_Save,F ;
swapf W_Save,W ;

;banksel W_Save
;movf STATUS_Save,W
;movwf STATUS
;movf W_Save,W

retfie

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


start
radix dec
;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////

;---------------------------------------------------------------------------
cblock 20h
brojac,brojac1,podatak,aktiviraj_transmisiju,STATUS_Save,W_Save
endc
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------



;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
; setting TRIS/A/B/C
;---------------------------------------------------------------------------


banksel TRISB

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


;---------------------------------------------------------------------------
;Seting UART
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 interrupts
;---------------------------------------------------------------------------
banksel PIE1
bcf PIE1,TXIE
bsf PIE1,RCIE
movlw b'11000000'
movwf INTCON

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

;---------------------------------------------------------------------------
Main

call Trans ;call routine for sending data

call Pauza ;make a pause

goto Main ;all the time
;---------------------------------------------------------------------------
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////
;Subroutine
;---------------------------------------------------------------------------
Trans
banksel INTCON
bcf INTCON,GIE
banksel PORTA
movf PORTA,W
movwf TXREG

banksel TXSTA
WtHere btfss TXSTA,TRMT
goto WtHere

banksel INTCON
bsf INTCON,GIE
nop

return

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

Pauza
movlw 50
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
 
ho pic2pic,
Downloaded your code, get back to you later.:)
 
I have tidied your code and removed parts that were repeated or unnecessary. The biggest problem I could see was that you enabled TX interrupts and didn't service them. Edit, just noticed it was bcf PIE1,TXIE not bsf so you weren't enabling the TX interrupt.

Here is the tidied code,
Code:
		list	p=16f877A	; list directive to define processor
		#include <p16f877A.inc> ; processor specific variable definitions
		errorlevel -302, -207

		org	00h
		goto	start


		org	04h
;save working and status register   
		movwf	W_Save
		swapf	STATUS,W 
		movwf	STATUS_Save 

;take data and put it on PORTB  
		banksel	RCREG
		movf	RCREG,W
		movwf	PORTB

;restore working and status register   
		banksel	W_Save
		swapf	STATUS_Save,W	; Restore Status and W 
		movwf	STATUS		;
		swapf	W_Save,F	;
		swapf	W_Save,W	;
		retfie


start
		radix	dec

		cblock	20h
brojac,brojac1,podatak,aktiviraj_transmisiju,STATUS_Save,W_Save 
		endc

; setting TRIS/A/B/C 
		banksel	TRISB
		movlw	0ffh
		movwf	TRISA
		movwf	TRISC
		movlw	00h
		movwf	TRISB
;Seting UART
		movlw	b'00000110'	; all analog pins = digital
		movwf	ADCON1

		banksel	SPBRG
		movlw	0x0C 
		movwf	SPBRG

		movlw	b'00100100'
		movwf	TXSTA

		banksel	RCSTA 
		movlw	b'10010000' 
		movwf	RCSTA 

;Setting interrupts 
		banksel	PIE1
		bsf	PIE1,RCIE
		movlw	b'11000000'
		movwf	INTCON

Main			 
		call	Trans		;call routine for sending data 
		call	Pauza		;make a pause 
		goto	Main		;all the time

Trans			 
		banksel	PORTA
		movf	PORTA,W
		movwf	TXREG
		banksel	TXSTA
WtHere		btfss	TXSTA,TRMT 
		goto	WtHere
		return


Pauza
		movlw	50
		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
Notice how easy it is to read. That is because I typed
Code:
 before it and
after it.

Mike.
 
Last edited:
ho pic2pic,
Downloaded your code, get back to you later.:)

hi,
Your program works OK, both in send and receive using the Oshonsoft simulator.
No sign of blinking LED's [port pins] on the PIC, the pins show and hold the data steady.

As pointed out, your code needs a tidy.
The problem must lie elsewhere in your circuit.:)

You say the LED's flash, can you give us more details of what is actually being seen.
 
Here is video capture of situation. Is not high quality. One can see LED on and LED off.
 

Attachments

  • vidcap0001.zip
    561.6 KB · Views: 106
Here is video capture of situation. Is not high quality. One can see LED on and LED off.

hi,
I see on the video that both sets of LED's are flashing at exactly the same time.

I cannot see any large decoupling capacitors on the power rails.???

The program runs without any problems.
I feel sure you have a hardware problem, check the power supply lines to the project board.
Use a scope or DVM to check if the power 'dips' when all the LED's are on.

EDIT:
How are you setting the CONFIG bits.?
Add this line just above the 'org 0x0000'

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _WRT_OFF & _LVP_OFF & _CPD_OFF

Add the required CONFIG to suit your osc.
 

Attachments

  • AAesp04.gif
    AAesp04.gif
    4.2 KB · Views: 135
Last edited:
Hi @ericgibbs

Thank you. My supply voltage is stable 5 V. I don't put CONFIG bits in code because program for transmit HEX file from my computer to PIC overrides CONFIG bits set in code. I use WINPICPRO 1.91 and I set fuses always "POWER UP" and "BROWN OUT" as check other clear, and XT for oscillator. I never had any problems before.
 
Is it possible to someone build the same circuit, to check if I am wrong. Its really simple.

One need :
2 -16f877
14 - LED and 14 resistors for LED (For PORTA and PORTB)
2 - chrystal
4 - 27 pF capacitors
Supply stable 5 V.

Just put LED and resistors on PORT/A/B. Connect what must one for PICs, set the program, connect two PICs . And that's it.
 
Is it possible to someone build the same circuit, to check if I am wrong. Its really simple.

One need :
2 -16f877
14 - LED and 14 resistors for LED (For PORTA and PORTB)
2 - chrystal
4 - 27 pF capacitors
Supply stable 5 V.

Just put LED and resistors on PORT/A/B. Connect what must one for PICs, set the program, connect two PICs . And that's it.

hi,
I have one 16F877A circuit already built, I could reprogram the PIC and link it to my PC via the RS232. I will get back to you later today.
 
Hi @ericgibbs

Thank you. My supply voltage is stable 5 V. I don't put CONFIG bits in code because program for transmit HEX file from my computer to PIC overrides CONFIG bits set in code. I use WINPICPRO 1.91 and I set fuses always "POWER UP" and "BROWN OUT" as check other clear, and XT for oscillator. I never had any problems before.

How do you set the WDT?

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top