Wednesday, 7 June 2017

Input button in LPC2148

In the last tutorial we saw how to interface LCD with the microcontroller. If you guys have noticed, till now we only programmed the controller in a way so that our pins are always output. So today we are going to make one of our pin as input and attach a button to it.

Objective-
If the button is pressed, LED will turn on and and will turn off on releasing the button.

While assigning a pin as an input, always remember that we have to use Pull-up resistor. Pull-up resistor is just like a normal resistor, connected between the line (the input) and Vcc. So by default, it pulls the line high or you can say input is high (1).

Coming back to button, as you can see in the figure below, a 100k resistor is connected between Vcc and the line.
Here by default line or input voltage will be high but as we press the button, the current will flow to the ground via sw1 and our input will be low. We can assign some function for this and wait for pin to go low, as soon that happens, another pin will go high and a LED will glow.

HOW TO-
Check if button is pressed..
    if (!(IOPIN0&(1<<0)))    // if the button is pressed
{
IOSET0 |= (1<<1);    // turn on green LED
                 }
The '!' sign in the if statement is also called 'not equal to'. So the if statement goes like this- if Port0.0 is not equal to 1, do the following.

CODE- 
#include <lpc214x.h>


int main ()
{
PINSEL0 = 0;    // setting PINSEL as normal GPIO
IODIR0 |= (0<<0)|(1<<1)|(1<<2);  //setting pin 0 as input and pin 1 and 2 a o/p
while (1)
{
if (!(IOPIN0&(1<<0)))    // if the button is pressed
{
IOSET0 |= (1<<1);    // turn on green LED
IOCLR0 |= (1<<2);    // turn off red LED
}
else 
{
IOSET0 |= (1<<2);    // turn on red LED
IOCLR0 |= (1<<1);    // turn off green LED
}
}
}

Congrats guys you have successfully programmed a button.
The result of the above code is - - >
When button is not pressed
When button is pressed

Subscribe for more tutorials
TO download full working Project click DOWNLOAD

Servo motor with STM32

I have already covered a tutorial about Pulse Width Modulation in STM32  HERE  and in this tutorial i am going to cover one of its applicat...