LPC2378 Example: How to detect Buttonpresses

Download pre-release library modules and new examples to use with Astrobe for LPC2000. Forum members can also upload their own source code examples.
Post Reply
4GlCoder
Posts: 27
Joined: Fri Jul 22, 2011 2:47 pm

LPC2378 Example: How to detect Buttonpresses

Post by 4GlCoder » Tue Aug 30, 2011 6:07 pm

Just a simple examples of how to detect buttonpresses. Tested on LPC2378-STK board:

Code: Select all

MODULE ButtonPress;
(* Olimex LPC2378-STK 
   Quick check for button-presses; Uses LCD Display *)
IMPORT Main, Display, LPC := LPC2378, SYSTEM, Timer, Out;

CONST
  B1Bit = 29; B1Mask = {B1Bit};  (* BUT1 = P0.29/U1D+*)
  B2Bit = 18; B2Mask = {B2Bit};  (* BUT2 = P0.18/DCD1/MOSI0/MOSI *)

PROCEDURE ButtonInit;
CONST
  PinBits = {4,5,26,27};      (* PINSEL1 function bits for BUT1 and BUT2 *)
  InBits = {B1Bit,B2Bit};
VAR
  bits, dirs, scsreg: SET;
BEGIN
  Display.Init;               (* Initialize LCD *)
  Out.Init(Display.Char);     (* Show output in LCD *)
  
  SYSTEM.GET(LPC.SCS, scsreg);
  SYSTEM.PUT(LPC.SCS, scsreg + {0});        (* configure for GPIO/FIO *)
  (* Select GPIO function for BUT1, BUT2 *)
  SYSTEM.GET(LPC.PINSEL1, bits);
  SYSTEM.PUT(LPC.PINSEL1, bits - PinBits);  (* Bits are now GPIO Ports *)
  (* Select buttons for input using Fast IO registers*)
  SYSTEM.GET(LPC.FIO0DIR, dirs);
  SYSTEM.PUT(LPC.FIO0DIR, dirs - InBits)    (* clearing the bits, sets them to input *)
END ButtonInit;

PROCEDURE Run;
CONST
  Delay = 140; (* Loop 7 times per second *)
VAR  
  PortBits: SET;
BEGIN
  Out.String("Press buttons"); Out.Ln;
  REPEAT
    (* Fast Read all pins on P0 *)
    SYSTEM.GET(LPC.FIO0PIN, PortBits);
    (* When button is pressed, the value of the pin becomes 0,
       as long as you press the button *)
    IF ~(B1Bit IN PortBits) THEN
      Out.String("BUT1 pressed"); Out.Ln
    END;
    IF ~(B2Bit IN PortBits) THEN
      Out.String("BUT2 pressed"); Out.Ln
    END;
    Timer.MSecDelay(Delay);
  UNTIL FALSE
END Run;

BEGIN
  ButtonInit;
  Run
END ButtonPress.

Post Reply