Delay Problem,Byte,Word


Closed Thread
Results 1 to 21 of 21
  1. #1

    Default Delay Problem,Byte,Word

    Hi,

    I have a little problem:

    Delay1 var word
    DataIn var byte [12]

    ...........

    Delay1 = Datain[3]

    ................
    pause Delay1

    Pause just work wenn Delay1 > 255 Why.

    I want for Example to have a Delay sometimes 1 or 10 or 50 Milliseconds and sometime over a Second.
    Its just work when Delay1 > as Byte.

    How can I transform this Datain to Word.
    I try with this:

    Delay1.byte0 = Datain[3]
    Delay1.byte1 = Datain[4]

    Pause delay1

    But this dont' Work.

    I need Please a Help.
    Thank You very mouch !!!

    Ciao.
    Last edited by Pesticida; - 30th December 2006 at 16:43.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Hi,
    As you figured out it won't work because DataIn[3] is a byte sized variable, it can only hold values between 0 and 255. To do what you want something like this may work:
    Code:
    Delay1 = DataIn[3] * 256 + DataIn[4]
    OR
    Code:
    Delay1.HighByte = DataIn[3]
    Delay1.LowByte = DataIn[4]
    In both examples DataIn[3] is the most is the upper 8 bits, DataIn[4] is the lower 8 bits of the word sized Delay1.

    /Henrik Olsson.

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Why not DataIn VAR WORD[12] ?

    Do I need more coffee again?

    Or am I missing something here again?

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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Why I must to multiple this with 256 I don't understand.
    What I can do to have for example 1 Millisecond or 5 Millisecond with Pause.

    Thanks.
    Ciao.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    What is the range of your values? if all of them are <255, i guess you just need to use..
    Delay1=0

    Delay1.LowByte=DataIn[3]
    Pause Delay1

    in case the range is greater than 255, Sayzer's suggestion have to work... or i miss something myself too
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Hi,
    (I may be missing something here too, but here comes the long winded answer....)

    Since you have an array of 12 bytes each byte can only hold a value between 0 and 255. If you want a delay longer than 255mS you need to, either use an array of words OR use two bytes in the byte-array to 'make' a word.

    The multiply by 256 "shifts" the 8 bits in DataIn[3] to the upper 8 bits in Delay1.

    Say you want a 1000mS delay. Since 1000 wont fit in a byte you'll use two bytes. 1000 in binary is 00000011 11101000 so DataIn[3] = 3 and DataIn[4] = 232.

    Then when you parse it you get Delay1 = DataIn[3] * 256 + DataIn[4] which is the same as 3 * 256 + 232 = 1000.

    Or the easier way:
    Delay1.HighByte = DataIn[3]
    Delay1.LowByte = DataIn[4]

    /Henrik Olsson.

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Thanks a lot.
    But When Delay1 < 256 dont' work.
    This is my Code:

    INCLUDE "modedefs.bas"
    DEFINE OSC 4
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_SPBRG 12 ' 19200 Bauds
    DEFINE HSER_CLROERR 1

    DataIn var byte [12]
    Adresse var byte
    AdresseIn var byte
    N var byte
    Delay1 var word
    Loop var word
    Kanal var byte [5]
    KanalCount var byte




    SOUND PORTB.1,[124,100]
    MainStart:
    Adresse = $1
    low PORTB.0 '''''''''''''''''''''''''''''''''''''''''''''RS485 Input Data Enable'''''''''''''''''''''''''''''''''''''''''''' ''''''''''''
    Main:

    hserin 10000,MainStart,[WAIT("!"),STR Datain\13]

    CheckAdr:

    AdresseIn = Datain[0]
    if Adresse = AdresseIn then RcvData

    GoTo MainStart


    RcvData:


    IF Datain[11] = 13 Then CheckEnd

    Goto MainStart
    CheckEnd:

    IF Datain[12] = 10 Then Discard

    Goto MainStart
    Discard:
    Delay1 = 256

    Delay1 = (DataIn[3] * 256) + DataIn[4]
    Loop = (Datain[2] * 10)
    Kanal[0] = Datain[5]
    Kanal[1] = Datain[6]
    Kanal[2] = Datain[7]
    Kanal[3] = Datain[8]
    Kanal[4] = Datain[9]
    Kanal[5] = Datain[10]
    KanalCount = 0

    'SOUND PORTB.1,[124,30]
    ''''''''''''''''''''''''''''''''''''''''''''Progra m 1''''''''''''''''''''''''''''''''''''''''''''''''' ''
    IF Datain[1] = 0 then
    for N = 0 to Loop

    High PortC.0
    High PortC.1
    low PortC.2
    low PortC.3

    Pause Delay1

    low PortC.0
    High PortC.1
    high PortC.2
    low PortC.3

    Pause Delay1

    low PortC.0
    low PortC.1
    high PortC.2
    high PortC.3

    Pause Delay1

    high PortC.0
    low PortC.1
    low PortC.2
    high PortC.3

    Pause Delay1
    Next N
    endif

    GoTo MainStart


    My Code Work just fine wehn the Delay Value > Byte is.
    But I need for Example : Pause Delay1 = 1

    I Wrote a visual Basic Software where I can send this Delays values,but this work just wehn this Word Variable > 255.

    Thanks.

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I'm curious to see the VB program too.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Yes I understand what you mean,but my problem ist that i want to have sometimes values that < as 255 is,for Example 1 Millisecond for My Stepping Motor.

    The Visual Basic Software Work very good,all what I send I receive with my Pic,My only Problem ist that wehn I send this Chr(1) to my Word variable Delay ,dont' Work,wehn I send Chr(1) And I use the Pic Code: Delay1 = (DataIn[3] * 256) + DataIn[4] this work but I need 1 Millisecond for my Stepping Motor.

    Thanks.
    Last edited by Pesticida; - 31st December 2006 at 12:39.

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    OK, so do a simple test, skip the HSERIN stuff and load the value in the array manually at the top of your code, then redo your tests.

    Also... there's a little mistake here
    Code:
    hserin 10000,MainStart,[WAIT("!"),STR Datain\13]
    Should be
    Code:
    hserin 10000,MainStart,[WAIT("!"),STR Datain\12]
    I think it's on the VB side... just drop the Mscomm1.Output line
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11


    Did you find this post helpful? Yes | No

    Default

    Hi,

    This is my Visual Basic Communication Code:

    MSComm1.Output = "!" & Chr(Val(CmbAdresse.Text)) & Chr(0) & Chr(Val(TxtLoop.Text)) & Chr(Val(TxtDelayOn1.Text)) _
    & Chr(Val(TxtDelayOn2.Text)) & Chr(Val(TxtKanal1.Text)) & Chr(Val(TxtKanal2.Text)) & Chr(Val(TxtKanal3.Text)) _
    & Chr(Val(TxtKanal4.Text)) & Chr(Val(TxtKanal5.Text)) & Chr(Val(TxtKanal6.Text)) & Chr(13) & Chr(10)
    Sleep 30

    I need for the moment just Byte CmbAdresse.Text,Byte TxtLoop.Text,Byte TxtDelayOn1.Text,Byte TxtDelayOn2.Text.
    I have 13 Bytes !
    Last edited by Pesticida; - 31st December 2006 at 13:01.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Ok I did what You told me,now I wrote direct too Delay1 = 1.

    But dont work,maybe I can not use a Word variable with Pause when this Values less then 256 is.

    I can not find the Answer.

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    EDIT: forgive me i was wrong... i'll be back!!!
    Last edited by mister_e; - 31st December 2006 at 13:29.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14


    Did you find this post helpful? Yes | No

    Default

    Ok,
    but what I dont understand is why when I write Direct in Picbasic Pro:
    Delay1 = 1

    .......

    High PortC.0
    High PortC.1
    low PortC.2
    low PortC.3

    Pause Delay1

    this dont work,but when i write Pause 1 or when :
    Delay1 var byte
    Delay1 = 1

    .......

    High PortC.0
    High PortC.1
    low PortC.2
    low PortC.3

    Pause Delay1
    This work.
    Last edited by Pesticida; - 31st December 2006 at 13:31.

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    It's certainely strange dude...
    Wich PIC #
    Wich PBP version?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  16. #16


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Pic 16F870 and PBP 2.46.

    Maybe is this a Compiler Bug for the Pause Command.
    Last edited by Pesticida; - 31st December 2006 at 13:50.

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    mmm, i think i may have spotted few problems...

    When you declare an array like this...
    Code:
    Array var BYTE[3]
    it will hold 3 different value. And they will be...
    Array[0]
    Array[1]
    Array[2]

    Not MUCH, if you Write to Array[3], the compiler won't complaint... but it will cause you some problem one day or another because you WILL overwrite x variable...

    so just modify your code and it should work better. In VB it work.. but it's a little bit different in PBP.

    Just for testing, try...
    Code:
    DelayArray  var byte[4]
        delayarray[0]=1
        delayarray[1]=10
        delayarray[2]=100
        delayarray[3]=200
    
    Delay       var word
    CounterA    var byte
    
    Start:
        for countera=0 to 3
            high PORTB.0
            delay=delayarray[countera]
            pause delay
            low PORTB.0
            pause delay
            next
        
        goto start
    at least it's working here
    Last edited by mister_e; - 31st December 2006 at 14:07.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  18. #18


    Did you find this post helpful? Yes | No

    Default

    Mister thank a lot I will try now your code,
    I try this too:
    Delay1 var Word
    Delay2 var Byte

    Delay1 = Datain[3] * Datain[4]

    if Delay1 < 256 then
    Delay2 = Delay1
    Delay1 = 0
    else
    Delay2 = 0
    endif

    Loop = (Datain[2] * 10)
    ''''''''''''''''''''''''''''''''''''''''''''Progra m 1''''''''''''''''''''''''''''''''''''''''''''''''' ''
    IF Datain[1] = 0 then
    for N = 0 to Widerholung

    High PortC.0
    High PortC.1
    low PortC.2
    low PortC.3

    Pause Delay1
    Pause Delay2

    low PortC.0
    High PortC.1
    high PortC.2
    low PortC.3

    Pause Delay1
    Pause Delay2

    low PortC.0
    low PortC.1
    high PortC.2
    high PortC.3

    Pause Delay1
    Pause Delay2

    high PortC.0
    low PortC.1
    low PortC.2
    high PortC.3

    Pause Delay1
    Pause Delay2

    Next N
    endif

    This Work ! :-)

  19. #19
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Just to mix some brains

    Quote Originally Posted by HenrikOlsson View Post
    Say you want a 1000mS delay. Since 1000 wont fit in a byte you'll use two bytes. 1000 in binary is 00000011 11101000 so DataIn[3] = 3 and DataIn[4] = 232.
    Or the cryptic way...
    Code:
    @ MOVE?CW 1000, _DataIn+3
    But, in this case DataIn[3]=232 AND DataIn[4]=3, no big deal...

    Or the easier way:
    Delay1.HighByte = DataIn[3]
    Delay1.LowByte = DataIn[4]
    With my example, you'll use...
    Code:
    @ MOVE?WW _DataIn+3, _Delay1
    TADA!
    Last edited by mister_e; - 31st December 2006 at 14:37.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  20. #20


    Did you find this post helpful? Yes | No

    Default

    This mean thet when I want to have One Millisecond i need to put in my DataIn[3] = 0 and DataIn[4] = 1 or I make again a Mistake ?! :-)

  21. #21


    Did you find this post helpful? Yes | No

    Default

    Thanks,
    Your Code work very well but just when my values > 255 is.

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. RF Transmitter
    By et_Fong in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th October 2005, 16:34
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

Members who have read this thread : 1

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