serial comm problem


Closed Thread
Results 1 to 17 of 17
  1. #1
    win_832001's Avatar
    win_832001 Guest

    Post serial comm problem

    hello there..i was trying to control the relay 12V by using the serial port comm.the PIC that i used is PIC 16 F84. can someone check my VB PIC basic code below,it does not work:

    VB code :
    Private Sub Form_Load()
    Optstate(0).Value = True
    MSComm1.CommPort = 1
    MSComm1.Settings = "2400,n,8,1"
    MSComm1.DTREnable = False
    MSComm1.RThreshold = 1
    MSComm1.InputLen = 1
    MSComm1.PortOpen = True
    End Sub
    Private Sub Command1_Click()
    Dim pinstate As Long
    If Optstate(0).Value = True Then
    pinstate = 0
    Else
    pinstate = 1
    End If
    MSComm1.Output = Chr$(255) & Chr$(pinstate)
    End Sub

    Private Sub form_unload(cancel As Integer)
    MSComm1.PortOpen = False

    End Sub


    PIC code :
    INCLUDE "modedefs.bas"

    Si VAR PORTA.4
    RELAY VAR PORTB.2
    pinstate VAR BYTE

    main

    SerIn Si,4,["255"],pinstate

    IF pinstate=1 Then
    GoSub Relayon
    Else
    GoSub Relayoff
    EndIF

    Relayon:
    High RELAY
    Return

    Relayoff:
    Low RELAY
    Return

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Place a goto Main at the end, and change ["255"] to [255].
    Code:
    Main:
        SerIn Si,4,[255],pinstate
        IF pinstate=1 Then 
          GoSub Relayon
        Else 
          GoSub Relayoff
        EndIF
        GOTO Main ' <-- forgot this
    
    Relayon:
        High RELAY
        Return
    
    Relayoff:
        Low RELAY 
        Return
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    win_832001's Avatar
    win_832001 Guest


    Did you find this post helpful? Yes | No

    Unhappy other problem

    tq for the reply..actually there are problem appear after i tried the code u have given..when i'm sending '0' from VB and after that send "1" to th PIC.the relay does not "on".

    the problem also appear after once i "on" and "off" the relay, and then i want to "on" again the relay is not "on"

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


    Did you find this post helpful? Yes | No

    Default

    how do you interface your PIC to the PC? resistor i guess, wich value? 1k-10K is fair enough. geater than 10K seems to cause erratic behavior in some case.

    Could also be a hardware problem as we see often.. psu filtering or else.
    Steve

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

  5. #5
    win_832001's Avatar
    win_832001 Guest


    Did you find this post helpful? Yes | No

    Default resistor

    the resistor i used is is 22K..

  6. #6
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    The PIC may not supply enough current to trigger your relay. Put an LED in its place and make sure it is going on and off.

  7. #7
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    For development i always use an LED to make sure my output is going on and off. Then I usually have to drive the relay with a transistor. I haven't found a relay yet I could directly drive.

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


    Did you find this post helpful? Yes | No

    Default

    forget me...i was wrong
    Last edited by mister_e; - 27th June 2006 at 15:50.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    dumb answer here too check the next...
    Last edited by mister_e; - 27th June 2006 at 15:49.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    no big solution... it's working here using
    Code:
    Private Sub Form_Load()
        Option1(0).Value = True
        MSComm1.CommPort = 1
        MSComm1.Settings = "2400,n,8,1"
        MSComm1.DTREnable = False
        MSComm1.RThreshold = 1
        MSComm1.InputLen = 1
        MSComm1.PortOpen = True
    End Sub
    Private Sub Command1_Click()
        Dim pinstate As Byte
        If Option1(0).Value = True Then
            pinstate = 1
            Else
                pinstate = 0
            End If
        
        MSComm1.Output = Chr(255) & Chr(pinstate)
    End Sub
    
    Private Sub form_unload(Cancel As Integer)
        MSComm1.PortOpen = False
    End Sub
    on the PICSide
    Code:
    INCLUDE "modedefs.bas"
    
    Si       VAR PORTA.4
    RELAY    VAR PORTB.2
    pinstate VAR BYTE
    
    main:
        SerIn Si,n2400,[255],pinstate
        
        IF pinstate=1 Then
            GoSub Relayon
            Else
               GoSub Relayoff
            EndIF
        
        goto main
        
    Relayon:
        High RELAY
        Return
    
    Relayoff:
        Low RELAY
        Return
    sorry, try a lower resistor value and be sure you share the same ground, be sure you place 0.1 uF cap close to your PIC and be sure of your crystal speed and capacitor value.

    Schu4647 have also point an interesting fact... be sure it's not the relay who create the bug.
    Last edited by mister_e; - 27th June 2006 at 15:54.
    Steve

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

  11. #11
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    > I haven't found a relay yet I could directly drive

    There are many 5v Reed Relays, many of which need less current than an LED... and some even have integral back-emf diodes across the coil which dispenses the need for any other external components. Meder Electronic www.meder.com springs to mind, at prices in volume of under $1 a lump, they're not exactly budget breaking either.

  12. #12
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie
    > I haven't found a relay yet I could directly drive

    There are many 5v Reed Relays, many of which need less current than an LED... at prices in volume of under $1 a lump, they're not exactly budget breaking either.
    And they tend to be rated for billions of cycles.

  13. #13
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Those are girly relays. I use manly relays.

  14. #14
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by schu4647
    Those are girly relays. I use manly relays.
    Manly relays can be turned on by girly relays.

  15. #15
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Haven't girly things always turned-on manly things since time began?

    I'd just want to add (before we digress too far), that as a matter of good practice, I'd never hang any power sucking switching component on the same supply line as that for your PIC (and that goes for your vibrator motor!). That +5v (3.3v) line is sacred. Your PIC and some other logic and that's it. All your Power devices get hung on the raw input supply, not on the nice regulated PIC supply. In following that rule, I've NEVER experienced any problems switching power devices.

    Big Power Relays need a big magnetic field to slam shut their contacts in order to carry Amps and things... and a big magnetic field unfortunately can only be generated by a current which a PIC can't provide. And if you did, then the induced back-emf and spikes means you'll have to significantly filter your PICs supply to stop it tripping out.

    Take my advice... NO power devices on the same supply line as your PIC.

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default

    And if I may add to Melanies post, use if possible optocouplers to drive external power devices. It is the most secure method of isolation.

    I had to go twice on a town up in the mountains 250Km away, for an automation once I made with 16C55 and assembly lanquage, but with no isolation and finally understood that I was too inexperienced that time in industrial automation design (long time ago, I would say...).

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default

    AND depending the design, having different ground is also a good practice. It's amazing sometime how a simple looking thing could ruin an entire design.

    Probably even worst when you mix, analog, digital and power signal on the same design... oh my god! How many hours i, and many other i guess, already lost to know why... even a bad ground plane design may ruine your life... ARGGGHH!

    Thanks to Bead & friends to exist
    Last edited by mister_e; - 29th June 2006 at 14:01.
    Steve

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

Similar Threads

  1. Problem using Microloader and Uart Comm at the same time
    By guanerrr in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 3rd August 2007, 09:31
  2. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  3. Serial Communication Problem
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 23rd February 2006, 02:11
  4. Replies: 8
    Last Post: - 11th November 2004, 20:08
  5. 16F877, DS18S20 and Serial Comm Problem
    By YellowTang in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th April 2004, 10:36

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