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
 
Thread Tools Display Modes
Old 22nd March 2007, 12:54 PM   (permalink)
Default ADC reading

Ok so I need opinion on how to implement this.
I am reading an analogue voltage (GP0) on 12f675, and I need to trigger an LED if the results in ADRESH or ADRESL, are >greater then 3volts, LED turns off if in between 2.99v and 1.70v, but if it is less then 1.70volts, the LED must turn on again.
I have setup my ports to be GP0 and GP1 as Analogue input, and GP3 as digital.
I turned Comparators off etc...

I'm no math wizard, but I just wondered if this is possible without reading thru a table.
Maybe interrupt??
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 01:06 PM   (permalink)
Default

Quote:
Originally Posted by hjl4
I am reading an analogue voltage (GP0) on 12f675, and I need to trigger an LED if the results in ADRESH or ADRESL, are >greater then 3volts, LED turns off if in between 2.99v and 1.70v, but if it is less then 1.70volts, the LED must turn on again.
Simply compare the result of the A/D conversion with some constants. If the reference voltage is 5 V and you use 10-bit resolution, then 3 V = 0x266, etc.


Quote:
Originally Posted by hjl4
I have setup my ports to be GP0 and GP1 as Analogue input, and GP3 as digital.
Why two analog inputs? What's the function of GP3 exactly?
eng1 is offline   Reply With Quote
Old 22nd March 2007, 01:24 PM   (permalink)
Default

The reason for GP0 and GP1 are both analogue, is that I will be reading both ports one after the other and if any of those two separate readings are greater then or less then set number, then LED goes on. I'm reading two different probes, and if either one or both is out of range, LED(alarm) goes off until voltage returns to acceptable levels.


Quote:
Simply compare the result of the A/D conversion with some constants. If the reference voltage is 5 V and you use 10-bit resolution, then 3 V = 0x266, etc.
I don't quite understand how to do that, when there is a whole range to cover.

In any case,thanks for your reply.
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 01:26 PM   (permalink)
Default

What I meant, was that comparing to one constant is easy, but not so when you have a range to got thru.
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 01:28 PM   (permalink)
Default

I asked you about GP3 because it's input only. So, if you planned to connect the LED there, you have to change pin.

EDIT: are you using assembly or higher-level language?

Last edited by eng1; 22nd March 2007 at 01:31 PM.
eng1 is offline   Reply With Quote
Old 22nd March 2007, 01:39 PM   (permalink)
Default

Quote:
eng1 I asked you about GP3 because it's input only. So, if you planned to connect the LED there, you have to change pin.

EDIT: are you using assembly or higher-level language?
Ok I see, no GP3 is not used, and GP5 is LED port.

I am using assembly, and even after 2 yrs, I still have some issues, when it comes to compare or math.
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 01:53 PM   (permalink)
Default

Ok. I am going to use 8-bit resolution to make it simple. For example, if you want to compare the A/D result (stored in ADRESH) with 3 V, you can write:

Code:
;select bank
movf    ADRESH, w
;select bank
sublw   0x99  
btfss   STATUS, C
goto    LED_ON

Last edited by eng1; 22nd March 2007 at 01:56 PM.
eng1 is offline   Reply With Quote
Old 22nd March 2007, 01:56 PM   (permalink)
Default

Ok Yes I understand your code. I will try that. Thanks a bunch.
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 02:17 PM   (permalink)
Default

Try checking on the PICList, there's an entire section about comparisons - greater than, less than, greater than or equal - etc. There are also 8 and 16 bit versions, but for your purpose I would suggest configuring the A2D as 8 bit (one bit change in a register) and use 8 bit routines.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now   Reply With Quote
Old 22nd March 2007, 02:28 PM   (permalink)
Default

Ok thanks Nigel, I will check Piclist.
hjl4 is offline   Reply With Quote
Old 22nd March 2007, 03:06 PM   (permalink)
Default

The subtract instructions in the pic chips are not very sensible. For example sublw does not equal subtract literal from word as you would expect. It actually executes subtract word from literal.

With this in mind, I thought I'd practice a little 16 bit comparison and came up with this,

Code:
	movlw	low(0x015B)
	subwf	Word,W
	movlw	high(0x015B)
	btfss	STATUS,C
	movlw	high(0x015B)+1
	subwf	Word+1,W
;	carry clear if word is less than 0x15B
	btfss	STATUS,C
	goto	LEDon

	movlw	low(0x0266)
	subwf	Word,W
	movlw	high(0x0266)
	btfss	STATUS,C
	movlw	high(0x0266)+1
	subwf	Word+1,W
;	carry set if word is greater or equal to 0x266
	btfsc	STATUS,C
	goto	LEDon

; turn LED off here
Copy the value of ADRESL into Word and ADRESH into Word+1 and the above should do what you want. Or, change the Word variable to ADRESL & ADRESH with the appropriate bank switching.

Mike.
Edit, I had the led on at the wrong time.

Last edited by Pommie; 22nd March 2007 at 03:11 PM.
Pommie is offline   Reply With Quote
Old 22nd March 2007, 03:09 PM   (permalink)
Default

P.S. the values are the voltages.
So,
0x015b = 1.7V
0x0266 = 3.0V
Pommie is offline   Reply With Quote
Old 22nd March 2007, 03:41 PM   (permalink)
Default

Thanks Pommie, I will try that, and unfortunately I'm at work right now, so my replies are pretty slow and far apart.
hjl4 is offline   Reply With Quote
Old 25th March 2007, 12:53 AM   (permalink)
Default

Sorry to cut in,

But does this style of code work for 16F628 PIC's as well? I'm considering the same thing. Thanks
burg is offline   Reply With Quote
Old 25th March 2007, 01:30 AM   (permalink)
Default

It will work on any of the pic chips.

Mike.
Pommie is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Microcontroller interface with ADC revjam Chit-Chat 4 1st August 2008 08:21 AM
rms reading mpj111 General Electronics Chat 3 10th July 2006 12:36 PM
is reading lots of schematics the best way to learn? psasidisrcum General Electronics Chat 17 18th April 2005 08:57 PM
Specifications Applied Caltech General Electronics Chat 1 25th December 2003 05:00 PM
A/D help steady reading in low range but bounces in high usaf1 Micro Controllers 3 30th September 2003 01:14 AM



All times are GMT. The time now is 11:35 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.