PDA

View Full Version : Equating Ports to Data Bits



Osiris
- 21st March 2006, 02:18
Hi There,

I'm to this forum so I'd first like to say hi to all you people out there. My question seems rather trivial but I can't seem to figure it out. Basically I have a 16 bit bus which is scattered through PortA, B, and C on an 16F877A. I would like to define the 16 bit data and have each bit of the data automatically associated with the PortX.Y like so

BUS_DATA var word
BUS_DATA.0 = PortA.0
..etc

The problem is that I can't get it to work and had to do the following routine:

If BUS_DATA=1 then
PortA.0 = 1
Else
PortA.0 = 0
Endif
...etc

This is rather lengthy and eats lots of space. I've done it before were I define a byte like MY_INFO var PortE.0 but that's an entity on it's own. I can't seem to associate data bits with port bits.

Can anybody help me out?

Many thanks,
Osiris

sougata
- 21st March 2006, 05:19
Hi there,

If you have a variable say "PORTDATA" then it could be like this


PORTA.1 = PORTDATA.0

"" "" ""

PORTB.1 = PORTDATA.15


and so on. Or you can create bit alliases like


PORTA.1 VAR PORTDATA.7



and so on.

Osiris
- 22nd March 2006, 00:21
Hi Sougata,

Thanks for the tips. I tried them today and they do not seem to work for me. I know they should because it seems logical that they should work but somehow it's not working.

The first idea: PortA.1 = Data.0 compiled without error, the second one gave me lots of errors "redefinition of VAR".

It's strange but there must be a simple way to do this.

Many thanks,
Osiris

sougata
- 22nd March 2006, 05:21
Hi,

I would like to see the entire code and find out whats going on. Sometimes "typos" are hard to find.

Melanie
- 22nd March 2006, 06:59
Re the second example...

A Variable is a Variable, and a Port is a Port. You CAN'T define one as another which is why the second example is illegal. You CAN make a Port equal the state of a Variable, or you can make a Variable equal the state of a Port.

Going back to your problem... if you have arranged your 16-bit BUS illogically and without thought scattering it randomly across many Port Pins, then you can only gather it together with (worst case) sixteen individual statements like so...

Myword.0=PortA.3
Myword.1=PortB.7
Myword.2=PortB.3
Myword.3=PortB.5
Myword.4=PortA.0
Myword.5=PortC.4

and so on...

The best scenario was you had your 16-bit bus arriving sensibly at two PIC Ports and you caould have gathered it like so...

Myword.Lowbyte=PortB
Myword.Highbyte=PortC

You can of course redefine the name 'Myword', and you can redefine the Port names, but you CANNOT automatically associate a physical lump of Hardware (ie a Port pin) to a virtual object (ie a RAM variable).

Osiris
- 22nd March 2006, 13:52
Hi Melanie,

Thanks for the explanation. Indeed I had no choice but to scatter my bus around because some of my port bits are for dedicated hardware and since the pins have special features (like hardware interrupt, HUART, HPWM, etc) I had no choice but to scatter them about.

My worry is that I did try the following:

Myword Var word

Myword.0=PortA.3
Myword.1=PortB.7
...
Myword.15=PortC.4


But when I write the following statement it does not transfer the configuration to the port:

newdata var word

Newdata = 1234
Myword = Newdata

I would assume it would transfer the bit pattering from "newdata" to "myword" which would then output the bits on the associated ports.

It seems too simple of a problem and at the back of my head I have a feeling that I'm missing something very fundamental which does not allow the program to work.

Many thanks,
Osiris

Melanie
- 22nd March 2006, 15:00
In your first example...

Myword Var word

Myword.0=PortA.3
Myword.1=PortB.7
...
Myword.15=PortC.4

...you transfer the contents of the Port Pins into your variable.

Newdata = 1234
Myword = Newdata

Now the above two lines stuffs Myword with the contents of Newdata ie 1234.

If now you want that to appear on the Port Pins, you have to do the reverse of what you originally did... ie...

PortA.3=Myword.0
PortB.7=Myword.1
...
PortC.4=Myword.15

Remember Myword is a RAM variable, not a Port alias.