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.

How to compare values?

Status
Not open for further replies.

Lac

New Member
How can I compare a value from an ADC-reading to a set value? f.ex. the ADC has a range of 8 bit (0-255) and when the vaue is above a set value, f.ex. 200, one pin goes high, and when the value goes below 200 the pin goes low.

How can this be done in assembler, or what is the comands to comapre values. ie. higher than >, lower than <? thanks.
 
Lac said:
How can I compare a value from an ADC-reading to a set value? f.ex. the ADC has a range of 8 bit (0-255) and when the vaue is above a set value, f.ex. 200, one pin goes high, and when the value goes below 200 the pin goes low.

How can this be done in assembler, or what is the comands to comapre values. ie. higher than >, lower than <? thanks.

It all depends on the processor, on a PIC you use either subtraction, or XOR (for equality), and check the results on the flags affected. Other processors should have (will have!) similar features.
 
Jupp, its the 16LF877A chip, but how can I use the SUB and XOR commands to check if the value is higher than/lower than? I cant figure it out :? . thanks
 
Lac said:
Jupp, its the 16LF877A chip, but how can I use the SUB and XOR commands to check if the value is higher than/lower than? I cant figure it out :? . thanks

Check on the PICList, there's an entire section on comparisons.

But basically you do the subtraction and check the flags in the status register.

If you subtract 5 from 5, the answer is (obviously) zero, so the zero flag will be set.

If you subtract 6 from 5 the function overflows, and the carry will be affected (I can't remember which way off hand).

If you subtract 5 from 6, the answer is 1, and neither flag will be affected.

So by checking both C and Z, you can determine =, <>, <, >, <=, >=.

The PICList explains it all.
 
It's coming up fine on my FireFox browswer... Let me check IE...

It's coming up ok in Internet Explorer too... Not sure why you're not getting in... Sorry...

Regards, Mike - K8LH
 
Works fine here with IE6 as well, the actual page is but here's some examples off of it.

Code:
;if RAMx <= K
;Tony Nixon
  movf RAMx,w
  addlw 255 - K           ; eg if RAMx > 5 ... addlw d'250'
  skpnc
  goto Endif

;if K <= RAMx
;Tony Nixon
  movf RAMx,w
  addlw 255 - K + 1       ; eg if RAMx < 5 ... addlw d'251'
  skpc
  goto Endif

;if RAMx < K
; Tony Nixon
  movf RAMx,w
  addlw 255 - K + 1       ; eg if RAMx >= 5 ... addlw d'251'
  skpnc
  goto Endif

;if K < RAMx
; Tony Nixon
  movf RAMx,w
  addlw 255 - K           ; eg if RAMx <= 5 ... addlw d'250'
  skpc
  goto Endif

;if RAMx <= RAMy
; Scott Dattalo
  movf RAMx,w
  subwf RAMy,w
  skpc
  goto Endif

;if RAMy < RAMx
; Scott Dattalo
  movf RAMx,w
  subwf RAMy,w
  skpnc
  goto Endif
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top