Thread: Need help on Loop implementation

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    34

    Need help on Loop implementation

    Hi,

    I am doing 8-bit microcontroller programming in C. I can send only 8-bit data at one time. I am getting input as union. After sending 16-bits, I have to insert a delay. Total 64-bit needs to be sent. How to use for loop efficiently. Here is the general code :

    Code:
    typedef union{
       uint16_t data0;
       uint16_t data0;
       uint16_t data0;
       uint16_t data0;
    }data_u;
    
    void get_data(data_u data)
    {
            //here i have to get all members of union and get the 8-bit(1 byte)  //of each into below for loop inserting delay after each 16-bit transfer(2 byte).
    
               for()
               {
                     exchange8bit(union_data);
                      //delay after 2-byte transfer 
          
                }
    // loop should transfer total 6-bits means 8 bytes.
    }
    
    void exchange8bit(uint8_t data)
    {
    
    }
    I am confused how to achieve it. Please help and send me the correct code.

    --David

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Do you know what a union is?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Firstly, your union members can't all have the same name.

    Secondly, do you realize that only one member of the union is "active" at any one time? It doesn't make sense to "get all members" of the union.

    In fact, your union doesn't really make sense at all.

    Did you mean it to be a struct instead of a union?

    // loop should transfer total 6-bits means 8 bytes.
    What???

    And what does "exchange8bit" do?

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Hobbit,

    Yes, I know. In union declaration there is typo. Read as data0, data1, data2, data3. May be you asked as i wrote
    exchange8bit(union_data);
    Actually I would get data_.data0 etc.. then get the both 8-bits of each and send them in loop.
    But little confused as after each 16-bit delay needs to be given. Please help.

    --david

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are you sure a union is appropriate for your purpose? I don't fully understand what you're trying to achieve (where are you "sending" data?), but I'd expect maybe an array of bytes instead. Then you can just send two at a time.

    If you're using a toolchain targeted specifically for the device, check the documentation for a standard "delay" function. Or, even better, use a timer interrupt for your delay.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi algorism,

    Read my reply to hobbit. There was a typo in union. Total 64-bits. exchange8bit sends 1 byte data at a time. Like 0x80.

    --david

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    So you know what a union is, and yet you still want to send all members? Do you realize that they will all have exactly the same value at all times? In essence they're just different names for the same thing. This is a senseless use of a union.

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Matticus...Read my reply . There was a typo in union. How can i transfer 8 bytes in one for loop giving delay after each 2-byte transfer.
    delay is just a single line func call

    Code:
    void get_data(data_u data)
    {
            //here i have to get all members of union and get the 8-bit(1 byte)  //of each into below for loop inserting delay after each 16-bit transfer(2 byte).
     
               for()
    
               {
                     exchange8bit(union_data);
                     __delay_us(5);       
    
                }
    // loop should transfer total 64-bits means 8 bytes.
    
    }

  9. #9
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    That union does not total 64 bits, it totals 16 bits. As I said do you actually know what a union is?

    Do you know how to split a 16 bit number into 2 8 bit numbers?

    What operating system does this run on? For delay you usually have to rely on operating systems's APIs.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Hobbit View Post
    What operating system does this run on? For delay you usually have to rely on operating systems's APIs.
    This is for an 8-bit microcontroller, so it's likely there's no OS.

  11. #11
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Hobbit, I am wrong. I feel I have to send as buffer of 8-bytes only. Didn't think correctly. Its microcontroller without OS. delay is just a call.
    Could you please tell if its structure how to do the same?

    --david.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are you supposed to use a union? If so, you're not using it correctly. Perhaps you want one member to be an 8-byte variable, and the other to be an array of one-byte variables?

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A union is supposed to reserve enough space for the largest member. When you ask for a specific member, the underlying bit pattern is interpreted as that thing.

    For example, you can store an 16-bit integer in the space of a double type variable, as long as you can guarantee the bit pattern is a double when you use that member, and a short when you use that member.

    All you are asking us for is code that essentially takes the bit pattern from the largest member in size and splits it into smaller chunks.

    Using a union does not mean or imply that you can set the largest member in size and then use the smaller members to inspect parts of its bit pattern.

  14. #14
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Matticus ... as per other's suggestion I would go with array of one-byte variables...

  15. #15
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Suppose array of 8-bytes has to be passed in

    Code:
    uint8_t buf[8] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};  void get_data(uint8_t buf[])
    
      {
    
            //here i have to send all members of array one by one calling delay after each 16-bit transfer(2 byte).
    
                 for() 
    
               {
    
                     exchange8bit(uint8_t data);
    
                     __delay_us(5);   //only after transferring -bytes.
    
               } // loop should transfer total 8 bytes.     
    
       }


    What should be initializer, comparator, increment values in above for loop.

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