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

Keypad 4x4 code "not working"
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
slahiri
Guest







Re: Keypad 4x4 code "not working"
PostPosted: Sun Mar 16, 2003 6:13 pm     Reply with quote

:=:=if you could post code or email me your code so that I can take look at.
:=-------------------------------------------------------------
:=
:=I'll just post the code. I don't know what else to do.
:=
:=The following code is nearly the same as the CCS example
:=code, except I removed all the excess comments at the
:=top of the files. I changed one or two things, such
:=as the LCD init. But those changes are noted in the files.
:=
:=I only have a 12 key keypad, so I don't have a COL3.
:=
:=<PRE>
:=// EX_LCDKB.C
:=<BR>
:=#include <16F877.h>
:=#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
:=#use delay(clock=8000000)
:=#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7, errors)
:=<BR>
:=#include "lcd.c"
:=#include "kbd.c"
:=<BR>
:=//----------------------
:=main()
:={
:= char k;
:=<BR>
:= lcd_init();
:= kbd_init();
:=<BR>
:= lcd_putc("\fReady !\n");
:=<BR>
:= while (TRUE) {
:= k=kbd_getc();
:= if(k!=0)
:= if(k=='*')
:= lcd_putc('\f');
:= else
:= lcd_putc(k);
:= }
:=}
:=<BR>
:=//==============================================
:=<BR>
:=// LCD.C
:=<BR>
:=// The pin connection from Port D to the LCD is as follows:
:=//
:=// D0 enable
:=// D1 rs
:=// D2 rw
:=// D4 D4
:=// D5 D5
:=// D6 D6
:=// D7 D7
:=//
:=// LCD pins D0-D3 are not used and PIC pin D3 is not used.
:=<BR>
:=struct lcd_pin_map
:={
:= boolean enable;
:= boolean rs;
:= boolean rw;
:= boolean unused;
:= int data : 4;
:=} lcd;
:=<BR>
:=#byte lcd = 8 // Assign the above structure to Port D
:=<BR>
:=#define set_tris_lcd(x) set_tris_d(x)
:=<BR>
:=#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
:=<BR>
:=#define lcd_line_two 0x40 // LCD RAM address for the second line
:=<BR>
:=// These bytes need to be sent to the LCD to start it up.
:=byte CONST LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
:= :=// The following structures are used for setting
:=// the I/O port direction register.
:=<BR>
:=// For write mode all pins are outputs.
:=STRUCT lcd_pin_map const LCD_WRITE = {0,0,0,0,0};
:=<BR>
:=// For read mode data pins are inputs.
:=STRUCT lcd_pin_map const LCD_READ = {0,0,0,0,15};
:=<BR>
:=byte lcd_read_byte() {
:= byte low,high;
:= set_tris_lcd(LCD_READ);
:= lcd.rw = 1;
:= delay_cycles(1);
:= lcd.enable = 1;
:= delay_cycles(1);
:= high = lcd.data;
:= lcd.enable = 0;
:= delay_cycles(1);
:= lcd.enable = 1;
:= delay_us(1);
:= low = lcd.data;
:= lcd.enable = 0;
:= set_tris_lcd(LCD_WRITE);
:= return( (high<<4) | low);
:=}
:= :=void lcd_send_nibble( byte n ) {
:= lcd.data = n;
:= delay_cycles(1);
:= lcd.enable = 1;
:= delay_us(2);
:= lcd.enable = 0;
:=}
:=<BR>
:=void lcd_send_byte( byte address, byte n ) {
:= lcd.rs = 0;
:= while ( bit_test(lcd_read_byte(),7) ) ;
:= lcd.rs = address;
:= delay_cycles(1);
:= lcd.rw = 0;
:= delay_cycles(1);
:= lcd.enable = 0;
:= lcd_send_nibble(n >> 4);
:= lcd_send_nibble(n & 0xf);
:=}
:=<BR>
:=void lcd_init() {
:= byte i;
:= set_tris_lcd(LCD_WRITE);
:= lcd.rs = 0;
:= lcd.rw = 0;
:= lcd.enable = 0;
:= delay_ms(15);
:= for(i=1;i<=3;++i) {
:= lcd_send_nibble(3);
:= delay_ms(5);
:= }
:= := lcd_send_nibble(2);
:=<BR>
:=// Note:
:=// My LCD did not work if the last byte in the init
:=// table was sent to it (last byte = 0x06). So I changed
:=// the count in the for() statement, so it would only
:=// send the first 3 bytes in the init table.
:=<BR>
:= for(i=0;i<=2;++i)
:= lcd_send_byte(0,LCD_INIT_STRING[i]);
:= :=}
:=<BR>
:=void lcd_gotoxy( byte x, byte y) {
:= byte address;
:=<BR>
:= if(y!=1)
:= address=lcd_line_two;
:= else
:= address=0;
:= address+=x-1;
:= lcd_send_byte(0,0x80|address);
:=}
:=<BR>
:=void lcd_putc( char c) {
:= switch (c) {
:= case '\f' : lcd_send_byte(0,1);
:= delay_ms(2);
:= break;
:= case '\n' : lcd_gotoxy(1,2); break;
:= case '\b' : lcd_send_byte(0,0x10); break;
:= default : lcd_send_byte(1,c); break;
:= }
:=}
:=<BR>
:=char lcd_getc( byte x, byte y) {
:= char value;
:=<BR>
:= lcd_gotoxy(x,y);
:= lcd.rs=1;
:= value = lcd_read_byte();
:= lcd.rs=0;
:= return(value);
:=}
:=<BR>
:=//=====================================================
:=// KBD.C
:=<BR>
:=#byte kbd = 6 // on to port B (at address 6)
:=<BR>
:=#define set_tris_kbd(x) set_tris_b(x)
:=<BR>
:=/*
:=#define COL0 (1 << 0)
:=#define COL1 (1 << 1)
:=#define COL2 (1 << 2)
:= :=#define ROW0 (1 << 4)
:=#define ROW1 (1 << 5)
:=#define ROW2 (1 << 6)
:=#define ROW3 (1 << 7)
:=*/
:= :=// The following bitmasks are the same as the above,
:=// but written in a different format.
:=#define COL0 0b00000001
:=#define COL1 0b00000010
:=#define COL2 0b00000100
:=// Note: My keypad is only 12 keys, so it doesn't
:=// have a COL3 pin.
:=#define ROW0 0b00010000
:=#define ROW1 0b00100000
:=#define ROW2 0b01000000
:=#define ROW3 0b10000000
:=<BR>
:=<BR>
:=#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
:=#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2)
:=<BR>
:=// Keypad layout:
:=char const KEYS[4][3] = {{'1','2','3'},
:= {'4','5','6'},
:= {'7','8','9'},
:= {'*','0','#'}};
:=<BR>
:=// Set this number to apx n/333 where
:=// n is the number of times you expect
:=// to call kbd_getc each second
:=#define KBD_DEBOUNCE_FACTOR 33
:=<BR>
:=void kbd_init()
:={
:=port_b_pullups(TRUE);
:=}
:=<BR>
:=char kbd_getc( ) {
:= static byte kbd_call_count;
:= static short int kbd_down;
:= static char last_key;
:= static byte col;
:=<BR>
:= byte kchar;
:= byte row;
:=
:= kchar='\0';
:= if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
:= switch (col) {
:= case 0 : set_tris_kbd(ALL_PINS&~COL0);
:= kbd=~COL0&ALL_PINS;
:= break;
:= case 1 : set_tris_kbd(ALL_PINS&~COL1);
:= kbd=~COL1&ALL_PINS;
:= break;
:= case 2 : set_tris_kbd(ALL_PINS&~COL2);
:= kbd=~COL2&ALL_PINS;
:= break;
:= }
:=<BR>
:= if(kbd_down) {
:= if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
:= kbd_down=false;
:= kchar=last_key;
:= last_key='\0';
:= }
:= } else {
:= if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
:= if((kbd & ROW0)==0)
:= row=0;
:= else if((kbd & ROW1)==0)
:= row=1;
:= else if((kbd & ROW2)==0)
:= row=2;
:= else if((kbd & ROW3)==0)
:= row=3;
:= last_key =KEYS[row][col];
:= kbd_down = true;
:= } else {
:= ++col;
:= if(col==3)
:= col=0;
:= }
:= }
:= kbd_call_count=0;
:= }
:= set_tris_kbd(ALL_PINS);
:= return(kchar);
:=}
:=
:=</PRE>



