CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ERROR for flex_kbd4x4.c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Easya



Joined: 28 May 2014
Posts: 3
Location: Malaysia

View user's profile Send private message

ERROR for flex_kbd4x4.c
PostPosted: Wed May 28, 2014 10:57 am     Reply with quote

hai,i'm using keypad 4x4 with pic18f4550.i had been continuously compile this programming, but every time i run it in the pic c compiler,there's always one error detected,& it was the same error again & again.the error was at the "char const KEYS [4][4]=....",the error state,"#DEVICE required before this line".someone,please help me!

Code:


///////////////////////////////////////////////////////////////////////////
////                             Flex_KBD.C                            ////
////                  Generic keypad scan driver                       ////
////                                                                   ////
////  kbd_init()   Must be called before any other function.           ////
////                                                                   ////
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
////                   This function should be called frequently so as ////
////                   not to miss a key press.                        ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////



//Keypad connection: 

#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3


// Keypad layout:
char const KEYS[4][4] =
{{'7','8','9','/'},
 {'4','5','6','X'},
 {'1','2','3','-'},
 {'*','0','=','+'}}; 

#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
                                  // n is the number of times you expect
                                  // to call kbd_getc each second



void kbd_init()
{
}


short int ALL_ROWS (void)
{
   if (input (row0) & input (row1) & input (row2) & input (row3))
      return (0);
   else
      return (1);
}



char kbd_getc( )
 {
   static byte kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static byte col;

   byte kchar;
   byte row;

   kchar='\0';
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR)
   {
       switch (col)
       {
         case 0   : output_low (col0);
                    output_high(col1);
                    output_high(col2);
                    output_high(col3);
                    break;
         case 1   : output_high(col0);
                    output_low (col1);
                    output_high(col2);
                    output_high(col3);
                    break;
         case 2   : output_high(col0);
                    output_high(col1);
                    output_low (col2);
                    output_high(col3);
                    break;
         case 3   : output_high(col0);
                    output_high(col1);
                    output_high(col2);
                    output_low (col3);
                    break;
       }

       if(kbd_down)
       {
         if(!ALL_ROWS())
         {
           kbd_down=false;
           kchar=last_key;
           last_key='\0';
         }
       } else
       {
          if(ALL_ROWS())
          {
             if(!input (row0))
               row=0;
             else if(!input (row1))
               row=1;
             else if(!input (row2))
               row=2;
             else if(!input (row3))
               row=3;
             last_key =KEYS[row][col];
             kbd_down = true;
       }
          else
          {
             ++col;
             if(col==3)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  return(kchar);
}

_________________
#yehet
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:20 am     Reply with quote

Hi,

Have a look at this thread: http://www.ccsinfo.com/forum/viewtopic.php?t=42707&start=4

Note the order in which things are defined at the top of the program. We can't tell because you didn't post this code, but my guess is that you've got something out of proper order.

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:23 am     Reply with quote

You need a program.....

You have to define your own 'main' program, with processor defines etc., and then 'include' this file.

Look at the CCS examples. Say ex_temp.c. You will see that you have this file, which defines the processor, and then includes the driver for the sensor it uses (ds1621.c). The keyboard file (and almost every other posted driver), needs to be used the same way. They are drivers, not complete programs, and won't compile without suitable 'main' code.
Easya



Joined: 28 May 2014
Posts: 3
Location: Malaysia

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:27 am     Reply with quote

ezflyr wrote:
Hi,

Have a look at this thread: http://www.ccsinfo.com/forum/viewtopic.php?t=42707&start=4

Note the order in which things are defined at the top of the program. We can't tell because you didn't post this code, but my guess is that you've got something out of proper order.

John


thanks for the quick reply.i'm sorry,i'm such a slow learner.especially in this field...did u mean i have to change the port that i connected with another port for the compatibility?
_________________
#yehet
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:35 am     Reply with quote

Hi,

No, maybe Ttelmah is right; you don't have a program?? I assumed you just didn't post it, but maybe it doesn't exist? Look at the example I posted. That is the "program" which makes use of the keypad "driver". You need both.

John
Easya



Joined: 28 May 2014
Posts: 3
Location: Malaysia

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:36 am     Reply with quote

Ttelmah wrote:
You need a program.....

You have to define your own 'main' program, with processor defines etc., and then 'include' this file.

Look at the CCS examples. Say ex_temp.c. You will see that you have this file, which defines the processor, and then includes the driver for the sensor it uses (ds1621.c). The keyboard file (and almost every other posted driver), needs to be used the same way. They are drivers, not complete programs, and won't compile without suitable 'main' code.


hai,i'm sorry but i have the program...because i can't run this program, i started to look for the error in the flex file,it turns out that the flex file is for keypad 4x3,then i try to change it to 4x4 keypad,then the error pop out. below are the main program

Code:

//Keypad Driver: flex_kbd4x4.c
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3

// Keypad layout:
char const KEYS[4][4] =
{{'7','8','9','/'},
 {'4','5','6','X'},
 {'1','2','3','-'},
 {'*','0','=','+'}};


#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second

void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true); 
}

