Hot!Variable PWM

Author
Tuurbo46
Super Member
  • Total Posts : 218
  • Reward points : 0
  • Joined: 2004/09/12 05:05:45
  • Location: UK
  • Status: offline
2018/11/06 01:43:11 (permalink)
0

Variable PWM

Hi,
 
I would like to control a motor with a variable pwm signal, controlled from an adc input from a joystick.  I understand how to control the motor at one speed using a simple if statement, but i do not really understand how different adc inputs equate to variable pwm outputs.  Would i use a lookup table, so read input adc value, refer to lookup table, then select motor speed?
 
Look forward to your reply.
 
Thanks,
 
Tuurbo46.
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9673
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Variable PWM 2018/11/06 02:13:44 (permalink)
    0
    You tell us, what relationship do you want between the joystick input and the motor output.
     
    #2
    katela
    Super Member
    • Total Posts : 1194
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: Variable PWM 2018/11/06 04:47:49 (permalink)
    0
    The duty cycle can be proportional to the adc value.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #3
    KTrenholm
    Super Member
    • Total Posts : 655
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: Variable PWM 2018/11/06 07:53:40 (permalink)
    0
    A table lookup could work, but your resolution would be limited by table size.  If that's OK (you don't necessarily NEED a ton of resolution, it depends on the application), then you can absolutely go for a table lookup method.  It's fast to look something up in a table, but can be annoying to put a large lookup table together manually.
     
    You can make the relationship between the ADC value and the PWM DC any mathematical relationship you want.  If you want a simple scale from one range to another this map macro may be of use.

    #define map(x,in_min,in_max,out_min,out_max) ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)

     
    So for something like a 10-bit ADC and a PWM with a period value of 5000:

    #define MIN_ADC_VAL 0
    #define MAX_ADC_VAL 1024
    #define MIN_PWM_DC 0
    #define MAX_PWM_DC 5000
    uint16_t pwm_val,adc_val;
    adc_val = Read_ADC();
    pwm_val = map(adc_val,MIN_ADC_VAL,MAX_ADC_VAL,MIN_PWM_DC,MAX_PWM_DC);

     
    This would scale your input value to an equal point on your output range.
    #4
    Tuurbo46
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2004/09/12 05:05:45
    • Location: UK
    • Status: offline
    Re: Variable PWM 2018/11/06 09:19:34 (permalink)
    0
    Hi,
     
    Thanks for all of your help, I will go and have a read and experiment.
     
    Many thanks,
     
    Tuurbo46
    #5
    jtemples
    Super Member
    • Total Posts : 11100
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Variable PWM 2018/11/06 12:49:18 (permalink)
    +1 (1)
    It can be as simple as the ADC reading is the duty cycle (both are 10 bits); you don't need a table or any math at all.  Be aware that if you want full 10-bit resolution in the duty cycle, you have to select a frequency that supports that.
    #6
    mpgmike
    Senior Member
    • Total Posts : 113
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Variable PWM 2018/11/06 14:06:05 (permalink)
    0
    Using the Multi PIC Calc, different frequencies have different resolution.  Some are only 0 >> 63, some 0 >> 500, etc.  To directly port ADC value to PWM (CCPRx) value, you need a frequency that accepts the bit resolution of both ADC & PWM.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5