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 21st January 2008, 02:35 AM   (permalink)
Question Sampling multiple AD inputs Coding Help

I’m sampling two AD channels every 1 second inside the ISR. My Xtal is 4 MHz & my AD conversion clock is set for Int RC.

When doing multiple channels there are some timing parameters required. I want to check my codings timing to get best AD results.

I want to know what I’m missing in my code or what I have to adjust.


Code:
		org	0x0004

Interrupt	movwf	W_Temp			;save W in a Temp
		swapf	STATUS,W		;
		movwf	S_Temp			;save status in a Temp		
		bcf	INTCON,T0IF		;reset T0IF int flag		
					
		decfsz	Count,F			;count 100 times to make 1 second
		goto	Exit
		movlw	.100
		movwf	Count			

			
Channel1	bcf	ADCON0,3		;turn on first AD channel (RA0)
		call	Del_Hold		;make a 40 uS delay after changing the channel
		call	Sample			;check the AD value & get a sample
		bcf	STATUS,C
		rrf	ADLResult,F		;make the value divide by 2
		call	BCD			;make the value into decimals
		call	BCD_Split1		;convert into segment digits 1 & 2	
					
Channel2	bsf	ADCON0,3		;turn on second AD channel (RA1)	
		call	Del_Hold		;make a 40 uS delay after changing the channel
		call	Sample			
		bcf	STATUS,C
		rrf	ADLResult,F		;make the value divide by 2
		call	BCD			;make the value into decimals
		call	BCD_Split2		;convert into segment digits 3 & 4


		
Exit		swapf	S_Temp,W
		movwf	STATUS
		swapf	W_Temp,F		;swap to file
		swapf	W_Temp,W		;swap to work
		retfie	


;***************
;AD subroutines
;***************


Sample		clrf	ADLResult		;clear the result 
		bsf	ADCON0,GO		;start convertion
		btfsc	ADCON0,GO
		goto	$-1
		bsf	STATUS,RP0		;switch to bank1
		movf	ADRESL,W
		bcf	STATUS,RP0		;back to bank0
		movwf	ADLResult		;place the 8 bit AD result
		return

				
Del_Hold	movlw	.12			;40 uS holding delay
		movwf	Hold
		decfsz	Hold,F
		goto	$-1
		return
Suraj143 is offline  
Reply With Quote
Old 21st January 2008, 09:36 AM   (permalink)
Default

I would suggest that's a very poor method?, it's best to keep ISR's as short as possible - use the ISR to set a flag, and check for the flag in the main program.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply With Quote
Old 21st January 2008, 02:59 PM   (permalink)
Default

It's really not a good idea for calling any delay in the ISR. Use interrupt to count, but not to use interrupt to perform ADC and delay.

What PIC are you using? Why do you need 40 us holding time which is so long?
__________________
Superman returns..
bananasiong is offline  
Reply With Quote
Old 21st January 2008, 03:07 PM   (permalink)
Default

Quote:
Originally Posted by bananasiong
What PIC are you using? Why do you need 40 us holding time which is so long?
I wouldn't say 40uS is a long time. The typical acquisition time for a pic is 20uS, if your source impedance is a little high then 40uS would probably be about right.

Mike.
Pommie is offline  
Reply With Quote
Old 21st January 2008, 03:39 PM   (permalink)
Default

How high can it be? According to the datasheet of 16F88, the acquisition time with the source impedance of 10 khm: is 19.72 µs. But if the acquisition time required is 40 µs, the source impedance will be around 32 khm: (from my calculation). But the recommended is 2.5 khm: max.
__________________
Superman returns..
bananasiong is offline  
Reply With Quote
Old 21st January 2008, 03:56 PM   (permalink)
Default

My point was that 40uS is not a long time and will definitely not cause any problems with the OP's code.

One problem that I do see is the lack of ADC initialisation. I assume that this is deliberate as there is no way that code will even build. Maybe Suraj should post his complete code.

Mike.
Pommie is offline  
Reply With Quote
Old 21st January 2008, 04:16 PM   (permalink)
Default

Yes, I agree that. Besides, it is two samples in 1 second, and it won't cause any problem.
Since 4 MHz is used, why RC is selected? Unless you want to perform ADC in sleep.
__________________
Superman returns..
bananasiong is offline  
Reply With Quote
Old 23rd January 2008, 02:56 AM   (permalink)
Default

