kranenborg
Senior Member
Dear forum members,
Often one needs a predefined range of frequencies to be generated, the values steered by a potentiometer or similar sensor.
The following simple code for a 08M2 provides for a range of frequencies (with fixed 50% duty cycle), using the M2's PWM module. In the attached program, a range of 10kHz  60kHz is generated on the PWMoutput of the 08M2 (C.2), based on the 8bit ADC input at C.4.
Significantly lower frequencies can be generated by adding the PWMDIVxx keyword as the first parameter to the PWMOUT command in the program (or changing the M2's clock frequency). For example, using PWMDIV64 the range becomes 156Hz  938Hz. Higher frequencies are possible using SETFREQ m16 or m32.
Since some expressions need to be evaluated given the M2's 16bit arithmetic and ditto register size constraints, and the period parameter of the PWMOUT command has a byte size, the code is optimized to limit loss of information during mathematical operations, primarily multiplications and divisions. For example, the peculiar definitions of the min and max frequency range in the code facilitate maximal value range usage during expression evaluation in the registers (for example, generally one wants to do multiplications first up to values close to the 16bit max value (65535) in a register before applying a division operator). The code is properly documented.
I hope this provides for a quickanddirty way to generate frequencies on M2processors without any external hardware.
Regards, Jurjen
http://www.kranenborg.org/electronics
Edit: Attached file replaced due to small correction: period must be a byte variable
Often one needs a predefined range of frequencies to be generated, the values steered by a potentiometer or similar sensor.
The following simple code for a 08M2 provides for a range of frequencies (with fixed 50% duty cycle), using the M2's PWM module. In the attached program, a range of 10kHz  60kHz is generated on the PWMoutput of the 08M2 (C.2), based on the 8bit ADC input at C.4.
Significantly lower frequencies can be generated by adding the PWMDIVxx keyword as the first parameter to the PWMOUT command in the program (or changing the M2's clock frequency). For example, using PWMDIV64 the range becomes 156Hz  938Hz. Higher frequencies are possible using SETFREQ m16 or m32.
Since some expressions need to be evaluated given the M2's 16bit arithmetic and ditto register size constraints, and the period parameter of the PWMOUT command has a byte size, the code is optimized to limit loss of information during mathematical operations, primarily multiplications and divisions. For example, the peculiar definitions of the min and max frequency range in the code facilitate maximal value range usage during expression evaluation in the registers (for example, generally one wants to do multiplications first up to values close to the 16bit max value (65535) in a register before applying a division operator). The code is properly documented.
I hope this provides for a quickanddirty way to generate frequencies on M2processors without any external hardware.
Regards, Jurjen
http://www.kranenborg.org/electronics
Edit: Attached file replaced due to small correction: period must be a byte variable
Attachments

2.4 KB Views: 11
Last edited: