Guest User

Driving LED's using MCP23S17(SPI) on Arduino!

a guest
Feb 25th, 2011
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. //#define SCK  13
  2. //#define MOSI  11
  3. //#define MISO  12
  4. //#define SS 10
  5. #include <SPI.h>
  6.  
  7. #define MCP23S17 B01000000  // MCP23017 SPI Address (Not used in this sketch)
  8. #define IOCON   0x0A        // MCP23017 Config Reg.
  9. #define IODIRA  0x00        // MCP23017 address of I/O direction
  10. #define IODIRB  0x01        // MCP23017 1=input
  11. #define IPOLA   0x02        // MCP23017 address of I/O Polarity
  12. #define IPOLB   0x03        // MCP23017 1= Inverted
  13. #define GPIOA   0x12        // MCP23017 address of GP Value
  14. #define GPIOB   0x13        // MCP23017 address of GP Value
  15. #define GPINTENA  0x04      // MCP23017 IOC Enable
  16. #define GPINTENB  0x05      // MCP23017 IOC Enable
  17. #define INTCONA 0x08        // MCP23017 Interrupt Cont
  18. #define INTCONB 0x09        // MCP23017 1= compair to DEFVAL(A or B) 0= change
  19. #define DEFVALA 0x06        // MCP23017 IOC Default value
  20. #define DEFVALB 0x07        // MCP23017 if INTCONA set then INT. if diff.
  21. #define GPPUA   0x0C        // MCP23017 Weak Pull-Ups
  22. #define GPPUB   0x0D        // MCP23017 1= Pulled HIgh via internal 100k
  23. #define OLATA   0x14
  24. #define OLATB   0x15
  25. #define INTFA   0x0E
  26. #define INTFB   0x0F
  27. #define INTCAPA 0x10
  28. #define INTCAPB 0x11
  29.  
  30. const int slaveSelectPin = 10; //アクティブLOW
  31.  
  32. void setup() {
  33.   // set the slaveSelectPin as an output:
  34.   pinMode (slaveSelectPin, OUTPUT);
  35.   // initialize SPI:
  36.   SPI.begin();
  37.  
  38.   //IOCON設定(SEQOPのみDisable→0x20)
  39.   writeData(IOCON,0x20);
  40.   writeData(IOCON+1,0x20);
  41.  
  42.   //IODIRA,B設定(0で出力,1で入力)
  43.   writeData(IODIRA,0x00);
  44.   writeData(IODIRB,0x00);
  45.  
  46. }
  47.  
  48. void loop()
  49. {
  50.   //GPIOB設定
  51.   writeData(GPIOB,0xff);
  52.   delay(300);
  53.   writeData(GPIOB,0x00);
  54.   delay(300);
  55. }
  56.  
  57. void writeData(uint8_t addr,uint8_t data)
  58. {
  59.   digitalWrite(slaveSelectPin,LOW);
  60.   SPI.transfer(MCP23S17);
  61.   SPI.transfer(addr);
  62.   SPI.transfer(data);
  63.   digitalWrite(slaveSelectPin,HIGH);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment