Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

weird results

Status
Not open for further replies.

VBguru

New Member
i can get good positive results from an encoder but my negative results from anything but a crawl are off... not sure how to fix it..... I have uploaded sample results from my project running



Total Var sword ' all vars are Swords so that they can be negetive.
Work var sword 'Work is low byte
Work2 var sword 'Work2 is high byte
neg var sword
oldval var sword

Work2 = 0
Work = 0
Total = 0
OE con P10
SEL con P11

High SEL
pause 1
Low OE 'select high and OE low starts reset
pause 1
High OE 'select high and OE high completes reset. Counter should be zero
Dirl = $FF 'sets 0-7 pins to input

Main
Low SEL ' starting action of reading HCTL
pause 1
low OE ' select low and OE low sets inhitit, and allows high byte to be read.
work2 = INL
pause 1

High SEL 'select high and OE low sets it to read low byte and starts the reset.
Work = INL

pause 1
High OE 'Select high and OE high completes the reset logic and Counter should be Zero.


'neg = (work2 * 256) + work 'combines high and low bytes. first attempt at it
neg = (work2 << 8) + work 'combines high and low bytes.
if neg > 2047 then 'Test for negitive
neg = neg - 4096 'If negitive then subtract 4096 to make number negitive
endif

Total = Total + neg 'Add final number to total for use in program (displaying in debug window)

if total <> oldval then
serout S_out,I38400,["Total = ",sdec Total," High = ",bin work2," Low = ",bin work," neg = ",bin neg,13]
oldval = total
endif

neg = 0
Large = 0 'resetting variable to zero
Small = 0 'resetting variable to zero

Goto main 'loop program
 

Attachments

  • negative.txt
    2.7 KB · Views: 180
  • Positive.txt
    3.9 KB · Views: 144
  • Code for Pic.txt
    1.4 KB · Views: 155
I can't realy follow your code. I would however hazard a guess that your error lies in having work (low byte) as a signed word. Try changing it to unsigned. You may also have to change your combine to "neg = (work2 << 8) or work".

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top