Easy way to do multiple write statements ?


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Easy way to do multiple write statements ?

    Guys, need some advice as I'm currently brain dead trying to work out a solution to this issue.

    I'm working on a project that has multiple array variables to store times and PWM value (word). Three are 16 variables for on hours, 16 variables for on minutes, 16 variables for off hours, 16 for off minutes and 16 for the PWM value

    Rather than have to do each line for the 80 WRITE statements, eg (not actual code, just to give the gist of things)
    Code:
    write address$, setHR(1),setMN(1),offHR(1),offMN(1),pwm(1)lowbyte, pwm(1)highbyte
    write address$+1, setHR(2),setMN(2),offHR(2),offMN(2),pwm(2)lowbyte, pwm(2)highbyte
    write address$ +2, setHR(3),setMN(3),offHR(3),offMN(3),pwm(3)lowbyte, pwm(3)highbyte
    etc
    is there a way of doing this with a single statement ?

    I did try a for / next loop, placing the counter variable in the brackets

    Code:
    address =$50
    for counterd = 1 to 16
    write address,lightsetHR(counterD),lightsetMN(counterD),lightoffHR(counterD),lightoffMN(counterD),fadeset(counterD)
    next counterD
    which works for the initial run when Counter =1, but then overwrites the address on the subsequent loops when counter = 2 to 15. I assume I need to add a value to the address or something, which I tried by adding a line which added 16 to the value, but that didn't work..

    Ideally what I'm after is the first time the device is run it reads a set of default timings and pwm values until these are changed by using menu options or a PC application, which then writes the new values to the PICs memory, and sets a flag. The next time the unit powers up, it checks to see if the flag has been set and if so loads the saved valued from memory.

    Any ideas

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    address =$50
    for counterd = 0 to 15
    write (address +counterD),lightsetHR(counterD),lightsetMN(counterD),lighto ffHR(counterD),lightoffMN(counterD),fadeset(counte rD)
    next counterD

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,121


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    Although not specified directly, i'd first would make the math and then place the result in the write command:

    temp1=addres+counterD

    write temp1... etc

    I2Cwrite and I2Cread commands work only this way.

    Ioannis

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    Quote Originally Posted by Ioannis View Post
    Although not specified directly, i'd first would make the math and then place the result in the write command:

    temp1=addres+counterD

    write temp1... etc

    I2Cwrite and I2Cread commands work only this way.

    Ioannis
    So the normal WRITE command won't work in this way ?

  5. #5
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    Assuming that each of the hours and minutes are bytes and PWM value is a word, the write command will fill up 6 bytes on each pass. So, the code would become
    Code:
       address =$50
       for counterd = 0 to 15
           write (address + (counterD*6)),lightsetHR(counterD),lightsetMN(counterD),lighto ffHR(counterD),lightoffMN(counterD),fadeset(counte rD)
       next counterD
    OR you could also do this
    Code:
      address =$50
      for counterd = 0 to 15
         write (address),lightsetHR(counterD),lightsetMN(counterD),lighto ffHR(counterD),lightoffMN(counterD),fadeset(counte rD)
         address = address+6  ' account for the data already filled into the storage
      next counterD

  6. #6
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    You spoke in your original post of an "easier way", while this is not exactly what you asked, I find it easier and so offer...

    Keeping track of hours and minutes separately is unnecessary. For me, over and again, I have determined that a word value representing the minutes since midnight (a value of 0 to 1440) is clearer and easier to manipulate. Switching to hours and minutes is as easy as /60 for hours and MOD 60 for minutes. All calculations loose the clumsiness of handling hours and minutes separately and determining if dates change when subtracting or adding periods is less complicated.

    It doesn't save any actual eeprom space, but it could reduce the typing by changing "lightsetHR(counterD),lightsetMN(counterD)," to SetTime(counterD) and "lightoffHR(counterD),lightoffMN(counterD)," to OnOffTime(counterD)

    ... just a thought.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Easy way to do multiple write statements ?

    Quote Originally Posted by Jerson View Post
    Assuming that each of the hours and minutes are bytes and PWM value is a word, the write command will fill up 6 bytes on each pass. So, the code would become
    Code:
       address =$50
       for counterd = 0 to 15
           write (address + (counterD*6)),lightsetHR(counterD),lightsetMN(counterD),lighto ffHR(counterD),lightoffMN(counterD),fadeset(counte rD)
       next counterD
    OR you could also do this
    Code:
      address =$50
      for counterd = 0 to 15
         write (address),lightsetHR(counterD),lightsetMN(counterD),lighto ffHR(counterD),lightoffMN(counterD),fadeset(counte rD)
         address = address+6  ' account for the data already filled into the storage
      next counterD
    Guys, I need a little help in a similar vein.

    I'm working on a slightly different program using similar variables only this time I save 48 word variables, which results in 96 lines of code for both WRITE and READ statements. The word variables are
    CH1_Max, CH1_on_Time, CH1_off_Time, so I have

    Code:
    read 40,CH1_on_Time.lowbyte          
    read 41,CH1_on_Time.highbyte
    read 42,CH2_on_Time.lowbyte          
    read 43,Ch2_on_Time.highbyte
    Rather than have 96 lines of code is there a simple way to do this with a FOR / NEXT loop similar to above

    I did try

    Code:
    for n = 8 to 38 step 2
    READ n, ch(n)_max.lowbyte
    next n
    
    
    for n = 9 to 39 step 2
    READ n, ch(n)_max.highbyte
    next n
    But this just threw up errors when I tried compiling (I'm using PBP 2.60c)

    Any suggestions ?

Similar Threads

  1. Darrels interrupts and multiple SOUND statements, will it work?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 31
    Last Post: - 26th January 2015, 07:42
  2. Programming multiple pics with multiple programs
    By Luckyborg in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 3rd April 2013, 17:47
  3. Multiple IF THEN Statements: PIC16F84A
    By bob425 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th August 2012, 06:01
  4. SD Multiple Write?
    By jimbostlawrence in forum General
    Replies: 1
    Last Post: - 21st October 2011, 17:45
  5. Multiple IF-THEN statements
    By DavidK in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th June 2007, 18:28

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