View previous topic :: View next topic |
Author |
Message |
BOB_SANTANA
Joined: 16 Oct 2006 Posts: 110 Location: HOVE, EAST SUSSEX
|
port mapping to vaiable |
Posted: Thu Nov 30, 2006 2:31 pm |
|
|
Hello All
I do need your help in explaining to me on how i can combine different ports pins to make up a variable.
i am using pic 16F877 on an existing project.
but i have only got 4 pins on porta (RA0-RA3) and also on portd (RD0-RD3) left on this pic.
Now i want to map those pins to an 8 bit variable so the i can use the variable to set the outputs of them as if it was a single port like portb.
i and thinking of something like
int8 myvariable;
myvariable bit 0= porta.0;
myvariable bit 1= porta.1;
myvariable bit 2= porta.2;
myvariable bit 3= porta.3;
myvariable bit 4= portd.0;
myvariable bit 5= portd.1;
myvariable bit 6= portd.2;
myvariable bit 7= portd.3;
If i define like those would that be right
any suggestions would be helpful i am a learner _________________ BOB_Santana |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
BOB_SANTANA
Joined: 16 Oct 2006 Posts: 110 Location: HOVE, EAST SUSSEX
|
|
Posted: Thu Nov 30, 2006 4:35 pm |
|
|
Thank you very much PCM
You have done it again ( i mean i have learnt something today)
this is what i learnt from you today
Code: |
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS) // Jumpers: 8 to 11, 7 to 1
// We want to be able to write a byte value to the following spare pins.
// RA0,RA1,RA2,RA3,RD0,RD1,RD2,RD3
// Define the bit addresses of the spare pins.
// The addresses shown below are for the 16F877.
#bit RA0 = 5.0
#bit RA1 = 5.1
#bit RA2 = 5.2
#bit RA3 = 5.3
#bit RD0 = 8.0
#bit RD1 = 8.1
#bit RD2 = 8.2
#bit RD3 = 8.3
// The following macro defines the "set_spare_bits()" function.
// This will be implemented by the compiler as eight sequential
// BSF or BCF instructions.
#define set_spare_bits(x) \
RA0 = x & 1; \
RA1 = (x >> 1) & 1; \
RA2 = (x >> 2) & 1; \
RA3 = (x >> 3) & 1; \
RD0 = (x >> 4) & 1; \
RD1 = (x >> 5) & 1; \
RD2 = (x >> 6) & 1; \
RD3 = (x >> 5) & 1
//==================================
void main()
{
int i;
// The TRIS must be set for the spare pins before the function is
// called. The spare pins must be set as outputs.
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_d(0b00000000);
for (i=0;i<255;i++)
{
set_spare_bits(i);
delay_ms(500);
set_spare_bits(0);
delay_ms(500);
}
while(1);
}
|
Best Regards _________________ BOB_Santana |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 30, 2006 4:42 pm |
|
|
Quote: |
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_d(0b00000000);
|
You've set the TRIS to all outputs for every bit in Ports A and D
(and also Port B), not just the ones that are used in the
set_spare_bits() function. Maybe that's what you want. |
|
|
BOB_SANTANA
Joined: 16 Oct 2006 Posts: 110 Location: HOVE, EAST SUSSEX
|
|
Posted: Fri Dec 01, 2006 6:07 am |
|
|
Yes i did know as i was only doing a quick test and also i spotted the typo
on
I have never used macro before so forgive me if i am asking for something simple
Can macro be only used on a byte ?
Can i modified it and use it for say 16bits _________________ BOB_Santana |
|
|
Ttelmah Guest
|
|
Posted: Fri Dec 01, 2006 7:06 am |
|
|
A 'Macro', is just a textual substitution. It is done before just about anything else. In itself, it doesn't have a 'size', it is completely dependant on the 'sizes' of the operation you select. You can do some quite powerful things with it. For example:
Code: |
union access_bytes {
int8 b[4];
int32 val;
};
int8 swap_tmp;
#define SWAP_LOW_BYTES32(x) {swap_tmp=((union access_bytes *)&x)->b[0];\
((union access_bytes *)&x)->b[0]=((union access_bytes *)&x)->b[0];\
((union access_bytes *)&x)->b[0]=swap_tmp;}
|
This gives you a function 'SWAP_LOW_BYTES32', which called with an int32, swaps the two low bytes. What happens is a whole new function block is generated, involving three lines. The first treats the int32 address, as if it is an address of a union, and grabs the low byte of this into the 'swap_tmp' variable. Then the second byte is copied to the first on the next line (the '\', means 'don't end the definition with the line feed), then the temporary value is swapped back.So there are some very powerful things that can be done this way.
Best Wishes |
|
|
|