Hello,
I have been experimenting with the example files with my superpro-pro+ board and
basic. Does anyone have example basic code for polling an ADC and changing the
PWM duty cycle from 0 to 100% based on potentiometer value?
Best regards,
Nick
AD and HWPWM
-
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: AD and HWPWM
> I have been experimenting with the example files with my superpro-pro+ board
and basic. Does anyone have example basic code for polling an ADC and changing
the PWM duty cycle from 0 to 100% based on potentiometer value?
To start, just to take an AD reading and set the PWM would be as simple as
#include "HWPWM17.bas" ' which copies in the hardware PWM library for the
LPC175x
HWPWM (1, 4096, AD(2) >> 4)
This is fine for setting the PWM based on a full scale reading of the AD
converter, but you can't just put this is a while loop. Why, that's because you
can read the AD value about every 5 usec, and the PWM takes at least 4096
microseconds to complete a cycle.
How would you find these things out, take a look at both the BASIC source for
HWPWM which is in the BASIClib directory where the Coridium tools were
installed. And also detailed descriptions of the hardware peripherals of the
LPC1756 in the NXP user manual.
So back to making this loop. You could just put a delay in the while loop, of
say only updating every 100 ms
Or looking at the BASIClib file and the NXP user manual, the PWM is done using
counters and match registers. One match register sets the cycle time for the
PWM, that's MR0. And another match register is used to set the pulse time, for
channel 1 that's MR1
So once you initialize the PWM with the statements above, your update can be
done with
PWM1_MR1 = 4096 - (AD(2) >>4)
Now if you blindly do this you might end up setting MR1 to a value lower than
the present count so you'll get one very long pulse among the short ones you are
trying to program
So you'll want to look at the actual count before updating that MR1 and that is
PWM1_TC
and basic. Does anyone have example basic code for polling an ADC and changing
the PWM duty cycle from 0 to 100% based on potentiometer value?
To start, just to take an AD reading and set the PWM would be as simple as
#include "HWPWM17.bas" ' which copies in the hardware PWM library for the
LPC175x
HWPWM (1, 4096, AD(2) >> 4)
This is fine for setting the PWM based on a full scale reading of the AD
converter, but you can't just put this is a while loop. Why, that's because you
can read the AD value about every 5 usec, and the PWM takes at least 4096
microseconds to complete a cycle.
How would you find these things out, take a look at both the BASIC source for
HWPWM which is in the BASIClib directory where the Coridium tools were
installed. And also detailed descriptions of the hardware peripherals of the
LPC1756 in the NXP user manual.
So back to making this loop. You could just put a delay in the while loop, of
say only updating every 100 ms
Or looking at the BASIClib file and the NXP user manual, the PWM is done using
counters and match registers. One match register sets the cycle time for the
PWM, that's MR0. And another match register is used to set the pulse time, for
channel 1 that's MR1
So once you initialize the PWM with the statements above, your update can be
done with
PWM1_MR1 = 4096 - (AD(2) >>4)
Now if you blindly do this you might end up setting MR1 to a value lower than
the present count so you'll get one very long pulse among the short ones you are
trying to program
So you'll want to look at the actual count before updating that MR1 and that is
PWM1_TC