Serial communication PIC to PIC help.


Closed Thread
Results 1 to 4 of 4
  1. #1
    Rubicon's Avatar
    Rubicon Guest

    Default Serial communication PIC to PIC help.

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    you're close... some things need to be modify IMO before...

    Code:
    '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.

    Code:
    '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...
    Code:
    '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 #.

    Code:
    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Rubicon's Avatar
    Rubicon Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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 !
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. 2 PIC, serial communication
    By lightgreen in forum Serial
    Replies: 6
    Last Post: - 21st November 2009, 15:20
  2. Replies: 11
    Last Post: - 12th July 2008, 02:36
  3. PIC to PIC "wired" serial one-way communication - SERIN2/SEROUT2
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th April 2008, 20:02
  4. Replies: 5
    Last Post: - 20th March 2006, 01:34
  5. Pic To Pic Serial Communication?
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 5th February 2005, 00:59

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts