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

  1. #16
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Please read your code carefully and think about it.

    I don't recall saying the loop should go before main. That makes no sense.

    You don't need the 'index' variable anymore.

    You left out the braces for the body of the for loop.

    Your pattern function still isn't returning anything.

    You don't need the 'read' variable at all.

    I don't understand your PORTB question. Looks like you're assigning to *PORTB in the loop.
    Last edited by algorism; 12-15-2016 at 04:50 PM.

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by algorism View Post
    I don't recall saying the loop should go before main. That makes no sense.
    The OP is referring to the function declaration. Changing the return type of "pattern" is producing errors, so I suggested they make sure the function is prototyped.

    Quote Originally Posted by algorism View Post
    Your pattern function still isn't returning anything.

    I don't understand your PORTB question. Looks like you're assigning to *PORTB in the loop.
    As stated above, this was apparently causing errors. I'm not sure why, though, based on the code.

    @OP: See an example of declaring a function (before main) when defining it after main: function calsum should have a prototype??

    If this is causing errors, post the updated code along with the error messages.

  3. #18
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Quote Originally Posted by Matticus View Post
    The OP is referring to the function declaration. Changing the return type of "pattern" is producing errors, so I suggested they make sure the function is prototyped.



    As stated above, this was apparently causing errors. I'm not sure why, though, based on the code.

    @OP: See an example of declaring a function (before main) when defining it after main: function calsum should have a prototype??

    If this is causing errors, post the updated code along with the error messages.
    this is my updated code:

    Code:
    #include <stdio.h> unsigned char pattern (int); // set up function // 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 index;    unsigned char i;     /******************* Declare function prototypes**********************/    void wait(int); //set up of function //      /************************** 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 //       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 **********************************/       unsigned char 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++);      }      }    }
    the error I am receiving is that there is conflicting types for pattern, it directs me to the two pattern uses, this is the only error in the program.
    Last edited by owenb11; 12-15-2016 at 06:06 PM.

  4. #19
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    not sure why the code is going like that

  5. #20
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by owenb11 View Post
    not sure why the code is going like that
    Try it again. It's not readable like that.
    Make sure you copy and paste it as plain text.

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by owenb11 View Post
    not sure why the code is going like that
    Well, try again. Apart from being unreadable, I think most of it didn't make it to the forum. Go to the advanced page, paste as text, and preview the post to ensure the code is formatted correctly.

  7. #22
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Quote Originally Posted by Matticus View Post
    Well, try again. Apart from being unreadable, I think most of it didn't make it to the forum. Go to the advanced page, paste as text, and preview the post to ensure the code is formatted correctly.
    Ok here goes again

    Code:
    #include <stdio.h> 
    unsigned char pattern (int); // set up function //
     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 index;    
    unsigned char i;    
    
     /******************* Declare function prototypes**********************/  
    
      void wait(int); //set up of function //   
    
       /************************** 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 //   
     
       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 **********************************/   
        unsigned char 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++); 
         }   
       } 
       }
    So the error message is conflicting types for pattern and leads me to both times pattern is declared.

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Function declaration:

    Code:
    unsigned char pattern (int);
    Function defintion:

    Code:
    unsigned char pattern (char sw_data)
    {
        ...
    Something isn't lining up correctly.

  9. #24
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    Duh! don't know how I missed that one! that's the error cleared.

    As for my pattern function not returning anything I really do not know where to go from where I am....?

  10. #25
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You return a value from the function. The link I posted above includes a return statement for illustration.

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Think about it with an analogy. Here was the original version of the function:

    Code:
    void pattern (char threshold)
    It expects a character, but does not return anything (void). It's like a parking meter - you put money into it (pass it a char), but you get nothing tangible back (void).

    Now look at the updated version:

    Code:
    unsigned char pattern (char sw_data)
    It expects a character, and returns an unsigned character. It's like a vending machine - you put money into it (pass it a char), and it returns a candy bar (unsigned char).

    Speaking of which, you should make that function argument unsigned.

  12. #27
    Registered User
    Join Date
    Dec 2016
    Posts
    12
    That's cleared things up thanks!

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