Mondalot
Junior Member level 3
Hello,
I want to regulate a motor with PWM and regulate I'm using a handlebar equipped with accelerator Hall effect (View photo).
The problem is the residual voltage of 0.8V.
The handlebar is fed to 5V.
At full throttle at the output provides a voltage 4.3V only.
Using this formula this means to regulate the PWM with 0.8V
I'm 84 steps of 255, without doing anything.
As I can make as smooth regulation of 0-255.
Without having to start 84.
Picture:
THX.
I want to regulate a motor with PWM and regulate I'm using a handlebar equipped with accelerator Hall effect (View photo).
The problem is the residual voltage of 0.8V.
The handlebar is fed to 5V.
At full throttle at the output provides a voltage 4.3V only.
Using this formula this means to regulate the PWM with 0.8V
I'm 84 steps of 255, without doing anything.
As I can make as smooth regulation of 0-255.
Without having to start 84.
Code:
Device = 18F26K22
Xtal = 16
Config_Start
FOSC = INTIO67
PLLCFG = On 'On ' Oscillator multiplied by 4
PRICLKEN = On ' Primary clock enabled
FCMEN = OFF ' Fail-Safe Clock Monitor disabled
IESO = Off ' Internal/External Oscillator Switchover mode disabled
PWRTEN = On ' Power up timer enabled
BOREN = SBORDIS ' Brown-out Reset enabled in hardware only (SBOREN is disabled)
BORV = 190 ' Brown Out Reset Voltage set to 1.90 V nominal
WDTEN = Off ' Watch dog timer is always disabled. SWDTEN has no effect.
WDTPS = 128 ' Watchdog Timer Postscale 1:128
CCP2MX = PORTC1 ' CCP2 input/output is multiplexed with RC1
PBADEN = Off ' PORTB<5:0> pins are configured as digital I/O on Reset
CCP3MX = PORTC6 ' P3A/CCP3 input/output is multiplexed with RC6
HFOFST = On ' HFINTOSC output and ready status are not delayed by the oscillator stable status
T3CMX = PORTC0 ' Timer3 Clock Input (T3CKI) is on RC0
P2BMX = PORTB5 ' ECCP2 B (P2B) is on RB5 (EXTMCLR = Enable MCLR/Disable RE Input)
MCLRE = INTMCLR ' MCLR pin enabled, RE3 input pin disabled (INTMCLR = Disable MCLR/Enabled RE Input)
STVREN = Off ' Stack full/underflow will not cause Reset
LVP = Off ' Single-Supply ICSP disabled
XINST = Off ' Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Debug = Off ' Disabled
Cp0 = Off ' Block 0 (000800-001FFFh) not code-protected
CP1 = Off ' Block 1 (002000-003FFFh) not code-protected
CP2 = Off ' Block 2 (004000-005FFFh) not code-protected
CP3 = Off ' Block 3 (006000-007FFFh) not code-protected
CPB = Off ' Boot block (000000-0007FFh) not code-protected
CPD = Off ' Data EEPROM not code-protected
WRT0 = Off ' Block 0 (000800-001FFFh) not write-protected
WRT1 = Off ' Block 1 (002000-003FFFh) not write-protected
WRT2 = Off ' Block 2 (004000-005FFFh) not write-protected
WRT3 = Off ' Block 3 (006000-007FFFh) not write-protected
WRTC = Off ' Configuration registers (300000-3000FFh) not write-protected
WRTB = Off ' Boot Block (000000-0007FFh) not write-protected
WRTD = Off ' Data EEPROM not write-protected
EBTR0 = Off ' Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
EBTR1 = Off ' Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
EBTR2 = Off ' Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
EBTR3 = Off ' Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
EBTRB = Off ' Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End
Declare Adin_Res = 10 ' Resolution 10 BITS
Declare Adin_Tad = FRC ' Oscilator RC AD
Declare Adin_Stime = 100 ' AD samples 100uS
Declare CCP1_Pin PORTC.2 ' Select Hpwm port and bit for CCP1 module (ch 1)
ANSELA = 00001111 ' Port A4-A7 Digital=0 / A0-A3 Analog
TRISA = 00101111 ' Port A I/O
ADCON1 = %1000000 ' Right justified
ADCON2 = %1000000 ' Right justified
Dim DUTY As Byte ' Duty PWM
Dim TENSION As ADRESL.Word ' Voltage
Dim VALOR_GAS As Float
Dim VOLT As Float
VOLT=0
DUTY=0
VALOR_GAS=0
CLS
Main:
GoSub ADC_Handlebar
Print At 4,6,Dec2 VOLT ," - ", Dec DUTY 'Display
HPWM 1,DUTY,20000
DelayMs 100
Goto Main
ADC_Handlebar:
TENSION = ADIn 1 ' Conversor AD
VALOR_GAS = 489 *(TENSION / 10) ' 489 = (5V/1023) 10Bits / Vref 5V
VOLT = (VALOR_GAS / 10000) ' Value in Volts
DUTY=(255/4.3)*VOLT ' Calculates PWM (4.3V full throttle not 5V )
Return
END
Picture:
THX.