Dear Members

I have an 8mhz 12F683 monitoring a 2khz pwm signal. I am using pulsin to determine the high time and at 8mhz I get 5us resolution or 100 units over a 2khz 500us pulse. That's all fine.

I also have two rail to rail pots being monitored by 8 bits adc's and then multiplied to give 0-100 units resolution.
Again that's fine.

I am also using the pic HPWM to duplicate the incoming pwm data stream multiplying the incoming puslin data to provide the 8 bit hpwm data reqd. That's fine.

What I am doing is modifying the incomming pwm data using the two pots and then sending the modified pwm out of the hpwm.
The issue is my maths and logic seems horribly complicated.

Lets take a pwm signal with a high time of 51-90% I want to modify that with one of the pots called AssistPot so that

The first pot called AssistPot is only reqd to work on signals with a duty of 51-90%
1) When the pot is at 50% (in the middle) then it has no effect on the signal.
2) When it is 51-100% I want it to lengthen the pwm high time by 0-100% upto a maximum of 90% duty.
3) When it is 49-0% I want it to shorten the pwm high time by 0-100% down to a minimum of 51% duty.


The second pot called RegenPot does something similar but is reqd to only works on signals with a duty of 49-10%
1) When the pot is at 50% (in the middle) then it has no effect on the signal.
2) When it is 51-100% I want it to shorten the pwm high time by 0-100% down to a minimum of 10% duty.
3) When it is 49-0% I want it to lengthen the pwm high time by 0-100% upto to a maximum of 49% duty.

Now my code below works but feels clunky and I had to add a couple of checks to stop the maths going negative.
I think there must be a more elegant solution. The RegenPot maths seemed much harder.
I have it working using the code below and proteus simulates correctly. It also works with my hardware.
So I'm really looking for improvements, coding tricks or other brilliant insight :?

Code:
pulsin CmdPwrIn,1,CmdPwrHigh   'Measures length of Incoming CmdPwr 2khz Pwm Duty High pulse  
        
    If CmdPwrHigh > 50 then  'Assist Requested Duty > 50%
    PwrLevel = CmdPwrHigh - 50
    High Led     'Turn On Red Assist Led
    ADCIN 0,AssistPot           'Get AssistPot Setting 
    AssistPot = (AssistPot * 100) / 255
    If AssistPot > 50 then 
    AssistPot = AssistPot - 50
    Multiplier = 100 + ((AssistPot * 100) / (100 - AssistPot))
    PwrLevel = 50 + ((Pwrlevel * Multiplier)/100)        
    elseIf AssistPot < 50 then 
    AssistPot = 50 - AssistPot
    Multiplier = 100 + ((AssistPot * 100) / (100 - AssistPot))
    PwrLevel = 50 + (PwrLevel-(((Pwrlevel * Multiplier)/100)-Pwrlevel))            
    elseif AssistPot = 50 then
    AssistPot = 0
    PwrLevel = CmdPwrHigh
    endif      
    endif
    
   
    If CmdPwrHigh < 50 then  'Regen Requested Duty < 50%
    PwrLevel = 50 - CmdPwrHigh 
    low Led      'Turn On Green Regen Led 
    ADCIN 1,RegenPot            'Get RegenPot Setting      
    RegenPot = (RegenPot * 100) / 255
    If RegenPot > 50 then 
    RegenPot = RegenPot - 50
    Multiplier = 100 + ((RegenPot * 100) / (100 - RegenPot))
    General16 = ((Pwrlevel * Multiplier)/100) 
    if General16 > 50 then General16 = 50 'Prevents Result Negative Overflow error
    PwrLevel = 50 - General16     
    elseIf RegenPot < 50 then 
    RegenPot = 50 - RegenPot
    Multiplier = 100 + ((RegenPot * 100) / (100 - RegenPot))
    PwrLevel = 50 - (PwrLevel-(((Pwrlevel * Multiplier)/100)-PwrLevel)) 
    elseif RegenPot = 50 then
    RegenPot = 0
    PwrLevel = CmdPwrHigh 
    endif     
    endif
    
    If CmdPwrHigh = 50 then 
    TRISIO.4 = 1  'Turn Off Led  
    PwrLevel = 128   
    else  
    if Pwrlevel > 90 then PwrLevel = 90   'Prevent Assist request Error Duty > 90%
    if PwrLevel < 10 then PwrLevel = 10     'Prevent Regen request Error Duty < 10%        
    PwrLevel = (PwrLevel * 255) / 100     'Calculate HPWM Duty 8 bit data
    endif
Any Ideas?