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

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

}
Guest








ICD-U40 and PIC18F452 Development Kit
PostPosted: Mon Aug 28, 2006 9:49 pm     Reply with quote

thanks for all the info and help. I will give it all a try and let you know how it comes out. I suspect that most of the problem has been with the CCS development board as it's not fully playing with the B port the way I want, or maybe I'm not doing what it thinks I should.

dave.
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

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

Finally got it working via the ICD2 and Picdem 2 plus. Thanks for the help. Now I can start having some fun. Do you know if anybody has hooked the CCS ICD-U40 to the Picdem 2 plus board and had them working together. That would give another option for development and debugging.

Thanks.

dave
kkkelis
Guest







PostPosted: Wed Dec 13, 2006 4:03 pm     Reply with quote

Hi
I am having trouble using this keypad driver with port C. What i did i just renamed the pin declarations from port B to port C. However when i ran the program , the LCD fills up with characters and numbers without pushing a button. Can someone give me a tip?

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 5:39 pm     Reply with quote

Quote:
What i did i just renamed the pin declarations from port B to port C.

You must have pull-up resistors on the Row pins. The sample code
enables the built-in Port B pull-ups inside the PIC. Since you moved
the keypad to Port C, you must add external pull-up resistors to the row
pins. You can use 4.7K or 10K resistors for this. Four resistors are
needed.
kkelis



Joined: 13 Dec 2006
Posts: 5

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 12:03 pm     Reply with quote

Thanks for the tip-Fortunately i figured that out my self.
I am trying to make an 8x8 scan driver.What i tried to do is to modify the code posted earlier.
This is what i did:


//Keypad connection:
#define row0 PIN_B0
#define row1 PIN_B1
#define row2 PIN_B2
#define row3 PIN_B3
#define row4 PIN_B4
#define row5 PIN_B5
#define row6 PIN_B6
#define row7 PIN_B7

#define col0 PIN_C0
#define col1 PIN_C1
#define col2 PIN_C2
#define col3 PIN_C3
#define col4 PIN_C4
#define col5 PIN_C5
#define col6 PIN_C6
#define col7 PIN_C7


// Keypad layout:
char const KEYS[8][8] =
{{'1','2','3','4','5','6','7','8'},
{'9','10','11','12','13','14','15','16'},
{'17','18','19','20','21','22','23','24'},
{'25','26','27','28','29','30','31','32'},
{'33','34','35','36','37','38','39','40'},
{'41','42','43','44','45','46','47','48'},
{'49','50','51','52','53','54','55','56'},
{'57','58','59','60','61','62','63','64'}};


#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) & input (row4)
& input (row5) & input (row6) & input (row7))
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);
output_high(col4);
output_high(col5);
output_high(col6);
output_high(col7);
break;

case 1:
output_high(col0);
output_low(col1);
output_high(col2);
output_high(col3);
output_high(col4);
output_high(col5);
output_high(col6);
output_high(col7);

break;

case 2:
output_high(col0);
output_high(col1);
output_low(col2);
output_high(col3);
output_high(col4);
output_high(col5);
output_high(col6);
output_high(col7);
break;

case 3:
output_high(col0);
output_high(col1);
output_high(col2);
output_low(col3);
output_high(col4);
output_high(col5);
output_high(col6);
output_high(col7);
break;

case 4:
output_high(col0);
output_high(col1);
output_high(col2);
output_high(col3);
output_low(col4);
output_high(col5);
output_high(col6);
output_high(col7);
break;

case 5:
output_high(col0);
output_high(col1);
output_high(col2);
output_high(col3);
output_high(col4);
output_low(col5);
output_high(col6);
output_high(col7);
break;

case 6:
output_high(col0);
output_high(col1);
output_high(col2);
output_high(col3);
output_high(col4);
output_high(col5);
output_low(col6);
output_high(col7);
break;

case 7:
output_high(col0);
output_high(col1);
output_high(col2);
output_high(col3);
output_high(col4);
output_high(col5);
output_high(col6);
output_low(col7);
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;
else if(!input (row4))
row=4;
else if(!input (row5))
row=5;
else if(!input (row6))
row=6;
else if(!input (row7))
row=7;

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


Unfortunately I am getting errors related with the Character constant constructed incorrectly??? I dont understand why this is happening
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 12:09 pm     Reply with quote

You mean you're getting error messages during compilation ?
Post your compiler version and the error message, and tell us
the line of code that's causing the error.
kkelis



Joined: 13 Dec 2006
Posts: 5

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 1:38 pm     Reply with quote

Yes i am getting error messages during compilation. I am Using ccs PCWH vesion 3.207 and MPLAB IDE 7.41

The lines that are causing the errors are those with // at the end:
char const KEYS[8][8] =
{{'1','2','3','4','5','6','7','8'},
{'9','10','11','12','13','14','15','16'}, // Line 26
{'17','18','19','20','21','22','23','24'}, // Line 27
{'25','26','27','28','29','30','31','32'}, // Line 28
{'33','34','35','36','37','38','39','40'}, // Line 29
{'41','42','43','44','45','46','47','48'}, // Line 30
{'49','50','51','52','53','54','55','56'}, // Line 31
{'57','58','59','60','61','62','63','64'}};


