Advertisement
prjbrook

spi0Slave6.ino Slave part of 2Nanos. Just starting..

Jul 3rd, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. // Written by Nick Gammon.
  2. // February 2011. PB: going to test three-pin PCINT0_vect
  3. //SS pin interrupt OK and receiving two adr Hi and Lo from Master with 2uS delay
  4. /* Fri Jul 3 11:02:43 NZST 2020
  5.  
  6. */
  7. #include <SPI.h>
  8. #define clockTo1802Pin 3
  9. volatile boolean gotSS=false,SSFell=false;
  10. uint16_t PB2Ints=0,SSFallen=0;
  11. uint16_t DELAY = 300;
  12. byte adr0,adr1,tempByte0;
  13. byte oldPortBVal=0x07;
  14. byte newPortBVal,oldNewXOR;
  15. byte SSMask=0b00000100; //SS* = PB2
  16. boolean newAdrComing = false,weirdPCINT0 = false;
  17. //boolean doGotAdr = false;
  18. boolean doingPCISR =false;
  19. //boolean showAdr = false;
  20. boolean gotAdr = false;
  21. volatile byte c;
  22. void setup (void) {
  23. pinMode(clockTo1802Pin,OUTPUT); //D3 goes to 1802 clock
  24. pinMode(10,INPUT); //this is SS* pin
  25. pinChangeSetup();
  26. Serial.begin (115200); // debugging
  27. // justStepping();
  28. // justClocking();
  29. // turn on SPI in slave mode
  30. SPCR =(1<<SPE);
  31.  
  32. // have to send on master in, *slave out*
  33. pinMode(MISO, OUTPUT);
  34.  
  35. SPI.attachInterrupt();
  36.  
  37. } // end of setup
  38.  
  39.  
  40. // SPI interrupt routine
  41. ISR (SPI_STC_vect) {
  42. c = SPDR; // grab byte from SPI Data Register
  43. if (SSFell==true){
  44. adr0=c;
  45. SSFell=false;
  46. } else {
  47. adr1=c;
  48. gotAdr=true;
  49. } //need 2uS gap between adresses to make this work.
  50. } // end of interrupt routine SPI_STC_vect
  51.  
  52. //-----------------------------
  53. void loop (void) {
  54. digitalWrite(clockTo1802Pin,HIGH);
  55. if ( gotAdr) {
  56. doGotAdr();
  57. }
  58. delay(DELAY);
  59. digitalWrite(clockTo1802Pin,LOW);
  60. if (doingPCISR==true) { doingPCISR = false; Serial.print("$$");}
  61. delay(DELAY);
  62. } //---------------------
  63.  
  64. void pinChangeSetup(void) { //make SS*pin = pin10 subject to pin change interrupt.
  65. PCICR=0x01; //turn on PORTB pin change interripts
  66. PCMSK0=0x04; //just do pin change interrupt on PCINT2 aka Pb2 aka pin10 aka SS*
  67. }
  68. ISR(PCINT0_vect) {
  69. weirdPCINT0 = false;
  70. newPortBVal=PINB;
  71. oldNewXOR=oldPortBVal^newPortBVal; //which bit was changed.
  72. doingPCISR = true;
  73. switch(oldNewXOR & 0x04){
  74. case 0x04: //SS*
  75. PB2Ints++;
  76. if((newPortBVal & SSMask)==0 ) { //if pinchange 1-->0
  77. SSFallen++;
  78. SSFell=true;
  79. newAdrComing = true;
  80. }
  81. break;
  82.  
  83. default:
  84. weirdPCINT0 = true;
  85. Serial.println(F("weird PCINT0"));
  86. tempByte0 = oldNewXOR & 0x04;
  87. break;
  88. }
  89. oldPortBVal=newPortBVal;
  90. }
  91. void justStepping(void) {
  92. while(true) {
  93. Serial.print("step");
  94. delay(1000);
  95. }
  96. }
  97. void justClocking(void) {
  98. while(true) {
  99. //Serial.print("step");
  100. digitalWrite(3,HIGH);
  101. delay(32);
  102. digitalWrite(3,LOW);
  103. delay(32);
  104. }
  105. }
  106. void doGotAdr(void) {
  107. Serial.print("##");
  108. if (SSFell==true) {Serial.print("SS Fell=true"); }
  109. Serial.println(PB2Ints);
  110. Serial.println(SSFallen);
  111. Serial.print("++");Serial.print(adr0,HEX);Serial.println(adr1,HEX);
  112. if (weirdPCINT0==true) {Serial.print("--");Serial.print(tempByte0,HEX);}
  113. gotAdr=false;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement