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.

16f877 - 16f876 simlpe USART

Status
Not open for further replies.

PIC2PIC

New Member
Hello !

I try to make simple serial communications between PIC 16f876 as transmiter and PIC 16f877 as receiver. But it won't work. I am new to this UART communications so I don't understand where the problem in my code is. Please tell me where the problem is.

The code simply reads PORTB on 16f876 ,takes that data and send it over UART to 16f877 witch turns on and off LED on PORTD in the same order.

The code for transmiter (i.e. 16f876) is :


bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm



bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm

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



;bit spen (rcsta<7>) and bits trisc<7:6> have to be set


bank0
bsf 18h,7 ;bit spen (rcsta<7>) set
bsf 07h,6 ; Initialize port C


bank1


movlw 0ffh
movwf 86h
bsf 87h,7




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

;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2

movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 Mhz Quartz



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

;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.

bank1
bcf 98h,4

bank0
bsf 18h,7

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

;3. if interrupts are desired, then set enable bit
;txie .


bank1
bcf 8ch,4

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

;4. if 9-bit transmission is desired, then set transmit
;bit tx9 .

bcf 98h,6


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


;5. enable the transmission by setting bit txen
;which will also set bit txif.

bank1
bsf 98h,5



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

;6. if 9-bit transmission is selected, the ninth bit
;should be loaded in bit tx9d.



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

;7. load data to the txreg register (starts transmission).


circle
bank0
movf 06h,0 ;byte that I take from PORTB and send it to
movwf 19h ;txreg



bank1

wait_a_moment btfss 98h,1
goto wait_a_moment



goto circle

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

;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.



end


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


The code for receiver (i.e. 16f877) is:


bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm



bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm

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



;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set


bank0
bsf 18h,7
bsf 07h,6

bank1


movlw 00h
movwf 88h
bsf 87h,7




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

;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2

movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 mhz


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

;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.

bank1
bcf 98h,4

bank0
bsf 18h,7

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

;3. If interrupts are desired, then set enable bit
;RCIE..


bank1
bcf 8ch,5

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

;4. 4. If 9-bit reception is desired, then set bit RX9.

bank0
bcf 18h,6


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


; 5. Enable the reception by setting bit CREN.
bank0
bsf 18h,4



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

;6. Flag bit RCIF will be set when reception is complete
;and an interrupt will be generated if enable
;bit RCIE is set.



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

;7. Read the RCSTA register to get the ninth bit (if
;enabled) and determine if any error occurred
;during reception.



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


;8. Read the 8-bit received data by reading the
;RCREG register.

vrti
bank0
movf 1ah,0 ;byte that I send to LED on PORTD
movwf 08h ;txreg



bank1

cekaj btfss 0ch,5
goto cekaj



goto vrti

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

;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.



end
 
hi,

I have checked your TX section on the Oshonsoft simulator it works OK.
I would suggest for testing that you add an inter character delay into the transmit rate.

The problem is in the RX section.

I'll look at that later.:)
 
hi,
PIR1 is in Bank0 not Bank1.
Deleted that Bank1 and the RX section works... go luck.:)

Code:
;8. Read the 8-bit received data by reading the  
;RCREG register.  

vrti
	bank0
	movf	RCREG,0		;byte that I send to LED on PORTD
	movwf	PORTD		;txreg

	;bank1

cekaj	btfss	PIR1,5
	goto	cekaj

	goto	vrti
 
I would VERY strongly recommend you use the MicroChip supplied mnemonics for the registers and bits, your code makes no sense otherwise, and is FAR more likely to contain errors.
 
ericgibbs
Thank you very very much. You make my life beter. I love you man:).

Nigel Goodwin
Thank you for your advice. I agree, but where are theese mnemonics. How can I find it.
 
ericgibbs
Thank you very very much. You make my life beter. I love you man:).

Nigel Goodwin
Thank you for your advice. I agree, but where are theese mnemonics. How can I find it.

hi,
Your welcome.

Goto Microchip Technology Inc. - a Leading Provider of Microcontroller and Analog Semiconductors and download the free MPLAB IDE v7.xx.

Install the program in the C: drive as recommended.

Create a new folder in the same folder as the program, called say, Mywork1.

Use the Wizard to start writing new programs, use the inbuilt Help files.

Attached your programs with some editing for the names, paste them into MPLAB
 

Attachments

  • commsRX1.asm
    2.5 KB · Views: 283
  • commsTX1.asm
    2.5 KB · Views: 276
Last edited:
ericgibbs
Thank you very very much. You make my life beter. I love you man:).

Nigel Goodwin
Thank you for your advice. I agree, but where are theese mnemonics. How can I find it.

They are the ones used in all the datasheets and examples, you add them using an include file - check my tutorials for examples.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top