Here are some error messeges i get:
*** Error 5 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(7,9): Character constant constructed incorrectly
*** Error 79 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(23,25): Expect }
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(28,30): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(30,33): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(33,35): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(35,38): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(38,40): Expecting a declaration
*** Error 5 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(40,42): Character constant constructed incorrectly
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 26(42,43): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(2,3): Expecting a declaration
*** Error 5 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(3,5): Character constant constructed incorrectly
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(5,6): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(6,9): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(9,11): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(11,14): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(14,16): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(16,19): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(19,21): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(21,24): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(24,26): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(26,29): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(29,31): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(31,34): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(34,36): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(36,39): Expecting a declaration
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(39,41): Expecting a declaration
*** Error 5 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(41,43): Character constant constructed incorrectly
*** Error 43 "F:\Documents and Settings\Alexandros\Desktop\c++ PIC\18f452\keyboard and lcd\8x8kbd.c" Line 27(43,44): Expecting a declaration
Ttelmah
Guest







PostPosted: Thu Dec 14, 2006 3:02 pm     Reply with quote

I.m not suprised it is complaining. Your constants _are_ constructed incorrectly...

'3', is code to replace _one_ character, with it's ASCII code.
0x33, is the same code declared as a hexadecimal number.
51, is the same code declared as a decimal number.

'51', means nothing....

Best Wishes
edp2009



Joined: 22 Dec 2008
Posts: 4

View user's profile Send private message

PostPosted: Mon Dec 22, 2008 7:34 pm     Reply with quote

Hello,

I have tried to compile the code posted by PCM Programmer to no success.

I am trying to make keypad work with LCD. I have successfully made the flex_LCD driver work for me.

Here is KBD.c code (I use PORTE on PIC24H):

Code:

//Keypad connection:
#define row0 PIN_E4
#define row1 PIN_E5
#define row2 PIN_E6
#define row3 PIN_E7
#define col0 PIN_E0
#define col1 PIN_E1
#define col2 PIN_E2
#define col3 PIN_E3

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


Here is modified EX_KBD.c:

Code:


#include "24HJ256GP210.h"
#FUSES XT, NOWDT, NOPROTECT, NOPUT
#use delay(clock = 7370000)


#include "flex_lcd.c"
#include "KBD.c"


void main() {
   char k;

   lcd_init();
   kbd_init();

   while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='*')
          lcd_putc('\f');
        else
          lcd_putc(k);
   }
}



When I try to compile I get following error messages:

Code:

Error 12 "C:\School 2008-2009\WINTER\PROJECT\Source Code\KEYPAD\KBD.c" Line 39(1,15): Undefined identifier   port_b_pullups
*** Error 28 "C:\School 2008-2009\WINTER\PROJECT\Source Code\KEYPAD\KBD.c" Line 42(11,14): Expecting an identifier
*** Error 43 "C:\School 2008-2009\WINTER\PROJECT\Source Code\KEYPAD\KBD.c" Line 42(20,21): Expecting a declaration
*** Error 81 "C:\School 2008-2009\WINTER\PROJECT\Source Code\KEYPAD\KBD.c" Line 43(1,2): Expecting a basic type


This is just a sample. Total is 100 errors mostly about declaration.

Any help is appreciated. Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 22, 2008 8:44 pm     Reply with quote

That code is written for 18F-series PICs and below.

You need to download the PCD manual and look at the functions that
CCS uses for the 24-series PICs. This is the function they use for pullups:
Quote:
set_Pullups()


See Ttelmah's comments in this thread for tips on how to use it:
http://www.ccsinfo.com/forum/viewtopic.php?t=34162
edp2009



Joined: 22 Dec 2008
Posts: 4

View user's profile Send private message

PostPosted: Mon Dec 22, 2008 10:41 pm     Reply with quote

PCM programmer wrote:
That code is written for 18F-series PICs and below.

You need to download the PCD manual and look at the functions that
CCS uses for the 24-series PICs. This is the function they use for pullups:
Quote:
set_Pullups()


See Ttelmah's comments in this thread for tips on how to use it:
http://www.ccsinfo.com/forum/viewtopic.php?t=34162


Thank you.
edp2009



Joined: 22 Dec 2008
Posts: 4

View user's profile Send private message

PostPosted: Tue Dec 23, 2008 12:20 am     Reply with quote

PCM programmer wrote:


I got the code to compile, but I get very weird results.

I am using portE, in the following code, do I need any pullups?
Code:
void kbd_init()
{
}


In terms of hardware, I have read that I need to pull up the rows.
Is this an adequate way of doing this:

(columns pulled down to ground, rows up to 3.3V pin)





Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 23, 2008 12:27 am     Reply with quote

Quote:
In terms of hardware, I have read that I need to pull up the rows.

Those are pull-down resistors. Pull-up resistors connect to the Vdd
voltage.

Also the resistors are on the wrong pins. They should be on the row pins
(like you said in your post). Also, 1K will work but there is no need for
such a low value. 10K would be better.
edp2009



Joined: 22 Dec 2008
Posts: 4

View user's profile Send private message

PostPosted: Tue Dec 23, 2008 12:40 am     Reply with quote

PCM programmer wrote:
Quote:
In terms of hardware, I have read that I need to pull up the rows.

Those are pull-down resistors. Pull-up resistors connect to the Vdd
voltage.

Also the resistors are on the wrong pins. They should be on the row pins
(like you said in your post). Also, 1K will work but there is no need for
such a low value. 10K would be better.


I changed to 10K and pullups on rows.

Works flawlessly.

Thanks so much for your help! Smile
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