Thread: Write and read byte at device address

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    Write and read byte at device address

    Hi

    I am trying to write program to write data into array and to readthe the values stored in Array

    For an example
    The Device has 5 registers with date & time info.
    Address 0: tens of seconds,
    Address 1: tens of minutes,
    Address 2: tens of hours,
    Address 3: Day of week, 01 to 07
    Address 4: tens of day of month,

    Code:
    #include<stdio.h>
    
    void send (int N, int *S )
    {
        int n;
        
        for ( n = 0; n <N; n++ )
        {
            printf("values %d \n", *S+n);
        }
    }
    
    
    void Receive (int N, int *P )
    {
        int n;
        
        for ( n = 0; n <N; n++ )
        {
            printf("values %p \n", P+n);
        }
    }
    void main ()
    {
        int values[] = {0,1,2,3,4};
        int i = 5;
        send (5, values);
        Receive (5, values);
    }
    I have problem with receive function I want to read the data store at the five register address

    How to write and read the data from array in functions ?
    I have problem with raceway function

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Quote Originally Posted by gajya View Post
    Hi

    I am trying to write program to write data into array and to readthe the values stored in Array

    For an example
    The Device has 5 registers with date & time info.
    Address 0: tens of seconds,
    Address 1: tens of minutes,
    Address 2: tens of hours,
    Address 3: Day of week, 01 to 07
    Address 4: tens of day of month,

    Code:
    #include<stdio.h>
    
    void send (int N, int *S )
    {
        int n;
        
        for ( n = 0; n <N; n++ )
        {
            printf("values %d \n", *S+n);
        }
    }
    
    
    void Receive (int N, int *P )
    {
        int n;
        
        for ( n = 0; n <N; n++ )
        {
            printf("values %p \n", P+n);
        }
    }
    void main ()
    {
        int values[] = {0,1,2,3,4};
        int i = 5;
        send (5, values);
        Receive (5, values);
    }
    I have problem with receive function I want to read the data store at the five register address

    How to write and read the data from array in functions ?
    I have problem with raceway function
    In the send function you are de-referencing the pointer S (the * operator) and then adding the value of n to it. The value of *S, in your example, is 0 so your program is printing 0 + the value of n as you iterate over the loop. if you used brackets: *(S + n) you would print the values of the array. As you can likely see in your Recieve function (%p tells the compiler you wish to print the value of the pointer P+n) the compiler adds to the pointer the size of int on your system.

    You mention registers but what you really mean is memory locations. Your compiler may put them into registers but that's its business not yours.

    To access the data addressed by P you can either use array syntax P[n] or pointer syntax *(P + n) but you will also need to use the specifier for an int in your printf statement (%d). As I mention %p asks for the value of the pointer itself, not what it points to.

    -Greg.
    Last edited by G.Martin; 12-27-2019 at 09:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read from i2c device with 2 byte internal address register
    By m.mogharabi in forum Networking/Device Communication
    Replies: 3
    Last Post: 02-28-2012, 11:14 PM
  2. Talking to device over Serial: Write works, Read doesn't
    By theBishop in forum C Programming
    Replies: 2
    Last Post: 07-09-2008, 01:40 PM
  3. write() function - no such device or address
    By threahdead in forum Linux Programming
    Replies: 3
    Last Post: 03-30-2003, 05:05 AM

Tags for this Thread