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.

Sampling multiple AD inputs Coding Help

Status
Not open for further replies.

Suraj143

Active Member
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)
		[COLOR="Red"]call	Del_Hold		;make a 40 uS delay after changing the channel[/COLOR]
		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)	
		[COLOR="Red"]call	Del_Hold		;make a 40 uS delay after changing the channel[/COLOR]
		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
		[COLOR="Red"]bsf	STATUS,RP0		;switch to bank1[/COLOR]
		movf	ADRESL,W
		[COLOR="Red"]bcf	STATUS,RP0		;back to bank0[/COLOR]
		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
 
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.
 
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?
 
bananasiong said:
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.
 
How high can it be? According to the datasheet of 16F88, the acquisition time with the source impedance of 10 k:eek:hm: is 19.72 :mu:s. But if the acquisition time required is 40 :mu:s, the source impedance will be around 32 k:eek:hm: :eek: (from my calculation). But the recommended is 2.5 k:eek:hm: max.
 
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.
 
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.
 
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.


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.
 
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 :mu:s, so the total conversion time is around 48 :mu:s which won't affect the seven segment display.
 
Last edited:
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?
 
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 said:
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.
 
bananasiong is it possible to sample two signals from external ADC at the same time on a PIC18XXXXXXX?
 
Not strictly speaking at the exact same time, but I see no reason why you couldn't get close enough, if you had enough pins.
 
Kryten said:
bananasiong is it possible to sample two signals from external ADC at the same time on a PIC18XXXXXXX?

You can if the ADC supports it. You won't be reading them into the PIC simultaneously though unless it has some crazy parallel or multi-serial interface (but you probably don't need to anyways).

A dsPIC for example has multiple sample-holds for it's ADC. It can capture all the samples at the same time but only uses one SAC register to convert them. So it samples them all at the same time, but only converts (ie. reads them) them sequentially. THey are all sampled at the same point in time though.
 
You don't need external ADC, just switching between two channels if time is allowed. Or if you want PIC18F2331 (or the same family) has two sample and hold which allow you to sample two channels simultaneously. But the conversion time channel by channel. It has 4 level FIFO so you don't have to worry about the data loss.
Depends on your sampling frequency, if you don't need that fast, just PIC16 will do by switching the channels.
 
Well im going to have at least 10 samples/sec (maybe more) so it will probaly be enough with a PIC16..
 
Status
Not open for further replies.

Latest threads

Back
Top