Thanks again for your ideas. I increased the encoder resolution to about 2500 pluses per inch of travel and counted the pulses. I seems to work quite well with good low speed movement. My sewing machine drive is a servo so I need to feed it with a frequency. I feed the PWM output to a filter and then to an ADC ( AD654) chip to give me 0 to 15kHz out.
Code:
'****************************************************************
'* Name : Pulse Count.BAS *
'* Author : Jan *
'* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 27/12/2013 *
'* Version : 1.0 *
'* Notes : Count pulses in *
'* : 16F88 *
'****************************************************************
Include "modedefs.bas" ' Include serial modes
define OSC 20
#CONFIG __config _CONFIG1, _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF & _CCPMX_RB3 & _CCP1_RB3
#ENDCONFIG 'Configure CCP1 (RB3) to PWM out
SO VAR PORTB.5 'Define serial out pin
P1 var PORTA.0 'Define input pin X
P2 VAR PORTA.1 'Define input pin Y
W1 var WORD
W2 VAR WORD
W3 VAR WORD
led var PORTB.0
TRISA = %11111111
ANSEL = %00000000
output led
low led 'set led off
displayloop:
count P1,10,W1
COUNT P2,10,W2
W3 = (W1 HYP W2) 'HYPOTENUSE CALCULATION
if W3 > 120 THEN 'Turn on lrd if Hypot is over
HIGH LED 'Turn on led on RB0
ENDIF
'if W3 < 100 then
'low led
'endif
Serout SO,N2400,[#W3]
disp: Serout SO,N2400,[" Pulses",13,10] ' Display trailer
hpwm 1,W3,2000
Goto DISPLAYLOOP ' Forever
Now I am trying to improve the design and I was told to try changing the PWM to a 50% duty cycle and very the frequency (hpwm 1,127,W5). This idea gives me my 15K but the lowest I can get is about 1.5KHz and I really need close to 0.
I am trying to come up with a scheme that will use Timer 0 quickly sample the PWM output and replicate the output with a 1,500 hz offset. Any ideas would be appreciated.
Code:
'****************************************************************
'* Name : Pulse Count.BAS *
'* Author : Jan *
'* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 27/12/2013 *
'* Version : 1.0 *
'* Notes : Count pulses in *
'* : 16F88 *
'****************************************************************
Include "modedefs.bas" ' Include serial modes
define OSC 20
#CONFIG __config _CONFIG1, _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF & _CCPMX_RB3 & _CCP1_RB3
#ENDCONFIG 'Configure CCP1 (RB3) to PWM out
SO VAR PORTB.5 'Define serial out pin
P1 var PORTA.0 'Define input pin X
P2 VAR PORTA.1 'Define input pin Y
W1 var WORD
W2 VAR WORD
W3 VAR WORD
W4 var word
W5 var word
led var PORTB.0
TRISA = %11111111
ANSEL = %00000000
output led
low led 'set led off
displayloop:
count P1,10,W1
COUNT P2,10,W2
W3 = (W1 HYP W2) 'HYPOTENUSE CALCULATION
if W3 > 120 THEN 'Turn on lrd if Hypot is over
HIGH LED 'Turn on led on RB0
ENDIF
W4 = (W3 + 20) 'Add 20 to overcome min pwm frequence
W5 = (W4 * 100) 'Times 100 to give min of 2 kHz
Serout SO,N2400,[#W5]
disp: Serout SO,N2400,[" Pulses",13,10] ' Display trailer
hpwm 1,127,W5
Goto DISPLAYLOOP ' Forever
Thanks Jan
Bookmarks