short int ALL_ROWS (void)
{
if(input (row0) & input (row1) & input (row2) & input (row3))
   return (0);
else
   return (1);
}



char kbd_getc()
{
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;

byte kchar;
byte row;

kchar='\0';

if(++kbd_call_count>KBD_DEBOUNCE_FACTOR)
  {
   switch (col)
     {
      case 0:
        output_low(col0);
        output_high(col1);
        output_high(col2);
        output_high(col3);
        break;
   
      case 1:
        output_high(col0);
        output_low(col1);
        output_high(col2);
        output_high(col3);
        break;

      case 2:
        output_high(col0);
        output_high(col1);
        output_low(col2);
        output_high(col3);
        break;

      case 3:
        output_high(col0);
        output_high(col1);
        output_high(col2);
        output_low(col3);
        break;
      }

   if(kbd_down)
     {
      if(!ALL_ROWS())
        {
         kbd_down=false;
         kchar=last_key;
         last_key='\0';
        }
     }
   else
     {
      if(ALL_ROWS())
        {
         if(!input (row0))
            row=0;
         else if(!input (row1))
            row=1;
         else if(!input (row2))
            row=2;
         else if(!input (row3))
            row=3;

         last_key =KEYS[row][col];
         kbd_down = true;
        }
      else
        {
         ++col;
         if(col==4)
            col=0;
        }
     }
   kbd_call_count=0;
  }
return(kchar);
}

//**********************************************************
//Main Program

#include <18F4550.h> // PIC18F4550 HEADER FILE
#fuses XT,NOWDT,NOLVP,NOPROTECT // EXTERNAL CLOCK, NO WATCH DOG TIMER, NO LOW VOLTAGE
#device adc=10 // USE 10 BIT ADC QUANTIZATION
#use delay (clock=4M) // 4 MHZ CRYSTAL

#include <flex_lcd420.c>
#include <flex_kbd4x4.c>


//Main Program

void main()

   char k;
   
   lcd_init();
   kbd_init();

   lcd_putc("\fReady...\n");
   //Pin Configuration
   
   while(true)
   {
      //Program start here
      k=kbd_getc();
      if(k!=0)
        if(k=='*')
         {
          lcd_putc('\f');
          delay_ms(100);
          lcd_putc("\fReady...\n");
         }
        else
          lcd_putc(k);
   }

}


_________________
#yehet
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed May 28, 2014 11:39 am     Reply with quote

Hi,

The problem is that you aren't supposed to include the "driver" code in the main listing like that. You can do it, but the order needs to be correct. So, get rid of the driver code at the top of your listing, and just reference the driver the way you've done with the #include.

John
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group