Results 1 to 2 of 2

Thread: Serial to VB from PICMicro

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    Indiana
    Posts
    2

    Serial to VB from PICMicro

    I posted this question on PIC Basic Pro with no takers. I have since been able
    to use MSComm.InBufferCount=0 in various locations so that sometimes the bytes will reverse on the 1st pass through then go back to their proper positions. However, even a 1st pass reversal is not good. The code is pretty much straight fromthe Reynolds Electronics website.
    Thanks,
    John

    Serial from 16F877 to VB

    --------------------------------------------------------------------------------

    This is driving me crazy. I'm displaying an analog voltage on an LCD attached to the PIC as adval.HighByte,adval.LowByte(3 233)
    I'm also sending the same values to a VB program using the same format, I've attached the code below, PBP then VB.
    Everytime I start the VB program, the HighByte and LowByte literally switch places. I added a debug.print to look at the HighByte and LowByte and everyother time I start the VB program, the displayed values agree with the LCD display and the next time the HighByte and LowByte has swapped positions while the LCD Display remains the same and true. I apoligize for the raggedness I've been cutting and hacking at it to learn.

    PIC Code
    'Attribute VB_Name = "Module1"
    ' PicBasic Pro program to display result of
    ' 10-bit A/D conversion on LCD
    '
    ' Connect analog input to channel-0 (RA0)

    ' Define LCD registers and bits
    DEFINE LCD_DREG PORTD
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_RSBIT 2
    DEFINE LCD_EREG PORTE
    DEFINE LCD_EBIT 1
    DEFINE LCD_RWREG PORTD
    DEFINE LCD_RWBIT 4

    DEFINE OSC 20

    'DEFINE CHAR_PACING 2000

    'DEFINE HSerout/in parameters
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 2400


    ' Define ADCIN parameters
    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

    adval VAR WORD ' Create adval to store result


    TRISA =%11111111 ' Set PORTA to all input
    ADCON1=%10001110 ' Set PORTA analog and right justify result
    TRISD=0
    TRISE=0
    ' PORTE.2=0 ' This worked with or without this statement
    'Porte.2 cause the LED's on the Dev Board to float(no ground)
    'which allows the LCD on the same pins to operate

    PORTD.3=0 ' LCD R/W line low (W)

    Pause 500 ' Wait .5 second

    Loop: ADCIN 0, adval ' Read channel 0 to adval

    LCDOut 254, 1 ' Clear LCD
    Pause 1000

    'LCDOut "Value= ", DEC adval ' Display the decimal value

    LCDOut DEC adval.highbyte," ",DEC adval.lowbyte ' Display the decimal value
    Pause 500
    HSerout [adval.HighByte,adval.LowByte]
    'HSerout [adval]

    Pause 1000 'Wait .1 second

    GoTo loop ' Do it forever
    End


    VB Code
    Private Sub Label1_Click()

    End Sub

    Private Sub Form_Load()
    ' Fire Rx Event Every Two Bytes
    MSComm1.RThreshold = 2

    ' When Inputting Data, Input 2 Bytes at a time
    MSComm1.InputLen = 2

    ' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
    MSComm1.Settings = "2400,N,8,1"
    ' Disable DTR
    MSComm1.DTREnable = False

    ' Open COM2
    MSComm1.CommPort = 2
    MSComm1.PortOpen = True


    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
    End Sub

    Private Sub lblRCTime_Click()

    End Sub

    Private Sub MSComm1_OnComm()
    Dim sData As String ' Holds our incoming data
    Dim lHighByte As Long ' Holds HighByte value
    Dim lLowByte As Long ' Holds LowByte value
    Dim lWord As Long ' Holds the Word result

    ' If comEvReceive Event then get data and display
    If MSComm1.CommEvent = comEvReceive Then

    sData = MSComm1.Input ' Get data (2 bytes)

    lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
    lLowByte = Asc(Mid$(sData, 2, 1)) ' Get 2nd byte
    Debug.Print "HB=", CStr(lHighByte), " ", "LB=", CStr(lLowByte)
    ' Combine bytes into a word
    lWord = (lHighByte * &H100) Or lLowByte

    ' Convert value to a string and display
    lblRCTime.Caption = CStr(lWord)

    'Debug.Print Val(lWord) / 10.1


    End If

    End Sub
    __________________
    JRudd

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Serial to VB from PICMicro

    Welcome to the Forums!

    You should always use [ vbcode ] / [ /vbcode ] tags around your code, to make it readable

    I think you have a problem which can best be expalined here:

    http://www.cs.umass.edu/~verts/cs32/endian.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width