ARM LPC2378 4×4 matrix keypad C Program

LPC2378_4X4 MATRIX KEYPAD INTERFACE CIRCUIT

/*****************************************************************************
//Key Interface To using IAR Cross Compiler
******************************************************************************/
#include
#include “irq.h”
#include “config.h”

unsigned int k;
unsigned int Read_Key;
unsigned char scan [] = { 0×00000E00,0×00000D00,0×00000B00,0×00000700};
unsigned int i;

void delay()
{
unsigned int i,j;
for(i=0;i<0xff;i++)
for(j=0;j<0xff;j++);
}

void send_serial_data(unsigned char serial)
{

while((U0LSR & 0×20)==0);
U0THR = serial;

}

/*****************************************************************************
** Main Function main()
******************************************************************************/
int main (void)
{
unsigned int Fdiv;
TargetResetInit();

PINSEL0 = 0×00000050; /* RxD0 and TxD0 */

U0LCR = 0×83; /* 8 bits, no Parity, 1 Stop bit */
Fdiv = ( Fpclk / 16 ) / 19200 ; /*baud rate */
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0×03; /* DLAB = 0 */
U0FCR = 0×07; /* Enable and reset TX and RX FIFO. */

FIO4DIR = 0X00000FFF;
FIO3DIR = 0X008000FF;

while(1)
{

//First Row
FIO4SET = 0X00000e00;
Read_Key = FIO4PIN;
Read_Key = (Read_Key & 0xf000) >> 12 ;

if((Read_Key==0×07))
{
send_serial_data(’0′);
FIO3PIN = 0X00000000;
}
if((Read_Key==0×0b))
{
send_serial_data(’1′);
FIO3PIN = 0X00000001;
}
if( (Read_Key==0×0d))
{
send_serial_data(’2′);
FIO3PIN = 0X00000002;
}
if((Read_Key==0×0e))
{
send_serial_data(’3′);
FIO3PIN = 0X00000003;
}

FIO4CLR = 0X00000e00;
delay();

//Second Row

FIO4SET = 0X00000d00;
Read_Key = FIO4PIN;
Read_Key = (Read_Key & 0xf000) >> 12 ;

if((Read_Key==0×07))
{
send_serial_data(’4′);
FIO3PIN = 0X00000004;
}
if((Read_Key==0×0b))
{
send_serial_data(’5′);
FIO3PIN = 0X00000005;
}
if( (Read_Key==0×0d))
{
send_serial_data(’6′);
FIO3PIN = 0X00000006;
}
if( (Read_Key==0×0e))
{
send_serial_data(’7′);
FIO3PIN = 0X00000007;
}
FIO4CLR = 0X00000d00;
delay();

//Third Row

FIO4SET = 0X00000b00;
Read_Key = FIO4PIN;
Read_Key = (Read_Key & 0xf000) >> 12 ;

if((Read_Key==0×07))
{
send_serial_data(’8′);
FIO3PIN = 0X00000008;
}
if((Read_Key==0×0b))
{
send_serial_data(’9′);
FIO3PIN = 0X00000009;
}
if( (Read_Key==0×0d))
{
send_serial_data(’a');
FIO3PIN = 0X0000000a;
}
if( (Read_Key==0×0e))
{
send_serial_data(’b');
FIO3PIN = 0X0000000b;
}
FIO4CLR = 0X00000b00;
delay();

//Fourth Row
FIO4SET = 0X00000700;
Read_Key = FIO4PIN;
Read_Key = (Read_Key & 0xf000) >> 12 ;

if((Read_Key==0×07))
{
send_serial_data(’c');
FIO3PIN = 0X0000000c;
}
if((Read_Key==0×0b))
{
send_serial_data(’d');
FIO3PIN = 0X0000000d;
}
if((Read_Key==0×0d))
{
send_serial_data(’e');
FIO3PIN = 0X0000000e;
}
if( (Read_Key==0×0e))
{
send_serial_data(’f');
FIO3PIN = 0X0000000f;
}
FIO4CLR = 0X00000700;
delay();

// send_serial_data(’c');
}
return 0;
}

/*****************************************************************************
//Config.h
******************************************************************************/

#if USE_USB /* 1 is USB, 0 is non-USB related */
/* Fcck = 57.6Mhz, Fosc = 288Mhz, and USB 48Mhz */
#define PLL_MValue 11
#define PLL_NValue 0
#define CCLKDivValue 4
#define USBCLKDivValue 5

/* System configuration: Fosc, Fcclk, Fcco, Fpclk must be defined */
/* PLL input Crystal frequence range 4KHz~20MHz. */
#define Fosc 12000000
/* System frequence,should be less than 80MHz. */
#define Fcclk 57600000
#define Fcco 288000000
#else

/* Fcck = 50Mhz, Fosc = 300Mhz, and USB 48Mhz */
#define PLL_MValue 24
#define PLL_NValue 1
#define CCLKDivValue 5
#define USBCLKDivValue 6

/* System configuration: Fosc, Fcclk, Fcco, Fpclk must be defined */
/* PLL input Crystal frequence range 4KHz~20MHz. */
#define Fosc 12000000
/* System frequence,should be less than 80MHz. */
#define Fcclk 50000000
#define Fcco 300000000

#endif

/* APB clock frequence , must be 1/2/4 multiples of ( Fcclk/4 ). */
/* If USB is enabled, the minimum APB must be greater than 16Mhz */
#if USE_USB
#define Fpclk (Fcclk / 2)
#else
#define Fpclk (Fcclk / 4)
#endif

void init_VIC(void)
{
long i = 0;
long *vect_addr, *vect_cntl;

/* initialize VIC*/
VICIntEnClear = 0xffffffff;
VICAddress = 0;
VICIntSelect = 0;

/* set all the vector and vector control register to 0 */
for ( i = 0; i < VIC_SIZE; i++ )
{
vect_addr = (long *)(VIC_BASE_ADDR + VECT_ADDR_INDEX + i*4);
vect_cntl = (long *)(VIC_BASE_ADDR + VECT_CNTL_INDEX + i*4);
*vect_addr = 0×0;
*vect_cntl = 0xF;
}
return;
}
void GPIOResetInit( void )
{
/* Reset all GPIO pins to default: primary function */
PINSEL0 = 0×00000000;
PINSEL1 = 0×00000000;
PINSEL2 = 0×00000000;
PINSEL3 = 0×00000000;
PINSEL4 = 0×00000000;
PINSEL5 = 0×00000000;
PINSEL6 = 0×00000000;
PINSEL7 = 0×00000000;
PINSEL8 = 0×00000000;
PINSEL9 = 0×00000000;
PINSEL10 = 0×00000000;

IO0DIR = 0×00000000;
IO1DIR = 0×00000000;

FIO0DIR = 0×00000000;
FIO1DIR = 0×00000000;
FIO2DIR = 0×00000000;
FIO3DIR = 0×00000000;
FIO4DIR = 0×00000000;

FIO0MASK = 0×00000000;
FIO1MASK = 0×00000000;
FIO2MASK = 0×00000000;
FIO3MASK = 0×00000000;
FIO4MASK = 0×00000000;

return;
}

void ConfigurePLL ( void )
{
long MValue, NValue;

if ( PLLSTAT & (1 << 25) )
{
PLLCON = 1; /* Enable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0×55;
}

PLLCON = 0; /* Disable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0×55;

SCS |= 0×20; /* Enable main OSC */
while( !(SCS & 0×40) ); /* Wait until main OSC is usable */

CLKSRCSEL = 0×1; /* select main OSC, 12MHz, as the PLL clock source */

PLLCFG = PLL_MValue | (PLL_NValue << 16);
PLLFEED = 0xaa;
PLLFEED = 0×55;

PLLCON = 1; /* Enable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0×55;

CCLKCFG = CCLKDivValue; /* Set clock divider */
#if USE_USB
USBCLKCFG = USBCLKDivValue; /* usbclk = 288 MHz/6 = 48 MHz */
#endif

while ( ((PLLSTAT & (1 << 26)) == 0) ); /* Check lock bit status */

MValue = PLLSTAT & 0×00007FFF;
NValue = (PLLSTAT & 0×00FF0000) >> 16;
while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );

PLLCON = 3; /* enable and connect */
PLLFEED = 0xaa;
PLLFEED = 0×55;
while ( ((PLLSTAT & (1 << 25)) == 0) ); /* Check connect bit status */
return;
}
void TargetResetInit(void)
{
/* This if-else statement detects if interrupt vectors located by the linker
command file are at memory location 0 or not. */
#pragma segment = “INTVEC”
if (( void * )0×00000000UL == __segment_begin( “INTVEC” ))
{
MEMMAP = 1; // normal flash mode
}
else
{
MEMMAP = 2 ; // user ram mode – Map lowest 64 bytes of the address space to
// bottom of internal RAM, moving exception vectors into place
}

#if USE_USB
PCONP |= 0×80000000; /* Turn On USB PCLK */
#endif
/* Configure PLL, switch from IRC to Main OSC */
ConfigurePLL();

/* Set system timers for each component */
#if (Fpclk / (Fcclk / 4)) == 1
PCLKSEL0 = 0×00000000; /* PCLK is 1/4 CCLK */
PCLKSEL1 = 0×00000000;
#endif
#if (Fpclk / (Fcclk / 4)) == 2
PCLKSEL0 = 0xAAAAAAAA; /* PCLK is 1/2 CCLK */
PCLKSEL1 = 0xAAAAAAAA;
#endif
#if (Fpclk / (Fcclk / 4)) == 4
PCLKSEL0 = 0×55555555; /* PCLK is the same as CCLK */
PCLKSEL1 = 0×55555555;
#endif

/* Set memory accelerater module*/
MAMCR = 0;
#if Fcclk < 20000000
MAMTIM = 1;
#else
#if Fcclk < 40000000
MAMTIM = 2;
#else
MAMTIM = 3;
#endif
#endif
MAMCR = 2;

GPIOResetInit();

init_VIC();

return;
}

/*****************************************************************************
//irq.h
******************************************************************************/
/******************************************************************************
* irq.h: Interrupt related Header file for NXP LPC230x Family
* Microprocessors
*
**********************************************************************/
#ifndef __IRQ_H
#define __IRQ_H

#define I_Bit 0×80
#define F_Bit 0×40

#define SYS32Mode 0×1F
#define IRQ32Mode 0×12
#define FIQ32Mode 0×11

#define HIGHEST_PRIORITY 0×01
#define LOWEST_PRIORITY 0×0F

#define WDT_INT 0
#define SWI_INT 1
#define ARM_CORE0_INT 2
#define ARM_CORE1_INT 3
#define TIMER0_INT 4
#define TIMER1_INT 5
#define UART0_INT 6
#define UART1_INT 7
#define PWM0_1_INT 8
#define I2C0_INT 9
#define SPI0_INT 10 /* SPI and SSP0 share VIC slot */
#define SSP0_INT 10
#define SSP1_INT 11
#define PLL_INT 12
#define RTC_INT 13
#define EINT0_INT 14
#define EINT1_INT 15
#define EINT2_INT 16
#define EINT3_INT 17
#define ADC0_INT 18
#define I2C1_INT 19
#define BOD_INT 20
#define EMAC_INT 21
#define USB_INT 22
#define CAN_INT 23
#define MCI_INT 24
#define GPDMA_INT 25
#define TIMER2_INT 26
#define TIMER3_INT 27
#define UART2_INT 28
#define UART3_INT 29
#define I2C2_INT 30
#define I2S_INT 31

#define VIC_SIZE 32

#define VIC_BASE_ADDR ((unsigned int)&VICIRQStatus)

#define VECT_ADDR_INDEX 0×100
#define VECT_CNTL_INDEX 0×200

void init_VIC( void );
long install_irq( long IntNumber, void *HandlerAddr, long Priority );

#endif /* end __IRQ_H */

/******************************************************************************
** End Of File
******************************************************************************/

Tags: , , , , , , , , , , , , , , , , , , , , , ,

4 Responses to “ARM LPC2378 4×4 matrix keypad C Program”

  1. Mulch Hockessin Says:

    Hey we was just reading your site on my Blackberry and I was thinking about how well it will work on the new ipad coming out . Fleeting thought…. Anyway thanks!

  2. Bank Online Says:

    Just wasting some free time on Digg and I found your post . Not normally what I prefer to read about, but it was certainly worth my time. Thanks.

  3. mundial 2010 zaklady Says:

    Great info, thanks for useful article. I’m waiting for more

  4. Reatha Bowren Says:

    This is my first time I have visited this site. I found a lot of interesting stuff in your blog. From the volume of comments on your posts, I guess I am not the only one! keep up the good work.

Leave a Reply