PDA

View Full Version : ADC multiplex with 4051



RFsolution
- 19th March 2010, 21:18
Hi

I want to multiplex 2 ADC's (AN0 and AN1) I have 2 8 to 1 multiplexers
(CD4051) infront of the ADC's

S0,S1,S2 are on RC0-RC2
Enable of Mux 1 on RC4
Enable of mux 2 on RC3

I have declared an array of 16 Words

AD var word[16]

How can I with a for next loop:

Set the first 3 bits of portC and then read the ADC and write to the
respectively AD.x Word varialble ? with mux 1 enabled

After passing 8 cycles I need to change from Enable mux1 to Mux2
and read the other 8 remaining AD.x variables ?

any suggestions ?

rmteo
- 19th March 2010, 22:07
Use an inner and outer loop.

for i = 1 to 2
for j = 0 to 7
PORTC = j+(i*8) 'PORTC4:3 selects the multiplexer
'PORTC2:0 selects the channel
' Read ADC here into AD[j+((i-1)*8)]
next j
next i

RFsolution
- 19th March 2010, 22:58
Thank you

I tried it with some led's on the I/O and it is counting nicely

Then I added the ADC and have put a potmeter to AN0

But the result following LCD_overview4 is showing nothing
LCD_overview3 is showing the right value

AD var word[16]
adval0 Var Word


READ_ADC:

ADCIN 0, adval0

for i = 1 to 2
for j = 0 to 7
PORTC = j+(i*8) 'PORTC4:3 selects the multiplexer
'PORTC2:0 selects the channel
' Read ADC here into AD[j+((i-1)*8)]
pause 1
ADCIN 0,AD[j+((i-1)*8)]
pause 1
next j

next i

lcd_overview3:
Lcdout $fe, 1 ' Clear screen
LCDOUT "AD0: ",DEC4 adval0
return

lcd_overview4:
Lcdout $fe, 1 ' Clear screen
LCDOUT "AD0: ",DEC4 AD.0," AD1: ",DEC4 ad.1," AD2: ",DEC4 ad.2," AD3: ",DEC4 ad.3
LCDOUT $FE,$c0
LCDOUT "AD4: ",DEC4 AD.4," AD5: ",DEC4 ad.5," AD6: ",DEC4 ad.6," AD7: ",DEC4 ad.7
return

goto read_adc

rmteo
- 19th March 2010, 23:19
I'm guessing that you left out the square brackets for the array subscripts in the lcd_overview4 subroutine.

rmteo
- 19th March 2010, 23:47
After thinking a bit more, you should be able to use a single loop.

for i = 8 to 23
ADCIN 0, AD[i-8]
next i

RFsolution
- 20th March 2010, 00:20
Offcourse I forgot the brackets :rolleyes:

I made one error in my explanation

the first mux is on AN0 the second one AN1
so in the middle of the for nextloop i need to switch to ADCIN 1

Your single loop for ADCin0 and 1 is not clear to me

aratti
- 20th March 2010, 00:34
Your single loop for ADCin0 and 1 is not clear to me

rmteo has forgotten to set portC. Just add the red line and it will work. (I sugget you to use a small delay)


for i = 8 to 23
PortC = i
Pause 1

ADCIN 0, AD[i-8]
next i


S0,S1,S2 are on RC0-RC2
Enable of Mux 1 on RC4
Enable of mux 2 on RC3

Remember that as you have wired the system mux2 will start first (from 8 to 15) and then to follow mux1 will be activated (from 16 to 23). If you need them the other way around then swap the wiring of portC.3 & portC.4

Al.

rmteo
- 20th March 2010, 01:21
Offcourse I forgot the brackets :rolleyes:

I made one error in my explanation

the first mux is on AN0 the second one AN1
so in the middle of the for nextloop i need to switch to ADCIN 1

Your single loop for ADCin0 and 1 is not clear to me
Yes, I did leave out setting PORTC in my single loop example. If you are using AN0 and AN1, try this.

for i = 8 to 23
PORTC = i
if i < 16 then
ADCIN 0, AD[i-8]
else
ADCIN 1, AD[i-8]
next i
You can also use a single analog input by tying the outputs (Z pin) of the muxes together to AN0 then the code is simplified to this. Insert appropriate delays in both cases.

for i = 8 to 23
PORTC = i
ADCIN 0, AD[i-8]
next i