PDA

View Full Version : Problem in Tension/compression



precision
- 11th July 2007, 12:15
This is my code for length measurements for fatigue testing machine with PEAK-HOLD.
I use 1000 PPR encoder with CH A and CH B for up/down, code working well when machine working in compression.
but problem is when machine work in tension side, encoder count 65535 below 00

i want this sequence

<<---------- 00 --------->>
Tension Compression
6-5-4-3-2-1-0-1-2-3-4-5-6

chip 16f88



DEFINE OSC 20
INCLUDE "MODEDEFS.BAS"
INCLUDE "MYLCD.BAS"

ANSEL = 0 'SELECT ANALOG INPUTS 0 = NONE AND ALL DIGITAL
ADCON0 = 0 'AD MODULE OFF & CONSUMES NO CURRENT
CMCON = 7 'COMPARATORS OFF

TRISA = %00000
TRISB = %00000011 ' INT0 = ENCODER CH. A
' PORTB.1 = ENCODER CH. B
W0 VAR WORD
B1 var word
W0 = 0
B1 = 0

PAUSE 500
'-------------------------------------------------------------------------
INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

OPTION_REG.6 = 0 ; 1 = Interrupt on rising edge of INT pin
; 0 = Interrupt on falling edge of INT pin

@ INT_ENABLE INT_INT ; enable external (INT) interrupts

'-------------------------------------------------------------------------


LOOP:
if W0 > B1 then B1 = W0 ' (Peak-Hold), Hold the last value of W0 in B1


LCDOUT $FE,1
LCDOUT "Length = ", dec5 w0, " mm"
lcdout $FE, $C0
lcdout "Peak- Value ",dec5 B1, " mm"
pause 500
GOTO LOOP

ToggleLED1:
if PORTB.1 = 0 then
W0 = W0+1
else
w0 = w0-1
IF (W0 < 0) THEN
W0 = 65535 + 1
endif
ENDIF

@ INT_RETURN

skimask
- 11th July 2007, 12:25
W0 VAR WORD

W0 = 65535 + 1

You have a word variable (16 bits)...
What's happens when you do the above math?
%1111111111111111 + %0000000000000001 = %10000000000000000
17 bits doesn't fit in a 16 bit variable.

An idea...divide all your numbers in half and use 32767 ($7FFF) as your new 'ZERO' point, change the LCD out routine to show that.

precision
- 11th July 2007, 13:18
When counts cross 65535, number will be 00000 and counts again upto 65535

but when i code
if W0 < 0 then W0 = 65535 + 1

after this counter will 00000 and hang on 00001

.

skimask
- 11th July 2007, 13:45
When counts cross 65535, number will be 00000 and counts again upto 65535
but when i code
if W0 < 0 then W0 = 65535 + 1
after this counter will 00000 and hang on 00001
.

That's my point...
W0 cannot be less than 'ZERO' with PBP...
PBP does not natively handle negative numbers using normal math routines. Yes there are a few commands that can handle signed integers, but not in the way you're looking to handle them. There are no 'minus' signs
You must figure out your own method to handle those negative numbers.

Darrel Taylor
- 12th July 2007, 01:36
precision,

Give this a try.

DEFINE OSC 20
INCLUDE "MODEDEFS.BAS"
INCLUDE "MYLCD.BAS"

ANSEL = 0 'SELECT ANALOG INPUTS 0 = NONE AND ALL DIGITAL
ADCON0 = 0 'AD MODULE OFF & CONSUMES NO CURRENT
CMCON = 7 'COMPARATORS OFF

TRISA = %00000
TRISB = %00000011 ' INT0 = ENCODER CH. A
' PORTB.1 = ENCODER CH. B
W0 VAR WORD
LastW0 VAR WORD
B1 var word
W0 = 0
B1 = 0

PAUSE 500
'-------------------------------------------------------------------------
INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

OPTION_REG.6 = 0 ; 1 = Interrupt on rising edge of INT pin
; 0 = Interrupt on falling edge of INT pin

@ INT_ENABLE INT_INT ; enable external (INT) interrupts

'-------------------------------------------------------------------------
LCDOUT $FE,1

LOOP:
IF W0 <> LastW0 then
LastW0 = W0
' (Peak-Hold), Hold the last value of W0 in B1
if ABS(LastW0) > ABS(B1) then B1 = LastW0

LCDOUT $FE,2
LCDOUT "Length = ", Sdec LastW0, " mm "
lcdout $FE, $C0
lcdout "Peak- Value ",Sdec B1, " mm "
ENDIF
GOTO LOOP

ToggleLED1:
if PORTB.1 = 0 then
W0 = W0+1
else
w0 = w0-1
ENDIF
@ INT_RETURN

The range will be +/-32767.

Added: Modified slightly to eliminate changes while displaying.

Modified again to simplify. ABS() works for both pos and neg numbers. Doh!
It also fixes a problem if it moves slightly negative before going positive.
I'll get it right one of these times :)
<br>

Darrel Taylor
- 12th July 2007, 02:44
Just posting this for an email notification that I changed the program above.

In case you're already trying it.
<br>

precision
- 12th July 2007, 05:05
Just posting this for an email notification that I changed the program above.

In case you're already trying it.
<br>

Sir Darrel Taylor
Thanks
Peak-hold now working well for both side.
But my problem is , i want to counting down side 0 to 1 ,2 , 3 ......... ( not 65535, 65534 , 65533 .......)


.

Darrel Taylor
- 12th July 2007, 05:15
You might want to just try the whole program I posted, instead of making the changes to yours. Because I think you've missed something.

With the SDEC modifier in the lcdout statements, it will read -1, -2, -3 etc. And will never show 65535.

There are several other subtle changes too.
<DT>

precision
- 12th July 2007, 06:19
Thanks it is work on lcd.
but on 7 seg. LED display , it is not work in +/- ,
this is my code



; CHIP 18F4550

DEFINE OSC 20
INCLUDE "MODEDEFS.BAS"
CMCON = 7
ADCON1 = 15
CVRCON = %00000000 'CVref turned off

TRISE = %000
TRISA = 11111
TRISB = %11111111
TRISC = %11111111
TRISD = %11111111


Symbol HC_latch = PORTE.2
symbol HC_Data = PORTE.1
SYMBOL HC_Clk = PORTE.0

B1 VAR BYTE
B2 VAR BYTE
B3 VAR BYTE
B4 VAR BYTE
B5 VAR BYTE
B6 VAR WORD

W0 VAR WORD
w3 var word
DUMMY VAR WORD

DIGIT1 VAR BYTE
DIGIT2 VAR BYTE
DIGIT3 VAR BYTE
DIGIT4 VAR BYTE
DIGIT5 VAR BYTE
DIGIT6 VAR BYTE

W1 VAR WORD
MASK VAR WORD

LastW0 VAR WORD

B1 = 0
B2 = 0
B3 = 0
B4 = 0
B5 = 0
W1 = 0
W0 = 0
w3 = 0
E1 VAR BYTE
E2 VAR BYTE
E3 VAR BYTE
E4 VAR BYTE
E5 VAR BYTE

PAUSE 500
'-------------------------------------------------------------------------
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT0_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT0_INT ; enable external (INT) interrupts@ INT_ENABLE INT1_INT ; enable external (INT) interrupts

'-------------------------------------------------------------------------


LOOP:
IF W0 <> LastW0 then
LastW0 = W0
if ABS(LastW0) > ABS(w3) then w3 = LastW0


B1 = LastW0 DIG 0
B2 = LastW0 DIG 1
B3 = LastW0 DIG 2
B4 = LastW0 DIG 3
B5 = LastW0 DIG 4


'------------------------------------------
DIGIT1 = B1
LOOKUP DIGIT1,[$C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90],MASK
E1 = MASK
'------------------------------------------
DIGIT2 = B2
LOOKUP DIGIT2,[$C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90],MASK
E2 = MASK
'-------------------------------------------
DIGIT3 = B3
LOOKUP DIGIT3,[$C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90],MASK
E3 = MASK
'----------------------------------------
DIGIT4 = B4
LOOKUP DIGIT4,[$C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90],MASK
E4 = MASK
'---------------------------------------
DIGIT5 = B5
LOOKUP DIGIT5,[$C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90],MASK
E5 = MASK
'---------------------------------------

HC_Latch=0
shiftout HC_data, HC_Clk, MSBFIRST,[E1,E2,E3,E4,E5,$C0]
pauseus 1
HC_Latch=1

'---------------------------------------
endif
GOTO LOOP

ToggleLED1: if PORTB.1 = 0 THEN
W0 = W0 + 1
ELSE
W0 = W0 - 1
ENDIF
@ INT_RETURN

Darrel Taylor
- 12th July 2007, 07:59
Oi vey!
Where'd the 7-segment come from?

Oh well.

B1 = ABS(LastW0) DIG 0
B2 = ABS(LastW0) DIG 1
B3 = ABS(LastW0) DIG 2
B4 = ABS(LastW0) DIG 3
B5 = ABS(LastW0) DIG 4

- or -



W4 = ABS(LastW0)
B1 = W4 DIG 0
B2 = W4 DIG 1
B3 = W4 DIG 2
B4 = W4 DIG 3
B5 = W4 DIG 4

<br>

precision
- 12th July 2007, 08:48
Oh..Thanks Sir Darrel Taylor.

working fine now. but how i show "-" sign for tension measurements.

One thing more , just for information.

my encoder is 1000 PPR
CHIP 18F4550
XTAL mount 20 mhz


__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
DEFINE OSC 20


I lose the count in > 5 RPM with above code.

And if mount XTAL 20 mhz
and if i code


__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
DEFINE OSC 3



this is work up to 20 RPM. No counts lose.
Is DEFINE OSC 3 will increase the speed of PIC ?

.

Darrel Taylor
- 12th July 2007, 09:26
18F4550
20Mhz crystal (22pF caps)

Use ...
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
DEFINE OSC 48
<br>

precision
- 13th July 2007, 08:53
how i show "-" sign for tension measurements. I mean when count below Zero.

.

Darrel Taylor
- 13th July 2007, 09:01
If LastW0.15 = 1 then the result is negative.

I'm not familiar with your display, so I don't know how to show the minus sign.
<br>

precision
- 13th July 2007, 10:15
Thanks a million Sir Darrel Taylor. My project is over.

i add this line,




If LastW0.15 = 1 then
E6 = $BF
ELSE
E6 = $FF
ENDIF

HC_Latch=0
shiftout HC_data, HC_Clk, MSBFIRST,[E1,E2,E3,E4,E5,E6]
pauseus 1
HC_Latch=1

'---------------------------------------
' endif
GOTO LOOP



Now my next step is "Calibration Factor" with Pitch of machine's screw.
I will start it Using Floating Point
Thanks again.

Darrel Taylor
- 13th July 2007, 12:50
I doubt that you need to use Floating Point.
It's a real pain.

Do you know the numbers yet?
pulses/mm or something like that?
<br>

precision
- 13th July 2007, 13:52
Sir my plan is,

When motor's pully moves 68 turns then Screw( worm gear ) turn 1 time.
Screw = 6 TPI ( then plateform moves 1 inch = 25.40 mm )
so 1 turn of screw = 25.40/6 = 4.233333 mm.
so 68 turns of pully = 4.233 mm
so 1 turn of pully = 4.233/68 = 0.062205882 mm

I will attach encoder of 6 PPR direct to motor pully.

code
pitch var word
pitch = 10368 ; (62208 / 6 = 10368)
dummy var word

loop: dummy = (pitch * ABS(lastw0))
w1 = div32 10000

so w1 = .01 mm least count
motor's 68 turns = encoder's 408 turns (10368 * 408 )/10000

so plateform moves 4.230 mm

This is my calculation

.

Darrel Taylor
- 13th July 2007, 14:29
Oh, OK.

I thought you meant the microchip Floating Point routines.

Sounds like you got it covered.
<br>