Can someone tell me if I am going in the right direction?


Closed Thread
Results 1 to 26 of 26

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Posts
    24

    Default Can someone tell me if I am going in the right direction?

    Hello all,

    So I am attempting to write a program for the Lab X1 and Lab X2 board to talk to one another serially, I have started to put together a code in what I think is reasonably close to what I need to do. Unfortunately, I am having a hard time finding out just how to express some types of commands, as such, I am pretty sure what I have is dead wrong in some way. Can someone tell me if I am at least in the ballpark with what I need to be writing? Any help is very much appreciated.

    What I am trying to do is as follows:

    The code on the Lab X1 sends a command serially to turn on a led on the Lab X2, right now I just have the program continuously sending the command, just so I can figure out how to send it.

    The code on the Lab X2 receives to code and turns on LED2, and keeps it on. It also turns on LED3, to show that the loop is in fact working.

    I have the two boards connected by pin D0 on the Lab X1 and pin B0 on the Lab X 2

    Here are the codes I have written thus far, they do not work at all, besides turning on LED3.

    Lab X1 code:

    DEFINE OSC 20
    INCLUDE "modedefs.bas"
    TRISD.0=1

    LED VAR BYTE

    LOOP:
    LED=%00000001
    SERout PORTD.0,T2400,[LED]
    GOTO LOOP

    END

    And the Lab X2 code:

    DEFINE OSC 20
    INCLUDE "modedefs.bas"
    TRISB=%00000001

    LED VAR BYTE
    LED=%00000000

    LOOP:
    PORTB.2=1
    SERIn PORTB.0,T2400,[LED]
    IF LED=%00000001 THEN
    PORTB.1=1
    ELSE
    GOTO LOOP
    ENDIF
    GOTO LOOP

    END

    Thank you for putting up with my extremely novice understanding of PicBasic Pro.
    Matt

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    DEFINE OSC 20
    INCLUDE "modedefs.bas"
    TRISD.0=1<<<===You've set D.0 to an INPUT here!!! Might want an OUPUT!
    LED VAR BYTE
    LOOP: LED=1 : SERout PORTD.0,T2400,[LED] : PAUSE 5 : GOTO LOOP
    END
    
    And the Lab X2 code:
    
    DEFINE OSC 20
    INCLUDE "modedefs.bas"
    TRISB=%00000001<<<===And here too, but this is correct
    LED VAR BYTE : LED=0
    LOOP: PORTB.2=1 : SERIn PORTB.0,T2400,[LED]
    IF LED=1 THEN
    PORTB.1=1
    ELSE
    PORTB.1=0<<<===Might want to turn the LED off if you don't receive anything
    PAUSE 100
    ENDIF
    GOTO LOOP
    END

  3. #3
    Join Date
    Jun 2008
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Okay, I fixed the rather obvious problems you pointed out (dumb on my part). But it still dosn't work on the receiving end...Is there something wrong with my SERIN command? I believe I told it to write the serial value to LED variable. But that seems to be the only thing left that could be wrong.

    Thank you for the help,
    Matt

  4. #4
    Join Date
    Jun 2008
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Actually, after some investigation, it turned out to be a hardware malfunction. Thank you again for the help.

    So, my Serial commands where more or less correct? Wow...I was sure with the little understanding of serial communication I have that they would be dead wrong.

    Thanks again man, this will help me out tons.
    Matt

  5. #5
    Join Date
    Jun 2008
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Actually, it's not working, after making changes to my program, I realized that it isn't working at all. (nothing responded to any changes in the code, what I took to be working, was really just the serial connection powering a led directly, damn my optimism)

    Anyways, here is what I have now

    Transmitting Code:

    CLEAR

    DEFINE OSC 20 'Defines oscillator rate
    INCLUDE "modedefs.bas"
    ADCON1=%11111111 'Sets all ports to digital
    TRISD.0=0 'Sets Portd.0 to output
    LED VAR BYTE : LED=1 'Declares and defines LED variable
    LOOP: 'Start of main loop
    SEROUT PORTD.0,T2400,[LED] : PAUSE 50 'Sends LED value serially
    'out of portd.0
    GOTO LOOP 'Repeat forever
    END

    Receiving Code:

    CLEAR

    DEFINE OSC 20 'Defines oscillator
    INCLUDE "modedefs.bas"
    ADCON1=%11111111 'Sets all ports to digital
    TRISB=%00001000 'Sets Portb.4 to input
    LED VAR BYTE : LED=0 'Declares and sets LED variable to 0
    LOOP: SERIN PORTB.4,T2400,[LED]: PAUSE 50 'Checks portb.4 for serial imput
    IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
    PORTB.1=1 'off, otherwise turn 3 on and two off
    PORTB.2=0
    ELSE
    PORTB.1=0
    PORTB.2=2
    ENDIF
    PAUSE 100 'Pause to see results
    GOTO LOOP 'Repeat
    END

    Thanks, I apologize for my lack of knowledge.
    Matt

  6. #6
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    Matt

    In the transmitting code. you turn the LED on with
    LED=1

    Then you enter the loop to send to the Port.

    Then on the receiving side you keep reading the LED value.

    and apply some logic

    Code:
    IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
    PORTB.1=1 'off, otherwise turn 3 on and two off
    PORTB.2=0
    ELSE 
    PORTB.1=0
    PORTB.2=2
    ENDIF
    Maybe you should check your logic and make sure it works on the LAB-X2 before you go to the Serial port.

    I might comment out this - 'SERIN PORTB.4,T2400,[LED]: PAUSE 50' and toggle the LED on and off. Then go from there.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    TX Side:
    Send byte value of "1" continuously with 50ms between each byte sent (1 = %00000001, as in the number right after 0, just to keep us both straight here)
    Code:
    DEFINE OSC 20                   'Defines oscillator rate
    INCLUDE "modedefs.bas"
    CLEAR
    LED VAR BYTE : LED = 1 : ADCON1=$ff : TRISD.0=0
    LOOP: SEROUT PORTD.0,T2400,[LED] : PAUSE 50 : GOTO LOOP
    END
    Rx Side:
    Start with the LED off. If a byte of value = %00000001 is received, turn the LED on PortB.1 on.
    If a value of %0000001 is received, PortB.1 will turn on and stay on.
    If something other than a value of %00000001 is received, the SERIN command will take it and PortB.2 will flash for 1/10 second then go back to trying again.
    If nothing is received after 2 seconds, both LEDs will flash for 1 second then go back to trying again.
    Code:
    DEFINE OSC 20                       'Defines oscillator
    INCLUDE "modedefs.bas"
    CLEAR
    LED VAR BYTE : LED = 0 :ADCON1=$ff : TRISB=8
    LOOP: SERIN PORTB.4,T2400,2000,nothing,[LED]
    IF LED=1 THEN
     PORTB.1=1
    ELSE
     PORTB.2 = 1 : pause 100 : PORTB.2 = 0
    ENDIF
    GOTO LOOP
    nothing: PortB=3:pause 100:portb.0:pause 100:portb=3:pause 100:portb=0:pause 100
    PortB=3:pause 100:portb.0:pause 100:portb=3:pause 100:portb=0:pause 100
    PortB=3:pause 100:portb.0:pause 100:goto loop
    END
    Of course, this assumes all the hardware is working correctly...

Similar Threads

  1. Math help - rolling average Wind Direction
    By wjsmarine in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 23rd July 2009, 00:08
  2. How to detect sound direction?
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th December 2008, 04:53
  3. Replies: 10
    Last Post: - 8th April 2008, 21:07
  4. Replies: 5
    Last Post: - 12th September 2007, 15:59
  5. Replies: 26
    Last Post: - 2nd January 2006, 18:31

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