Looking for Basic Pic-to-Pic Serial Example


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Location
    British Columbia, Canada
    Posts
    20


    Did you find this post helpful? Yes | No

    Default Update

    I have updated the code as per Bruce and Al's suggestions.

    The lights stay on for a short period after the last button is released then go out.

    I am interested in reading the value of RB0-RB3 only from PORTB to a byte variable on the first PIC, then sending that over the wire to be written to PORTA on the second PIC.
    I ONLY want to read RB0-RB3 though, and ONLY want to write to RA0-RA3. Is there a statement that will allow this?

    Charles, I am very interested in using HSERIN in a future version of the project as it would be nice to let the PIC do something else besides wait for a signal. If you can point me at any example code it would be very much appreciated.


    Anyway, this is the code so far:
    Code:
    '****************************************************************
    '*  Name    : Output15.pbp                                      *
    '*  Author  : Greensasquatch                                    *
    '*  Date    : 10/11/2008                                        *
    '*  Chip    : PIC16F628A                                        *
    '*  Version : 1.5                                               *
    '*  Notes   : To output a number corresponding to which button  *
    '*          : was pressed via serial communication              *
    '****************************************************************
    '*  This Program is part 1 of 2.  The other half receives a 
    '*  number and lights a corresponding LED.
    '****************************************************************
    '* REVISIONS: comparators disabled
    '*
    '*
    '****************************************************************
    '*
    '*  Connections are as follows:
    '*                         
    '*                N/C  -01-|####|-18-   N/C
    '*                N/C  -02-|####|-17-  SEROUT Line
    '*                N/C  -03-|####|-16-   N/C
    '*      MCLR 10K to V+ -04-|####|-15-   N/C
    '*                 Gnd -05-|####|-14-  V+
    '*               Butt1 -06-|####|-13-   N/C
    '*               Butt1 -07-|####|-12-   N/C
    '*               Butt1 -08-|####|-11-   N/C
    '*               Butt1 -09-|####|-10-  Green Power LED
    '*
    '****************************************************************
    
    INCLUDE "MODEDEFS.BAS"
    
    'uses internal oscillator (should use crystal but ran out)
    @ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT      
    
    CMCON=7                 'disable comparators
    
    GreenLED var PORTB.4
    
    Butt1 var PORTB.0                             
    Butt2 var PORTB.1
    Butt3 var PORTB.2
    Butt4 var PORTB.3
    
    
    Pause 2000        'wait to begin
    high greenled     'turn on status light
    
    Loop:
    IF BUTt1 = 1 THEN SEROUT PORTA.0,T1200,[9,1]  'sends 9 and 1 if butt1 is high
    IF BUTt2 = 1 THEN SEROUT PORTA.0,T1200,[9,2]  'sends 9 and 2 if butt2 is high
    IF BUTt3 = 1 THEN SEROUT PORTA.0,T1200,[9,3]  'sends 9 and 3 if butt3 is high
    IF BUTt4 = 1 THEN SEROUT PORTA.0,T1200,[9,4]  'sends 9 and 4 if butt4 is high
    GOTO LOOP
    
    end
    And
    Code:
    '****************************************************************
    '*  Name    : Input15.pbp                                       *
    '*  Author  : Greensasquatch                                    *
    '*  Date    : 10/11/2008                                        *
    '*  Chip    : PIC16F628A                                        *
    '*  Version : 1.5                                               *
    '*  Notes   : To read a number via serial communication and     *
    '*          : light a corresponding LED                         *
    '****************************************************************
    '*  This Program is part 2 of 2.  The other half sends a 
    '*  number corresponding to a button state.
    '****************************************************************
    '* REVISIONS:  comparators disabled, lights out if no signal received
    '*
    '*
    '****************************************************************
    '*
    '*  Connections are as follows:
    '*                         
    '*                LED3 -01-|####|-18-  LED2
    '*                LED4 -02-|####|-17-  LED1
    '*                N/C  -03-|####|-16-   N/C
    '*      MCLR 10K to V+ -04-|####|-15-   N/C
    '*                 Gnd -05-|####|-14-  V+
    '*           Serial In -06-|####|-13-   N/C
    '*     Green Power LED -07-|####|-12-   N/C
    '*                N/C  -08-|####|-11-   N/C
    '*                N/C  -09-|####|-10-   N/C
    '*
    '****************************************************************
    
    INCLUDE "MODEDEFS.BAS"
    
    'uses internal oscillator (should use crystal but ran out)
    @ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT      
    
    CMCON=7                 'disable comparators
    
    GreenLED var PORTB.1   
    
    LED1 var PORTA.0                             
    LED2 var PORTA.1
    LED3 var PORTA.2
    LED4 var PORTA.3
    
    NET var byte                  'holding variable for input
    
    Pause 2000                    'wait to begin
    high greenled                 'turn on status light
    
    Loop:
    'waits for a "9" and writes the next value to NET, turn off LEDs if no signal
    SERIN PORTB.0,T1200,500,OffLed,[9],NET
        IF NET = 1 THEN HIGH led1  
        IF NET = 2 THEN HIGH led2
        IF NET = 3 THEN HIGH led3
        IF NET = 4 THEN HIGH led4
    GOTO LOOP
    
    OffLED:
        low led1
        low led2
        low led3
        low led4
    Goto Loop
    
    end
    And thanks again to all who have contributed.
    -Justin

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Here is a simple hardware sample to receive and send serially.
    It will echo from a terminal program.
    If I remember correctly mister E posted this a long time ago.
    Works well for testing a setup.
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 129 ' 2400 Baud @ 20MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_EVEN    1
    DEFINE HSER_BITS    7
    
    RCIF   VAR PIR1.5
    SERIALDATA  VAR BYTE
    
    PAUSE 100
    
    MAIN:
    IF RCIF THEN
        HSERIN  [SERIALDATA]
        HSEROUT [SERIALDATA]
        ENDIF
    GOTO MAIN
    Dave
    Always wear safety glasses while programming.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Here is a simple hardware sample to receive and send serially.
    It will echo from a terminal program.
    If I remember correctly mister E posted this a long time ago.
    Works well for testing a setup.
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 129 ' 2400 Baud @ 20MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_EVEN    1
    DEFINE HSER_BITS    7
    
    RCIF   VAR PIR1.5
    SERIALDATA  VAR BYTE
    
    PAUSE 100
    
    MAIN:
    IF RCIF THEN
        HSERIN  [SERIALDATA]
        HSEROUT [SERIALDATA]
        ENDIF
    GOTO MAIN
    Hello mackrackit,

    With this code can test the operation of a port rs232 with a pic?, I can say that pic would be used and the configuration of the pins is very interesting!.

    Thank you

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Leonardo View Post
    Hello mackrackit,

    With this code can test the operation of a port rs232 with a pic?, I can say that pic would be used and the configuration of the pins is very interesting!.

    Thank you
    You will need a level converter (max232) between the PIC and PC.
    Also the code was to show how to setup for different bits and so forth.
    Sorry if it confused things.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Aug 2008
    Location
    British Columbia, Canada
    Posts
    20


    Did you find this post helpful? Yes | No

    Default

    Thanks for the links, I found the third one to be the most help so far. The HSERIN info looks great too, I'm going to have to do some reading tomorrow on the defines.

    I've updated the code again a bit. It's now broken. I've set it up so that I read the lower 4 bits of PORTB and if they are greater than 0 a button must have been pushed so the data is sent via SEROUT. I've also set the TRIS registers, although I may have set them wrong since the chips no longer wait 2 seconds to light the Green Status LED, they just turn on right away. Also 3/4 of the indicator LEDs come on automatically and pushing buttons does nothing.

    I'm certain it's something to do with the TRIS registers but I haven't done anything different from the example in link 3 (that I can see) except to use BIN instead of HEX.

    It's late, I might be missing something obvious, I'll check to code again tomorrow night. For now I'll post what I have.

    Code:
    '****************************************************************
    '*  Title   : One Wire Serial Control                           *
    '*  File    : Output17.pbp                                      *
    '*  Author  : Greensasquatch                                    *
    '*  Date    : 10/13/2008                                        *
    '*  Chip    : PIC16F628A                                        *
    '*  Version : 1.7                                               *
    '*  Notes   : To output a number corresponding to which button  *
    '*          : was pressed via serial communication              *
    '****************************************************************
    '*  This Program is part 1 of 2.  The other half receives a 
    '*  number and lights a corresponding LED.
    '****************************************************************
    '* REVISIONS: 
    '*   -comparators disabled
    '*   -read RB0-RB3 to a byte and send this
    '*   -TRIS registers set
    '****************************************************************
    '*
    '*  Connections are as follows:
    '*                         
    '*                N/C  -01-|####|-18-   N/C
    '*                N/C  -02-|####|-17-  SEROUT Line
    '*                N/C  -03-|####|-16-   N/C
    '*      MCLR 10K to V+ -04-|####|-15-   N/C
    '*                 Gnd -05-|####|-14-  V+
    '*               Butt1 -06-|####|-13-   N/C
    '*               Butt1 -07-|####|-12-   N/C
    '*               Butt1 -08-|####|-11-   N/C
    '*               Butt1 -09-|####|-10-  Green Power LED
    '*
    '****************************************************************
    
    INCLUDE "MODEDEFS.BAS"
    
    'uses internal oscillator (should use crystal but ran out)
    @ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT      
    
    CMCON = 7                 'disable comparators
    TRISB = %00001111         'RB0-RB3 Inputs, all others output
    TRISA = 0                 'set portA as output
    
    GreenLED var PORTB.4    'alias RB4 to GreenLED
    Butt1 var PORTB.0       'alias Butt1 to RB0                      
    Butt2 var PORTB.1       'alias Butt2 to RB1
    Butt3 var PORTB.2       'alias Butt3 to RB2
    Butt4 var PORTB.3       'alias Butt4 to RBZ
    ButStat Var Byte        'Variable for button status
    
    Pause 2000              'wait to begin
    
    high greenled           'turn on status light
    
    Loop:
    ButStat = PORTA & %00001111 'isolate lower 4 bits of portA & write to ButStat
    
    'If ButStat has value greater than 0, then buttons must have been pushed,
    'then send 9 and the value of ButStat
    IF Butstat > 0 THEN SEROUT PORTA.0,T1200,[9,Butstat]
    GOTO LOOP
    
    end
    and

    Code:
    '****************************************************************
    '*  Title   : One Wire Serial Control                           *
    '*  File    : Input17.pbp                                       *
    '*  Author  : Greensasquatch                                    *
    '*  Date    : 10/13/2008                                        *
    '*  Chip    : PIC16F628A                                        *
    '*  Version : 1.7                                               *
    '*  Notes   : To read a number via serial communication and     *
    '*          : light a corresponding LED                         *
    '****************************************************************
    '*  This Program is part 2 of 2.  The other half sends a 
    '*  number corresponding to a button state.
    '****************************************************************
    '* REVISIONS:  
    '*  -comparators disabled, turn out lights when no signal 
    '*  -receive a byte and output to portA
    '*  -set TRIS registers
    '****************************************************************
    '*
    '*  Connections are as follows:
    '*                         
    '*                LED3 -01-|####|-18-  LED2
    '*                LED4 -02-|####|-17-  LED1
    '*                N/C  -03-|####|-16-   N/C
    '*      MCLR 10K to V+ -04-|####|-15-   N/C
    '*                 Gnd -05-|####|-14-  V+
    '*           Serial In -06-|####|-13-   N/C
    '*     Green Power LED -07-|####|-12-   N/C
    '*                N/C  -08-|####|-11-   N/C
    '*                N/C  -09-|####|-10-   N/C
    '*
    '****************************************************************
    
    INCLUDE "MODEDEFS.BAS"
    
    'uses internal oscillator (should use crystal but ran out)
    @ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT      
    
    CMCON=7                       'disable comparators
    TRISA = %11110000             'set RA0-RA3 as output, rest as input
    TRISB = %00000001             'set RB0 as input, rest as output
    
    GreenLED var PORTB.1          'alias RB1 to GreenLED
    
    NET var byte                  'holding variable for input
    
    Pause 2000                    'wait to begin
    high greenled                 'turn on status light
    
    Loop:
    'waits for a "9" and writes the next value to NET
    'turn off LEDs if no signal after 200ms
    SERIN PORTB.0,T1200,200,OffLed,[9],NET
    PORTA = NET                   'set PORTA to NET value
    GOTO LOOP
    
    OffLED:
        PORTA=0                   'set porta pins low
    Goto Loop
    
    end
    -Justin

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Did you read this one? It was a link from one ot the links.
    http://www.picbasic.co.uk/forum/showthread.php?t=7529
    Bruce and Steve explain.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Aug 2008
    Location
    British Columbia, Canada
    Posts
    20


    Did you find this post helpful? Yes | No

    Default

    I read the links through and through. Picked up on a few tip but haven't been able to get my code working. When I went back to a previous version where it HAD been working it now doesn't work. I'm thinking the internal OSC is just not stable enough for this application. I'll resume work on the project next week when I get new parts.
    Thanks to all who have contributed so far.
    -Justin

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


    Did you find this post helpful? Yes | No

    Default

    Could be... but @1200 baud, you shouldn't have much problem.

    However, could you also try few things
    1. Make sure your circuit is properly filtered, no noise on your Vdd line (decoupling caps here and there)
    2. Change SERIN/SEROUT for DEBUGIN/DEBUG
    3. Try with HSERIN/HSEROUT with the proper DEFINEs.
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 51  ' 1200 Baud @ 4MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    Steve

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

Similar Threads

  1. 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
  2. Automatic VB6 to pic serial connection
    By arniepj in forum Code Examples
    Replies: 13
    Last Post: - 10th January 2008, 07:57
  3. Serial Com Pic to Pic
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st May 2005, 10:06
  4. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14
  5. Serial communication PIC to PIC help.
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th January 2005, 15:45

Members who have read this thread : 0

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