I didn't ask for 12 key keypad I know that works. Question I have how do you know COl1 will have this "#define COL1 0b00000010" is it just the assumption or there is way of doing this.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12751
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Keypad 4x4 code "not working"
PostPosted: Sun Mar 16, 2003 8:17 pm     Reply with quote

<font face="Courier New" size=-1>:=
:=I didn't ask for 12 key keypad I know that works. Question I have how do you know COl1 will have this "#define COL1 0b00000010" is it just the assumption or there is way of doing this.
----------------------------------------------------------

You know that 12 key works ?! In other words, you have
it working ? You should have said so. I assumed that *no*
keypad worked at all, on your system. That's why I kept
asking you questions about ICD, NOLVP, etc.

So your real problem is just how to modify a working 12-key
project, to a 16-key project ?

If you really have a 12-key system working, then you should
be able to take your 16-key keypad, and just use 3 of the
columns on it, and make it work as a 12-key keypad, initially.
Then, once that's working, then connect the last column wire
to the PIC, and make some small changes to the code, and debug
it. It shouldn't be that hard.

------

Re: your question on how do I know that COL1 = 0b00000010 ?

My keypad is a Grayhill p/n 96AB2-102-F. See this link
for the data sheet: <a href="http://embrace.grayhill.com/embrace/IMAGES/PDF/c-06-09.pdf" TARGET="_blank"> <a href="http://embrace.grayhill.com/embrace/IMAGES/PDF/c-06-09.pdf" TARGET="_blank">http://embrace.grayhill.com/embrace/IMAGES/PDF/c-06-09.pdf</a></a>

