1

I have the following code:

def read_adc(adc, lsb):
# first read 
adc.writebytes([0x8E]) # ask for channel 0
my_bytes = adc.readbytes(2)  # read channel 
value = (my_bytes[0] << 3)
print(value*lsb*4.01)
# second read
adc.writebytes([0x8E]) # ask for channel 0
my_bytes = adc.readbytes(2)  # read channel 
value = (my_bytes[0] << 3)
print(value*lsb*4.01)

    # sending pulses with PWM
def tx_pulses(pi, GPIO, frequency, num, dutycycle=0.5):
    assert 1 <= frequency <= 500000
    assert 1 <= num <= 65535
    assert 0.0 <= dutycycle <= 1.0
    duration = int(1000000/frequency)
    on_micros = int(duration * dutycycle)
    num_low = num % 256
    num_high = num // 256
    wf = []
    #                           on      off    time
    wf.append(pigpio.pulse(1<<GPIO,       0, on_micros))
    wf.append(pigpio.pulse(      0, 1<<GPIO, duration - on_micros))
    pi.wave_add_generic(wf)
    wid = pi.wave_create()
    if wid >= 0:
        pi.wave_chain([255, 0, wid, 255, 1, num_low, num_high])
        while pi.wave_tx_busy():
            time.sleep(0.01)
        pi.wave_delete(wid)

...
#=======================================================
## FOR THE PWM
#=======================================================
pi = pigpio.pi()
GPIO_PWM=19
pi.set_mode(GPIO_PWM, pigpio.OUTPUT)


#=======================================================
## Read from ADC while PWM'ing
#=======================================================
lsb = 0.004  # 4.096/1024 
#process for PWM
pwm_p=Process(target=tx_pulses, args=(pi, GPIO_PWM, 1000, 65535))
pwm_p.start()
while(pwm_p.is_alive()):
    cb = pi.callback(19,pigpio.RISING_EDGE, read_adc(adc, lsb))

The goal is to read my ADC on a rising edge. However, my code exits after a few callbacks, and the pwm_p process is still alive. I know this because I have connected a scope to the GPIO output, and we still see the waveform. Any idea why?

1 Answer 1

3

As the code is written in your question, it is creating an unlimited number of callbacks. Eventually the system will fall over because of a lack of resources.

Also a pigpio callback receives a set number of parameters (being gpio, level, and tick). You can't pass the return value of a function call as a callback.

Change

while(pwm_p.is_alive()):
    cb = pi.callback(19,pigpio.RISING_EDGE, read_adc(adc, lsb))

to

def mycallback(gpio, level, tick):
   read_adc(adc, lsb)

cb = pi.callback(19,pigpio.RISING_EDGE, mycallback)
while(pwm_p.is_alive()):
   time.sleep(1)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.