Communication between two PicChip


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    When I mentioned hardware serial port, I was referring to TxD and RxD. Then you could use HSEROUT and HSERIN. Connect those two lines together on your PIC, and you can communicate at 9600 baud at least, on the internal oscillators.
    Charles Linquist

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    I just want to do a very simple communication at first, then I will work on better one. I am using the same code as i used a few ago for a project on mine. It was working fine at the time. I was using a different compiler and different software.
    I am trying to re-use that code. Now I have taken THE code I used and just removed the extra stuff in the programming that is not needed ( calculation from a wireless thermometer) here is my sending code:

    INCLUDE "modedefs.bas"
    OSCCON = %01110000 '8 Mhz
    Define OSC 8

    CMCON = 7 : ANSEL = 0 : ADCON1 = 7
    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////

    DEFINE LCD_DREG PORTA ' Set LCD Data port
    DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 0 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 6 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 500

    TRISB = %00000000

    count_remain var byte
    temperature1 var byte
    temperature2 var byte

    Mainloop:

    temperature1 =11: temperature2 =22
    count_remain =44



    lcdout $FE,1, "TempC: ", dec (temperature1) , ".", dec2 temperature2," ",$DF,"C"
    lcdout $FE,$C0, bin count_remain , ".","F"
    pause 400

    serout portb.2, n2400,[$55,$55,$55,$55,$aa,temperature1,temperature2,coun t_remain]

    lcdout $FE,1 : LCDOUT "second ", dec (temperature2 / 100) , ".", dec2 temperature1," ",$DF,"r"
    lcdout $FE,$C0, dec count_remain , ".","23"
    pause 400

    goto mainloop

    end
    and here is the receiving code:
    INCLUDE "modedefs.bas"
    OSCCON = %01110000 '8 Mhz
    Define OSC 8

    CMCON = 7 : ANSEL = 0 : ADCON1 = 7
    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////

    DEFINE LCD_DREG PORTA ' Set LCD Data port
    DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 0 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 6 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 500

    ADCON1 = %00100010

    encoded11 var word : encoded22 var word : encoded33 var word : encoded44 var word : encoded4 var byte
    count_remain var byte :temp var byte


    TRISA = %00000001 ' Set PORTA to all output
    TRISB = %00001111


    Mainloop:
    '23

    lcdout $FE,1 , "hello ", dec 105," ","C"
    lcdout $FE,$C0, "before"
    pause 300

    waitfor55:
    serin portb.2 , n2400 , temp : if temp <> $55 then goto waitfor55

    waitforaa:
    serin portb.2 , n2400 , temp : if temp <> $aa then goto waitforaa

    serin portb.2, n2400, encoded11.HighBYTE : serin portb.2, n2400, encoded11.LowBYTE
    serin portb.2, n2400, encoded22.HighBYTE


    lcdout $FE,1, "encoded11.HighBYTE", dec encoded11.HighBYTE , ".", dec2 encoded11.LowBYTE
    lcdout $FE,$C0, "TempF: ", "-", dec encoded22.HighBYTE , "After"
    Pause 300


    goto Mainloop

    end

    the sending code seems to work. but the receiving code seems to be stuck on the first LCDOUT, I guess it does not see the 55 or aa...

    k

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    That is why you need to use the hardware serial port! If you use SERINx you have to sit and wait for the first transition of the start bit of the first char to come in. If you use HSERIN instead, you could simply test for PIR1.5 (at least that is what it is in the 18Fs). It gets set AFTER the whole byte is received, so in just a few instructions, you can tell if the first byte came in. If that bit isn't set, move on. If it is set, then you know there is a char in the receive buffer, so then you can issue the command HSERIN [dummy]. If Dummy = $55 then you can continue to grab chars. Virtually no overhead, and as long as you give it a
    little thought, you don't even need interrupts. It will NEVER lose the first character.

    Although I always advocate using interrupts, if you didn't want to mess with them, you could:

    Figure out the loop time of your slave device. Then you could do the following:

    Main:
    GotChars = 0
    If PIR1.5 then
    Hserin [FirstByte] ;------------ You don't need a timeout here because you already know you have a char
    if FirstByte = $55 then
    HSERIN 50,NotValid, [STR receivebuffer \numbytes]
    GotChars = 1
    endif
    endif
    NotValid:
    ....rest of program here....

    goto main

    ;------------------------------------------------------------------------------------
    And your master -

    .....

    HSEROUT [$55]

    pause (this should equal the length of the slave's program loop + a few mSec)

    HSEROUT [whatever you want to send]

    .......
    Charles Linquist

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    i know what you mean. but why did it work before and now it does not.... i can see from my scope the the same signal is going in the pic.. why the heck doesn't it read it.

    K

  5. #5
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    Maybe you should try smaller-value pull-up resistors. Although most I2C devices can't sink much current, a PIC can sink 25mA easily. You could use 560 ohm pull-up resistors on each end if you had an extremely noisy environment or long wires.

    Also, if your two devices aren't on the same PCB, could there be a difference in GND potential between the two devices?
    Charles Linquist

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    and i do have a I have the two breadboards sitting side by side, so maybe 9inch of wire between the two. I added 560ohmpull up resistors but nothing changed from before. and I do have a ground wire between them ..
    Last edited by lerameur; - 14th November 2011 at 04:40.

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Communication between two PicChip

    WHAT is the best technique to communicatebetween two Pic Chip ??
    I2C ? HSEROUT ? .....

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