Thread: Need help on Loop implementation

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The steps that allow you to access each element of the array.

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by davidmeetsall View Post
    What should be initializer, comparator, increment values in above for loop.
    Where in the array do you want to start? At the beginning, I'm assuming. What is the first index of that array?

    Where in the array do you want to end? At the end, I'm assuming. What is the last valid index of that array?

    If you want to send two bytes before your delay, and repeat that process, how many indices are you moving through your array each time?

  3. #18
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    I feel
    Code:
    for ( i = 0; i < 8; i+=2 )
    {  exchange8bit(union_data);
    
      __delay_us(5);        
    }
    __delay_us(100);
    One more thing, __delay_us(5) should be given only at 2,4,6 bytes transfer. After 8 bytes delay is different as written.
    Is it fine?

  4. #19
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Is this more appropriate?
    Code:
    uint8_t buf[8] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};  
    void get_data(uint8_t buf[]) 
    { 
        for ( i = 0; i < 8; i+=2 )  
        {
     
             exchange8bit(uint8_t data);
    
             __delay_us(5);   //only after transferring 2-bytes.
     
        } // loop should transfer total 8 bytes.   
        __delay_us(100);
    }

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If "exchange8bit()" sends one byte, and you want to send two, then you're not quite there. Also, you're not calling that function correctly.

    And if your function sends bytes, perhaps "send_data()" is a more appropriate name.

    I would also recommend passing the size of the array to your function, instead of using the hard-coded value of 8.

  6. #21
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    I have modified as per suggestion

    Code:
    uint8_t buf[8] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};  
    void get_data(uint8_t buf[]) 
    { 
        for ( i = 0; i < sizeof(buf); i+=2 )  
        {
     
             send_data(uint8_t buf);
    
             __delay_us(5);   //only after transferring -bytes.
     
        } // loop should transfer total 8 bytes.   
        __delay_us(100);
    }
    But still I feel there is issue which you pointed "If "exchange8bit()" sends one byte, and you want to send two, then you're not quite there." I have modified exchange8bit() as send_data. Yes it sends 1-byte only at a time. How to handle delay after -byte transfer only.
    Please rectify the code.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    sizeof(buf) won't work, as the function actually receives a pointer, and does not know the buffer size. You need to pass the buffer size to the function along with the buffer itself.

    Question 6.4

  8. #23
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Ok. Will do. What about the loop? How to handle delay after 2-byte transfer only and as send_data sends one byte at a time, and not two. Please rectify.

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I meant the function "get_data()" should be called "send_data()", if that's what it is doing.
    The function "exchange8bit()" should be called perhaps "send_byte()", if that is what it is doing.

    Code:
    uint8_t buf[8] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
    
    void send_data(uint8_t buf[], int size) 
    { 
        for ( /* ... */ )
        {
             send_byte(uint8_t buf);
             __delay_us(5);
        }
    }
    If you want to send two bytes, perhaps you should call the "send_byte()" function twice. And you need to fix the function call, as the part in parenthesis is not right.

    Again, I'm not completely sure what that inner function is supposed to be doing, so let us know if it is doing something other than sending a data byte.

  10. #25
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Ok. fine. send_byte will send 1-byte only. once 2-bytes are sent one after other delay func will be called. This way all 8-bytes needs to be transferred.
    One more thing, __delay_us(5) should be given only at 2,4,6 bytes transfer. After 8 bytes delay is different.

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As long as you want that last delay to be longer, then your example in post #18 is fine. After the last two bytes are sent, the (5) delay is called and then the (100) delay is called, which just add to an approximate delay of 105 us.

  12. #27
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Matticus.. thanks for the help. I am able to achieve this.
    I have one more question, I have declared a variable in a function in a .c file as
    extern volatile uint8_t *new_data;

    The same has been declared before main function which is in different file as
    uint8_t *new_data;

    When I compile I get this error
    Conflicting declarations for variable "_new_data".
    What could be the reason?

    If I change both places *new_data to new_data[]....it works.

  13. #28
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Of course it conflicts, one is volatile, one isn't.

    volatile uint8_t *new_data;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. aes implementation
    By andydufresne in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2012, 02:40 AM
  2. GMP RSA implementation
    By andydufresne in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2012, 10:59 AM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. help with implementation
    By NANO in forum C++ Programming
    Replies: 14
    Last Post: 04-30-2002, 01:09 PM
  5. implementation?
    By calQlaterb in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:25 AM

Tags for this Thread