project
Dependencies: MCP23S17 TextLCD mbed
main.cpp@0:e200d9463a30, 2016-09-27 (annotated)
- Committer:
- mn147147
- Date:
- Tue Sep 27 12:16:12 2016 +0000
- Revision:
- 0:e200d9463a30
project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mn147147 | 0:e200d9463a30 | 1 | |
mn147147 | 0:e200d9463a30 | 2 | |
mn147147 | 0:e200d9463a30 | 3 | #include "mbed.h" |
mn147147 | 0:e200d9463a30 | 4 | #include "MCP23S17.h" |
mn147147 | 0:e200d9463a30 | 5 | #include "TextLCD.h" |
mn147147 | 0:e200d9463a30 | 6 | |
mn147147 | 0:e200d9463a30 | 7 | SPI spi(p5, p6, p7); |
mn147147 | 0:e200d9463a30 | 8 | I2C i2c(p9, p10); |
mn147147 | 0:e200d9463a30 | 9 | DigitalOut cs(p21); |
mn147147 | 0:e200d9463a30 | 10 | |
mn147147 | 0:e200d9463a30 | 11 | |
mn147147 | 0:e200d9463a30 | 12 | |
mn147147 | 0:e200d9463a30 | 13 | |
mn147147 | 0:e200d9463a30 | 14 | void relay_porta(int bitoutput) |
mn147147 | 0:e200d9463a30 | 15 | { |
mn147147 | 0:e200d9463a30 | 16 | |
mn147147 | 0:e200d9463a30 | 17 | cs=1; |
mn147147 | 0:e200d9463a30 | 18 | |
mn147147 | 0:e200d9463a30 | 19 | cs=0; |
mn147147 | 0:e200d9463a30 | 20 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 21 | spi.write(0x42); |
mn147147 | 0:e200d9463a30 | 22 | spi.write(0x12); |
mn147147 | 0:e200d9463a30 | 23 | spi.write(bitoutput); |
mn147147 | 0:e200d9463a30 | 24 | cs=1; |
mn147147 | 0:e200d9463a30 | 25 | } |
mn147147 | 0:e200d9463a30 | 26 | |
mn147147 | 0:e200d9463a30 | 27 | void relay_portb(int bitoutput) |
mn147147 | 0:e200d9463a30 | 28 | { |
mn147147 | 0:e200d9463a30 | 29 | |
mn147147 | 0:e200d9463a30 | 30 | cs=1; |
mn147147 | 0:e200d9463a30 | 31 | |
mn147147 | 0:e200d9463a30 | 32 | cs=0; |
mn147147 | 0:e200d9463a30 | 33 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 34 | spi.write(0x42); |
mn147147 | 0:e200d9463a30 | 35 | spi.write(0x13); |
mn147147 | 0:e200d9463a30 | 36 | spi.write(bitoutput); |
mn147147 | 0:e200d9463a30 | 37 | cs=1; |
mn147147 | 0:e200d9463a30 | 38 | } |
mn147147 | 0:e200d9463a30 | 39 | |
mn147147 | 0:e200d9463a30 | 40 | void mosfet_porta(int bitoutput) |
mn147147 | 0:e200d9463a30 | 41 | { |
mn147147 | 0:e200d9463a30 | 42 | |
mn147147 | 0:e200d9463a30 | 43 | cs=1; |
mn147147 | 0:e200d9463a30 | 44 | |
mn147147 | 0:e200d9463a30 | 45 | cs=0; |
mn147147 | 0:e200d9463a30 | 46 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 47 | spi.write(0x40); |
mn147147 | 0:e200d9463a30 | 48 | spi.write(0x12); |
mn147147 | 0:e200d9463a30 | 49 | spi.write(bitoutput); |
mn147147 | 0:e200d9463a30 | 50 | cs=1; |
mn147147 | 0:e200d9463a30 | 51 | } |
mn147147 | 0:e200d9463a30 | 52 | |
mn147147 | 0:e200d9463a30 | 53 | void mosfet_portb(int bitoutput) |
mn147147 | 0:e200d9463a30 | 54 | { |
mn147147 | 0:e200d9463a30 | 55 | cs=1; |
mn147147 | 0:e200d9463a30 | 56 | |
mn147147 | 0:e200d9463a30 | 57 | cs=0; |
mn147147 | 0:e200d9463a30 | 58 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 59 | spi.write(0x40); |
mn147147 | 0:e200d9463a30 | 60 | spi.write(0x13); |
mn147147 | 0:e200d9463a30 | 61 | spi.write(bitoutput); |
mn147147 | 0:e200d9463a30 | 62 | cs=1; |
mn147147 | 0:e200d9463a30 | 63 | } |
mn147147 | 0:e200d9463a30 | 64 | |
mn147147 | 0:e200d9463a30 | 65 | void autotest() |
mn147147 | 0:e200d9463a30 | 66 | { |
mn147147 | 0:e200d9463a30 | 67 | relay_porta(0x00); |
mn147147 | 0:e200d9463a30 | 68 | wait_ms(100); |
mn147147 | 0:e200d9463a30 | 69 | for(int bitoutput=0x01; bitoutput <= 0x80; bitoutput= bitoutput*2) { |
mn147147 | 0:e200d9463a30 | 70 | relay_porta(bitoutput); |
mn147147 | 0:e200d9463a30 | 71 | wait_ms(100); |
mn147147 | 0:e200d9463a30 | 72 | } |
mn147147 | 0:e200d9463a30 | 73 | } |
mn147147 | 0:e200d9463a30 | 74 | |
mn147147 | 0:e200d9463a30 | 75 | int main() |
mn147147 | 0:e200d9463a30 | 76 | { |
mn147147 | 0:e200d9463a30 | 77 | |
mn147147 | 0:e200d9463a30 | 78 | cs=1; |
mn147147 | 0:e200d9463a30 | 79 | spi.format(8,0); |
mn147147 | 0:e200d9463a30 | 80 | |
mn147147 | 0:e200d9463a30 | 81 | cs=0; |
mn147147 | 0:e200d9463a30 | 82 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 83 | spi.write(0x42); //write to address 001 |
mn147147 | 0:e200d9463a30 | 84 | spi.write(0x0A); //iocon bank 0 |
mn147147 | 0:e200d9463a30 | 85 | spi.write(0x2A); // setup af control register (BANK = 0, MIRROR = 0, SEQOP = 1, DISSLW = 0, HAEN = 1, ODR = 0, INTPOL = 1, NC = 0) |
mn147147 | 0:e200d9463a30 | 86 | cs=1; |
mn147147 | 0:e200d9463a30 | 87 | |
mn147147 | 0:e200d9463a30 | 88 | |
mn147147 | 0:e200d9463a30 | 89 | |
mn147147 | 0:e200d9463a30 | 90 | cs=0; //chipselect enable |
mn147147 | 0:e200d9463a30 | 91 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 92 | spi.write(0x42); //write to address 001 |
mn147147 | 0:e200d9463a30 | 93 | spi.write(0x00); //set direction I/O portA |
mn147147 | 0:e200d9463a30 | 94 | spi.write(0x00); //all ports are outputs |
mn147147 | 0:e200d9463a30 | 95 | cs=1; |
mn147147 | 0:e200d9463a30 | 96 | |
mn147147 | 0:e200d9463a30 | 97 | cs=0; //chipselect enable |
mn147147 | 0:e200d9463a30 | 98 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 99 | spi.write(0x42); //write to address 001 |
mn147147 | 0:e200d9463a30 | 100 | spi.write(0x01); //set direction I/O portB |
mn147147 | 0:e200d9463a30 | 101 | spi.write(0x00); //all ports are outputs |
mn147147 | 0:e200d9463a30 | 102 | cs=1; |
mn147147 | 0:e200d9463a30 | 103 | |
mn147147 | 0:e200d9463a30 | 104 | cs=0; |
mn147147 | 0:e200d9463a30 | 105 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 106 | spi.write(0x42); |
mn147147 | 0:e200d9463a30 | 107 | spi.write(0x0C); //GPPUA |
mn147147 | 0:e200d9463a30 | 108 | spi.write(0x00); //pullup resistors disabled on all bits |
mn147147 | 0:e200d9463a30 | 109 | cs=1; |
mn147147 | 0:e200d9463a30 | 110 | |
mn147147 | 0:e200d9463a30 | 111 | cs=0; |
mn147147 | 0:e200d9463a30 | 112 | wait_us(1); |
mn147147 | 0:e200d9463a30 | 113 | spi.write(0x42); |
mn147147 | 0:e200d9463a30 | 114 | spi.write(0x0D); //GPPUB |
mn147147 | 0:e200d9463a30 | 115 | spi.write(0x00); //pullup resistors disabled on all bits |
mn147147 | 0:e200d9463a30 | 116 | cs=1; |
mn147147 | 0:e200d9463a30 | 117 | |
mn147147 | 0:e200d9463a30 | 118 | |
mn147147 | 0:e200d9463a30 | 119 | while(1) { |
mn147147 | 0:e200d9463a30 | 120 | autotest(); |
mn147147 | 0:e200d9463a30 | 121 | |
mn147147 | 0:e200d9463a30 | 122 | } |
mn147147 | 0:e200d9463a30 | 123 | |
mn147147 | 0:e200d9463a30 | 124 | } |
mn147147 | 0:e200d9463a30 | 125 | |
mn147147 | 0:e200d9463a30 | 126 | |
mn147147 | 0:e200d9463a30 | 127 | |
mn147147 | 0:e200d9463a30 | 128 |