ARM LPC2138/48 Mini Development Board – Switches Interfacing
The board has 3 switches (SW1,SW2 & SW3) connected to port pins P0.14, P0.15 & P0.16 via jumpers J4, J5 & J6 respectively. On shorting these jumpers, the switches can be used as pull_up keys and if the jumpers are left open, then the port pins can be used independently.
Sample code to test the switches is given below. In the code, switch press is detected by polling and upon pressing a switch, its name(SW1,SW2 or SW3) gets displayed on the LCD.
Schematic
Note: To use switches, don’t forget to short the corresponding jumpers J4, J5 & J6. Leaving them open frees P0.14, P0.15 & P0.16 and can be used for any other desired purpose.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
/********************************************************************************* HEADER FILES **********************************************************************************/ #include<lpc213x.h> #define RS 0x00020000 /* RS - P1.17 */ #define RW 0X00040000 /* R/W - P1.18 */ #define EN 0X00080000 /* E - P1.19 */ #define CLR 0X00FE0000 volatile unsigned int key_scan=0; unsigned char key_count=0; /********************************************************************************* * Function : Delay * * * * Description : Function for Delay * * * * Parameters : x - Delay time * **********************************************************************************/ int Delay(unsigned int x) { x=x*8000; while(x!=0) { x--; } return 0; } /********************************************************************************* * Function : LCD_Command * * * * Description : Function to give command to LCD * * * * Parameters : command - command to be given * **********************************************************************************/ void LCD_Command(char command) { int Temp; IO1CLR = CLR; /* Clearing the port pins */ IO1SET = EN; /* Enable pin high */ IO1CLR = RS; /* RS=0 for command register */ IO1CLR = RW; /* R/W=0 for write */ Temp = (command & 0xF0) << 16; /* Taking the first nibble of command */ IO1SET = IO1SET | Temp; /* Writing it to data line */ Delay(2); IO1CLR = EN; /* Enable pin low to give H-L pulse */ } /********************************************************************************* * Function : LCD_Command1 * * * * Description : Function to give command to LCD * * * * Parameters : command1 - command command to be given * **********************************************************************************/ void LCD_Command1(char command1) { int Temp; IO1CLR = CLR; /* Clearing the port pins */ IO1SET = EN; /* Enable pin high */ IO1CLR = RS; /* RS=0 for command register */ IO1CLR = RW; /* R/W=0 for write */ Temp = (command1 & 0xF0); /* Taking the first nibble of command */ Temp = Temp << 16; /* Shift it 16 bits to left */ IO1SET = IO1SET | Temp; /* Writing it to dataline(P1.20-P1.23)*/ Delay(2); IO1CLR = EN; /* Enable pin low to give H-L pulse */ IO1CLR = CLR; /* Clearing the port pins */ IO1SET = EN; /* Enable pin high */ IO1CLR = RS; /* RS=0 for command register */ IO1CLR = RW; /* R/W=0 for write */ Temp = (command1 & 0x0F); /* Taking the second nibble of command*/ Temp = Temp << 20; /* Shift it 20 bits to left */ IO1SET = IO1SET | Temp; /* Writing it to data line */ Delay(2); IO1CLR = EN; /* Enable pin low to give H-L pulse */ } /********************************************************************************* * Function : LCD_Data * * * * Description : Function to display single character on LCD * * * * Parameters : data - character to be displayed * **********************************************************************************/ void LCD_Data(char data) { int Temp; IO1CLR = CLR; /* Clearing the port pins */ IO1SET = EN; /* Enable pin high */ IO1SET = RS; /* RS=1 for data register */ IO1CLR = RW; /* R/W=0 for write */ Temp = (data & 0xF0); /* Taking the first nibble of data */ Temp = Temp << 16; /* Shift it 16 bits to left */ IO1SET = IO1SET | Temp; /* Writing it to data line */ Delay(2); IO1CLR = EN; /* Enable pin low to give H-L pulse */ IO1CLR = CLR; /* Clearing the port pins */ IO1SET = EN; /* Enable pin high */ IO1SET = RS; /* RS=1 for data register */ IO1CLR = RW; /* R/W=0 for write */ Temp = (data & 0x0F); /* Taking the second nibble of data */ Temp = Temp << 20; /* Shift it 20 bits to left */ IO1SET = IO1SET | Temp; /* Writing it to data line */ Delay(2); IO1CLR = EN; /* Enable pin low to give H-L pulse */ } /********************************************************************************* * Function : LCD_String * * * * Description : Function to display string on LCD * * * * Parameters : String to be displayed * **********************************************************************************/ void LCD_String(unsigned char *dat) { while(*dat!='\0') /* Check for termination character */ { LCD_Data(*dat); /* Display the character on LCD */ dat++; /* Increment the pointer */ } } /********************************************************************************* * Function : LCD_Init * * * * Description : Function to initialize LCD * * * * Parameters : None * **********************************************************************************/ void LCD_Init(void) { Delay(15); LCD_Command(0x30); Delay(10); LCD_Command(0x30); Delay(5); LCD_Command(0x30); LCD_Command(0x20); LCD_Command1(0x28); LCD_Command1(0x01); /* Clear display */ LCD_Command1(0x06); /* Auto increment */ LCD_Command1(0x0C); /* Cursor off */ } /********************************************************************************* * Function : Key_Check * * * * Description : Function to check keypress * * * * Parameters : None * **********************************************************************************/ void Key_Check(void) { IO0CLR = 0X0001C000; key_scan = IO0PIN & 0X0001C000; /* Reading PORT0 */ LCD_Command1(0xC5); LCD_String(" "); if(key_scan!=0X0001C000) /* Check for key press */ { if(key_scan==0X00018000) /* If SW1 is pressed, P0.14 gets low */ { LCD_Command1(0xC5); /* Command for 2nd row, 6th position */ LCD_String("SW1"); /* Display on LCD */ while((IO0PIN&0X0001C000)!=0X0001C000); /* Wait till switch is released */ } if(key_scan==0X00014000) /* If SW2 is pressed, P0.15 gets low */ { LCD_Command1(0xC5); /* Command for 2nd row, 6th position */ LCD_String("SW2"); /* Display on LCD */ while((IO0PIN&0X0001C000)!=0X0001C000); /* Wait till switch is released */ } if(key_scan==0X0000C000) /* If SW3 is pressed, P0.16 gets low */ { LCD_Command1(0xC5); /* Command for 2nd row, 6th position */ LCD_String("SW3"); /* Display on LCD */ while((IO0PIN&0X0001C000)!=0X0001C000); /* Wait till switch is released */ } } } /********************************************************************************* * Function : PORT_Initial * * * * Description : Function to initialize ports * * * * Parameters : None * **********************************************************************************/ void PORT_Initial(void) { IO1DIR = 0x00FE0000; /* LCD pins set as o/p */ IO0DIR = 0x00000000; /* PORT0 set as i/p */ PINSEL0 = 0x00000000; PINSEL1 = 0x00000000; PINSEL2 = 0x00000000; } /********************************************************************************* MAIN FUNCTION **********************************************************************************/ int main() { PORT_Initial(); /* Initialize the ports */ LCD_Init(); /* Initialize LCD */ LCD_Command1(0x80); /* 1st row */ LCD_String("KEY Checking."); while(1) { Key_Check(); /* Check for key press */ } } /****************************** END OF PROGRAM *********************************/ |
Topics related to ARM LPC2138/48 Mini Development Board
- ARM LPC2138/48 Mini Development Board – Overview
- ARM LPC2138/48 Mini Development Board – LED Interfacing
- ARM LPC2138/48 Mini Development Board – LCD Interfacing
- ARM LPC2138/48 Mini Development Board – UART0 Interfacing
- ARM LPC2138/48 Mini Development Board – UART1 Interfacing
- ARM LPC2138/48 Mini Development Board – Switches Interfacing
- ARM LPC2138/48 Mini Development Board – BUZZER Interfacing
- ARM LPC2138/48 Mini Development Board – POT Interfacing (ADC)
- ARM LPC2138/48 Mini Development Board – Temperature Sensor Interfacing(ADC)
- ARM LPC2138/48 Mini Development Board – Interfacing Servo motor
- ARM LPC2138/48 Mini Development Board – Internal Real Time Clock (RTC)
- ARM LPC2148 Mini Development Board – USB Interfacing (Human Interface Device)
Resources
- Softwares
- Datasheets
How to buy?
- Click here to buy rhydoLABZ ARM LPC2129 Development board-Mini
- Click here to buy rhydoLABZ ARM LPC2138 Development Board-Mini
- Click here to buy rhydoLABZ ARM LPC2148 Development Board-Mini
Support
Please share your ideas with us, visit our forum for discussion