A code scrutiny question


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default A code scrutiny question

    Hello all,

    I am a newbie when it comes to picbasic, which brings me to my question. If I post my present code I am entertaining, will one of you fine programmers scrutinize it for me? It should be pretty straight-forward. I am looking at controlling relays with a pic. I don't need advice on what controller to use, and so forth...moreover, am I using the correct code or is there a more efficient way.
    Anyhow, here goes:

    \\CODE START//

    INCLUDE "bs2defs.bas"
    DEFINE OSC 20
    SerialInput var PORTA.0 'serial input pin
    board var byte
    relay var byte
    stat var byte
    trisa = %00000001
    trisb = %11111111
    trisc = %00000000
    trisd = %00000000
    trise = %00000000
    boardAddr = PORTB '<<<<<<<8 bit address set via dip switches

    Start :

    ' Wait until the character “B” AND board address is received serially on porta.0
    serin2 serialinput,84,[WAIT ("B",board)]
    if board <> boardAddr then goto start
    ' Wait until the character “R” is received serially on porta.0 and put next character into B1
    SERIN SerialInput,N9600,["R"],relay
    ' Wait until the character “S” is received serially on porta.0 and put next character into B2
    SERIN SerialInput,N9600,["S"],stat

    '****Send serial from pc like so?: MSComm1.Output = "B??" + Chr$(13) + "R??" + Chr$(13) + "S??" + Chr$(13)
    '****Send serial from pc like so?: MSComm1.Output = "B??R??S??" + Chr$(13)

    SELECT CASE relay
    CASE 1
    IF stat = 1 THEN HIGH PORTA.4
    LOW PORTA.3
    CASE 2
    IF stat = 1 THEN HIGH PORTA.3
    LOW PORTA.4
    CASE 3
    IF stat = 1 THEN HIGH PORTA.5
    LOW PORTA.2
    CASE 4
    IF stat = 1 THEN HIGH PORTA.2
    LOW PORTA.5
    CASE 5
    IF stat = 1 THEN HIGH PORTE.0
    LOW PORTA.1
    CASE 6..........

    END SELECT
    goto Start

    \\CODE END//

    I sure hope one of you fine people can assist me. If my application is unclear, please let me know. Thank you much.

    Chris

  2. #2
    Join Date
    Aug 2007
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    you should state what kind of Uc u using.

    anywhere if you want to use PORTA which is analog mode.Normally alot people for get to change it to digital mode. you need to add this command in your code

    CMCON=7 'change the pin to digital
    ADCON1 = 7 ' for PIC16C7xx, 16F87x, 12C67Fx
    ANSEL = 0 ' register for 12F675 and 16F676

    people tend to forget to set all this before using the analog pin.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by kevlar129bp View Post
    Hello all,
    Code:
    ' Wait until the character “B” AND board address is received serially on porta.0
    serin2 serialinput,84,[WAIT ("B",board)]
    if board <> boardAddr then goto start 
    ' Wait until the character “R” is received serially on porta.0 and put next character into B1
    SERIN SerialInput,N9600,["R"],relay
    ' Wait until the character “S” is received serially on porta.0 and put next character into B2
    SERIN SerialInput,N9600,["S"],stat
    Chris
    I think you will need to change the statements as shown below
    Code:
    ' Wait until the character “B” AND board address is received serially on porta.0
    SERIN2 serialinput,84,[WAIT ("B",DEC2 board)]
    if board <> boardAddr then goto start 
    ' Wait until the character “R” is received serially on porta.0 and put next character into B1
    SERIN2 SerialInput,84,["R"],DEC2 relay
    ' Wait until the character “S” is received serially on porta.0 and put next character into B2
    SERIN2 SerialInput,84,["S"],DEC2 stat
    Here I am assuming the B??R??S?? each gets a 2 digit decimal value

  4. #4
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108


    Did you find this post helpful? Yes | No

    Default

    Thank you Mike and Jerson. To explain in more detail, I'll try to answer your questions as best I can.

    The mcu I plan on using is the 16F871.

    On port B, I'll have 8 dip switches for the "board" address (set high or low).
    0 to 256 ex: B129

    There will be 24 GPIO's left over, therefore they will be the "relay".
    1 to 24 ex: R22

    The "stat" will either be on or off.
    0 or 1 ex: S0

    In the above example, I would like to:
    On board 129, turn relay 22 off

    On that note, I commented 2 lines in the code to possibly garner advice on the proper way to send those commands from vb.net. If any of you could comment on that as well, that would be great.
    Thank you guys again.

    Chris

  5. #5
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    I think the second line is good.
    MSComm1.Output = "B??R??S??" + Chr$(13)

  6. #6
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108


    Did you find this post helpful? Yes | No

    Default

    Thank you Jerson,
    I suppose I could try both in VB. I'll go with your suggestion first.

    Chris

Similar Threads

  1. 16F to 18F transition pains
    By lurker in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th December 2008, 01:35
  2. Code to control 4 LEDs and 2 servos
    By The Master in forum Off Topic
    Replies: 2
    Last Post: - 26th November 2008, 13:33
  3. General Question About Multiple Interrupts
    By grzeh666 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 31st August 2008, 17:09
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Code for I2C Slave
    By Balachandar in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st November 2006, 19:14

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