PDA

View Full Version : PIC to PIC communication between 18F4620 & 10F202



rikaine
- 2nd May 2014, 08:49
Hello everyone,

This is my first post here. I would appreciate if you could show my mistakes.

I am trying to form a serial communication between 2 PICs: 18F4620 (Let's call this one A) and 10F202 (B)

18F4620(A) | 10F202 (B)
------------------------
AN0 -------|------ GP0
AN1 -------|------ GP1

PIC A has an LCD attached to it, so I can check what is going on, no such thing on B.

What I am trying to do is send data from A to B, then receive it back: First A -> B then B -> A

For test purposes I sent data from B to A and confirmed it is working.

Here is my code for A (18F4620)


include "modedefs.bas"

TRISA = 0 'Set PORTA to all outputs - NOT SURE IF THESE ARE REQUIRED
TRISB = 0 'Set PORTB to all outputs


DEFINE OSC 20
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
RW var portb.1:output portb.1:rw=0 ' LCD RW
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
Pause 1000
DEFINE HSER_BAUD 9600
ADCON1 = 15 ' ANALOGS to DIGITAL


SI VAR PORTA.0:input PORTA.0
SO VAR PORTA.1:output PORTA.1


GG VAR WORD:GG=18
TT var byte:TT=4


lcdout $FE,130,"SEROUT:[",#gg,"]"
SERout rout,N9600,[gg]


SERIN SI,1000,loop,N9600,tt
lcdout $FE,214,"SERIN:[",#tt,"]"


end


My code for B (10F202)

include "modedefs.bas"

TRISA = 0 'Set PORTA to all outputs ' Are these necessary?
TRISB = 0 'Set PORTB to all outputs


DEFINE OSC 4
ADCON1 = 7 ' ANALOGS to DIGITAL ?


ROUT var GPIO.0:output GPIO.0
RIN VAR GPIO.1:INPUT GPIO.1


RR var word


SERIN rin,N9600,rr
pause 1000


SERout rout,N9600,[rr]
END

I am quite new to PicBasic Pro, just put together some stuff I found but it is not working. I tried the above with loops as well, but since SERIN waits there till it receives something it looks fine to me this way.

Thanks in advance

Jerson
- 2nd May 2014, 10:06
The way you have coded, it will function like a single shot.

You may want to repeat the operation infinitely to ensure that it keeps doing the operations over and over again.

A Do-Loop construct or even a While-Wend might be helpful.
Something like this


Do
SERIN rin,N9600,rr
SERout rout,N9600,[rr]
Loop

You will need this on both sides. Eventually though, you will realize, you may need to receive using interrupts. That makes your code much more reliable as the communications you are trying to realize are asynchronous.

END does nothing other than tell the processor to spin like a dog running after its own tail on the same spot.

rikaine
- 2nd May 2014, 14:03
Re: Jerson

Thank you for your answer.

After reading your post I started testing with loops, but did not work. Now I will try interrupt.

mackrackit
- 3rd May 2014, 12:54
If you can not get it working in a loop you will never get an interrupt to work.
Take a look at this
http://melabs.com/samples/PBP-mixed/serin.htm

CuriousOne
- 6th May 2014, 06:17
Have you set DEFINE_OSC to proper values on both chips? various speeds can cause devices not to listen each other properly, unless you use HSERIN statement.

rikaine
- 6th May 2014, 13:30
Re: CuriousOne

Yes, OSC has been defined on both chips, however they are different from each other. 18F4620 is using OSC 20 (external), 10F202 is using OSC 4 (internal). Could this be the reason?

Re: mackrackit

I tried with loops, interrupts but nothing has worked so far. I think the main problem is synchronization. Let me try to explain:

In an ideal case it should be (skipping definitions):

18F4620
--------
PAUSE 1000
SEROUT PORTA.1,N9600,[GG]
SERIN PORTA.0,N9600,[TT]
lcdout $FE,214,"SERIN:[",#tt,"]"

10F202
-------
SERIN GPIO.1,N9600,[RR]
SEROUT GPIO.0,N9600,[RR]

time analysis:
TIME | 18F4620 ----------| 10F202
---------------------------------------
0 ---| PAUSE 1000 ------|SERIN (waiting)
1 ---| SEROUT ----------| message received
2 ---| SERIN (waiting) ---| SEROUT
3 ---| message received -|
4 ---| LCDOUT ----------|

I think 2-pic communications usually go in 1-way. Pic_1 receives data from Pic_2 and lights a led or something. But in this case both pics sending and receiving messages in a very short time should be causing the problem.

Demon
- 7th May 2014, 01:29
I've had PICs talk together with different osc speeds.

As long as the registers and communication commands are set properly, the hardware wired properly, it should work.

Robert

Demon
- 7th May 2014, 01:35
10F202 should wait to receive data, the master should wait to receive reply.

I don't see any waiting in the code above.


But in this case both pics sending and receiving messages in a very short time should be causing the problem.
Most likely.

Robert

rikaine
- 15th May 2014, 15:03
DEFINE OSCCAL_1K 1 'Load oscillator calibration

This single line of code added to 10F202 fixed all my problems.