PDA

View Full Version : serial comm problem



win_832001
- 24th June 2006, 02:11
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

Bruce
- 24th June 2006, 04:56
Place a goto Main at the end, and change ["255"] to [255].


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

win_832001
- 27th June 2006, 04:28
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"

mister_e
- 27th June 2006, 04:40
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.

win_832001
- 27th June 2006, 13:22
the resistor i used is is 22K..

schu4647
- 27th June 2006, 14:13
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.

schu4647
- 27th June 2006, 14:25
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.

mister_e
- 27th June 2006, 15:08
forget me...i was wrong

mister_e
- 27th June 2006, 15:24
dumb answer here too check the next...

mister_e
- 27th June 2006, 15:52
no big solution... it's working here using


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


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.

Melanie
- 27th June 2006, 16:49
> 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.

dhouston
- 27th June 2006, 17:05
> 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.

schu4647
- 27th June 2006, 18:10
Those are girly relays. I use manly relays.

dhouston
- 27th June 2006, 18:13
Those are girly relays. I use manly relays.
Manly relays can be turned on by girly relays.

Melanie
- 27th June 2006, 18:40
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.

Ioannis
- 29th June 2006, 09:55
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

mister_e
- 29th June 2006, 13:57
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 :)