labjack / LabJackPython

The official Python modules and classes for interacting with the LabJack U3, U6, UE9 and U12
https://labjack.com/support/software/examples/ud/labjackpython
MIT License
117 stars 79 forks source link

Output frequency on u3 #121

Closed j-c-cook closed 2 years ago

j-c-cook commented 2 years ago

I am using the LabJack U3-LV to generate a square frequency wave. I am having trouble. I have consulted the frequency list and learned what I could from the pwm-looping example.

I cannot get the frequency to output anything but 60.9 Hz. The combination of base clock, clock divisor and timer value should be outputting 1500 Hz based on the frequency list. Does anyone know how I can fix this?

import u3
import time

def main():
    d = u3.U3()  # Open Labjack u3 low voltage device

    # Define inputs
    base_clock = 12000000
    clock_divisor = 16
    timer_value = 250

    # Configure timer
    d.configTimerClock(TimerClockBase=base_clock,
                       TimerClockDivisor=clock_divisor)
    d.configIO(TimerCounterPinOffset=4, NumberOfTimersEnabled=1)  # FIO4
    d.getFeedback(u3.Timer0Config(TimerMode=7, Value=timer_value))  # U3
    d.writeRegister(addr=7100, value=timer_value)

    time.sleep(5)  # hold for 10 seconds

    d.getFeedback(u3.Timer0(Value=0, UpdateReset=True))  # U3
    d.writeRegister(7200, 0)

    d.close()  # Close u3

if __name__ == '__main__':
    main()
sjarman28 commented 2 years ago

This is not an issue or bug in LabJackPython. In the future, please direct any support inquiries to us (LabJack) via one of the contact methods described on our website such as email (support@labjack.com) or live chat: https://labjack.com/contact

The timer clock base you are using is not valid, you must pass a clock base index instead of the clock speed. See our timer/counter documentation. The clock base options are described in Table 2.9-2. You would use index 5 for 12MHz with divisor: https://labjack.com/support/datasheets/u3/hardware-description/timers-counters

Also note that you are doing a timer configuration command and timer update command twice, I would recommend removing your d.writeRegister calls.

Also note that 0 is not a valid timer value, only 1-256 are. [Edit: 0 is valid, but it just means 256 divisor]. To disable the feature you should disable the timer. You could use configIO: d.configIO(NumberOfTimersEnabled=0)