// In this experiment single switch taken from the 16 switches of 4X4 matrix keypad
//4 columns are connected to the 4-7 as I/P and 4 rows are connected to the 0-3 as O/P pins of the //PORTC
// blue LED connected on the PORTD pin1, will lit whenever configured switch is pressed
#include <MKL25Z4.H>
//user define function to provide a delay in micro second
void delayUs(int n);
//function to detect the switch
char Switch_detect(void);
int main(void)
{
SIM->SCGC5 |= 0x1800; /* enable clock to Port C and D */
//initialization switch 1 from matrix keypad
// PTC0 and PTC4
PORTC->PCR[0] = 0x103; /* make PTC0 pin as GPIO and enable pullup*/
PORTC->PCR[4] = 0x103; /* make PTC4 pin as GPIO and enable pullup*/
PTC->PDDR = 0x01; /* make PTC0 as output pin and PTC4 input pin*/
//PortD pin 1 configure for blue LED
PORTD->PCR[1] = 0x100; /* make PTD1 pin as GPIO */
PTD->PDDR |= 0x02; /* make PTD1 as output pin */
while(1)
{
if (Switch_detect() != 0) /* if a key is pressed? */
PTD->PDOR &= ~0x02; /* turn on blue LED */
else
PTD->PDOR |= 0x02; /* turn off blue LED */
}
}
char Switch_detect(void)
{
int col;
PTC->PDDR |= 0x01; /* enable row1 */
PTC->PDOR &= ~0x01; /* we make sure it must clear before switch detection process*/
delayUs(2); /* wait for signal return */
col = PTC->PDIR & 0x10; /* read column1 */
PTC->PDDR = 0; /* disable row1*/
if (col == 0x10)
return 0; /* no key pressed */
else
return 1; /* a key is pressed */
}
void delayUs(int n)
{
int i; int j;
for(i = 0 ; i < n; i++) {
for(j = 0; j < 8; j++) ;
}
}
No comments:
Post a Comment