Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Nick Gammon.
- // February 2011. PB: going to test three-pin PCINT0_vect
- //SS pin interrupt OK and receiving two adr Hi and Lo from Master with 2uS delay
- /* Fri Jul 3 11:02:43 NZST 2020
- */
- #include <SPI.h>
- #define clockTo1802Pin 3
- volatile boolean gotSS=false,SSFell=false;
- uint16_t PB2Ints=0,SSFallen=0;
- uint16_t DELAY = 300;
- byte adr0,adr1,tempByte0;
- byte oldPortBVal=0x07;
- byte newPortBVal,oldNewXOR;
- byte SSMask=0b00000100; //SS* = PB2
- boolean newAdrComing = false,weirdPCINT0 = false;
- //boolean doGotAdr = false;
- boolean doingPCISR =false;
- //boolean showAdr = false;
- boolean gotAdr = false;
- volatile byte c;
- void setup (void) {
- pinMode(clockTo1802Pin,OUTPUT); //D3 goes to 1802 clock
- pinMode(10,INPUT); //this is SS* pin
- pinChangeSetup();
- Serial.begin (115200); // debugging
- // justStepping();
- // justClocking();
- // turn on SPI in slave mode
- SPCR =(1<<SPE);
- // have to send on master in, *slave out*
- pinMode(MISO, OUTPUT);
- SPI.attachInterrupt();
- } // end of setup
- // SPI interrupt routine
- ISR (SPI_STC_vect) {
- c = SPDR; // grab byte from SPI Data Register
- if (SSFell==true){
- adr0=c;
- SSFell=false;
- } else {
- adr1=c;
- gotAdr=true;
- } //need 2uS gap between adresses to make this work.
- } // end of interrupt routine SPI_STC_vect
- //-----------------------------
- void loop (void) {
- digitalWrite(clockTo1802Pin,HIGH);
- if ( gotAdr) {
- doGotAdr();
- }
- delay(DELAY);
- digitalWrite(clockTo1802Pin,LOW);
- if (doingPCISR==true) { doingPCISR = false; Serial.print("$$");}
- delay(DELAY);
- } //---------------------
- void pinChangeSetup(void) { //make SS*pin = pin10 subject to pin change interrupt.
- PCICR=0x01; //turn on PORTB pin change interripts
- PCMSK0=0x04; //just do pin change interrupt on PCINT2 aka Pb2 aka pin10 aka SS*
- }
- ISR(PCINT0_vect) {
- weirdPCINT0 = false;
- newPortBVal=PINB;
- oldNewXOR=oldPortBVal^newPortBVal; //which bit was changed.
- doingPCISR = true;
- switch(oldNewXOR & 0x04){
- case 0x04: //SS*
- PB2Ints++;
- if((newPortBVal & SSMask)==0 ) { //if pinchange 1-->0
- SSFallen++;
- SSFell=true;
- newAdrComing = true;
- }
- break;
- default:
- weirdPCINT0 = true;
- Serial.println(F("weird PCINT0"));
- tempByte0 = oldNewXOR & 0x04;
- break;
- }
- oldPortBVal=newPortBVal;
- }
- void justStepping(void) {
- while(true) {
- Serial.print("step");
- delay(1000);
- }
- }
- void justClocking(void) {
- while(true) {
- //Serial.print("step");
- digitalWrite(3,HIGH);
- delay(32);
- digitalWrite(3,LOW);
- delay(32);
- }
- }
- void doGotAdr(void) {
- Serial.print("##");
- if (SSFell==true) {Serial.print("SS Fell=true"); }
- Serial.println(PB2Ints);
- Serial.println(SSFallen);
- Serial.print("++");Serial.print(adr0,HEX);Serial.println(adr1,HEX);
- if (weirdPCINT0==true) {Serial.print("--");Serial.print(tempByte0,HEX);}
- gotAdr=false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement