blob: 3c987c6ac2506eed6d57cd15096c5b2766ade27f [file] [log] [blame]
Deokgyu Yang2c7e86b2020-04-28 10:56:11 +09001/*
2 * binary.c:
3 * Using the Quick 2 wire 16-bit GPIO expansion board to output
4 * a binary counter.
5 *
6 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
7 ***********************************************************************
8 * This file is part of wiringPi:
9 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 *
11 * wiringPi is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * wiringPi is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************
24 */
25
26#include <stdio.h>
27#include <wiringPi.h>
28#include <mcp23017.h>
29
30#define Q2W_BASE 100
31
32int main (void)
33{
34 int i, bit ;
35
36// Enable the on-goard GPIO
37
38 wiringPiSetup () ;
39
40// Add in the mcp23017 on the q2w board
41
42 mcp23017Setup (Q2W_BASE, 0x20) ;
43
44 printf ("Raspberry Pi - quite2Wire MCP23017 Test\n") ;
45
46// On-board button Input:
47
48 pinMode (0, INPUT) ;
49
50// First 10 pins on q2w board as outputs:
51
52 for (i = 0 ; i < 10 ; ++i)
53 pinMode (Q2W_BASE + i, OUTPUT) ;
54
55// Last pin as an input with the internal pull-up enabled
56
57 pinMode (Q2W_BASE + 15, INPUT) ;
58 pullUpDnControl (Q2W_BASE + 15, PUD_UP) ;
59
60// Loop, outputting a binary number,
61// Go faster with the button, or stop if the
62// on-board button is pushed
63
64 for (;;)
65 {
66 for (i = 0 ; i < 1024 ; ++i)
67 {
68 for (bit = 0 ; bit < 10 ; ++bit)
69 digitalWrite (Q2W_BASE + bit, i & (1 << bit)) ;
70
71 while (digitalRead (0) == HIGH) // While pushed
72 delay (1) ;
73
74 if (digitalRead (Q2W_BASE + 15) == HIGH) // Not Pushed
75 delay (100) ;
76 }
77 }
78 return 0 ;
79}