Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 26th January 2005, 11:23 AM   #1
Default How to compare values?

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 is offline  
Old 26th January 2005, 11:48 AM   #2
Default Re: How to compare values?

Quote:
Originally Posted by Lac
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 26th January 2005, 12:07 PM   #3
Default

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 is offline  
Old 26th January 2005, 01:13 PM   #4
Default

Theory and code examples can be found on PICList.com... Here's one (of many);

http://www.piclist.com/techref/microchip/compcon.htm

Regards, Mike - K8LH
Mike, K8LH is offline  
Old 26th January 2005, 01:19 PM   #5
Default

Quote:
Originally Posted by Lac
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 26th January 2005, 02:26 PM   #6
Default

The piclist webpage is down? cant get in, loads for ever.
Lac is offline  
Old 26th January 2005, 02:40 PM   #7
Default

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
Mike, K8LH is offline  
Old 26th January 2005, 03:45 PM   #8
Default

Works fine here with IE6 as well, the actual page is http://www.piclist.com/techref/microchip/compcon.htm 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
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Tags
compare, values

Thread Tools
Display Modes




All times are GMT. The time now is 10:21 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker