This a basic program that uses digital input-output functions. It counts how many times the button is pressed and displays the resulting value as 4-bit binary on LPC1768 leds.
main.cpp@1:55585733d77e, 2015-07-21 (annotated)
- Committer:
- BaserK
- Date:
- Tue Jul 21 08:05:53 2015 +0000
- Revision:
- 1:55585733d77e
- Parent:
- 0:4b9e06ca482c
MIT license is added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BaserK | 0:4b9e06ca482c | 1 | /* Digital input output example on LPC1768 |
BaserK | 0:4b9e06ca482c | 2 | * |
BaserK | 0:4b9e06ca482c | 3 | * @author: Baser Kandehir |
BaserK | 0:4b9e06ca482c | 4 | * @date: July 9, 2015 |
BaserK | 1:55585733d77e | 5 | * @license: MIT license |
BaserK | 1:55585733d77e | 6 | * |
BaserK | 1:55585733d77e | 7 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 1:55585733d77e | 8 | * |
BaserK | 1:55585733d77e | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 1:55585733d77e | 10 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 1:55585733d77e | 11 | * in the Software without restriction, including without limitation the rights |
BaserK | 1:55585733d77e | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 1:55585733d77e | 13 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 1:55585733d77e | 14 | * furnished to do so, subject to the following conditions: |
BaserK | 1:55585733d77e | 15 | * |
BaserK | 1:55585733d77e | 16 | * The above copyright notice and this permission notice shall be included in |
BaserK | 1:55585733d77e | 17 | * all copies or substantial portions of the Software. |
BaserK | 1:55585733d77e | 18 | * |
BaserK | 1:55585733d77e | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 1:55585733d77e | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 1:55585733d77e | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 1:55585733d77e | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 1:55585733d77e | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 1:55585733d77e | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 1:55585733d77e | 25 | * THE SOFTWARE. |
BaserK | 1:55585733d77e | 26 | * |
BaserK | 0:4b9e06ca482c | 27 | * @description of the program: |
BaserK | 0:4b9e06ca482c | 28 | * |
BaserK | 0:4b9e06ca482c | 29 | * This a basic program that uses digital input-output functions. |
BaserK | 0:4b9e06ca482c | 30 | * It counts how many times the button is pressed and displays the |
BaserK | 0:4b9e06ca482c | 31 | * resulting value as 4-bit binary on LPC1768 leds. |
BaserK | 0:4b9e06ca482c | 32 | * |
BaserK | 0:4b9e06ca482c | 33 | * @connections: Pull up button with 10k resistor is connected to pin 18. |
BaserK | 0:4b9e06ca482c | 34 | * |
BaserK | 0:4b9e06ca482c | 35 | */ |
BaserK | 0:4b9e06ca482c | 36 | |
BaserK | 0:4b9e06ca482c | 37 | #include "mbed.h" |
BaserK | 0:4b9e06ca482c | 38 | |
BaserK | 0:4b9e06ca482c | 39 | /* Activate on board LEDs */ |
BaserK | 0:4b9e06ca482c | 40 | DigitalOut LPC_led1(LED1); |
BaserK | 0:4b9e06ca482c | 41 | DigitalOut LPC_led2(LED2); |
BaserK | 0:4b9e06ca482c | 42 | DigitalOut LPC_led3(LED3); |
BaserK | 0:4b9e06ca482c | 43 | DigitalOut LPC_led4(LED4); |
BaserK | 0:4b9e06ca482c | 44 | |
BaserK | 0:4b9e06ca482c | 45 | DigitalIn button(p18); // button (pull-up) is connected to P18 |
BaserK | 0:4b9e06ca482c | 46 | |
BaserK | 0:4b9e06ca482c | 47 | /* Function prototype */ |
BaserK | 0:4b9e06ca482c | 48 | void ledCounter(int value); |
BaserK | 0:4b9e06ca482c | 49 | |
BaserK | 0:4b9e06ca482c | 50 | int count=0; |
BaserK | 0:4b9e06ca482c | 51 | |
BaserK | 0:4b9e06ca482c | 52 | int main() |
BaserK | 0:4b9e06ca482c | 53 | { |
BaserK | 0:4b9e06ca482c | 54 | while(1) |
BaserK | 0:4b9e06ca482c | 55 | { |
BaserK | 0:4b9e06ca482c | 56 | if(button.read()==0) // if the button is pressed |
BaserK | 0:4b9e06ca482c | 57 | { |
BaserK | 0:4b9e06ca482c | 58 | while(button.read()==0); // wait until release |
BaserK | 0:4b9e06ca482c | 59 | wait_ms(20); // button debounce |
BaserK | 0:4b9e06ca482c | 60 | count++; // count up |
BaserK | 0:4b9e06ca482c | 61 | count%=16; // count (mod 16) |
BaserK | 0:4b9e06ca482c | 62 | } |
BaserK | 0:4b9e06ca482c | 63 | ledCounter(count); // display the count |
BaserK | 0:4b9e06ca482c | 64 | } |
BaserK | 0:4b9e06ca482c | 65 | } |
BaserK | 0:4b9e06ca482c | 66 | |
BaserK | 0:4b9e06ca482c | 67 | // This function can display value(mod 16) as binary on LPC1768 |
BaserK | 0:4b9e06ca482c | 68 | // For example = 12 = (1010) led2 and led4 will be ON, led1 and led3 will be OFF. |
BaserK | 0:4b9e06ca482c | 69 | void ledCounter(int value) |
BaserK | 0:4b9e06ca482c | 70 | { |
BaserK | 0:4b9e06ca482c | 71 | LPC_led1=value%2; |
BaserK | 0:4b9e06ca482c | 72 | LPC_led2=(value>>1)%2; |
BaserK | 0:4b9e06ca482c | 73 | LPC_led3=(value>>2)%2; |
BaserK | 0:4b9e06ca482c | 74 | LPC_led4=(value>>3)%2; |
BaserK | 0:4b9e06ca482c | 75 | } |