Serial VB 2005 pic 16f877a problems


Results 1 to 29 of 29

Threaded View

  1. #7
    Join Date
    Apr 2007
    Location
    Southern Ontario
    Posts
    50


    Did you find this post helpful? Yes | No

    Smile Sweet Sweet Successsss

    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
    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
    Code:
    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

    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
    Last edited by Snap; - 15th May 2008 at 22:40. Reason: wrong code line
    Beer is proof that God loves us and wants us to be happy.

Similar Threads

  1. Midi, Interrupts and Pic better than SX28?
    By Lajko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th September 2008, 01:26
  2. Automatic VB6 to pic serial connection
    By arniepj in forum Code Examples
    Replies: 13
    Last Post: - 10th January 2008, 08:57
  3. PIC serial going to sleep!
    By stma in forum Off Topic
    Replies: 0
    Last Post: - 31st October 2007, 09:17
  4. PIC to serial with visual basic
    By mbw123 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 31st March 2007, 17:06
  5. serial comm from Pic to STAMP
    By d1camero in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th April 2004, 00:58

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