PDA

View Full Version : Serial communication PIC to PIC help.



Rubicon
- 22nd January 2005, 20:48
Hello,

I'm a PBP novice using an old verion and have been having a hard time getting one way serial comminucation between two 16F84A pics to work. I find the manual confusing as to just what happens to numeric value when using SEROUT/SERIN

I've been trying to send a binary number from one PIC to the other where it's compared to the value in a vairable/constant. If there's a B1 then the program goes to a subroutine, if not then it continues on and repeats.

I've tried many different combinations of numeric value, baud rate etc, to no avail and I hope that someone here can post some SEROUT/SERIN sample code. If I can get some code that works then I can experiment to understand the way it works.

The code below is as I left it one frustrating night so please don't take the SEROUT/SERIN sections too seriously!

Any help here is appreciated.

Thankyou,

Rubicon.


'TRANSMITTER PIC 16F84A
'HARDWARE SETUP
DEFINE OSC 4
INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
PORTA = 0 'PORTA (133) outputs LOW
TRISA = %00000 'Set PORTA to all outputs
PORTB = 0 'PORTB (134) outputs LOW
TRISB = %00000000 'Set PORTB to all outputs
CLEAR 'Clear buffers and registers

'VAIRABLES
B0 VAR BYTE 'Serout vairable
B0 = %11100010 'Vairable value

'MAIN PROGRAM
LOOP1:
PAUSE 1000 'Pause for 1 second
SEROUT PORTB.0,T1200,[#B0] 'Output decimal equivalent
GOTO LOOP1 'Return to main program start
END

****************************************

'RECEIVER PIC 16F84A
'HARDWARE SETUP
DEFINE OSC 4
INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
PORTA = 0 'PORTA (133) outputs LOW
TRISA = %00000 'Set PORTA to all outputs
PORTB = 0 'PORTB (134) outputs LOW
TRISB = %00000001 'Set PORTB to all outputs but RB0
CLEAR 'Clear buffers and registers

'VAIRABLES
B0 VAR BYTE 'SERIN input vairable
B1 VAR BYTE 'B1 vairable
B1 = 226 'B1 vairable value

'MAIN PROGRAM
LOOP1:
SERIN PORTB.0,T1200,20,LED1,B0
IF B0 = B1 THEN LED2
GOTO LOOP1 'Return to main program start
END

'SUBROUTINES
LED1:
PORTB.1 = 1 'Light LED1 on RB1
PAUSE 1000 'Pause for 1 second
PORTB.1 = 0 'Extinguish LED1 on RB1
GOTO LOOP1 'Return to main program start

LED2:
PORTB.2 = 1 'Light LED2 on RB2
PAUSE 1000 'Pause for 1 second
PORTB.2 = 0 'Extinguish LED2 on RB2
GOTO LOOP1 'Return to main program start

mister_e
- 22nd January 2005, 21:21
you're close... some things need to be modify IMO before...



'TRANSMITTER PIC 16F84A
'HARDWARE SETUP
DEFINE OSC 4
INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
PORTA = 0 'PORTA (133) outputs LOW
TRISA = %00000 'Set PORTA to all outputs
PORTB = 0 'PORTB (134) outputs LOW
TRISB = %00000000 'Set PORTB to all outputs
CLEAR 'Clear buffers and registers


Must be set TRIS before sending 0 to PORT so.



'TRANSMITTER PIC 16F84A
'HARDWARE SETUP
DEFINE OSC 4
INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
TRISA = 0 'Set PORTA to all outputs
TRISB = 0 'Set PORTB to all outputs

PORTB = 0 'PORTB (134) outputs LOW
PORTA = 0 'PORTA (133) outputs LOW
'
'remove CLEAR ... you don't need it
'


I know i don't have to consider your SERIN/SEROUT but...


'MAIN PROGRAM
LOOP1:
PAUSE 1000 'Pause for 1 second
SEROUT PORTB.0,T1200,[#B0] 'Output decimal equivalent
GOTO LOOP1 'Return to main program start
END


By using # you'll send not decimal value... ASCII representation of the decimal value. In your case you'll send ASCII "2" + ASCII "2" + ASCII "6" wich is not what you want.

Simply remove #.



PORTA = 0 'PORTA (133) outputs LOW
TRISA = %00000 'Set PORTA to all outputs
PORTB = 0 'PORTB (134) outputs LOW
TRISB = %00000001 'Set PORTB to all outputs but RB0
CLEAR 'Clear buffers and registers

' ...........


'MAIN PROGRAM
LOOP1:
SERIN PORTB.0,T1200,20,LED1,B0
IF B0 = B1 THEN LED2
GOTO LOOP1 'Return to main program start
END



1. Change TRIS and PORT like i did before
2. Begin with SERIN PORTB.0,T1200,[B0] and after use SELECT CASE to test B0. If it's work, begin with timeout stuff.


In your case The timeout subroutine is killer, your pauses take too much time. Timeout 20ms... Timeout 1 sec... Synchronisation probleme. If you still want to use a timeout, at least use a bigger timeout period or use flow control pin.

Rubicon
- 24th January 2005, 10:49
mister_e

Sorry for the late response to your reply post. Unfortunately I had to deal with yet more adware and malware that slipped through.

Thankyou for the help. I had tried that combination of SEROUT/SERIN but it seems that my original IF-THEN statement was the problem here.
IF B0 = B1 or B0 = B2 THEN LED2

After truncating it to IF B0 = B1 THEN LED2 and starting again from the very beginning things began working for me. It was good to get your changes done and be using something that works.

My TRIS and PORT setup was originally like you advised but I must have misread a post and I changed it. It's back to how it should be now.

I haven't used SELECT CASE or the flow control pin before so I'll read up on them.

Thankyou,

Rubicon.

mister_e
- 24th January 2005, 15:45
Thankyou for the help. I had tried that combination of SEROUT/SERIN but it seems that my original IF-THEN statement was the problem here.
IF B0 = B1 or B0 = B2 THEN LED2


IF (B0 = B1) OR (B0 = B2) THEN LED2

can give you good result.

Great to know it's working for you now !