|
|
View previous topic :: View next topic |
Author |
Message |
nguyenchituyen2801
Joined: 23 Nov 2007 Posts: 2
|
Help me for Scankey 3*4 |
Posted: Wed Jul 22, 2009 3:55 am |
|
|
I have problem about scankey.
Sometimes I don't receiving key right, I adjust time delay I will result orther, why...?
I use "printf("%x",scan_code);"" for see var scan_code, I see it right from 1 to 9, but F1, F2, F3, up, down, F5, Clear is wrong.
My keypad:
Code: |
________________________________
|7(F1) |8(up) |9(F2) |
|4(left) |5(F3) |6(right) |
|1(F4) |2(down) |3(F5) |
|shift |0 |enter(clear)|
|________________________________|
|
Help me soon.
thank, best regards
Code: | #include <C:\Program Files\PICC\Devices\16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#FUSES PROTECT //Code protected from reads
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#byte INTCON = 0x0B
#bit Port_B_Enable = 0x0B.0
int8 scan_code=0;
int8 luu_scan_code=0;
int8 key_code=0;
int8 my_add=0;
//###############################################
int8 Scan_KeyPress()
{
scan_code=0xFF;
//look row
SET_TRIS_B( 0b00000111 );
Port_B_Enable = 0;
delay_cycles(25);
if(!input(PIN_B1)) bit_clear(scan_code, 0);
if(!input(PIN_B2)) bit_clear(scan_code, 1);
if(!input(PIN_B3)) bit_clear(scan_code, 2);
delay_cycles(25);
//look col
SET_TRIS_B( 0b1111000 );
Port_B_Enable = 1;
delay_cycles(25);
if(!input(PIN_B3)) bit_clear(scan_code, 3);
if(!input(PIN_B4)) bit_clear(scan_code, 4);
if(!input(PIN_B5)) bit_clear(scan_code, 5);
if(!input(PIN_B6)) bit_clear(scan_code, 6);
switch(scan_code)
{
case 0xbe:return 0;
case 0xf3:return 1;
case 0xf2:return 2;
case 0xf1:return 3;
case 0xef:return 4;
case 0xee:return 5;
case 0xed:return 6;
case 0xdf:return 7;
case 0xde:return 8;
case 0xdd:return 9;
}//switch
}//VOID
void main ( )
{
int8 Key_press;
Port_B_Pullups(True);
SET_TRIS_B( 0b11100000 );
INTCON=0X08;
while( true )
{
luu_scan_code=scan_code;
Key_press =Scan_KeyPress();
if(luu_scan_code!=scan_code && scan_code!=0xFF)
{
//printf("%x",scan_code);
//putc(scan_code);
//putc(key_code);
printf("%x",Key_press);
delay_ms(400);
key_code=0;
}//if
}//while
} //main |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 22, 2009 12:05 pm |
|
|
Post a link to the data sheet for your keypad. |
|
|
nguyenchituyen2801
Joined: 23 Nov 2007 Posts: 2
|
keypad |
Posted: Wed Jul 22, 2009 6:50 pm |
|
|
My keypad:
http://www.mediafire.com/?sharekey=fe2303cfc7208b8a0dec85adfe0a530ae04e75f6e8ebb871
I am successful with code RF:
Code: |
#include <C:\Program Files\PICC\Devices\16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#FUSES PROTECT //Code protected from reads
#use delay(clock=20000000)
#byte portb = 0x06
#byte portc = 0x07
#byte INTCON = 0x0B
#define out_bit PIN_A0 //chan2
#define DK PIN_A1 //chan3
int16 dem_de_sleep=0;
int8 scan_code=0;
int8 luu_scan_code=0;
int8 key_code=0;
int8 my_add=0;
//int8 temp_portC=0;
//##########################################
void quet_phim()
{
scan_code=0xFF;
//tim hang
SET_TRIS_B( 0b00011111 );
delay_cycles(2);
if(!input(PIN_B0)) bit_clear(scan_code, 0);
if(!input(PIN_B1)) bit_clear(scan_code, 1);
if(!input(PIN_B2)) bit_clear(scan_code, 2);
if(!input(PIN_B4)) bit_clear(scan_code, 4);
delay_cycles(2);
SET_TRIS_B( 0b11100000 );
delay_cycles(2);
if(!input(PIN_B5)) bit_clear(scan_code, 5);
if(!input(PIN_B6)) bit_clear(scan_code, 6);
if(!input(PIN_B7)) bit_clear(scan_code, 7);
bit_clear(INTCON, 0);
bit_clear(INTCON, 0);
bit_clear(INTCON, 0);
switch(scan_code)
{
case 190:key_code=0;break;
case 125:key_code=1;break;
case 189:key_code=2;break;
case 221:key_code=3;break;
case 123:key_code=4;break;
case 187:key_code=5;break;
case 219:key_code=6;break;
case 111:key_code=7;break;
case 175:key_code=8;break;
case 207:key_code=9;break;
case 94 :key_code=10;break;
case 222:key_code=11;break;
case 60 :key_code=12;break;
case 122:key_code=13;break;
case 90 :key_code=14;break;
case 46 :key_code=15;break;
case 126:key_code=16;break;
case 110:key_code=17;break;
case 78: key_code=18;break;
case 58: key_code=19;break;
case 124:key_code=20;break;
case 92: key_code=21;break;
case 108:key_code=22;break;
case 76: key_code=23;break;
default:break;
}
}
//##########################################
void xuat_xung_dong_bo()
{
Output_low (out_bit);Delay_us (1500 );
output_high(out_bit);Delay_us (1500 );
}
//##########################################
void xuat_mot_byte(int8 data_out)
{
int8 dem;
xuat_xung_dong_bo();
for(dem=0;dem<8;dem++)
{
if( bit_test(data_out,dem))
{
Output_low (out_bit);Delay_us (1000 );
output_high(out_bit);Delay_us (1000 );
}
else
{
Output_low (out_bit);Delay_us (500 );
output_high(out_bit);Delay_us (500 );
}
}
}
//##########################################
void main ( )
{
portb=0;
// portC=0xFF;
Port_B_Pullups(True);
SET_TRIS_B( 0b11100000 );
set_tris_C ( 0b11111111 ) ;
// set_tris_C ( 0 ) ;
/* Output_float(PIN_C0);
Output_float(PIN_C1);
Output_float(PIN_C2);
Output_float(PIN_C3);
Output_float(PIN_C4);
Output_float(PIN_C5);
Output_float(PIN_C6);
Output_float(PIN_C7);*/
INTCON=0X08;
while( true )
{
/* if(input(PIN_C0))my_add=1;
else my_add=2;
*/
my_add=0;
if (!input(pin_C0)) BIT_SET(my_add,0);
if (!input(pin_C1)) BIT_SET(my_add,1);
if (!input(pin_C2)) BIT_SET(my_add,2);
if (!input(pin_C3)) BIT_SET(my_add,3);
if (!input(pin_C4)) BIT_SET(my_add,4);
my_add++;
luu_scan_code=scan_code;
quet_phim();
xuat_mot_byte(0xFF);
if(luu_scan_code!=scan_code && scan_code!=0xFF)
{
dem_de_sleep=0;
output_high(DK);
/* key_code =0;
if(input(PIN_C0)) bit_set(key_code,0);
if(input(PIN_C1)) bit_set(key_code,1);
if(input(PIN_C2)) bit_set(key_code,2);
if(input(PIN_C3)) bit_set(key_code,3);
if(input(PIN_C4)) bit_set(key_code,4);
if(input(PIN_C5)) bit_set(key_code,5);
xuat_mot_byte(my_add);
xuat_mot_byte(my_add);
xuat_mot_byte(my_add);
xuat_mot_byte(my_add);*/
//xuat_mot_byte(0xFF);
//xuat_mot_byte(0xFF);
xuat_mot_byte(0xFF);
xuat_mot_byte(0xFF);
key_code++;
xuat_mot_byte(my_add);//0
xuat_mot_byte(key_code);//1
xuat_mot_byte(255-key_code);//2
xuat_mot_byte(key_code);//3
xuat_mot_byte(255-key_code);//4
xuat_mot_byte(key_code);//5
xuat_mot_byte(255-key_code);//6
xuat_mot_byte(key_code);//7
/*xuat_mot_byte(255-key_code);//8
xuat_mot_byte(key_code);//9
xuat_mot_byte(255-key_code);//10*/
}
dem_de_sleep++;
//output_toggle(out_bit);
//delay_ms(10);
if(dem_de_sleep>=150)
{
output_low(DK);
sleep();
bit_clear(INTCON, 0);
bit_clear(INTCON, 0);
bit_clear(INTCON, 0);
dem_de_sleep=0;
}//if
}//while
} //main |
but I am not successful with code RS232, help me! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 22, 2009 7:15 pm |
|
|
Quote: |
I have problem about scankey. Sometimes I don't receiving key right. |
Your keypad is a normal 4x3 keypad with 3 column pins and 4 row pins.
Try using the Flex keypad driver in the Code Library forum:
http://www.ccsinfo.com/forum/viewtopic.php?t=26333
Make sure you put pull-up resistors on the Row pins. You can turn on
the Port B pull-ups to do this. |
|
|
|
|
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
|