Hi ard_newbie,
Thanks for your reply, and your right, it works, and I totally forgot, AdderD give me a function in nano seconds and up to 4.3 ms so it is exactly what I want, so it works to, and my precision is around +/- 35ns with the oscilloscope.
Thanks for all, but... yeah I know I have so much difficulties, but I don't have idea, I have a problem with my variable in my function nanoseconds, I put my code here and i wiil explain what I want =D.
/*
int val1=0 ;
int val2=0 ;
int val4=0 ;
int val8=0 ;
uint32_t value_buffer=0;
uint32_t delay_value_100ns;
inline void digitalWriteDirect(int pin, boolean val){
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}
static inline void delayNanoseconds(uint32_t) __attribute__((always_inline, unused));
static inline void delayNanoseconds(uint32_t nsec){
/*
* Based on Paul Stoffregen's implementation
* for Teensy 3.0 (http://www.pjrc.com/)
*/
if (nsec == 0) return;
uint32_t n = (nsec * 1000) / 35714;
asm volatile(
"L_%=_delayNanos:" "\n\t"
"subs %0, #1" "\n\t"
"bne L_%=_delayNanos" "\n"
: "+r" (n) :
);
}
void setup() {
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,INPUT);
pinMode(5,INPUT);
//0.1
pinMode(28,INPUT);
pinMode(26,INPUT);
pinMode(24,INPUT);
pinMode(22,INPUT);
attachInterrupt(22, pin_ISR, CHANGE);
}
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
while ((PIOB->PIO_PDSR & PIO_PDSR_P25) == 0); //wait input become high
// Toggle pin 3
delayNanoseconds(delay_value_100ns);
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(int value1, int value2, int value3, int value4){
//Read thumbwheel switches BCD, if val1 = 1 and val2,val3 and val4 = 0 so the value is 1
if((val1==0)&&(val2==0)&&(val4==0)&&(val8==0)){
value_buffer=0;
Test=0;
}
else if((val1==1)&&(val2==0)&&(val4==0)&&(val8==0)){
value_buffer=1;
Test=1;
}
else if((val1==0)&&(val2==1)&&(val4==0)&&(val8==0)){
value_buffer=2;
Test=2;
}
else if((val1==1)&&(val2==1)&&(val4==0)&&(val8==0)){
value_buffer=3;
}
else if((val1==0)&&(val2==0)&&(val4==1)&&(val8==0)){
value_buffer=4;
}
else if((val1==1)&&(val2==0)&&(val4==1)&&(val8==0)){
value_buffer=5;
}
else if((val1==0)&&(val2==1)&&(val4==1)&&(val8==0)){
value_buffer=6;
}
else if((val1==1)&&(val2==1)&&(val4==1)&&(val8==0)){
value_buffer=7;
}
else if((val1==0)&&(val2==0)&&(val4==0)&&(val8==1)){
value_buffer=8;
}
else if((val1==1)&&(val2==0)&&(val4==0)&&(val8==1)){
value_buffer=9;
}
return value_buffer;
}
void pin_ISR() {
val1 = digitalRead(23);
val2 = digitalRead(25);
val4 = digitalRead(27);
val8 = digitalRead(29);
delay_value_100ns = read_bcd(val1,val2,val4,val8)*1000;
if(delay_value_us_Prec == 2000){
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
else if(delay_value_us_Prec == 1000){
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
}
else{
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}
}
So, My delay works correctly, now I want to select my delay with a ThumbWheel Switche, So I'm reading The pin 23,25,27 and 29, I put a variable "Test" which help me to debug, and my function read BCD works fine.
But my value in "delay_value_100ns" doesn't refresh in my main, well I think, because nothing change... only if I restart the Arduino...
Thanks if any one could help me. =D