Marout Yasuo Sluijter-Borms
/
PCF8574_ButtonPress
Read status of buttons on D5-D7 of PCF8574. Turn on corresponding led D0-D3.
main.cpp@0:c862449fe089, 2016-04-04 (annotated)
- Committer:
- hainjedaf
- Date:
- Mon Apr 04 20:59:46 2016 +0000
- Revision:
- 0:c862449fe089
- Child:
- 1:85d249817f55
werkt niet;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hainjedaf | 0:c862449fe089 | 1 | /* |
hainjedaf | 0:c862449fe089 | 2 | * Naam: PCF8574_ButtonPress |
hainjedaf | 0:c862449fe089 | 3 | * |
hainjedaf | 0:c862449fe089 | 4 | * Omschrijving: |
hainjedaf | 0:c862449fe089 | 5 | * I2C testprogramma: |
hainjedaf | 0:c862449fe089 | 6 | * -Configureer PCF8574 4x OUT, 4x IN. |
hainjedaf | 0:c862449fe089 | 7 | * -Zet alle PCF8574-Leds aan en geef terminal echo. |
hainjedaf | 0:c862449fe089 | 8 | * -Zet alle PCF8574-Leds uit en geef terminal echo. |
hainjedaf | 0:c862449fe089 | 9 | * -Zet led 2 aan ten teken 'Initialisatie Klaar' en geef terminal echo. |
hainjedaf | 0:c862449fe089 | 10 | * -Als PCF8574-Knop# ingedrukt: PCF8574-Led# aan |
hainjedaf | 0:c862449fe089 | 11 | * -Als PCF8574-Knop# losgelaten: PCF8574-Led# uit. |
hainjedaf | 0:c862449fe089 | 12 | * |
hainjedaf | 0:c862449fe089 | 13 | * Aanmaakdatum: 04 april 2016 |
hainjedaf | 0:c862449fe089 | 14 | * |
hainjedaf | 0:c862449fe089 | 15 | * Geschreven door: Marout Yasuo Sluijter-Borms |
hainjedaf | 0:c862449fe089 | 16 | * |
hainjedaf | 0:c862449fe089 | 17 | * Copyright (c) 2016, MYSB, The Manhattan Project |
hainjedaf | 0:c862449fe089 | 18 | * Zie einde programma / See end of program. |
hainjedaf | 0:c862449fe089 | 19 | */ |
hainjedaf | 0:c862449fe089 | 20 | |
hainjedaf | 0:c862449fe089 | 21 | /* Libraries en classes insluiten */ |
hainjedaf | 0:c862449fe089 | 22 | #include "mbed.h" |
hainjedaf | 0:c862449fe089 | 23 | #include "PCF8574.h" |
hainjedaf | 0:c862449fe089 | 24 | |
hainjedaf | 0:c862449fe089 | 25 | /* Definieer seriële poorten */ |
hainjedaf | 0:c862449fe089 | 26 | Serial pc(USBTX, USBRX); // tx, rx communicatie met PC terminal via USB. |
hainjedaf | 0:c862449fe089 | 27 | I2C i2c_bus( p9, p10); // I2C bus. |
hainjedaf | 0:c862449fe089 | 28 | PCF8574 pcf0( p9, p10, 0x70); // Eerste PCF8574 chip |
hainjedaf | 0:c862449fe089 | 29 | PCF8574 pcf1( p9, p10, 0x72); // Tweede PCF8574 chip |
hainjedaf | 0:c862449fe089 | 30 | |
hainjedaf | 0:c862449fe089 | 31 | |
hainjedaf | 0:c862449fe089 | 32 | /* Definieer digitale IO */ |
hainjedaf | 0:c862449fe089 | 33 | DigitalOut led(LED2); // Led 2 op mbed is signaal 'Initialisatie klaar'. |
hainjedaf | 0:c862449fe089 | 34 | |
hainjedaf | 0:c862449fe089 | 35 | /* Definieer Analoge IO */ |
hainjedaf | 0:c862449fe089 | 36 | |
hainjedaf | 0:c862449fe089 | 37 | /* Definieer globale variabelen */ |
hainjedaf | 0:c862449fe089 | 38 | char DataIn; // Data ingelezen van PCF8574 |
hainjedaf | 0:c862449fe089 | 39 | char DataOut; // Data te schrijven naar PCF8574 |
hainjedaf | 0:c862449fe089 | 40 | |
hainjedaf | 0:c862449fe089 | 41 | /* Definieer globale constanten */ |
hainjedaf | 0:c862449fe089 | 42 | // const int chipaddress = 0x40; // Adres van de PCF8574 |
hainjedaf | 0:c862449fe089 | 43 | const int i2c_speed = 400000; // Bussnelheid 400kHz |
hainjedaf | 0:c862449fe089 | 44 | const int baudrate = 38400; // Baudrate communicatie met terminal |
hainjedaf | 0:c862449fe089 | 45 | |
hainjedaf | 0:c862449fe089 | 46 | /* Start hoofprogramma */ |
hainjedaf | 0:c862449fe089 | 47 | |
hainjedaf | 0:c862449fe089 | 48 | int main() |
hainjedaf | 0:c862449fe089 | 49 | { |
hainjedaf | 0:c862449fe089 | 50 | // pc.baud(baudrate); // stel baudrate in van usb serial |
hainjedaf | 0:c862449fe089 | 51 | pc.printf("\x1B\x48\x1B\x4A\r"); // scherm schoon en cursor linksboven |
hainjedaf | 0:c862449fe089 | 52 | pc.printf("PCF8574A als bidirectionele IO\n\r"); |
hainjedaf | 0:c862449fe089 | 53 | |
hainjedaf | 0:c862449fe089 | 54 | i2c_bus.frequency( i2c_speed); // Zet de I2C bus op 400kHz |
hainjedaf | 0:c862449fe089 | 55 | |
hainjedaf | 0:c862449fe089 | 56 | pc.printf("Zet I2C speed op %d Hz.\n\r", i2c_speed); // echo naar de terminal |
hainjedaf | 0:c862449fe089 | 57 | |
hainjedaf | 0:c862449fe089 | 58 | while (1) |
hainjedaf | 0:c862449fe089 | 59 | { |
hainjedaf | 0:c862449fe089 | 60 | DataOut = 0xF0; |
hainjedaf | 0:c862449fe089 | 61 | // pcf0.write(DataOut); |
hainjedaf | 0:c862449fe089 | 62 | DataIn = pcf0.read(); // lees eerste chip uit |
hainjedaf | 0:c862449fe089 | 63 | pc.printf("Waaarde van inputs: %d.\r\n", DataIn); // echo gelezen data naar terminal |
hainjedaf | 0:c862449fe089 | 64 | wait ( 0.2); |
hainjedaf | 0:c862449fe089 | 65 | DataOut = DataIn >> 4; // Shift DataIn 4 plaatsen rechts |
hainjedaf | 0:c862449fe089 | 66 | pc.printf("Waaarde van outputs: %d.\r\n", DataOut); // echo gemodificeerde data naar terminal |
hainjedaf | 0:c862449fe089 | 67 | wait ( 0.2); |
hainjedaf | 0:c862449fe089 | 68 | pcf0.write(DataOut); |
hainjedaf | 0:c862449fe089 | 69 | } // endwhile |
hainjedaf | 0:c862449fe089 | 70 | |
hainjedaf | 0:c862449fe089 | 71 | } // endmain |
hainjedaf | 0:c862449fe089 | 72 | |
hainjedaf | 0:c862449fe089 | 73 | /* |
hainjedaf | 0:c862449fe089 | 74 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hainjedaf | 0:c862449fe089 | 75 | * of this software and associated documentation files (the "Software"), to deal |
hainjedaf | 0:c862449fe089 | 76 | * in the Software without restriction, including without limitation the rights |
hainjedaf | 0:c862449fe089 | 77 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hainjedaf | 0:c862449fe089 | 78 | * copies of the Software, and to permit persons to whom the Software is |
hainjedaf | 0:c862449fe089 | 79 | * furnished to do so, subject to the following conditions: |
hainjedaf | 0:c862449fe089 | 80 | * |
hainjedaf | 0:c862449fe089 | 81 | * The above copyright notice and this permission notice shall be included in |
hainjedaf | 0:c862449fe089 | 82 | * all copies or substantial portions of the Software. |
hainjedaf | 0:c862449fe089 | 83 | * |
hainjedaf | 0:c862449fe089 | 84 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hainjedaf | 0:c862449fe089 | 85 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hainjedaf | 0:c862449fe089 | 86 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hainjedaf | 0:c862449fe089 | 87 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hainjedaf | 0:c862449fe089 | 88 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hainjedaf | 0:c862449fe089 | 89 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hainjedaf | 0:c862449fe089 | 90 | * THE SOFTWARE. |
hainjedaf | 0:c862449fe089 | 91 | */ |