HI I'm here here is my initialization part.

Code:
Init	bsf	STATUS,RP0
	clrf	TRISB
	clrf	TRISD
	movlw	b'00000011'	;make RA0,RA1 inputs (sensors)
	movwf	TRISA
	movlw	b'10001110'	;result right justified,Vref+=Vdd,Vref-=Vss
	movwf	ADCON1
	bcf	STATUS,RP0			
	movlw	b'11000001'	;ADON,Int RC
	movwf	ADCON0
My program works.Is it good?

Hi Mike !

I just want to ask your own idea what precautions do you take to minimize the bubble effect?I got some answers through past thread but I like your comments too.


Quote:
Unless you want to perform ADC in sleep.
Hi bananasiong I'm using seven segments sow can i do sleep & show the segments?If its an LCD no problem.
Suraj143 is offline  
Reply With Quote
Old 23rd January 2008, 03:35 AM   (permalink)
Default

Hi,

That's what I was wondering. Is there any reason for you to use the internal RC as the clock source for ADC? According to the datasheet, for higher oscillation frequency (more than 1 MHz), internal RC is only recommended for performing ADC during the sleep. So you can just derive from your fosc.

Besides, you can get the device into sleep just for the ADC operation, such as
Code:
bsf ADCON0, GO
sleep
When ADC is completed, the ADC complete interrupt will wake the device up.

*EDIT: Around 12 Tad for the conversion. If internal RC is used, Tad is 4 µs, so the total conversion time is around 48 µs which won't affect the seven segment display.
__________________
Superman returns..

Last edited by bananasiong; 23rd January 2008 at 03:39 AM.
bananasiong is offline  
Reply With Quote
Old 23rd January 2008, 03:48 AM   (permalink)
Default

Hi bananasiong thanks for that tip. I’ll try that also.

After calling few multiplexing routines in the main program I’ll put the PIC to sleep & take the latest readings & update the digits inside the ISR.

But when returning from ISR will it comes to the sleep line?
Suraj143 is offline  
Reply With Quote
Old 23rd January 2008, 03:50 AM   (permalink)
Default

No, it goes the instruction after the sleep. But for a good practice add a nop after the sleep.
__________________
Superman returns..
bananasiong is offline  
Reply With Quote
Old 23rd January 2008, 04:23 AM   (permalink)
Default

Quote:
Originally Posted by bananasiong
No, it goes the instruction after the sleep. But for a good practice add a nop after the sleep.
Oh I see thanks bananasiong.
Suraj143 is offline  
Reply With Quote
Old 24th January 2008, 05:51 AM   (permalink)
Default

hi suraj..
how's ur project ? did u manage to get rite source code? i'm having same prob like. i need u help. i need know hw program two input two analogue signal. thank u
tawa is offline  
Reply With Quote
Old 24th January 2008, 01:58 PM   (permalink)
Default

Quote:
Originally Posted by tawa
hi suraj..
how's ur project ? did u manage to get rite source code? i'm having same prob like. i need u help. i need know hw program two input two analogue signal. thank u
Basically you can't sample 2 channels simultaneously (very few PIC can). Just switch to another after one is completed by choosing the channels accordingly. Everything is in the datasheet.
__________________
Superman returns..
bananasiong is offline  
Reply With Quote
Old 5th February 2008, 08:11 PM   (permalink)
Default

bananasiong is it possible to sample two signals from external ADC at the same time on a PIC18XXXXXXX?
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
The Oscilloscope ElectroMaster Electronic Theory 12 3rd February 2008 01:45 PM
3 Inputs & 3 Outputs Coding problem Suraj143 Micro Controllers 16 17th August 2007 04:19 AM
Hardwiring multiple switches to common inputs Franknstein Electronic Projects Design/Ideas/Reviews 7 25th April 2007 11:02 AM
PIC A/D Sampling multiple inputs NJ Roadmap Micro Controllers 10 21st February 2007 01:53 AM
Multiple Units Transmission Coding Spasm Micro Controllers 4 18th March 2004 08:52 AM



All times are GMT. The time now is 10:55 PM.


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