Serin serout problem


Closed Thread
Results 1 to 40 of 337

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    anywau here si the code fo the sending part:
    i am just worried about the two serout line, should I put it into one line, or is it just the same like this,

    I am leaving now, coming back ina few hours


    'TRANSMIT PIC
    INCLUDE "modedefs.bas"
    DEFINE OSC 20 'use external 20mhz crystal
    CMCON = 7 : ANSEL = 0 : ADCON1 = 7
    DEFINE LCD_DREG PORTA ' Set LCD Data port
    DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 0 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000

    DQ var PortB.5
    temp var word

    temperature var word
    count_remain var byte
    count_per_c var byte
    counter var byte

    encoded1 var word
    encoded2 var word
    encoded22 var word
    encoded11 var word
    encoded33 var word
    encoded44 var word

    loop:
    owout DQ,1,[$cc]
    owout DQ,0,[$44]
    Pause 500
    owout DQ,1,[$cc]
    owout DQ,0,[$be]
    owin DQ, 0, [temperature.LOwBYTE, temperature.Highbyte, Skip 4, count_remain, count_per_c]
    '50

    encoded1 =temperature.LowBYTE
    encoded2 =temperature.HighBYTE

    For counter=0 TO 7
    IF encoded1.0[counter]=0 Then
    encoded11.0[counter*2]=0
    encoded11.0[counter*2+1]=1
    Else
    encoded11.0[counter*2]=1
    encoded11.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF encoded2.0[counter]=0 Then
    encoded22.0[counter*2]=0
    encoded22.0[counter*2+1]=1
    Else
    encoded22.0[counter*2]=1
    encoded22.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF count_remain.0[counter]=0 Then
    encoded33.0[counter*2]=0
    encoded33.0[counter*2+1]=1
    Else
    encoded33.0[counter*2]=1
    encoded33.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF count_per_c.0[counter]=0 Then
    encoded44.0[counter*2]=0
    encoded44.0[counter*2+1]=1
    Else
    encoded44.0[counter*2]=1
    encoded44.0[counter*2+1]=0
    EndIF
    Next counter


    lcdout $FE,1
    LCDOUT "TempC: ", dec (temperature / 100) , ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, bin count_remain , ".", bin count_per_c," ",$DF,"F"

    serout portb.2, n2400, [$55, $55, $55, $55, $aa]
    serout portb.2, n2400, [encoded11.HighBYTE, encoded11.LowBYTE, encoded22.HighBYTE, encoded22.LowBYTE ]
    serout portb.2, n2400, [encoded33.HighBYTE, encoded33.LowBYTE, encoded44.HighBYTE, encoded44.LowBYTE ]
    goto loop

    End

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Now that the whole thing is working, you can probably condense those serout lines down into one line...try it and find out...
    That and it makes the program shorter on the screen so you don't have to scroll thru so much stuff...

    'TRANSMIT PIC
    INCLUDE "modedefs.bas"
    DEFINE OSC 20 'use external 20mhz crystal
    CMCON = 7 : ANSEL = 0 : ADCON1 = 7
    DEFINE LCD_DREG PORTA ' Set LCD Data port
    DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 0 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000

    DQ var PortB.5 : temp var word : temperature var word : count_remain var byte : count_per_c var byte : counter var byte
    encoded1 var word:encoded2 var word:encoded22 var word:encoded11 var word:encoded33 var word:encoded44 var word

    loop:
    owout DQ,1,[$cc] : owout DQ,0,[$44] : Pause 500 : owout DQ,1,[$cc] : owout DQ,0,[$be]
    owin DQ, 0, [temperature.LOwBYTE, temperature.Highbyte, Skip 4, count_remain, count_per_c]
    '50

    encoded1 =temperature.LowBYTE : encoded2 =temperature.HighBYTE

    'I'd put these encoding/decoding sections into their own subroutine to GOSUB to...
    For counter=0 TO 7
    IF encoded1.0[counter]=0 Then
    encoded11.0[counter*2]=0 : encoded11.0[counter*2+1]=1
    Else
    encoded11.0[counter*2]=1 : encoded11.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF encoded2.0[counter]=0 Then
    encoded22.0[counter*2]=0 : encoded22.0[counter*2+1]=1
    Else
    encoded22.0[counter*2]=1 : encoded22.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF count_remain.0[counter]=0 Then
    encoded33.0[counter*2]=0 : encoded33.0[counter*2+1]=1
    Else
    encoded33.0[counter*2]=1 : encoded33.0[counter*2+1]=0
    EndIF
    Next counter

    For counter=0 TO 7
    IF count_per_c.0[counter]=0 Then
    encoded44.0[counter*2]=0 : encoded44.0[counter*2+1]=1
    Else
    encoded44.0[counter*2]=1 : encoded44.0[counter*2+1]=0
    EndIF
    Next counter


    lcdout $FE,1 : LCDOUT "TempC: ", dec (temperature / 100) , ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, bin count_remain , ".", bin count_per_c," ",$DF,"F"

    serout portb.2, n2400,[$55,$55,$55,$55,$aa,encoded11.HighBYTE,encoded11.L owBYTE,encoded22.HighBYTE]
    serout portb.2, n2400,[encoded22.LowBYTE,encoded33.HighBYTE,encoded33.Low BYTE,encoded44.HighBYTE]
    serout portb.2, n2400,[encoded44.LowBYTE]
    goto loop
    Last edited by skimask; - 28th December 2006 at 16:16.

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    OK, I must be getting lucky, I'm not getting many mistakes as before. The system works now, the sending code is good. The receiving is good for positive celcius, For some undetermined reason it is not giving the right Ferenheit degrees, and I am using the same math equation as before to translate from C to F.
    I am now working on the negative celcius and also in that scenatio there is two cases, positive and negative F ...
    I will the subroutine for later, I never touched it before so I will do it at the end once everything is working

    'RECEIVE PIC
    INCLUDE "modedefs.bas"
    DEFINE OSC 20 'use external 20mhz crystal
    CMCON = 7 : ANSEL = 0 : ADCON1 = 7
    DEFINE LCD_DREG PORTA ' Set LCD Data port
    DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 0 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000

    counter var byte : temperature var word : encoded1 var word : encoded2 var word : encoded3 var byte
    encoded11 var word : encoded22 var word : encoded33 var word : encoded44 var word : encoded4 var byte
    count_remain var byte : count_per_c var byte : temp var byte : tempc var byte
    pause 2000

    loop:
    '23
    waitfor55:
    serin portb.2 , n2400 , temp : if temp <> $55 then goto waitfor55

    waitforaa:
    serin portb.2 , n2400 , temp : if temp <> $aa then goto waitforaa

    serin portb.2, n2400, encoded11.HighBYTE : serin portb.2, n2400, encoded11.LowBYTE
    serin portb.2, n2400, encoded22.HighBYTE : serin portb.2, n2400, encoded22.LowBYTE
    serin portb.2, n2400, encoded33.HighBYTE : serin portb.2, n2400, encoded33.LowBYTE
    serin portb.2, n2400, encoded44.HighBYTE : serin portb.2, n2400, encoded44.LowBYTE

    For counter=0 TO 7 'decoding
    encoded1.0[counter]=encoded11.0[counter*2] : encoded2.0[counter]=encoded22.0[counter*2]
    encoded3.0[counter]=encoded33.0[counter*2] : encoded4.0[counter]=encoded44.0[counter*2]
    Next counter
    '41
    temperature= encoded1 ' putting back together as the original temperature
    For counter=0 TO 7
    temperature.0[counter+8]=encoded2.0[counter+8]
    Next counter

    temperature.LowBYTE = encoded1
    temperature.HighBYTE = encoded2
    count_remain = encoded3
    count_per_c = encoded4

    IF temperature.11 = 1 Then goto Zerotemp
    temperature = ((( temperature >> 1) *100)- 25) + (((count_per_c - count_remain) * 100) / count_per_c)
    tempc = (((temperature *9) / 5)+3200)

    lcdout $FE,1, "TempC: ", "+", dec (temperature / 100) , ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, "TempF: ", dec (tempc / 100) , ".", dec2 tempc >> 1," ",$DF,"F"
    goto loop

    Zerotemp:
    temperature = temperature >> 1 'removing lowest bit
    for counter=0 to 14 ' 2's compliment, starting to invert all the digits
    if temperature.0[counter] = 1 then temperature.0[counter] =1
    if temperature.0[counter] = 0 then temperature.0[counter] =0
    next counter
    temperature = temperature + 1 ' 2,s compliment ends by adding a one

    temperature = ((( temperature >> 1) *100)- 25) + (((count_per_c - count_remain) * 100) / count_per_c)
    tempc = (((temperature *9) / 5)+3200)

    lcdout $FE,1, "TempC: ", "-", dec (temperature / 100) , ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, "TempF: ", dec (tempc / 100) , ".", dec2 tempc >> 1," ",$DF,"F"
    goto loop


    end

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    for one thing, tempc should be tempf shouldn't it?
    Another thing, if your temp value (which I assume is multiplied by 100 by that conversion/correction thing you've got going on) is above 7281, you'll overflow your 16 bit tempc value (65536 / 9 = 7281.xxx). So try dividing by 5 first, then multiply by 9.

    tempc = ( ( ( temperature / 5 ) * 9 ) + 3200 )

    another thing...what are you trying to do with the lcdout line?
    lcdout $FE,$C0,"TempF:",dec (tempc/100),".",dec2 tempc >>1," ",$DF,"F"

    I think what you really mean is:
    lcdout $fe,$c0,"TempF:",DEC3 (tempc/100),".",DEC2 (tempc//100)," ",$DF,"F"

    use the remainder (modulus) ( // ) function, not the shift ( >> ) function.

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    its working good, now, I think I have a problem with the 2,s compliment, working on it
    I am just doing 2,s compliment, I get all 1,s at the output


    Zerotemp:
    temperature = temperature >> 1 'removing lowest bit
    temperature1 = temperature
    for counter=0 to 6 ' 2's compliment, starting to invert all the digits
    if temperature.0[counter] = 1 then temperature.0[counter] =0
    if temperature.0[counter] = 0 then temperature.0[counter] =1
    next counter
    'temperature = temperature + 1 ' 2,s compliment ends by adding a one

    'temperature = ((( temperature >> 1) *100)- 25) + (((count_per_c - count_remain) * 100) / count_per_c)
    'tempc = (((temperature *9) / 5) + 3200)

    lcdout $FE,1, "TempC: ", bin temperature1, ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, "TempF: ", bin temperature , ".", dec2 (tempF // 100)," ",$DF,"F"
    goto loop
    Last edited by lerameur; - 28th December 2006 at 22:36.

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I just flip these around and it works

    if temperature.0[counter] = 0 then temperature.0[counter] =1
    if temperature.0[counter] = 1 then temperature.0[counter] =0

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    when it get to ZERO, it shows 655,
    then when it reach about -3 then it shows the right temp, weird..
    any idea?

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    I just flip these around and it works

    if temperature.0[counter] = 0 then temperature.0[counter] =1
    if temperature.0[counter] = 1 then temperature.0[counter] =0
    See my post above referring to your post #114...your logic above is flawed also

    (temperature.0[counter] - just call it temp for right now)
    if temp is zero, set it to one. then go to the next line down
    if temp is one, set it to zero. again, it just got set to 1 on the line above!

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Zerotemp:
    temperature = temperature >> 1 'removing lowest bit
    temperature1 = temperature
    ------------------------------------------why are you removing the lowest bit?

    for counter=0 to 6 ' 2's compliment, starting to invert all the digits
    if temperature.0[counter] = 1 then temperature.0[counter] =0
    if temperature.0[counter] = 0 then temperature.0[counter] =1
    next counter
    ------------------------------------this will never work....follow it thru
    1. if temp.counter = 1 then set it to 0
    2. if it's zero set it to 1.....it just got set to zero by the above line!!!!!
    what you want is:

    for counter = 0 to 6
    if temperature.0[counter] = 1 then
    temperature.0[counter] = 0
    else
    temperature.0[counter] = 1
    endif

    or just use this:
    temperature = ~ temperature (bitwise inversion)



    'temperature = temperature + 1 ' 2,s compliment ends by adding a one

    'temperature = ((( temperature >> 1) *100)- 25) + (((count_per_c - count_remain) * 100) / count_per_c)
    'tempc = (((temperature *9) / 5) + 3200)

    lcdout $FE,1, "TempC: ", bin temperature1, ".", dec2 temperature," ",$DF,"C"
    lcdout $FE,$C0, "TempF: ", bin temperature , ".", dec2 (tempF // 100)," ",$DF,"F"
    goto loop

    And you didn't change the temperature to doing the /5 first, and then the *9. If you leave it the way you've got it, you'll get an overflow when it gets really warm outside!

Similar Threads

  1. A Serial GLCD 128x64 Simple Project
    By Oldspring in forum Off Topic
    Replies: 0
    Last Post: - 8th March 2010, 20:58
  2. PIC16f877 code crosses boundary @800h
    By inventosrl in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th April 2009, 22:03
  3. serout and serin problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th April 2006, 19:44
  4. Replies: 11
    Last Post: - 13th July 2005, 19:26
  5. SerIn and SerOut
    By Dwayne in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 21st July 2004, 15:54

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