PDA

View Full Version : Multiplexer channel selection



Castor
- 14th May 2008, 16:57
It's more of a conceptual question then a programming issue. I use a pic to control the inputs of a 16 channel multiplexer, thus allowing me to select a certain channel and read voltages from a multi cell battery pack. How do I inteligently cycle through the mux control inputs other then aliasing ports and attributing values ?



a_mux var porta.1
b_mux var porta.2
c_mux var porta.3
d_mux var porta.4
inh var porta.5

'select MUX channel X0
a_mux = 0:b_mux = 0:c_mux = 0:d_mux = 0:inh = 0
'select MUX channel X1
a_mux = 1:b_mux = 0:c_mux = 0:d_mux = 0:inh = 0
'select MUX channel X2
a_mux = 0:b_mux = 1:c_mux = 0:d_mux = 0:inh = 0
'select MUX channel X3
a_mux = 1:b_mux = 1:c_mux = 0:d_mux = 0:inh = 0
'and do this up to channel 15


It would be really useful to cycle using a for.. next loop and I belive it's quite possible because the multiplexer's truth table consists of consecutive binary numbers for the selection inputs. I'm thinking something like:



i var byte

a_mux var porta.1
b_mux var porta.2
c_mux var porta.3
d_mux var porta.4

i.7 var a_mux
i.6 var b_mux
i.5 var c_mux
i.4 var d_mux

for i= 0 to 15 ' meaning i=00000000
00000001
00000010

' do stuff
next i


But it doesn't compile well because quote "Variable already an alias". How do I get around this ? All the code above is just for demo purposes, I know TRIS is not set and the other stuff that goes into an actual program is missing

Archangel
- 14th May 2008, 17:24
Hello Castor,
I am not good at them,but I believe you are looking for an ARRAY.
see:http://www.picbasic.co.uk/forum/showthread.php?t=8486
http://www.google.com/custom?hl=en&cof=AH%3Aleft%3BS%3Ahttp%3A%2F%2Fwww.picbasic.co.u k%2Fforum%3BL%3Ahttp%3A%2F%2Fwww.crownhill.co.uk%2 Flogo.gif%3BLH%3A37%3BLW%3A174%3B&domains=picbasic.co.uk&q=input+array&btnG=Search&sitesearch=picbasic.co.uk

Acetronics2
- 14th May 2008, 17:57
hi,

your answer is "something like that"

... but as the PIC will perform the A/D conversion ... a Porta Analog input has some good chance to be in use ... if you do not use another ADC input ( PortE 0,1 or 2 for a 16F877a )


without any headaches ... it gives




Porta.5 = 0 ' Enable the Mpxer

for I = 0 to 15

Porta = (Porta & %11000001 ) + (I *2) ' Keep Porta.5 low, Porta.0,6,7 as is, and Porta1-4 select the input
Pause 5 ' to stabilize Mpxer Output voltage
ADCin Portx.y, value[I] ' Portx.y is where the Mpxer output is connected ...
'value[I] is the table location corresponding to the cell
NEXT I

.
.
.



too simple !

Alain

PS: May be someone could explain me why writing [] with UPPERCASE I inside the brackets show
[I] ...

skimask
- 14th May 2008, 18:13
PS: May be someone could explain me why writing [] with UPPERCASE I inside the brackets show
[I] ...

Italics vs. Italics

Italics vs. [ I ] Italics [ / I ]

http://www.phpbb.com/community/faq.php?mode=bbcode

Acetronics2
- 14th May 2008, 18:16
Italics vs. Italics

Italics vs. [ I ] Italics [ / I ]


Hi, Ski

Yes ... Ok, But ALSO inside a CODE window ...???

could we talk about a BUGGGGGGGGG ????

Alain

Castor
- 15th May 2008, 09:52
... but as the PIC will perform the A/D conversion ... a Porta Analog input has some good chance to be in use ... if you do not use another ADC input ( PortE 0,1 or 2 for a 16F877a )


Hey, thanks for the great answer.
It works like a charm. In fact I use a 16 bit external A/D converter so using PORTA pins as digital I/O doesn't pose an issue. What if the selection pins are scattered across two ports ? Can I get around that without selecting each and every output manually ? Or reworking the board ? (I didn't quite think about channel selection when I built it) :)


'DCD
'----------
'RE2 G1,G2
'RB7 A
'RB6 B
'RB5 C
'RC6 D

