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 support@ccsinfo.com

ICD-U40 and PIC18F452 Development Kit
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

ICD-U40 and PIC18F452 Development Kit
PostPosted: Thu Aug 24, 2006 8:57 pm     Reply with quote

I'm trying to get back into the swing of things for development. Been out a few years. I'm using the Development kit and I have a 4x4 matrix keypad hooked to the B0 to B8 ports. I've followed about every example I can get my hands on but I can't get the program to scan the keypad with valid results. It seems some of the keys are ok but not all of them. I'll see multiple values being generated and sent to my serial display.

Is perhaps the development board doing other things with the 'B' port and not allowing me to scan the keypad?

It seems to be a bummer trying to do something so simple and not having any success.

Thanks for any help.

dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 24, 2006 9:15 pm     Reply with quote

1. Post a link to the code that you're currently using.

2. If you're using a driver that was derived from the CCS kbd.c driver,
do you have pull-up resistors on the Row pins ?
(This could be done by enabling Port B pull-ups).
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

ICD-U40 and PIC18F452 Development Kit
PostPosted: Thu Aug 24, 2006 9:33 pm     Reply with quote

Below is what I have thus far. I'm using pullups on port B. Hope you can understand this. Looks like a mess here.

dave


//Main Line//
#include <protoalone.h>
#include <CrystalFonz_Support.h>

/*#if defined(__PCB__)
#include <16c56.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

#elif defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif*/

//#include <lcd.c>
//#include <New_Test_KBD.c>
//#include <Test_Original_kbd.c>
#include <fLEX_kbd.c>

//#include <myKBD.c>


void main() {
char k;

//lcd_init();
kbd_init();

Clear_Screen(); //clear and home cursor
printf("%c%c%c", 17, 0, CFLine0); //position to line
printf("\r\Starting ...");

while (TRUE) {
k=kbd_getc();
if(k!=0)
printf("%c%c%c", 17, 0, CFLine0); //position to line
if(k=='*')
printf("%c", '\f');
//lcd_putc('\f');
// else if(k==0)
// printf("?");
else
printf("%c", k);
//lcd_putc(k);
}
}

///////////////////////////////////////////////////////////////////////////
//// 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 col0 PIN_E1
#define col1 PIN_A5
#define col2 PIN_C0
#define row0 PIN_E0
#define row1 PIN_C2
#define row2 PIN_C1
#define row3 PIN_E2*/

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

/*#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*/

/*#define row0 PIN_C0
#define row1 PIN_C1
#define row2 PIN_C2
#define row3 PIN_C3
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3*/

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


/*char const KEYS[4][4] = {{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};*/

char const KEYS[4][4] = {{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}};

#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); // If not PCM be sure to use external pullups
}

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);
}

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);
}

//Crystal Fontz Support//
#define CFLine0 0
#define CFLine1 1
#define CFLine2 2
#define CFLine3 3

#use rs232(BAUD=19200,XMIT=PIN_C6,RCV=PIN_C7)

Fontz_Show_Boot()
{
printf("%c%c", 9, 6);
}

Clear_Screen()
{
printf("%c%c", 12, 1);
}

Set_BackLite(BackLite)
{
printf("%c%c", 14, BackLite);
}

Set_Contrast(Contrast)
{
printf("%c%c", 14, Contrast);
}
Cursor_Off()
{
printf("%c", 4);
}