On page 1 of the data sheet, it shows the numbering of the
terminal pins: 1 to 7.

On page 3, there is a chart called "12 button keypads".
On the bottom of the chart, it lists the pin numbers.
Look at pin number 6. It show that buttons at locations
2, 5, 8, and 11 are connected to that pin. That's the
keys for "2", "5", "8" and "0", which is the 2nd column.
So in other words, that chart shows me that the 2nd column
of keys is connected to pin 6 on the keypad.

In the C source code, the 2nd column is called "COL1".
Then, we decide to assign COL1 to bit 1 of port B. (Because
it's easy. COL0 = Bit 0, COL1 = Bit 1, COL2 = Bit 2, etc.).
So, the bitmask for bit 1 of Port B is 0b00000010.
The following little chart shows that if you want to
create a bitmask for bit 1 of port B, then you set the
bit that's in the position for Bit 1:

<PRE>
bit number: 76543210
COL1 bitmask: 0b00000010
</PRE>
----

Getting back to the data sheet:
In the same way, the 1st column of keys is connected to pin 5,
and the 3rd column is connected to pin 7.

To see what pin is used for each row of keys, notice that
keys 1, 2, and 3 are assigned to pin 1. Keys 1, 2, and 3
are on Row 1. So, Row 1 = pin 1. In the same way, you can
see that Row 2 (keys 4,5,6) = pin 2, etc.









</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 12753
slahiri
Guest







Re: Keypad 4x4 code "not working"
PostPosted: Mon Mar 17, 2003 8:12 am     Reply with quote

I am petty possitive it is the code and the pin set up, if I can get it work I will post on the board. But before that could you please tell me how did you define your col and rows.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12764
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Keypad 4x4 code "not working"
PostPosted: Mon Mar 17, 2003 12:24 pm     Reply with quote

:=I am petty possitive it is the code and the pin set up, if I can get it work I will post on the board. But before that could you please tell me how did you define your col and rows.
---------------------------------------------------------

I'm not sure whether you posted this to me, or to Mehmet.

I think you need to get the data sheet for your keypad,
so that you know for certain, what pins are COL0,1,2,3
and ROW0,1,2,3. You should contact Velleman and ask
them for the data sheet, or buy a new keypad from a company
that will give you a data sheet.

I think I've done as much as I can, in terms of helping
you on this problem. I don't think there is anything
more I can do.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12772
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 Previous  1, 2
Page 2 of 2

 
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