Acetronics2
- 15th May 2008, 10:06
What if the selection pins are scattered across two ports ?


Why not ...

you'll have to write two lines for Ports masking instead of one !



Can I get around that without selecting each and every output manually ? Or reworking the board ?



if you have a pot or button available and a free Pic input or two ... you can easily make the manual channel selection "by soft" ...




(I didn't quite think about channel selection when I built it) :)



but , may be, you'll have to add a small board on the existing one ( in french : une impériale !!! )

Alain

skimask
- 15th May 2008, 13:39
Yes ... Ok, But ALSO inside a CODE window ...???
could we talk about a BUGGGGGGGGG ????
I don't know if it's a bug or not. Maybe bbCode doesn't handle nested tokens, or at least that nested token correctly, maybe it was intended to be that way.

Castor
- 15th May 2008, 16:56
Please stay on topic.

Here's a summary: I have two pesky devices in my circuit. A multiplexer and a decoder.The multiplexer is connected as:

MUX
'------------
'RA5 INH
'RA1 A
'RA2 B
'RA3 C
'RA4 D
'------------

notice how the selection channels are on consecutive pins thus making things easy for selecting the channels:

'mux channel selection
'low inh_mux
'for i= 0 to 15
' Porta = (Porta & %11000001 ) + (i *2) 'port masking for cycle purposes
'next i

On the other hand the decoder pins are scattered across PORT B and PORTC:

'DCD
'----------
'RE2 G1,G2
'RB7 A
'RB6 B
'RB5 C
'RC6 D

here's the code i've devised:



'dcd channel selection
low g_dcd
for i=0 to 15
portb = (portb & %00011111 ) + (i rev 3)<<5
portc = (portc & %10111111 ) + (i<<3)
next i


The PORTB pins selection works fine (meaning lines A, B and C of the decoder behave as I expected). But line D (the only one on PORTC) gives me a headache. Am I missing something in the mask ?

skimask
- 15th May 2008, 17:27
Please stay on topic.
Guess what? Free help! Deal with it.



'dcd channel selection
low g_dcd
for i=0 to 15
portb = (portb & %00011111 ) | ( ( i rev 3 ) << 5 )
portc = (portc & %10111111 ) | ( i << 3 )
next i

OR don't ADD

Acetronics2
- 15th May 2008, 18:20
Guess what? Free help! Deal with it.



'dcd channel selection
low g_dcd

for i=0 to 15
portb = (portb & %00011111 ) | ( ( i rev 3 ) << 5 )
portc = (portc & %10111111 ) | ( i << 3 )
next i

OR don't ADD

to get rid of the i lower bits for portC ...

i would have used




portc = (portc & %10111111 ) | ( i.3 << 6 )



but, it's me ...

Alain

skimask
- 15th May 2008, 19:44
portc = (portc & %10111111 ) | ( i.3 << 6 )[/code]
I've tried to do something like that before. If I remember right, the fact that i.3 is a bit didn't work well with the << 6. It ended up shifting itself out into nowhere and always ended up being a '0'.
Maybe that was back in the day, in an older version of PBP or something. It just seems to me like that didn't work very well.

Acetronics2
- 16th May 2008, 09:17
Hi,

I did at test on the breadboard this morning ...

Everything fine ...

Alain

Castor
- 16th May 2008, 09:39
This is the winner:
portb = (portb & %00011111 ) | ( ( i rev 3 ) << 5 )
portc = (portc & %10111111 ) | ( i.3 << 6 )

I've tried both in MPLAB SIM but

portc = (portc & %10111111 ) | ( i << 3 ) doesn't mask the port right.

THANK YOU. Problem solved

Acetronics2
- 16th May 2008, 10:14
Good news !

Just for the fun ...

How many boards saved ???

Alain

Castor
- 16th May 2008, 11:46
uhm .. one :)

I'll post the code in the apropriate section when it's done. It's for a Li-Ion Battery Management System (BMS)/Balancer

Acetronics2
- 16th May 2008, 12:16
Hi, Castor

What is your device for ??? Modelling purposes ?

By the way, I realized a voltage warning device for transmitters ( for a Nicad to LiPo conversion ), some times ago, and, by the way noticed there's a not negligible tempco with those batts ...

doesn't simplify the measurements !!!

Regards from ... an ex Tegel " blue uniform " temporary citizen.

Alain

Castor
- 19th May 2008, 10:51
it's for an electric golf cart I'm building