CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

port mapping to vaiable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

port mapping to vaiable
PostPosted: Thu Nov 30, 2006 2:31 pm     Reply with quote

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 Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 30, 2006 2:42 pm     Reply with quote

See the post near the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=18949&highlight=set_spare_bits
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Thu Nov 30, 2006 4:35 pm     Reply with quote

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 Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 30, 2006 4:42 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 01, 2006 6:07 am     Reply with quote

Yes i did know as i was only doing a quick test and also i spotted the typo
on
Code:
RD3 = (x >> 5) & 1


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 Smile
Ttelmah
Guest







PostPosted: Fri Dec 01, 2006 7:06 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group