PDA

View Full Version : communicating pic18f452 with pic16f877a



tamersoss
- 13th July 2007, 10:29
Dear Freinds ...

I tried to make a serial communication between two mcu's which are pic18f452 with 10 mhz osc and a compiled at HSPLL and on the other side a pic16f877a With 20 mhz compiled at HS ... Note that i uses pic basic pro compiler and a MPASM assembler .... I defined the first mcu to have 40 Mhz speed and the other 20 mhz ... THe two mcu's are connected directly with no resistors . The pic 18f452 uses RA2 and RA3 to send and receive data while pic16f877a uses RE1 and RE2 ... I set these pins to digital using Adcon1 = 7 .... i really couldn't find anything wrong ... Could any body help?

Here is the two piece of code that should communicate together ... Note that the message is sent first from PIC18f452 (master controller) with a qualifier "MS" and the first item = the 3rd one , also the 2nd item=the 4th ... this is made to let the pic16f877a evaluate the message and if true it gives a confirmation =250 to the master controller ... else it may give 0 or even no response if no message is received ... The master controller retries 3 more times if cnf received =0 or no response then it goes to a subroutine called "DN" which stops every thing .

Here is the code :


'PIC18F452 SUBROUTINE :

DEFINE OSC 40
DEFINE CHAR_PACING 100
ADCON1=7
SFVC VAR PORTA.2
STVC VAR PORTA.3

VC: I=0
SR: IF I>3 THEN
ERR=36
GOTO DN 'GOTO DANGER SUBROUTINE IF COMMUNICATION FAILED 4 TIMES.
ENDIF

PAUSEUS 1000 'DELAY TO MAKE SURE THAT THE OTHER CONTROLLER IS WAITING THE SERIAL MESSAGE.
serout STVC,4,["MS",V1V8,V9V11,V1V8,V9V11] 'SERIAL MESSAGE TO VALVES CONTROLLER.
I=I+1
CNF=0
serin SFVC,4,100,SR,[CNF] 'RECEIVE CONFIRMATION.
IF CNF <> 250 THEN GOTO SR
RETURN


' PIC16F877A SUBROUTINE

DEFINE OSC 20
DEFINE CHAR_PACING 100
ADCON1=%10001110 'ONLY AN0 IS ANALOG PIN.
SR: CNF=0
SERIN PORTE.2,4,1000,A,["MS"],V1V8,V9V11,C1C8,C9C11 'SERIAL MESSAGE FROM MASTER CONTROLLER.
IF V1V8 = C1C8 AND V9V11 = C9C11 THEN
PORTB=NOT(V1V8)
PORTD=NOT(V9V11)
CNF=250
ENDIF
PAUSEUS 1000 'DELAY TO MAKE SURE MASTER CONTROLLER IS WAITING.
serout PORTE.1,4,[CNF ] 'SERIAL MESSAGE TO MASTER CONTROLLER.
IF CNF <> 250 THEN SR

mackrackit
- 13th July 2007, 11:24
The first thing I notice is a 10 Mhz osc defined as 40 Mhz.

Second is the SERIN/SEROUT commands.

Should read something like

SEROUT PIN, BAUDE RATE,[DATA]


Looks like the BAUD RATE (MODE) is missing.

Third, you will need to have

INCLUDE "modedefs.bas
at the beginning of your program.

Darrel Taylor
- 13th July 2007, 15:44
I think the problem might be on the TX side ...
CNF=0
serin SFVC,4,100,SR,[CNF] 'RECEIVE CONFIRMATION.

The CNF is in the "qualifier" brackets, so SERIN will wait to receive a 0 byte.
But 250 is what's being sent for confirmation, and it'll never see it.

To receive the byte into the CNF variable, it should be without the brackets.
CNF=0
serin SFVC,4,100,SR,CNF 'RECEIVE CONFIRMATION.

Then the

IF CNF <> 250 THEN GOTO SR

should work.

tamersoss
- 13th July 2007, 19:54
Dear Taylor ....

You were right ... It works now because of you ... It's really amazing to have a freind like you ... many thanks.