General > General Technical Chat

Schmitt Buffer/Trigger with Encoder and buttons - Inverting...

<< < (7/8) > >>

elcrni:
Alright, changed the prescaler to 256 and now reading ~496Hz on the scope port.
I am very happy with this now!



--- Code: ---#define encoderA 2
#define encoderB 3

volatile int encoderCount = 0;
boolean A_set = false;
boolean B_set = false;

const int scope_pin =PB0;

ISR(TIMER1_COMPA_vect) {
 
 
  if ( digitalRead(encoderA) != A_set ) { // debounce once more
    A_set = !A_set;
    if ( A_set && !B_set )
        encoderCount ++;
  }
    if ( digitalRead(encoderB) != B_set ) {
     B_set = !B_set;
      if ( B_set && !A_set )
        encoderCount --;
    }
    PORTB ^= (1<< scope_pin);
}


void setup() {
  pinMode(encoderA, INPUT);
  pinMode(encoderB, INPUT);

  DDRB |= (1 << scope_pin);

  TIMSK1 |= (1<<OCIE1A);

  TCCR1A = 0;
  TCCR1B = (1<<WGM12) | (1<<CS12);
  OCR1A = 125;
 
  sei();
 
}

void loop() {
 
}
--- End code ---


Many thanks for all your help MarkF!

Alek

elcrni:
very strange thing happens, i have just measured frequency on the scope pin again, whatever i do i get 248Hz, half of the 500Hz target... once i remove the ground connection and measure with positive scope probe i get 496Hz...
either i am doing something very wrong or my math in the code is no good...

Thanks,
Alek

MarkF:

--- Quote from: elcrni on March 19, 2021, 04:24:24 pm ---very strange thing happens, i have just measured frequency on the scope pin again, whatever i do i get 248Hz, half of the 500Hz target... once i remove the ground connection and measure with positive scope probe i get 496Hz...
either i am doing something very wrong or my math in the code is no good...

Thanks,
Alek

--- End quote ---

Think about what your code is doing...

Hint:  Every time you enter the ISR() routine you toggle the scope_pin.

If you set the scope_pin when you enter the ISR() and then clear the scope_pin when you exit the ISR(), you will be able to measure the time spent inside the ISR().

Question:  Why are you doing Port writes for the scope_pin and bit reads for the encoder lines?
  1)  Doing the Port write for the scope_pin effects ALL bits in that port.
  2)  I would expect the opposite.  Bit write for scope_pin and port read for encoder bits.
Ideally, doing a port read for the encoder will avoid time skew of encoder_A and encoder_B lines.  All be it, the time delay should be very small.  You never really know how much overhead is inside the digitalRead() function.

elcrni:
if i do it like you suggested:


--- Code: ---uint8_t pins, pinA, pinB, pinS;

   digitalWrite(0, HIGH);  // set pin 0 high when entering ISR(),  check this pin with scope
-----------------
digitalWrite(0, LOW);  // set pin 0 low when leaving ISR()


--- End code ---

I get no readout at all on pin0 (digital pin 53 on mega2560)

As for the port/pin part of it, i am still trying to get a hang of it, still a bit confusing so i am basically shooting in the dark here :-)

Many thanks,
Alek

elcrni:
ok, just changed your pin0 to pin53 and i have a reading of ~500Hz now. this should be it.


json object validator

Off to the next thing, pin/port confusion and trying to figure out your encoder code.

Thanks,
Alek

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod