newbie with serial com question...


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default newbie with serial com question...

    I am a newbie, to say the least, to pic programming. I have a bit of vb software that i would like to use to control simple outputs on a pic. I believe the application is commonly referred to as a relay controller. I don't really need design help, moreover serin command help. I am using an example from http://www.rentron.com/PIC16F84.htm. I'll be using a PIC16F871.
    Here's some of the code...

    INCLUDE "bs2defs.bas"
    DEFINE OSC 20
    address VAR b2 'board number storage variable
    relay VAR b3 'relay number storage variable
    stat VAR b4 'relay status ON/OFF variable
    serpin VAR porta.0 'serial input pin
    trisa = %00000001
    trisb = %11111111
    trisc = %00000000
    trisd = %00000000
    trise = %00000000
    addr = portb

    If address = addr Then
    SERIN serpin,N9600,board,relay,stat
    SELECT CASE relay
    CASE 1
    IF stat = 1 THEN HIGH PORTA.1
    LOW PORTA.1
    CASE 2
    IF stat = 1 THEN HIGH PORTA.2
    LOW PORTA.2
    CASE 3
    IF stat = 1 THEN HIGH PORTA.3
    LOW PORTA.3
    CASE 4
    AND SO ON.....

    My question is in regards to the use of portb as the 8 bit address for the pic itself. I would like to send a command from the pc to the pic similar to:

    MSComm1.Output = address + relay + stat

    I would like the pic to allow further processing only upon matching the portb status to the incoming address. Wow, I hope I explained that well enough!?
    My goal is to have a few of these boards on the same serial bus, and be able to address each output individually. Well, hopefully one of you gurus can steer me in the right direction.

    Thanks to all,
    Chris

  2. #2
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Highly Doable

    Hi,

    As far as I understood you want to have a network of PICs on a common drop down serial bus and address them on specific requests . So your protocol may be a simple two byte thing, the first byte mentioning which PIC and the second what to do actually. Well if you are putting a DIP switch on a portb and using your entire portb as inputs then you can compare it with incoming address. In all PICs the ports are memory mapped registers so you can do a read directly.

    So as you did, It should work, like:

    IF incoming_address = portb then

    or you can have your own byte variable

    myvariable = portb

    Then compare or process your variable.
    Last edited by sougata; - 10th December 2006 at 05:48.
    Regards

    Sougata

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


    Did you find this post helpful? Yes | No

    Smile Thanks Sougata

    You described it exactly! As far as the code I posted, does it look like it should do what you described, or did I miss something?

    Thanks again,
    Chris

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


    Did you find this post helpful? Yes | No

    Default

    Steve

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

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


    Did you find this post helpful? Yes | No

    Question Sorry I'm so late to respond...another simple? question

    Thank you guys for the responses you gave me. I'm sorry I couldn't thank you sooner...been busy trying picbasic. I've got one more question, that I hope one of you can help me with. I can't seem to get my head around how the pic can take in three bytes in a row from the PC...

    (ie: mscomm1.output = 9 + 22 + 1)

    and know that the first byte is the address, the second is the output, and the third is the state? I think I'm just over-thinking this. Can one of you gentleman help me understand this?
    Do I do it like this:

    serpin VAR porta.0
    address VAR b3
    output VAR b4
    stat VAR b5

    If b3 = PORTB then
    SERIN serpin,N2400,address,output,stat..........

    is serpin b1
    is N2400 b2
    is address b3
    is output b4
    is stat b5

    Am I thinking correctly, or am I way off base?

    Thanks for letting me bend your ear...
    Chris

  6. #6
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default When you can't afford to loose a byte.

    Hi,

    Most of the time your PIC should be listening to the available data. Use some sort of acknowledge when it receives a valid packet. You can try respond through the control lines of the RS232 ports.

    Other possibilities are using the PIC's hardware USART. The hardware USART normally has a two byte buffer and generates an interrupt when a byte is received.

    By using Darrel's Instant Interrupt implementing it should not be a big deal.

    All you need to do it get the bytes in a ring array or FIFO for processing later. Use some sort of unique Header and Terminator in your packet send by the PC. If your are using a FIFO in your main program loop go on hunting for the terminator(the last byte received). When found you can check your FIFO's deepest location (since the data packet length is fixed) for a valid header. If found then anything between your header and terminator is your data packet.
    Regards

    Sougata

Similar Threads

  1. Wireless serial communication question
    By Ahmadabuomar in forum Serial
    Replies: 3
    Last Post: - 21st December 2009, 03:49
  2. serial LCD/SLCD question
    By MatthewM in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 18th May 2009, 14:56
  3. Serial Question + General Review
    By Freman in forum General
    Replies: 2
    Last Post: - 20th June 2008, 22:27
  4. AN Question for "word" variable read The serial port
    By redfoen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th December 2007, 17:39
  5. Simple question, I hope? (Serial control, 16F84A)
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 31st December 2005, 19:47

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