|
|
View previous topic :: View next topic |
Author |
Message |
akaka
Joined: 18 Jul 2012 Posts: 14
|
Easy Pic6 keypad 4x4 interfacing |
Posted: Mon Jun 15, 2015 12:18 pm |
|
|
Hi all,
I am using a new to me development board EasyPic6 and I am trying to interface all peripheral. I hate mikroe and hidden libraries I and want to make my own libs.
So I am using ccs 5.008 version and making multi-compilation (relocatable) files.
I have done some modification in flexible_keypad driver but it's not working.
Here is the keypad schematic: http://imgur.com/rHxalRw
(series diodes there aren't in this board).
here is my code:
s_init.c
Code: | void InitSystem(void)
{
TRISA = 0x00; //set all pins as outputs
TRISB = 0x00; //set all pins as outputs
TRISC = 0x0; //set all pins as outputs
TRISD = 0xf0; //set all pins as outputs
TRISE = 0x00; //set all pins as outputs
ADCON1 = 0X0F; //set A/D con as DIO
CMCON = 0X07; //set Compare Con all as Digital
UCON=0;
//UCFG=0x0f;
SPPCON=0;
/*PORTA=0x00;
PORTB=0x00;
PORTC=0x00;
PORTD=0x00;
LATD=0x00;
*/
PORTE=0x0F;
setup_adc_ports(NO_ANALOGS);
//LCDInit();
//SSPCON2=0x00;
//SSPCON1=0x00;
//delay_ms(50);
//SetCursor(CUR_OFF);
/*printf(lcd_putc,"\fInitialize");
delay_ms(1000);
printf(lcd_putc,"\fReady"); */
//PutCh('b');
}
|
main.c
Code: |
void main()
{
char k;
InitSystem();
LATD=0xe;
PORTA=0xff;
LATB=0x00;
LATC=0xff;
LATE=0x7;
//lcd_init();
//delay_ms(1000);
//lcd_putc("akaka");
//PutStrD(1,1,"akaka");
//SetCursor(CUR_OFF);
while (TRUE)
{
k=kbd_getc();
//SetCursor(CUR_OFF);
//output_toggle(PIN_C5);
// delay_ms(500);
//LATC=0xff;
portb=k;
}
} |
keybord_driver.c
Code: |
#include "sys_def.h"
#include "s_init.h"
#include "s_lcd.h"
#include "s_dspl.h"
#include "s_keyboard4x4.h"
// Keypad layout:
char const KEYS[ 4 ][ 4 ] =
{{0x1,0x2,0x3,0xa},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
void kbd_init()
{
//set_tris_d( 0xF0) ;
//output_d( 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()
{
//portb=1;
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;
}//switch
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;
porta=1;
PORTB=last_key =KEYS[row][col];
/* Οι παράμετροι rows και colums είναι Global και τις έχει βάλει ο Σαπουνίδης Θεοδόσιος
για να εκτελεστεί η άσκηση. Ο πραγματικός Driver δεν απαιτεί αυτές τις μεταβλητές.
Οπότε αν θέλουμε να τον έχουμε στην αρχική του μορφή μπορούμε να διαγράψουμε τις 2 παρακάτω
γραμμές.*/
//colums=col; //διαγραφή
//rows=row; //διαγραφή
kbd_down = true;
}
else
{
++col;
if( col==4 )
col=0;
}
}
kbd_call_count=0;
}
return(kchar) ;
}
|
keybord_driver.h
Code: |
//=============================
//Keypad connection:
#define row0 PIN_D4
#define row1 PIN_D5
#define row2 PIN_D6
#define row3 PIN_D7
#define col0 PIN_D0
#define col1 PIN_D1
#define col2 PIN_D2
#define col3 PIN_D3
#define KBD_DEBOUNCE_FACTOR 255 // 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);
char kbd_getc();
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 15, 2015 1:21 pm |
|
|
The row pins on D4 to D7 need 10K pullup resistors. These pullups
provide a logic high level as the idle state. So when the keypad driver
reads the row pins, it sees a logic '1' if no key is pressed. If a key is
pressed, it will connect a logic '0' to that pin and override the pullup's
logic level. So pullups on the row pins are essential. |
|
|
akaka
Joined: 18 Jul 2012 Posts: 14
|
|
Posted: Mon Jun 15, 2015 2:34 pm |
|
|
PCM programmer wrote: | The row pins on D4 to D7 need 10K pullup resistors. These pullups
provide a logic high level as the idle state. So when the keypad driver
reads the row pins, it sees a logic '1' if no key is pressed. If a key is
pressed, it will connect a logic '0' to that pin and override the pullup's
logic level. So pullups on the row pins are essential. |
Hi PCM programmer thanks for your response. Yes I have connected pullup 10K to the rows from the beginning but there is no result. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 15, 2015 3:21 pm |
|
|
This is a series resistor. This is what your schematic shows:
Code: | PIC pin ------/\/\/\/-------- Keypad pin |
This is a pull-up resistor. This is what you need on each of the row pins:
Code: |
+5v
|
<
> 10K
<
|
PIC pin -------o-------- Keypad pin
|
But the main problem is the way you are calling the keypad driver, as
shown below:
Quote: |
while (TRUE)
{
k=kbd_getc();
//SetCursor(CUR_OFF);
//output_toggle(PIN_C5);
// delay_ms(500);
//LATC=0xff;
portb=k;
} |
First, get rid of all the commented lines. They block your eyes from
seeing the actual problem:
Quote: |
while (TRUE)
{
k=kbd_getc();
portb=k;
}
|
The problem is that the majority of time kbd_getc() is called, it will
return 0. You won't see it on PortB if kbd_getc() returns a non-zero
value, which indicates a key press. It will quickly be over-written by
another 0x00 value. You need to modify your code to only write to PortB
if a key was actually pressed. See the modifications shown below:
Code: |
portb = 0; // Initialize port B
while(TRUE)
{
k=kbd_getc();
if(k != 0) // Add this line
portb=k;
}
|
|
|
|
akaka
Joined: 18 Jul 2012 Posts: 14
|
|
Posted: Mon Jun 15, 2015 11:27 pm |
|
|
Good morning!
I am so sorry I have forgotten to inform you that there are dip switches with 10k resistors connected to each port and you can make them either pullup or pull down by changing the position of a jumper.
Take a look at the full schematic of easypic6
http://www.mikroe.com/pdf/easypic6/easypic6_schematic_v101.pdf
but you have right in the main loop.
Thanks again for your time.
I'll do this change and I'll keep you informed.
Regards
Chris |
|
|
|
|
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
|