This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Timers and activity on positive pulse

I am trying to set a timer pulse at 500kHz and on every high pulse i want to do a read on another pin.

I have an IC HX711, that has a 24bit output MSB. every positive cycle it changes to the next bit. 

If i were to bit bang it would be like this, but I feel there is a more elegant way to do this and i hate using delays. I know they have PPI or times and PWM. I just dont know how to ensure i do a reading during a low signal of the period. An arduino function that would be similar is the shiftIn function, if nrf libraries have something similar

for(int i = 0; i < 24; i++){
    if(i > 0) result = result << 1;
    
    nrf_gpio_pin_write(_SCLK, 1);
    nrf_delay_ms(1000);
    nrf_gpio_pin_write(_SCLK, 0);
    nrf_delay_ms(1000);
    
    result = result & nrf_gpio_pin_read(_SDATA);
    result = result << 1;
}

  • Hi

    There are a couple of ways to do this, and the one that is conceptually the simplest might be to use a timer to schedule the clock signal toggle and the pin readout, and use the PPI and GPIOTE to connect the timer to the clock pin directly. 

    Essentially you set up one CC register in the timer to schedule when the clock pin should go low, one CC register to schedule when it goes high, and a third CC register to schedule when the pin should be read. By enabling a shortcut from the last CC even to the clear task you can have the timer cleared automatically, in order to run multiple loops of this sequence. 

    The easy way to read the pin is to enable the interrupt handler for the timer, and enable the interrupt for the third CC register. Then you can simply read out the state of the pin in the interrupt. By adjusting the value of the CC register you can adjust when the pin is sampled relative to the clock output. 

    The drawback of this method is if you have higher priority interrupts in the system, as they might delay the reading out of the pin and give you an incorrect value.  

    The 100% foolproof way of doing this is to use the SAADC module to sample the pin, as you can connect the SAADC to the CC event directly through the PPI. 

    Then you will not be affected by high priority interrupts, since everything will run in the background based on the TIMER, PPI, GPIOTE and SAADC peripherals, but you will need to do some conversion in order to change 10-bit analog ADC values into bits. 

    Best regards
    Torbjørn

Related