Go Down

Topic: Delay minimum 100ns (Read 7766 times) previous topic - next topic

ard_newbie

#15
Jun 13, 2017, 01:44 pm Last Edit: Jun 13, 2017, 01:46 pm by ard_newbie
your global variables valx updated in an ISR should be declared as volatile boolean valx, otherwise they are not properly updated.

GCombault

Ok, so I delete all variable Varx, so i should have no problem, but no =/. I found a solution and I think it is not the best.

Code: [Select]

void loop() {

   //PIOB->PIO_CODR = PIO_CODR_P25;  // Be sure to start with a low level for output pin
  while (true) {
    // wait until pin 2 is high
    analogWrite(9,delay_value_us);
    while ((PIOB->PIO_PDSR & PIO_PDSR_P25) == 0); //wait input become high
    // Toggle pin 11
    delayNanoseconds(delay_value_us);
    PIOD->PIO_ODSR ^= PIO_ODSR_P7;  // low to high
    PIOD->PIO_ODSR ^= PIO_ODSR_P7;  // high to low
    while ((PIOB->PIO_PDSR & PIO_PDSR_P25) > 0); //wait input become low
  }
}


int read_bcd(boolean value1, boolean value2, boolean value3, boolean value4){
  //Read BCD wheel, if val1 = 1 and val2,val3 and val4 = 0 so the value is 1
  if((value1==LOW)&&(value2==LOW)&&(value3==LOW)&&(value4==LOW)){
    value_buffer=0;
  }
  else if((value1==HIGH)&&(value2==LOW)&&(value3==LOW)&&(value4==LOW)){
    value_buffer=1;
  }
  else if((value1==LOW)&&(value2==HIGH)&&(value3==LOW)&&(value4==LOW)){
    value_buffer=2;
  }
  else if((value1==HIGH)&&(value2==HIGH)&&(value3==LOW)&&(value4==LOW)){
    value_buffer=3;
  }
  else if((value1==LOW)&&(value2==LOW)&&(value3==HIGH)&&(value4==LOW)){
    value_buffer=4;
  }
  else if((value1==HIGH)&&(value2==LOW)&&(value3==HIGH)&&(value4==LOW)){
    value_buffer=5;
  }
  else if((value1==LOW)&&(value2==HIGH)&&(value3==HIGH)&&(value4==LOW)){
    value_buffer=6;
  }
  else if((value1==HIGH)&&(value2==HIGH)&&(value3==HIGH)&&(value4==LOW)){
    value_buffer=7;
  }
  else if((value1==LOW)&&(value2==LOW)&&(value3==LOW)&&(value4==HIGH)){
    value_buffer=8;
  }
  else if((value1==HIGH)&&(value2==LOW)&&(value3==LOW)&&(value4==HIGH)){
    value_buffer=9;
  }
  return value_buffer;
}

void pin_ISR() {
delay_value_us_Prec = read_bcd(digitalRead(47),digitalRead(49),digitalRead(51), digitalRead(53))*1000000;
delay_value_us_Prec = delay_value_us_Prec+read_bcd(digitalRead(39),digitalRead(41),digitalRead(43),digitalRead(45))*100000;
delay_value_us_Prec = delay_value_us_Prec+read_bcd(digitalRead(31),digitalRead(33),digitalRead(35),digitalRead(37))*10000;
delay_value_us_Prec = delay_value_us_Prec+read_bcd(digitalRead(23),digitalRead(25),digitalRead(27),digitalRead(29))*1000;
delay_value_ns = read_bcd(digitalRead(22),digitalRead(24),digitalRead(26),digitalRead(28))*100;

delay_value_us= delay_value_us_Prec+delay_value_ns;
}
}



If I use AnalogWrite so my value in delay_value_us will be updated in my function, if I use a simple equal it will not work.
Of cours with the analogWrite it works, but with a latency around 400ns between 250 ns without analogwrite.

Thanks for your help ard_newbie, if nobody have idea so I will use analogWrite.

Thanks in advance!

ard_newbie

#17
Jun 13, 2017, 03:02 pm Last Edit: Jun 13, 2017, 03:06 pm by ard_newbie
I wrote : << declare your global variables Inside an ISR as VOLATILE >>

Before your setup()  write:

volatile boolean val1, val2, val3, val4;

Go Up