keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

MCP23S17_write.ino 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* this works with volt meter (MCP23S17 on 3.3v does not output enough power for LEDs)
  2. LED lights w/o resistor, not light with 56 ohm resistor
  3. blink LED on MCP23S17 port A pin
  4. from Example 41.1 - Microchip MCP23017 with Arduino
  5. http://tronixstuff.com/tutorials > chapter 41
  6. http://tronixstuff.com/2011/08/26/tutorial-maximising-your-arduinos-io-ports/
  7. John Boxall | CC by-sa-nc
  8. from http://69.5.26.215/forum/?id=10945&page=3 #35
  9. modified to test MCP23S17 (SPI) using syntax from
  10. http://arduino.stackexchange.com/questions/16348/how-do-you-use-spi-on-an-arduino
  11. >
  12. SPISettings from http://arduino.stackexchange.com/questions/14191/mcp23s17-programming-iodirx-register-works-in-loop-but-not-in-setup
  13. */
  14. #include <SPI.h>
  15. const uint8_t ADDR = 0x20; //MCP23S17 address, all ADDR pins are grounded
  16. const uint8_t OPCODE_WRITE = (ADDR << 1); //MCP23S17 opcode write has LSB clear
  17. const uint8_t IODIRA = 0x00; //LEDs are on port A
  18. const uint8_t GPIOA = 0x12;
  19. uint8_t LED_state = 0; //bit wise
  20. void IOEWrite(const uint8_t registerAddr, const uint8_t data)
  21. {
  22. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //slower clock
  23. digitalWrite(SS, LOW); //enable Slave Select
  24. SPI.transfer(OPCODE_WRITE); //write command
  25. SPI.transfer(registerAddr); //register address to write data to
  26. SPI.transfer(data); //data
  27. digitalWrite(SS, HIGH); //disable Slave Select
  28. SPI.endTransaction(); //release the SPI bus
  29. }
  30. void setup()
  31. {
  32. Serial.begin(9600);
  33. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  34. digitalWrite(SS, HIGH); //disable Slave Select
  35. SPI.begin();
  36. IOEWrite(IODIRA, 0x00); //configure IODIRA register to output
  37. }
  38. void loop()
  39. {
  40. IOEWrite(GPIOA, LED_state); //set all GPIOA pins
  41. delay(2000);
  42. //Serial.println(LED_state, BIN); //prints alternating 0 and 11111111
  43. LED_state = ~LED_state; //toggle LED on/off
  44. }