(MaxDuty - MinDuty) would be 250(2)-100= 150
this would make more sense if your pwm value is a byte
it now becomes
Code:MotorDuty = ADCInVAL */ 150(2) + 100
not knowing whats in the sub ChngMotorHPWM
snippets lead to speculation
(MaxDuty - MinDuty) would be 250(2)-100= 150
this would make more sense if your pwm value is a byte
it now becomes
Code:MotorDuty = ADCInVAL */ 150(2) + 100
not knowing whats in the sub ChngMotorHPWM
snippets lead to speculation
I'm using 8-bit resolution for PWM so it should be a byte but I have the variable defined as WORD in case the math takes it over 255:
Do you think the issue would be resolved if I change the datatype of MotorDuty to BYTE? I was also thinking I might switch to 10-bit resolution for both ADC and PWM to get the finest grain control of the motor speed, but that may be overkill.Code:MotorDuty VAR WORD ; Actual duty cycle for motor ChngMotorHPWM: CCP3CON.4 = MotorDuty.0 CCP3CON.5 = MotorDuty.1 CCPR3L = MotorDuty >> 2 CCP4CON.4 = MotorDuty.0 CCP4CON.5 = MotorDuty.1 CCPR4L = MotorDuty >> 2 RETURN
> Do you think the issue would be resolved....
What issue? What exactly is it that doesn't work the way you expect? What do you expect and what does it do?
75% dutycycle (8bit resolution) +/-25% from an 8bit ADC readin:191 is 75% of 255, your initial PWM duty cycle. When ADCvalue (BYTE) is 0 PWMDuty (BYTE) will be 255=100%. When ADCValue is 255 PWMDuty will be 127=50%.Code:PWMDuty = 191 + (64 - ADCValue >> 1)
/Henrik.
/Henrik.
The goal is to scale the ADC values of 0-255 to a range of duty cycles appropriate for my project (100-252 with the chosen prescaler). If I manually calculate the results are correct:
- ADCInVal =0 yields 100
- ADCInVal=127 yields 175
- ADCinVal=255 yields 252
But perhaps I don't have the order right in the expression because I don't see those results with the above code.
from excel chart, multiply adc val by .6 and add 100........ but can't mult decimal so multiply by 6 then divide by 10 then add 100
Last edited by amgen; - 29th October 2015 at 21:03.
Hi,
> but can't mult decimal
Sure you can, sort of, by using the */ or ** operators.
/Henrik.Code:PWMDuty = 100 + (ADCInValue ** 39322) ' Same as 100 + ADCInValue * 0.600006 but likely faster than 100+ADCInValue*6/10
Thanks Henrik! I'll try that tonight.
Bookmarks