PDA

View Full Version : Serial VB 2005 pic 16f877a problems



Snap
- 5th May 2008, 04:47
Hi I m back with a new project. Thank you for all you help and support on the slot car timmer. I am working on a simple thermostat withthe 1620 chip and it works great with the lcd out. I wanted to get it to the pc so I wrote a small program in VB2005 and it took hours just to get them talking but I did it... except the pic is not listening. I can send to vb fine and read it but when I try and send back to the pic it does not get it. I can see the PC trying to send out on the scope but it does not get stored in my pic variable. I even ran a wire from the tx side of the pic to the rx and still nothing. The pic is sending but not recieving.
here is some of my code
I decalred the variable as a word and later as byte
I am using a max 232 converted and like I said I can see both signals on the scope
I am sure the pc it sending the code i think :)
I am just trying to get the pc to tell the pic to turn on a fan when it gets too hot.
YES I know there are many simpler ways to do this directly threw the pic but I want to use the PC.

serout portd.3,t2400,[#temp]
serin porta.0, t2400,pcsig ' my data in variable
if pcsig > 1 then high porta.1 ' pc writes 100 so even if the pic gets anything it should fire


thanks again
Snap

mister_e
- 5th May 2008, 04:55
There's quite a few culprit in VBExpress, that would be really nice if you could post your VB and the whole PIC code here.

As you're using PORTA... make sure you have disabled the ADCs

PortA Doesn't Work & Common 8-Bit PICŪ Microcontroller I/O Pin Issues (TB3009)
http://www.picbasic.co.uk/forum/showthread.php?t=561

Any specific reason why you don't use the built-in PIC USART?

ardhuru
- 5th May 2008, 07:55
I can see the PC trying to send out on the scope but it does not get stored in my pic variable. I even ran a wire from the tx side of the pic to the rx and still nothing. The pic is sending but not recieving.
Snap

I have found this http://www.serial-port-monitor.com/index.html utility extremely useful while doing anything with VB and the serial port.

Also, if you have another serial port, try and read what your VB code is in fact sending although serial port monitor would do the same anyway.

Regards,

Anand

T.Jackson
- 5th May 2008, 11:31
Don't ever leave this forum mister_e, my life depends on you being here sharing your knowledge. I think I speak for everyone.

Snap
- 5th May 2008, 22:01
Hello again, Ok I switched off the adc'd with adcon1 statement.
And that must have done something because now everytime I start the program it locks up at the display temp part.
If I remove the serin code it works (just the program not the recieve signal from the PC)
If I remove the wire from the serin port to the rx on the max 232 adapter it works
This is all when the PC is not even plugged in the the max 232
the wire seems to be shorting out the program or the signal is screwing it up
To me the USART seems to be more work then a simple serin command why would I use it?
here is the pic code


Include "MODEDEFS.BAS"
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 7
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATUS 50
ADCON1=%00000111

'
RST var portd.2
DQ var portd.0
CLK var portd.1
temp var word 'storage for temperature
pcsig var word


'
low rst 'reset 1620
'
lcdout $fe,1,"Temp in Degrees C"

' Main loop
mainloop:
RST = 1'enable device might be high
Shiftout DQ,CLK,LSBFIRST,[$ee] 'send read command
RST = 0 ' reset device
Pause 1000 ' wait for conversion to compleate
'
RST = 1 ' SAA
Shiftout DQ,CLK,LSBFIRST, [$aa] ' send read command
Shiftin DQ,CLK,LSBPRE, [temp\9]'read 9 bits temp
RST = 0
'
lcdout $fe,1,dec(temp>>1), ".", dec(temp.0*5), " Degrees C"
pause 1000
serout portd.3,t2400,[#temp]
pause 1000
serin porta.1,t2400,pcsig
if pcsig > 1 then high porta.1

lcdout $fe,1,pcsig,"pcsig"
pause 1000

goto mainloop

END



Here is the VB code
I ommitted a class

Public Class Form1
Dim WithEvents serialport As New IO.Ports.SerialPort
Dim temperature As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If serialport.IsOpen Then
serialport.Close()

End If
With serialport
.PortName = "com1"
.BaudRate = 2400
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
'.DtrEnable = True
'.ReadBufferSize = 40
.ReceivedBytesThreshold = 2
End With
serialport.Open()

End Sub
Private Sub Datarecieved(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
TextBox1.Invoke(New mydelegate(AddressOf updatetextbox), New Object() {})

End Sub
Public Delegate Sub mydelegate()
Public Sub updatetextbox()


Dim mytemp As New temp
mytemp.binary = serialport.ReadExisting
Dim resultc As Decimal = celciusconversion(mytemp.binary)
Dim resultf As Decimal = farconversion(resultc)
TextBox1.Text = resultc
TextBox2.Text = resultf
If resultc >= 24 Then
'serialport.Write("a")
serialport.Write(100)
TextBox3.BackColor = Color.Green
TextBox3.Text = "fan on"
Else
TextBox3.BackColor = Color.Red
TextBox3.Text = "fan off"
End If




End Sub
Public Function celciusconversion(ByVal datarecieved As String) As Decimal
Return datarecieved * 0.5
End Function

Public Function farconversion(ByVal datarecieved As String) As Decimal
Return (datarecieved * 1.8) + 32
End Function


Thank you for your time.
I am trying to help out on this forum as much as I can, I have been sending copies of my slot car timer to users who ask for it.

mister_e
- 6th May 2008, 03:53
serialport.Write(100)
this should send string, so i guess you'll need to change the SERIN to

serin porta.1,t2400,#pcsig

I'll second Ardhuru's advice. A serial COMport sniffer is always handy to have on hand. I use HHDSoft's one
http://www.hhdsoftware.com

There's a load of those here and there, some are 100% free.

Having one on hand... priceless!

Snap
- 6th May 2008, 04:24
Thanks for the reply but the problem is that the program is now (after fixing port a) locking up as soon as I connect the rx pin on the 232 adapter, the serial cable is not even connected and it locks up.
if i comment out the serin code with the 232 connected it works (without serin)
I think the serin command is locking up the program but I don't know why.
The voltage is always 5 volts untill a code comes threw would a constant 5 volts mess up the serin pin? this is a falling trigger do i need to tell the pic what type to look for?
t2400 ? n2400?
I hope I have given enough information, I have been.
what would cause the program to lock up as soon as i connect the serin pin?

thanks again

mister_e
- 6th May 2008, 04:52
Well that's strange indeed, why it locks... well it will lock anyways until you have some valid data.

You said you're using a MAX232 and you're capable of sending data to your PC... T2400 is the setting. Weird enough.. my computer don't work with such low baudrates...


serout portd.3,t2400,[#temp]
pause 1000
serin porta.1,t2400,pcsig

i would suggest to remove the pause 1000, and change your SERIN line



serout portd.3,t2400,[#temp]
serin porta.1,t2400,#pcsig

skimask
- 6th May 2008, 04:55
what would cause the program to lock up as soon as i connect the serin pin?

Post #20 here...http://www.picbasic.co.uk/forum/showthread.php?t=7881&highlight=serin+idle
might help you out a bit...

Or this might:
http://www.picbasic.co.uk/forum/showthread.php?t=1944&highlight=idle

mister_e
- 6th May 2008, 04:58
Valid for Timeout use...

skimask
- 6th May 2008, 05:04
Valid for Timeout use...

Ayy......good point...
But what if the serial cable is 'amplifying' the unloaded pin, as in a floating input, and it ends up causing garbage to try to get into the SERIN statement.

How about adding a pullup or pulldown as appropriate?

mister_e
- 6th May 2008, 05:08
He said he use MAX232 :D

But yes i see what you mean.

Snap
- 6th May 2008, 05:41
ok, I took out the pause, added the #, so the serin now looks like this...
serin portc.3,t2400,10,mainloop,[#pcsig]
it still locks up,
i added a 4.7 pull down to rx, no luck
pull up no luck
serial cable still not even connected (just trying to get it to run first with serin command intact)
will keep trying
thanks

mister_e
- 6th May 2008, 05:43
nope... no square brackets in that.

Snap
- 6th May 2008, 05:50
how is this.........still does not work.
serin portc.3,t2400, 10,mainloop,#pcsig

mister_e
- 6th May 2008, 05:52
seems i'll need to try it here :D

can you post your missing class or your whole VB code please?

mister_e
- 6th May 2008, 06:03
and woops i just saw that...

serin porta.1,t2400,pcsig
if pcsig > 1 then high porta.1

you should use another pin...

and then

lcdout $fe,1,pcsig,"pcsig"

Should have another #

lcdout $fe,1,#pcsig,"pcsig"

Snap
- 6th May 2008, 06:07
First of all I would like to thank you again for all of your wonderful help. It is nice to know there are people like yourself and the other members of this forum in this world who devote so much time and energy to helping newbee's like myself.

I don't wan't you to go through all the trouble of re-createing my project unless of course, you want the code (it is a pritty nifty way of showing the temp in windows).

Besides all that is it possible that it is the VB code that is locking up the pic when it isent even connected when the pic program hangs up?

my next thing will be testing out the vb program on a basic stamp I have, if that works at least I know the vb part works, the next will be trying it out on a 16f84 and then if that works I will buy another 16f877 and see if it's the ic.

If anyone has any other sugestions or you still want the code let me know
Thank You
Snap

mister_e
- 6th May 2008, 06:10
If POST #17 don't highlight anything... I still want it :D

Those three textbox and this single button are already on the form!

Snap
- 6th May 2008, 06:11
i changed the serin pin after i sent the program, it now is portc.3
sorry i forgot to mention it

Snap
- 6th May 2008, 06:19
i changed the serin pin after i sent the program, it now is portc.3
sorry i forgot to mention it

Snap
- 6th May 2008, 06:22
here is the missing class

Public Class temp

Private m_binary As String
Private m_celsius As String
Private m_farenheight As String

Public Property binary()
Get
Return m_binary
End Get
Set(ByVal value)
m_binary = value
End Set
End Property

Public Property celsius()
Get
Return m_celsius
End Get
Set(ByVal value)
m_celsius = value
End Set


End Property
Public Property farenheight()
Get
Return m_farenheight
End Get
Set(ByVal value)
m_farenheight = value

End Set
End Property


End Class

I added the extra methods for future things
create a main form with a cercius and farenheit text box and a button to get it started
good luck

Snap
- 6th May 2008, 06:41
I tried to zip my vb project but it is not working, I will try again in the morning ,
sorry by the way it is 23.5 c in here ;)

mister_e
- 6th May 2008, 07:08
I see what's happening here... as the pic is waiting for DECIMAL number, it will stuck untill there's a none-numeric ASCII value... damn... so just send something else after you send your data and you should be in business.

try this...
vb code


Public Sub updatetextbox()
TextBox1.Text = serialport.ReadLine
serialport.Write("aaaaOK" & TextBox1.Text & "z")
End Sub


The PIC side..

@ __CONFIG _XT_OSC & _LVP_OFF
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 7
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
ADCON1=%00000111


DEFINE DEBUG_REG PORTD ' Set Debug pin port
DEFINE DEBUG_BIT 3 ' Set Debug pin bit
DEFINE DEBUG_BAUD 2400 ' Set Debug baud rate
DEFINE DEBUG_MODE 0 ' Set Debug mode: 0 = true, 1 = inverted

DEFINE DEBUGIN_REG PORTA ' Set Debugin pin port
DEFINE DEBUGIN_BIT 1 ' Set Debugin pin bit
DEFINE DEBUGIN_MODE 0 ' Set Debugin mode: 0 = true, 1 = inverted


temp var word 'storage for temperature
pcsig var word

' Main loop
portc=0
trisc=0
HIGH PORTD.3 ' Serial OUT idle state
pause 500

mainloop:
for temp = 0 to 1000 step 100
lcdout $fe,1,"Temp=", dec temp
debug DEC temp,10
DEBUGIN [WAIT ("OK"), DEC pcsig]
lcdout $fe,$c0,"pcsig=", dec pcsig
PAUSE 1000
next

goto mainloop

END

This echo back the data, not much.

Snap
- 6th May 2008, 22:39
first I tried substatuting some of yor lines of code into my program for mine like...
serIN [WAIT ("OK"), DEC pcsig] insted of
DEBUGIN [WAIT ("OK"), DEC pcsig]
and i added the
portc=0
trisc=0
HIGH PORTD.3 ' Serial OUT idle state
pause 500
at the beginning
but it stilll locked up at the exact same spot.
So I started a new project and used your code exactly
I recieved the
Error[118] C:\PBP\SAMPLES\BLINK.ASM 122 : Overwriting previous address contents (2007)
Error[118] C:\PBP\SAMPLES\BLINK.ASM 122 : Overwriting previous address contents (2007)
when i compiled but programmed it anyway
it now locks up at
Temp=0
and will not move at all
and the vb still cant open the port because it is locked (just like always)
Sorry i can't get it to work, i am at a total brick wall on this one.

mister_e
- 6th May 2008, 22:56
use it as is, paste my own first.. even better, try at 9600 bauds.. that's what i've used yesterday.

For the error message refer to post #5 at the following...

http://www.picbasic.co.uk/forum/showthread.php?t=543

Snap
- 7th May 2008, 01:07
fixed the error messages but it still locks up after the temp= 0 lcdout
and the vb program still can't open the port because of it
sorry again i think it's time to give up for a while, i don't want to take up any more of your time. I will add to the post if I get it going.
Thanks again to everybody.
I can't believe such a simple thing as a serinn(now a debug) is causing such a problem.
Snap

Snap
- 15th May 2008, 21:38
I finaly did it (or should I say We....Mr e).The program works fine.
The key hang ups were add in a "z" in the VB write code and sending code all the time to the pic. Im sure there are way better ways of doing this but this works.
You need a pic and a 1620 temp ic and a serial converter and a pc.
here is the code and thank you everyone who helped especially the big E.
Snap
VB2005 code

Public Class Form1
Dim WithEvents serialport As New IO.Ports.SerialPort
Dim temperature As Integer

Dim setting As Decimal


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If serialport.IsOpen Then
serialport.Close()

End If
With serialport
.PortName = "com1"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
'.DtrEnable = True
'.ReadBufferSize = 40
.ReceivedBytesThreshold = 2
End With
TextBox4.Text = 23
serialport.Open()
serialport.Write(200 & "z")


End Sub
Private Sub Datarecieved(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
TextBox1.Invoke(New mydelegate(AddressOf updatetextbox), New Object() {})

End Sub
Public Delegate Sub mydelegate()
Public Sub updatetextbox()


Dim mytemp As New temp
mytemp.binary = serialport.ReadExisting
Dim resultc As Decimal = celciusconversion(mytemp.binary)
Dim resultf As Decimal = farconversion(resultc)
TextBox1.Text = resultc
TextBox2.Text = resultf
If TextBox1.Text >= setting Then
TextBox3.BackColor = Color.Green
TextBox3.Text = "fan on"
serialport.Write(100 & "z")

ElseIf TextBox1.Text < setting Then
TextBox3.BackColor = Color.Red
TextBox3.Text = "fan off"
serialport.Write(200 & "z")

End If





End Sub
Public Function celciusconversion(ByVal datarecieved As String) As Decimal
Return datarecieved * 0.5
End Function

Public Function farconversion(ByVal datarecieved As String) As Decimal
Return (datarecieved * 1.8) + 32
End Function

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
setting = TextBox4.Text
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class



Temp Class

Public Class temp

Private m_binary As String
Private m_celsius As String
Private m_farenheight As String

Public Property binary()
Get
Return m_binary
End Get
Set(ByVal value)
m_binary = value
End Set
End Property

Public Property celsius()
Get
Return m_celsius
End Get
Set(ByVal value)
m_celsius = value
End Set


End Property
Public Property farenheight()
Get
Return m_farenheight
End Get
Set(ByVal value)
m_farenheight = value

End Set
End Property


pic code



' Pic16f877a
' LCD should be connected as follows:
' LCD PIC
' DB4 PortB.0
' DB5 PortB.1
' DB6 PortB.2
' DB7 PortB.3
' RS PortB.5 (add 4.7K pullup resistor to 5 volts)
' E PortD.7
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 20K potentiometer (or ground)
' DB0-3 No connect
Include "MODEDEFS.BAS"
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 7
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATUS 50
ADCON1=%00000111

'
RST var portd.2
DQ var portd.0
CLK var portd.1
temp var word 'storage for temperature
pcsig var word
'portc=0
'trisc=0
HIGH PORTD.3 ' Serial OUT idle state
pause 500


'
low rst 'reset 1620
'
lcdout $fe,1,"Temp in Degrees C"

' Main loop
mainloop:
RST = 1'enable device might be high
Shiftout DQ,CLK,LSBFIRST,[$ee] 'send read command
RST = 0 ' reset device
Pause 1000 ' wait for conversion to compleate
'
RST = 1 ' SAA
Shiftout DQ,CLK,LSBFIRST, [$aa] ' send read command
Shiftin DQ,CLK,LSBPRE, [temp\9]'read 9 bits temp
RST = 0
lcdout $fe,1,dec(temp>>1), ".", dec(temp.0*5), " Degrees C"
serout portd.3,t9600,[#temp]

serin portc.4,t9600,#pcsig
if pcsig = 100 then high portc.5
if pcsig = 300 then goto mainloop

if pcsig = 200 then low portc.5

goto mainloop

END

RudyPutra123
- 8th July 2013, 00:52
Sorry Mr. Snap...

Can you tell me about meaning of code visual basic there line by line...
I wanna to learn visual basic, but I'm a Beginner !!

Please help me Mr.