prjbrook

spiMaster6.ino 1802 two nano system start

Jul 3rd, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1.  
  2. // Based on Code Written by Nick Gammon// February 2011
  3. //TPA and B reading well from 1802. Now reading address from MA0..MA7 directly.Wed Jul 1 12:20:36 NZST 2020
  4. /*Sat Jul 4 13:29:59 NZST 2020. C:\Users\Dell\Documents\Arduino\PB_Sketches_May20\spiMaster6\spiMaster6.ino
  5.  
  6. */
  7. #include <SPI.h>
  8. //byte adrHi,adrLo;
  9. #define TPAPin 8
  10. #define TPBPin 9
  11. byte adrHi=0x12;
  12. byte adrLo=0x34;
  13. void setup (void)
  14. {
  15. Serial.begin (115200);
  16. pinMode(TPAPin,INPUT);
  17. pinMode(TPBPin,INPUT);
  18. DDRD=0x0;
  19. DDRC=0x0;
  20. pinMode(SS, OUTPUT);
  21. Serial.begin (115200); // debugging
  22. //togSS();
  23. //TPLoop() ;
  24. digitalWrite(SS, HIGH); // ensure SS stays high for now
  25.  
  26. // Put SCK, MOSI, SS pins into output mode
  27. // also put SCK, MOSI into LOW state, and SS into HIGH state.
  28. // Then put SPI hardware into Master mode and turn SPI on
  29. SPI.begin ();
  30.  
  31. // Slow down the master a bit
  32. SPI.setClockDivider(SPI_CLOCK_DIV8);
  33.  
  34. } // end of setup
  35.  
  36.  
  37. void loop (void) {
  38.  
  39. if (digitalRead(TPAPin)==HIGH) {
  40. doTPAStuff();
  41. }
  42. if (digitalRead(TPBPin)==HIGH) {
  43. doTPBStuff();
  44. }
  45. }
  46.  
  47. void SPITx2(void) { //send two adresses via spi
  48. digitalWrite(SS, LOW); // SS is pin 10
  49. SPI.transfer(adrHi);
  50. delayMicroseconds(8); //2 microsecond delay seems to work but not 1 uS. Check with nano when this comes up.
  51. SPI.transfer(adrLo);
  52. digitalWrite(SS, HIGH);
  53. } // -------------------end of loop
  54.  
  55. /*void togSS(void) {
  56. while(true) {
  57. digitalWrite(SS,HIGH);
  58. delay(1000);
  59. digitalWrite(SS,LOW);
  60. delay(1000);
  61. }
  62. }
  63. void TPLoop(void) {
  64. while(true) {
  65. if (digitalRead(TPAPin)==HIGH) {
  66. doTPAStuff();
  67. }
  68. if (digitalRead(TPBPin)==HIGH) {
  69. doTPBStuff();
  70. }
  71. }
  72. }*/
  73. void doTPAStuff(void) {
  74. byte lowNib = PINC & 0x0f;
  75. byte hiNib = PIND & 0xf0;
  76. while(digitalRead(TPAPin) ==HIGH) { }; //do nothing
  77. adrHi = hiNib | lowNib; //transfer later during TPB
  78. Serial.print("A..");Serial.println(adrHi,HEX);
  79. }
  80. void doTPBStuff(void) {
  81. byte lowNib = PINC & 0x0f;
  82. byte hiNib = PIND & 0xf0;
  83. while(digitalRead(TPBPin) ==HIGH) { }; //do nothing
  84. adrLo = hiNib | lowNib;
  85. Serial.print("B,,");Serial.println(adrLo,HEX);
  86. SPITx2(); //send adrHi and adrLo to Nb
  87. }
Add Comment
Please, Sign In to add comment