keybrd library is an open source library for creating custom-keyboard firmware.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

MCP23S17_read.ino 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* this works
  2. The setup is an MCP23S17 I/O expander on a Teensy LC controller.
  3. MCP23S17 port B pins are alternately grounded and energized.
  4. portBState is a bitwise reading of port B.
  5. output is: 10101010
  6. posted on http://arduino.stackexchange.com/questions/tagged/spi
  7. http://arduino.stackexchange.com/questions/28792/reading-an-mcp23s17-i-o-expander-port-with-the-arduino-spi-library
  8. */
  9. #include <SPI.h>
  10. const uint8_t ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
  11. const uint8_t OPCODE_READ = (ADDR << 1 | 0x01); //MCP23S17 opcode read has LSB set
  12. const uint8_t IODIRB = 0x01;
  13. const uint8_t GPIOB = 0x13;
  14. uint8_t portBState = 0; //bit wise
  15. void setup()
  16. {
  17. Serial.begin(9600);
  18. delay(1000);
  19. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  20. digitalWrite(SS, HIGH); //disable Slave Select
  21. SPI.begin();
  22. //IODIRB register is already configured to input by default
  23. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //gain control of SPI bus
  24. digitalWrite(SS, LOW); //enable Slave Select
  25. SPI.transfer(OPCODE_READ); //read command
  26. SPI.transfer(GPIOB); //register address to read data from
  27. portBState = SPI.transfer(0); //save the data (0 is dummy data to send)
  28. digitalWrite(SS, HIGH); //disable Slave Select
  29. SPI.endTransaction(); //release the SPI bus
  30. Serial.println(portBState, BIN); //should print 10101010
  31. }
  32. void loop() { }