Thread: Reading an array and displaying the outcome using a look up table in C

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

    Reading an array and displaying the outcome using a look up table in C

    So far I have this code, I have been told that I am reading the switches on porth rather than reading the array ( scanner ) as I should be.

    The original problem: Reading an array and displaying the outcome using a look up table in C-capture-jpg

    My Code:

    Code:
    #include <stdio.h>
    int main(void)
    {
    /******************* Declare the port addresses **********************/
     unsigned char *DDRA  = (unsigned char *)0x0002;
     unsigned char *PORTB = (unsigned char *)0x0001;
     unsigned char *DDRB  = (unsigned char *)0x0003;
     unsigned char *PTH   = (unsigned char *)0x0260;
     unsigned char *DDRH  = (unsigned char *)0x0262;
     unsigned char *PERH  = (unsigned char *)0x0264;
    
     /******************* Declare functions*******************************/
     unsigned char threshold;
     unsigned char read;
     unsigned char index;
     /************************** Scanner Data ******************************/
    
     unsigned char scanner[ 255 ] = { 50,
                                      4,  9, 14, 18, 23, 26, 29, 30, 31, 32,
                                     34, 37, 41, 47, 54, 63, 71, 80, 87, 92,
                                     94, 94, 92, 89, 85, 83, 83, 86, 93,102,
                                    115,128,141,153,161,164,164,160,152,144,
                                    137,132,132,136,146,161,178,196,213,226
                                   };
    
    /******************* Set up I/O ports ********************************/
    *DDRH = 0x00;                   /* make Port H an input port */
    *PERH = 0xFF;                   /* enable Port H */
    *DDRA = 0xFF;                   /* make Port A an output port */
    *DDRB = 0xFF;                   /* make Port B an output port */
    
    /******************* Main loop ***************************************/
    
      *PORTB = 255; // This clears the display on PORTB //
    
      threshold = *PTH; // Make PORTH equal to threshold //
    
      read = scanner[0];
    
      index = 1;
    
      do
      {
       *PORTB = pattern (scanner[index]/32);
    
        wait (1);
      }
    
      while
    
      ((index<=scanner[0]) && (read<threshold));
    
    
      if ((index < threshold))
    
      {
    
      printf("threshold reached at reading %d with a current value %  d.",index ,scanner[index] );
    
      }
    
      else
    
      {
    
      printf (" Threshold not reached after %d readings." ,scanner[255]);
    
      }
    
      return 0 ;
      }
     /******************* Pattern Function **********************************/
    
      void pattern (char threshold)
    
      {
            unsigned char LedTable [8] = { 0b00000001, 0b00000011, 0b00000111, 00001111,
                                           0b00011111, 0b00111111, 0b01111111, 0b11111111 };
            unsigned char index;
            unsigned char display;
            unsigned char sw_on;
            unsigned char sw_off;
    
            index = threshold;
            sw_on = ((unsigned char) LedTable[index]);
            sw_off = threshold & 0b11111111;
    
    
        if  ( sw_off & 0b00010000)
    
            {
            display = sw_on | 0b10000000 ;
            }
    
        else
            {
            display = sw_on & 0b01111111;
            }
    
         }
    
    /********************** Wait Function **********************************/
    
    void wait (int seconds)
    
    {
    unsigned int i;
    unsigned int j;
    unsigned int k;
    
      for ( k = 0; k <= seconds; k++)
      {
       for ( i = 0; i <= 50; i++)
      {
       for (j =0; j <= 2000; j++);
      }
      }
    }
    Any help would be much appreciated...

    I am currently using QT for this and cannot generate a s19 file to use in the simulator I have. also cannot download files as I am at work.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by owenb11 View Post
    So far I have this code, I have been told that I am reading the switches on porth rather than reading the array ( scanner ) as I should be.
    Who told you this? I think they may be confused.

    This switch input is taken on Port H, correct?

    On line 36, you're reading the switches (on Port H) into a variable called "threshold". (Note your comment on this line is backwards; you're making "threshold" equal to the value on Port H.) This variable "threshold" is local to main.

    On line 44, you're passing a single element from the "scanner" array to the function called "pattern".

    Skip to line 74, the parameter to the "pattern" function is (probably erroneously) named "threshold", which would be a completely separate variable from the "threshold" declared in main. You still appear to be passing the correct value to the function, it just has a confusing variable name.

    I suspect whoever told you you were reading the wrong thing confused these two (separate yet same-named) variables.

    I would be more impressed if they had asked you where you were incrementing your "index" variable.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You're missing the '0b' prefix on one of your values in the LedTable array.

    You try to access scanner[255] in your final printf (for some reason), but that element doesn't exist.

    You are in desperate need of a typedef:
    Code:
        typedef unsigned char uchar;
    And as Matticus said, you're not incrementing your index variable.
    And I don't see where 'read' and/or 'threshold' are changing, either.
    Last edited by algorism; 12-15-2016 at 02:37 PM.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    First let me say thanks for taking the time to reply.

    I have edited my code and hope I have made things a little more clear, I used index twice as well and have gotten myself a bit confused, is it in the pattern function that I need to be incrementing the index? ( now data) or in the main loop (line 40) where I have index = 1?

    Other than this do I have any issues which would stop the LED's on port b displaying the required pattern?

    Thanks again,
    Owen

    Code:
     
    
    #include <stdio.h>
    int main(void)
    {
       /******************* Declare the port addresses **********************/
        unsigned char *DDRA  = (unsigned char *)0x0002;
        unsigned char *PORTB = (unsigned char *)0x0001;
        unsigned char *DDRB  = (unsigned char *)0x0003;
        unsigned char *PTH   = (unsigned char *)0x0260;
        unsigned char *DDRH  = (unsigned char *)0x0262;
        unsigned char *PERH  = (unsigned char *)0x0264;
     
        /******************* Declare functions*******************************/
        unsigned char threshold;
        unsigned char read;
        unsigned char index;
        /************************** Scanner Data ******************************/
     
        unsigned char scanner[ 255 ] = { 50,
                                          4,  9, 14, 18, 23, 26, 29, 30, 31, 32,
                                         34, 37, 41, 47, 54, 63, 71, 80, 87, 92,
                                         94, 94, 92, 89, 85, 83, 83, 86, 93,102,
                                        115,128,141,153,161,164,164,160,152,144,
                                        137,132,132,136,146,161,178,196,213,226
                                       };
     
        /******************* Set up I/O ports ********************************/
        *DDRH = 0x00;                   /* make Port H an input port */
        *PERH = 0xFF;                   /* enable Port H */
        *DDRA = 0xFF;                   /* make Port A an output port */
        *DDRB = 0xFF;                   /* make Port B an output port */
     
        /******************* Main loop ***************************************/
     
          *PORTB = 255; // This clears the display on PORTB //
     
          threshold = *PTH; // Make threshold equal to *PTH //
     
          read = scanner[0];
     
          index = 1;
     
          do
          {
           *PORTB = pattern (scanner[index]/32);
     
            wait (1);
          }
     
          while
     
          ((index<=scanner[0]) && (read<threshold));
     
     
          if ((index < threshold))
     
          {
     
          printf("threshold reached at reading %d with a current value %d.",index ,scanner[index] );
     
          }
     
          else
     
          {
     
          printf (" Threshold not reached after %d readings." ,scanner[255]);
     
          }
     
          return 0 ;
          }
       /******************* Pattern Function **********************************/
     
          void pattern (char sw_data)
     
          {
                unsigned char LedTable [8] = { 0b00000001, 0b00000011, 0b00000111, 00001111,
                                               0b00011111, 0b00111111, 0b01111111, 0b11111111 };
                unsigned char data;
                unsigned char display;
                unsigned char sw_on;
                unsigned char sw_off;
               
                data = sw_data;
                sw_on = ((unsigned char) LedTable[data]);
                sw_off = threshold & 0b11111111;
     
     
            if  ( sw_off & 0b00010000)
     
                {
                display = sw_on | 0b10000000 ;
                }
     
            else
                {
                display = sw_on & 0b01111111;
                }
               
             }
     
      /********************** Wait Function **********************************/
     
        void wait (int seconds)
     
        {
        unsigned int i;
        unsigned int j;
        unsigned int k;
     
          for ( k = 0; k <= seconds; k++)
          {
           for ( i = 0; i <= 50; i++)
          {
           for (j =0; j <= 2000; j++);
          }
          }
        }

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Sorry to sound like an idiot but what is the purpose of the typedef? I have never come across this before, also for the second printf that you have mentioned I was not sure how to display that the array had reached its maximum value as asked for in the original question.

    Thanks for your help!

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    I see now where I need to increment my index value, but how do I go about this so that I can still have the scanner[index]/32 in the do/while loop

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    That typedef would allow you to write 'uchar' everywhere you have 'unsigned char'. It's not really necessary, of course. Don't worry about it.

    I assume you mean to print scanner[0] in that final printf, since that's how many values you would have scanned.

    In the following loop, it would seem that you mean to increment index in order to step through the scanner array:

    Code:
        index = 1;
        do {
            *PORTB = pattern(scanner[index] / 32);
            wait (1);
        } while (index <= scanner[0] && read < threshold);
    Simply changing index to index++ might do the trick.
    I still don't see how read or threshold can change their values, though.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I believe the threshold is only set once, at the beginning of the program. Ditto read; it takes the number of readings once at the beginning.

    The data array is bigger than it needs to be - it only needs to be able to store 200 bytes values (plus the number of readings at index 0).

    Code:
    /******************* Declare functions*******************************/
    unsigned char threshold;
    unsigned char read;
    unsigned char index;
    These are variables, not functions.

    Code:
    *PORTB = pattern (scanner[index]/32);
    Note that your "pattern" function returns void. It should return unsigned char, if you want to update Port B as shown above in main. And be sure to return a value (the one that holds the binary data of interest).

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    the threshold is read from a set of 8 DIL switches so will change depending on the input from *PTH and as for read I have no idea what so ever. I will have a go with index ++ and see what happens.

    Thanks!

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Quote Originally Posted by Matticus View Post
    I believe the threshold is only set once, at the beginning of the program. Ditto read; it takes the number of readings once at the beginning.

    The data array is bigger than it needs to be - it only needs to be able to store 200 bytes values (plus the number of readings at index 0).

    Code:
    /******************* Declare functions*******************************/
    unsigned char threshold;
    unsigned char read;
    unsigned char index;
    These are variables, not functions.

    Code:
    *PORTB = pattern (scanner[index]/32);
    Note that your "pattern" function returns void. It should return unsigned char, if you want to update Port B as shown above in main. And be sure to return a value (the one that holds the binary data of interest).

    When changing the pattern function to unsigned char pattern (char sw_data) it gives me an error stating that I have conflicting types of pattern? any ideas?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    If the function is defined after main, it should be declared before main. I don't see that in your code. Try putting a function prototype before main, or updating it if it's there.

    Code:
    unsigned char pattern(char sw_data);  // function declaration
    
    int main(void)
    {
        // ...
    }
    
    unsigned char pattern(char sw_data)  // function definition
    {
        // ...
    }

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Matticus View Post
    I believe the threshold is only set once, at the beginning of the program. Ditto read; it takes the number of readings once at the beginning.
    Reading the instructions he posted, I'm thinking that 'read' is just supposed to be the value of the current element. I.e., the 'read' variable is not needed and the loop should be:
    Code:
    index = 1;
    do {
        *PORTB = pattern(scanner[index] / 32);
        wait(1);
    } while (index < scanner[0] && scanner[index++] < threshold);
    OP: Also note that I changed the condition from <= to <. And I moved the increment!

    Of course, using a do/while loop means we're assuming there will always be at least one value in the array, i.e., that scanner[0] >= 1.
    Last edited by algorism; 12-15-2016 at 03:16 PM.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by algorism View Post
    OP: Also note that I changed the condition from <= to <.
    I believe the OP wants the <=, since the data starts at index one (as index zero holds the total elements).

    Code:
    scanner[0] = 5    // total elements: 5
    scanner[1] = 'A'  // index = 1, valid
    scanner[2] = 'B'  // index = 2, valid
    scanner[3] = 'C'  // index = 3, valid
    scanner[4] = 'D'  // index = 4, valid
    scanner[5] = 'E'  // index = 5, valid

  14. #14
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Matticus View Post
    I believe the OP wants the <=, since the data starts at index one (as index zero holds the total elements).
    Agreed. My code degraded as I hacked away at it without rethinking. I'd change it to a for loop:
    Code:
        for (i = 1; i <= scanner[0] && scanner[i] < threshold; i++) {
            *PORTB = pattern(scanner[i] / 32);
            wait(1);
        }
    Whether the threshold comparision should be < or <= depends on how you interpret "reaches" in "if it reaches a threshold".

  15. #15
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Quote Originally Posted by algorism View Post
    Agreed. My code degraded as I hacked away at it without rethinking. I'd change it to a for loop:
    Code:
        for (i = 1; i <= scanner[0] && scanner[i] < threshold; i++) {
            *PORTB = pattern(scanner[i] / 32);
            wait(1);
        }
    Whether the threshold comparision should be < or <= depends on how you interpret "reaches" in "if it reaches a threshold".
    ok I have edited the code to include the recommended for, instead of the do while. I am still having the issues with the conflicting pattern, I may have misunderstood what you meant by adding it before the main as this just produced many errors.

    Also in the pattern function itself I have used display but I have not actually made display do anything, should this line be at the end of the pattern function *PORTB = Display??

    Code:
    #include <stdio.h>
    int main(void)
    {
       /******************* Declare the port addresses **********************/
        unsigned char *DDRA  = (unsigned char *)0x0002;
        unsigned char *PORTB = (unsigned char *)0x0001;
        unsigned char *DDRB  = (unsigned char *)0x0003;
        unsigned char *PTH   = (unsigned char *)0x0260;
        unsigned char *DDRH  = (unsigned char *)0x0262;
        unsigned char *PERH  = (unsigned char *)0x0264;
     
        /******************* Declare variables *******************************/
        unsigned char threshold;
        unsigned char read;
        unsigned char index;
        unsigned char i;
     
        /************************** Scanner Data ******************************/
     
        unsigned char scanner[ 255 ] = { 50,
                                          4,  9, 14, 18, 23, 26, 29, 30, 31, 32,
                                         34, 37, 41, 47, 54, 63, 71, 80, 87, 92,
                                         94, 94, 92, 89, 85, 83, 83, 86, 93,102,
                                        115,128,141,153,161,164,164,160,152,144,
                                        137,132,132,136,146,161,178,196,213,226
                                       };
     
        /******************* Set up I/O ports ********************************/
        *DDRH = 0x00;                   /* make Port H an input port */
        *PERH = 0xFF;                   /* enable Port H */
        *DDRA = 0xFF;                   /* make Port A an output port */
        *DDRB = 0xFF;                   /* make Port B an output port */
     
        /******************* Main loop ***************************************/
     
          *PORTB = 255; // This clears the display on PORTB //
     
          threshold = *PTH; // Make threshold equal to *PTH //
     
          read = scanner[0];
     
          index = 1;
     
          for ( i = 1; i <= scanner[0] && scanner [i] <threshold; i++)
     
               *PORTB = pattern (scanner[i]/32);
     
                wait (1);
     
     
          if ((index < threshold))
     
          {
     
          printf("threshold reached at reading %d with a current value %d." ,index ,scanner[index] );
     
          }
     
          else
     
          {
     
          printf (" Threshold not reached after %d readings." ,scanner[0]);
     
          }
     
          return 0 ;
          }
       /******************* Pattern Function **********************************/
     
          void pattern (char sw_data)
     
          {
                unsigned char LedTable [8] = { 0b00000001, 0b00000011, 0b00000111, 0b00001111,
                                               0b00011111, 0b00111111, 0b01111111, 0b11111111 };
                unsigned char data;
                unsigned char display;
                unsigned char sw_on;
                unsigned char sw_off;
     
                data = sw_data;
                sw_on = ((unsigned char) LedTable[data]);
                sw_off = sw_data & 0b11111111;
     
     
            if  ( sw_off & 0b00010000)
     
                {
                display = sw_on | 0b10000000 ;
                }
     
            else
                {
                display = sw_on & 0b01111111;
                }
     
             }
     
      /********************** Wait Function **********************************/
     
        void wait (int seconds)
     
        {
        unsigned int i;
        unsigned int j;
        unsigned int k;
     
          for ( k = 0; k <= seconds; k++)
          {
           for ( i = 0; i <= 50; i++)
          {
           for (j =0; j <= 2000; j++);
          }
          }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying distance from a table of values
    By JJ_007 in forum C Programming
    Replies: 1
    Last Post: 10-01-2014, 10:44 PM
  2. Displaying a Well-Formatted Table
    By Euph11 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2013, 06:04 PM
  3. Displaying Scores in Table
    By jappy512 in forum C Programming
    Replies: 11
    Last Post: 12-15-2009, 09:59 PM
  4. Displaying a table using nested loops
    By leviterande in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 04:42 PM
  5. Displaying a table
    By hkguy in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2004, 07:49 AM

Tags for this Thread