Stumped with word variable


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Stumped with word variable

    Hi guys,

    I have four variables in my code that are words as I need to store numbers between 0 and 1439. That part is fairly straight forward and I can manually enter "White_on_Time =840" and "White_off_Time = 1230" etc However I want to use some pre-sets in my code via the data statement. This works fine with byte variables but I can't for the life of me get correct value into the variable via the data / read statements.

    I've tried "data word" and then the values but that fails, so consulting the manual I tried the READ (variable).byte
    Code:
    read 10,Blue_on_Time.0
    read 11,Blue_on_Time.1
    But that didn't work either.

    The strange thing is I have a menu option to set these values thus
    Code:
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set On Time For "
        IF H_butt = 0 THEN GOSUB delHours
        IF M_butt = 0 THEN GOSUB delMinutes
        lightsetHR[fn]=Hours
        lightsetMN[fn]=Minutes
        if viv >=2 then viv =2
        if viv= 1 then LCDOUT $FE,$C0,"Blue Leds "
        if viv= 2 then LCDOUT $FE,$C0,"white Leds "   
        lcdout $FE,$94,#lightsetHR[fn] DIG 1,#lightsetHR[fn] DIG 0,":",#lightsetMN[fn] DIG 1,#lightsetMN[fn] DIG 0
        Blue_on_Time = (lightsetHR[0]*60)+lightsetMN[0]
        White_on_Time = (lightsetHR[1]*60)+lightsetMN[1]
    /snip
    If I left the read / write as normal (ie without the 1st byte / 2nd byte modifier) and set the time to 14:00 hrs (ie (14*60)+0 = 840)via the menu option it works. I have a test line on the LCD which shows the dec value for blue_on_time is 840.

    Any pointers as to what I'm doing wrong ?

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,642


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    read 10,Blue_on_Time.0 is bit 0 not the low byte
    read 11,Blue_on_Time.1 is bit 1 not the high byte
    you want
    read 10,Blue_on_Time.lowbyte
    read 11,Blue_on_Time.highbyte

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Cheers Richard, will give that a go.

    Guess I need to use the same method to write the value to memory ie write 10,blue_on_time.lowbyte and write 11,blue_on_time.highbyte

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,642


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    yes its hard to remember wether the data statement saves words as highbyte first or not
    and then does the write word do the same ?, to read them back properly you must get it right.
    but write 1 ,word wordvar and read 1, wordvar is the way to go if you can't be bothered understanding what happens

    data @1 ,1,2 ; (wordvar.highbyte),( wordvar.lowbyte) $0102 =258 the question is ,is data @1,word 258 is it the same thing try it and see

    the easy way out if your not sure about data is to :-
    write 1, wordvar.highbyte, wordvar.lowbyte
    read 1 , wordvar.highbyte,wordvar.lowbyte
    its less efficient but it leaves no doubt.
    you could always read the manual or just experiment to find out , the trick is though to be able to remember it
    for next time

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Quote Originally Posted by richard View Post
    you could always read the manual
    I had referred to the manual, but just didn't comprehend how this worked...

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Uhmmm.

    Just tried
    write 10,Blue_on_Time.highbyte

    and on compiling it complains of illegal character, with a code [108].

    I've gone through the code and it compiles fine if I remove the .highbyte .... confused !!

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Hi,
    I copy/pasted the above line into a blank program, added Blue_On_Time VAR WORD to the top and it compiled just fine for a 16F628.
    Code:
    Blue_On_Time VAR WORD
    
    write 10,Blue_on_Time.highbyte
    
    END
    Try removing the complete line, then re-enter it or copy/paste it from your own post. Perhaps there's some strange hidden character or something lurking. Like I said, compiles just fine here.

    /Henrik.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,642


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    I just tried it for a 12f683 and it won't compile for me,i sure I have been successful for other chips or else I could just be going mad
    jumping between pic's and arduino's
    are you using pbp3 ?
    maybe just use write 10 ,word Blue_on_Time and read 10 ,word Blue_on_Time

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    That's strange. Tried it again, compiled for a 12F683 and it worked, exactly as the same code as before. I'm on PBP3, don't know about Scampy.

    /Henrik,

  10. #10
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Thanks guys for the additional input. I'm running 2.60 and MPLAB 8.87 which has never been a problem.

    I've come across a previous bit of code that used the highbyte / lowbyte and that compiled fine so it must be to do with something in that statement. I'll try changing the variables (removing the underscore) and see if that has any affect, but as the example code I found also used underscores than I can't see that being the issue.

    Malcolm

  11. #11
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Confused !

    I loaded a version of the code that contained hard coded values for the light on times. EG 14:00 is 840 minutes after midnight. I commented out the hard coded values and enterd the data statements etc so this is what I had

    Code:
    Blue_on_Time var word            'stores the blue on time as a digit in minutes, eg 14:00hrs = 840
    I then added the data statement
    Code:
    data @0
        data    00      'Blue fade IN duration hours
        data    30       'Blue fade IN duration minutes
        data    00      'Blues fade OUT duration hours
        data    30       'Blue fade OUT duration minutes
        data    00       'Whites fade IN duration hours
        data    30      'Whites Fade IN duration MINS
        data    00       'Whites Fade OUT duration HOURS
        data    30       'Whites Fade OUT duration MINS
        data    255       'Blue MAX Intensity %
        data    255      'Whites MAX Intensity %
        data    840
    I then added the read statement

    Code:
    read 10,Blue_on_time
    I then added the line so I could see what values for Blue on time I was getting
    Code:
    lcdout $FE,$D4+11,dec Blue_on_Time
    When that was complied and loaded, the program gave a value of 74 when first run, and obviously the condition was not matched and the blue LEDs remained off. If I then go into the menu option to set the on time to 14:00 the LCD correctly displays 840 as the LCD (simplified part of the code here)
    Code:
    Lighton:
    
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set On Time For "
        IF H_butt = 0 THEN GOSUB delHours
        IF M_butt = 0 THEN GOSUB delMinutes
        lightsetHR[fn]=Hours
        lightsetMN[fn]=Minutes
        if viv >=2 then viv =2
        if viv= 1 then LCDOUT $FE,$C0,"Blue Leds "
        if viv= 2 then LCDOUT $FE,$C0,"white Leds "   
        lcdout $FE,$94,#lightsetHR[fn] DIG 1,#lightsetHR[fn] DIG 0,":",#lightsetMN[fn] DIG 1,#lightsetMN[fn] DIG 0
        Blue_on_Time = (lightsetHR[0]*60)+lightsetMN[0]
        White_on_Time = (lightsetHR[1]*60)+lightsetMN[1]
        
        write 10,Blue_on_Time
    If then add the high / low byte to the option in the read and write statements I get the Illegal character error again.

    The other thing that is now not happening is that if I remove the highbyte / lowbyte from the read / write commands program the chip and then manually set the on time the LCD displays 840 as the value, but when the counter value is reached (ie the clock turns to 14:00) there is no change in state of the LEDs. So its not matching the correct value for the word variable. Could this be due I'm comparing the decimal value for the variable, when in real terms it's still in hex decimal or something ?

    My other option would be to use the lightsetHR[0] and lightsetMN[0] as the data values saved and then try and let the program do the conversion Blue_on_Time = (lightsetHR[0]*60)+lightsetMN[0].


    Update: I've tried the above and I still can't get the case to change from off to on

    Code:
    data @0
        data    00      'Blue fade IN duration hours
        data    30       'Blue fade IN duration minutes
        data    00      'Blues fade OUT duration hours
        data    30       'Blue fade OUT duration minutes
        data    00       'Whites fade IN duration hours
        data    30      'Whites Fade IN duration MINS
        data    00       'Whites Fade OUT duration HOURS
        data    30       'Whites Fade OUT duration MINS
        data    255       'Blue MAX Intensity %
        data    255      'Whites MAX Intensity %
        data    14
        data    00
    And then have this in the read routine

    Code:
    Read 10,lightsetHR[0]
    Read 11,lightsetMN[0]
    This compiled Ok and when loaded the LCD correctly displayed 840 as the value of Blue_on_time, but when the clock turned over from 13:59 to 14:00 the case still remains the same and the LED failed to light.

    If I comment out the read write and data lines, and code the value blue_on_time=840 it works !!!! - obviously I would like to be able to change the on and off times of the two channels (white and blue) rather than had coded.
    Last edited by Scampy; - 9th November 2013 at 17:46. Reason: update on further test

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    Hi,
    You can't just say DATA 840 since that is larger than what fits in a byte.
    Are you sure the LCD said 74 and not 72? 840 in binary is 1101001000, look at the 8 least significant bits (marked in red), that's what will actually fit within the byte in EEPROM. It says 72.

    As been written previously you need to use the WORD modifier to store a WORD in EEPROM, and then use it again to read it. Or read the high and low byte "manually" after figuring out in which order the DATA statement stores them.

    /Henrik.

  13. #13
    Join Date
    May 2013
    Location
    australia
    Posts
    2,642


    Did you find this post helpful? Yes | No

    Default Re: Stumped with word variable

    the highbyte,lowbyte works for ok me now (as it should) I must have too tired last nite , interesting when I used write 10, tst.highbyte before declaring tst as a word mcs5 crashed with no error message just died.
    scampy
    it looks like your reading from location 10 but your data @0 writes from location 0

Similar Threads

  1. When variable may not fits into WORD limitations
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th July 2013, 09:26
  2. How to isolate 10 bits from word variable?
    By rcbandwidth123 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 14th April 2008, 20:18
  3. adding new word variable ?
    By iugmoh in forum General
    Replies: 4
    Last Post: - 21st February 2008, 00:26
  4. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16
  5. word variable to 25lc640
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th July 2004, 19:59

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