Logical not of a define

I am testing timers of Due. At first I tested wave mode with following sketch:

#include <Arduino.h>

volatile uint32_t  tm=0, cntval=0;

void setup() {
  Serial.begin(19200);

PMC->PMC_PCER1 |= PMC_PCER1_PID32;   // activate clock for Timer5
  PIOB->PIO_ABSR &= !PIO_ABSR_P16;     // Switch the multiplexer on pin PB16 to peripheral A for TCLK5
  PIOB->PIO_PDR |= PIO_PDR_P16;        // Disable the GPIO on PB16
  TC1->TC_BMR = TC_BMR_TC2XC2S_TCLK2;  // TCLK5 on XC2
  TC1->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_XC2;            // external clock on XC2
  TC1->TC_CHANNEL[2].TC_CMR |= TC_CMR_WAVE;                // wave mode
  TC1->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;  // enable clock and trigger counter
tm=millis();
}

void loop() {
  // put your main code here, to run repeatedly:

  if (millis()-tm > 300) {
    uint32_t cntact=TC1->TC_CHANNEL[2].TC_CV;
    Serial.print(" -> "); Serial.print(cntact);
    Serial.print(" delt="); Serial.println(cntact-cntval);
    cntval=cntact;
    tm=millis();
  }
}

On pin D67 which also is DAC1 a function generator is connected. On Serial Monitor I can see the counter of Timer5 running up according to the set frequency.

Then I changed the line

TC1->TC_CHANNEL[2].TC_CMR |= TC_CMR_WAVE;                // wave mode

to

TC1->TC_CHANNEL[2].TC_CMR &= !TC_CMR_WAVE;                // capture mode

and expected to see the same counter values.

But really the counter was running up as if the function generator would send 42 MHz. Indeed for a short time unintentionally I had set the amplitude of the function generator to 5V which can damage internals of the Due. And I was afraid of that.

But the timer5 still works :smiley:
TC_CMR_WAVE is a define:
#define TC_CMR_WAVE (0x1u << 15)
but !TC_CMR_WAVE != !(0x1u << 15) but 0

I am happy that my timer is ok, but I don't understand why !TC_CMR_WAVE=0.
What is the explanation?

! is the boolean NOT operator, not the bitwise one.

(0x1u << 15) is non null, so the boolean operator sees it as true and when you ask for not true you get false, which is promoted to 0 is you ask for its integral value

( The bitwise NOT operator in C++ is the tilde character ~)

2 Likes

have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.