Cursor_ON()
{
printf("%c", 6);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 24, 2006 9:43 pm     Reply with quote

1. You have many different pin assignments and keypad layouts.
Do you you guarantee that the ones that are un-commented
fit your current keypad layout and connections ?
Can you check the wiring, just to be sure ?

2. Can you post the manufacturer and part number of your keypad ?
I need to look at a data sheet for it.

3. Also, do you have anything else connected to Port B, such as an LCD ?
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

ICD-U40 and PIC18F452 Development Kit
PostPosted: Thu Aug 24, 2006 9:52 pm     Reply with quote

I've tried to check and I think the un-commented matches what I have. It is a Grayhill 96BB2-006-R. Nothing else is connected to Port B except what is on the development board. It uses some parts of port B for ICD and a couple of LED's. I've tried to put it in standalone mode without the ICD and get the same results.

dave
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

ICD-U40 and PIC18F452 Development Kit
PostPosted: Thu Aug 24, 2006 9:58 pm     Reply with quote

I should add that from the keypad to the development board;
Pin 1 goes to B4
Pin 2 to B5
Pin 3 to B6
Pin 4 to B7

Pin 5 to B0
Pin 6 to B1
Pin 7 to B2
Pin 8 to B3

thanks again for the help.

dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 24, 2006 10:51 pm     Reply with quote

1. Your keypad layout is incorrect.

If you go to http://www.digikey.com and put in that part number in
the search engine, you get this page:
http://rocky.digikey.com/scripts/ProductInfo.dll?Site=US&V=136&M=96BB2-006-R
Click on the link for the photo. The keypad layout that you have
un-commented, doesn't match the photo. The layout in the middle
does match it. You need to keep the layout that matches the photo
and delete the other ones.

This is the one you need to use:
Code:
char const KEYS[4][4] =
{{'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}};



2. Your pin definitions are incorrect.

Here are your connections, with the Row and Column numbers
of the keypad added in. I got these by looking at the drawing
of the 4x4 Front Mount keypad at the bottom left on page 2
of the data sheet, and also from the Code and Truth table for
the 16 Button Keypad (Standard style) at the bottom right on page 3.
Code:

Pin 1 goes to B4   // Row 0
Pin 2 to B5        // Row 1
Pin 3 to B6        // Row 2
Pin 4 to B7        // Row 3

Pin 5 to B0        // Col 0
Pin 6 to B1        // Col 1
Pin 7 to B2        // Col 2
Pin 8 to B3        // Col 3


Here are the pin definitions that you need to use. You currently
have them commented out. Uncomment these and delete the others.
Code:

#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
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

ICD-U40 and PIC18F452 Development Kit
PostPosted: Fri Aug 25, 2006 7:58 pm     Reply with quote

Thanks for the help on that. I made the changes and got rid of the unused stuff. I fired it up and this is what I get.
When I press key 1 = the yellow led on the board lites up.
2 to A = nothing to the display
4 = lites up Red led
5 to B = nothing
7 = 8 to the display
8 = 7
9 = 8
c = 9
* to D = nothing

This is the type of stuff I've been fighting. Is this somehow interfering with the board? Strange that the led's come on when I press a button.

dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 25, 2006 10:16 pm     Reply with quote

I tried playing around with it a little bit on a PicDem2-Plus board.
It doesn't work. I don't know whether that keypad driver even works.
If I have time later this weekend I'll look at it. I don't feel really
thrilled about debugging someone else's driver.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 27, 2006 4:47 pm     Reply with quote

OK. I looked at it further and I was able to make it work.
Here's what you have to do:

1. In the while() loop in main, you need to enclose the code that
comes after the if(k!=0) statement in braces. "K not equal to 0"
means that you got a keypress. Any code that checks the key's
value must be within braces that are for that "if" statement.
See the lines indicated in comments below.

Code:
while (TRUE) {
   k=kbd_getc();
   if(k!=0)
     {    // Add this brace
        // printf("%c%c%c", 17, 0, CFLine0); //position to line
      if(k=='*')
         printf("%c", '*');
      else
         printf("%c", k);
      }   // Add this closing brace
  }



2. After you program the PIC, remove the ICD2 cable. The problem
is that the ICD2 uses pins B6 and B7. But so does your keypad.
The ICD2 interferes with the operation of your code. If you unplug
it, then it will work. For this reason, most people don't use pins
B6 and B7, at least during development.


Make sure that you keep all the other changes that I posted earlier in
the thread, including the pin assignments, the keypad layout, and the
enabling of Port B pull-ups.

I tested this on a PicDem2-Plus, which has LEDs on your column pins.
I enabled them and it didn't affect anything. That's because they're on
the column pins. If the LEDs on your board are on the Row pins, then
it could prevent proper operation. Disconnect them in that case.
Guest








ICD-U40 and PIC18F452 Development Kit
PostPosted: Sun Aug 27, 2006 8:22 pm     Reply with quote

Thanks again for the help and sorry you had to debug that stuff. I pulled most of it off the net and it's supposed to work. The brackets are my fault. I tried your changes and it still doesn't work.

So, I do have a PICDEM2 and ICD2 kit. I'm beginning to think that it just doesn't play on the CCS development board and their ICD-U40. I'm going to try what you used and was wondering this.

I assume you are using the CCS compiler. Are you using MPLAB to do all this? I'm trying to figure out how to get all this integrated so I can move on.

I really appreciate your help on this and hope it hasn't been too painful.

Thanks again.

dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 27, 2006 9:17 pm     Reply with quote

It could be something about the CCS demo board. I would assume
that you can use the CCS ICD with PicDem2-Plus. Other than the
pin used for debugger serial output, the connector pinouts are the
same. If you want to use MPLAB, here are some brief instructions:

Download MPLAB vs. 7.41, and the CCS "plug-in".
1. Install MPLAB.
2. Install the CCS plug-in.
3. I assume you have PCWH. I believe it's installed in the same
directory as the command line compilers. The CCS plug-in should
take care of setting up MPLAB to refer to this directory.

4. Install the ICD2, very carefully following the instructions that
come with MPLAB.

5. Create a directory for your projects, if you don't have one already,
such as c:\Program Files\PICC\Projects

6. Use the Project Wizard in MPLAB to create a new project.
When you enter a new directory name for the project, (such as
"Keypad"), it will ask if you want to create it. Do so.

7. Only your main source file will be listed in the "Source Files" section
of the Project window. All other files must be #included in your
main source file. You are already doing this. Just a reminder.

At this point, the compiler is integrated in MPLAB and the project is
ready to compile.
Guest








ICD-U40 and PIC18F452 Development Kit
PostPosted: Sun Aug 27, 2006 10:10 pm     Reply with quote

Thanks again. Will try it. If you don't mind could you please send me the code that you have for this project? I've done what you suggested except the ICD2 board. Will do that in the next day or so. I did load in the project and got these results when I compiled.

Clean: Deleting intermediary and output files.
Clean: Deleted file "Flex_KBD.$$$".
Clean: Deleted file "Flex_KBD.ERR".
Clean Warning: File "D:\PICC\MPLAB Projects\Trailer_Circuit\Flex_KBD.o" doesn't exist.
Clean: Done.
Executing: "D:\PICC\Ccsc.exe" "Flex_KBD.c" +FH +DF +LN +T -A +M +Z +Y=9 +EA
*** Error 128 "D:\PICC\MPLAB Projects\Trailer_Circuit\Flex_KBD.c" Line 25(1,8): A #DEVICE required before this line
Halting build on first failure as requested.
BUILD FAILED: Sun Aug 27 22:03:09 2006

Little confused about the Flex_Kbd.o. Don't know where that's coming from. Thought maybe I could look at your code and start from there.

thanks again.

dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 27, 2006 10:59 pm     Reply with quote

I will post them tomorrow morning.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 28, 2006 11:58 am     Reply with quote

Here's the code that you posted, after I modified it to run on a PicDem2-
Plus board. Instead of having the output go to an LCD, I send it to the
serial port and display it in a terminal window on a PC.

I'm using the EC_IO oscillator fuse setting because PicDem2-Plus comes
with a 4 MHz "can" external oscillator.

Remember that you after you program the PIC with the ICD2, you must
remove the ICD2 cable from the board so that the program can run
properly.

Also, here's the CCS FAQ on how to install CCS into MPLAB.
I had forgotten about this. I could have saved myself some typing.
http://www.ccsinfo.com/faq.php?page=ccs_mplab6

Code:

#include <18F452.h>
#fuses EC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=============================

//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] =
{{'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}};


#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);
}

//===========================
void main()
{
char k;

kbd_init();

printf("\r\Starting ...");

while(TRUE)
  {
   k=kbd_getc();
   if(k!=0)
     {
      if(k=='*')
         printf("%c", '*');
      else
         printf("%c", k);
     }
  }

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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