Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

PIC to PIC communication using USART

Status
Not open for further replies.

tomas

Member level 1
Joined
Dec 16, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,553
I have wrote assembler code in MPLAB, which controls light diodes connected to RB0, RB1, RB2 and RB3 with switches. Switches connected to RB4, RB5, RB6 and RB7.

Here's the code: (working)
Code:
list P = 16F877 
#INCLUDE <p16f877.inc> 


;  -------------------------------------------------------------
#Define Switch1 PORTB,4
#Define Switch2 PORTB,5
#Define Switch3 PORTB,6
#Define Switch4 PORTB,7

#Define Output1 PORTB,0
#Define Output2 PORTB,1
#Define Output3 PORTB,2
#Define Output4 PORTB,3

                org     0x0000

;##########################################################################


Start
                
bsf STATUS,5
                movlw b'11110000'       ;4 inputs 4 outputs
                movwf TRISB             ;RB outputs
bcf STATUS,5
             clrf PORTB

MainLoop
                btfsc Switch1     
                goto Switch_1_Is_0
                bcf Output1 ;set "0" RB0
                goto Check_Switch_2
Switch_1_Is_0
                bsf Output1 ;set "1" RB0

Check_Switch_2
                btfsc Switch2
                goto Switch_2_Is_0
                bcf Output2
                goto Check_Switch_3
Switch_2_Is_0
                bsf Output2

Check_Switch_3
                btfsc Switch3
                goto Switch_3_Is_0
                bcf Output3
                goto Check_Switch_4
Switch_3_Is_0
                bsf Output3

Check_Switch_4
                btfsc Switch4
                goto Switch_4_Is_0
                bcf Output4
                goto MainLoop
Switch_4_Is_0
                bsf Output4
                goto MainLoop


                end             ; end of program
Problem:

I need that 2 PIC's would be connected via USART. On 1 Pic (transmmiter) should be 4 switches and they could control 4 leds connected to second PIC.
1 PIC: RB0, RB1, RB2, RB3 - switches
2 Pic: RB0, RB1, RB2, RB3 - LEDS

Can anyone help me with code? How to change it?
Or maybe someone already has this code in assembler? Please help:(

Im using two PIC16f877's.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,580
Helped
4,779
Reputation
9,575
Reaction score
4,558
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
124,278
Just set the two PICs up to the same Baud rate and cross over the TX and RX pins between them (TX on PIC1 to RX on PIC2). Any value you write to TXREG on one PIC will appear in RCREG on the other. If the distance between the PICs is greater than a few metres you may have to use a line driver and receiver to keep the waveform clean.

Brian.
 

tomas

Member level 1
Joined
Dec 16, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,553
I have the schematic in proteus, but I have a hard time when I try to initialize transmitter and receiver. Can anyone post here code for transmitter and receiver when first pic has one switch and second Pic has one LED? :(
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
I have the schematic in proteus, but I have a hard time when I try to initialize transmitter and receiver. Can anyone post here code for transmitter and receiver when first pic has one switch and second Pic has one LED? :(
Hi,

Why not post your code and .dsn in a zip or rar file so we can see where you have gone wrong .
 

tomas

Member level 1
Joined
Dec 16, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,553
Here is mine initialization code for transmitter:
Code:
list p=16f877	; set processor type
include <P16f877.INC>
 __CONFIG _XT_OSC & _PWRTE_ON & _BODEN_OFF & _CP_OFF & _WDT_ON

;-----------------------------------------------------------
; Reset and Interrupt Vectors

org	0x0000	; Reset Vector


; Konstantos -------------------------------------------------------------
#Define Switch1 PORTB,0
#Define Switch2 PORTB,1
#Define Switch3 PORTB,2
#Define Switch4 PORTB,3

; start program by enabling USART
 BANKSEL SPBRG ;select bank 1
movlw d'25'
movwf SPBRG ;initialize SPBRG
movlw h'24'
movwf TXSTA ;initialize TXSTA
BANKSEL RCSTA ;select bank 0
movlw h'90'
movwf RCSTA ;initialize RCSTA

;main
Start
                



end

and receiver:
Code:
list p=16f877	; set processor type
include <P16f877.INC>
 __CONFIG _XT_OSC & _PWRTE_ON & _BODEN_OFF & _CP_OFF & _WDT_ON

;-----------------------------------------------------------
; Reset and Interrupt Vectors

org	0x0000	; Reset Vector


; Konstantos -------------------------------------------------------------
#Define Output1 PORTB,0
#Define Output2 PORTB,1
#Define Output3 PORTB,2
#Define Output4 PORTB,3


; start program by enabling USART
 BANKSEL SPBRG ;select bank 1
movlw d'25'
movwf SPBRG ;initialize SPBRG
movlw h'24'
movwf TXSTA ;initialize TXSTA
BANKSEL RCSTA ;select bank 0
movlw h'90'
movwf RCSTA ;initialize RCSTA


;main

Start
                


end
Are they good? Can I write my main program next, or do I need some changes? :(
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Here is mine initialization code for transmitter:


Are they good? Can I write my main program next, or do I need some changes? :(

Hi,

I assume you are using a 4mhz xtal ? if so then yes that bit of code is good for 9600 baud.

However it is just the Initialisation part for the Usart routine.
Before that you must set the USART TrisC as below.

; RC6=TX RC7=RX TRISC must be set to 1 for each bit

movlw b'11000000' ; set TRISC rc6,rc7 as INPUTS for USART
movwf TRISC

followed by your init routine.

The Usart is then running but you need a basic input and output routine to send the ASCII characters.

movlw 'T' ; to output acscii character T
call tx_rs232


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


For receiving data -
serial_in call rx_rs232 ; wait for a character to be received
movwf useregister ; place received data in file

rx_rs232 will let you code that from the links I have given you.


If you want fuller details of Usart look here http://www.eti.pg.gda.pl/katedry/ksg/dydaktyka/dla_studentow//usart.pdf
 

tomas

Member level 1
Joined
Dec 16, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,553
I have no time writing this code :( maybe someone can write it for me? I can pay for it.
If there is anyone, write me email to discuss price justirtomas@gmail.com

- - - Updated - - -

image.jpg

Schematic
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
I have no time writing this code :( maybe someone can write it for me? I can pay for it.
If there is anyone, write me email to discuss price justirtomas@gmail.com

- - - Updated - - -

View attachment 84552

Schematic
Hi,

Yes, assembly does need a lot of detailed work.

Perhaps better to look at C compliers like MikroE which have lots of ready made modules or even Arduino which is even easier with so many existing modules and projects.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top