Hi :
Take a look at this function
sbit a=P0^1; void manipulate_port() { a=1; Delay(); a=0; Delay(); a=1; Delay(); a=0; }
In above code, the hardware(P0.1) is manipulated in the function manipulate_port(); Now I want to manipulate one of the pins of PORT0 according to different parameter. I can program eight different process with eight different pin manipulation and then select which process to be executed by a parameter. But I donot think it is a properly method. Should I only use one process to complete such goal. Because the manipulation is fixed(no matter which pin is use) So shall I program the process in such way.
sbit a0=P0^1; sbit a1=P0^2; ........ sbit a7=P0^7; void manipulate_port(uchar x) { // according different x, the a will be point to different pin,then execute the following a=1; Delay(); a=0; Delay(); a=1; Delay(); a=0; }
Is this resonable(or is this idea will be supported by keil)? If so ,how can I do it.
Thanks for your replying.
Jason Liu
Perform bitwise AND/OR operations on P0 with 'x' being the bit mask or a bit number used to create the bit mask.
// according different x, the a will be point to different pin,then execute the following
That part is impossible. The 8051 architecture doesn't have any thing that can "point to" a port pin. In terms of machine language there is no indirect addressing of bits or SFRs. On the C side that's why you can't take the address of an sbit (or SFR, for that matter), nor give anything the type pointer-to-sbit.
Thanks
But in actual situation, the pin be used is not only in one port,so I can not mask only one port depend on the x input. Is there any way else can be used.
Jason.Liu
Next step up: Pointer plus mask.
As already noted, the 8051 doesn't have any means to indirectly address the ports (or any other SFR) - so you can't have a pointer.
You could try passing an "index", and use that to select the appropriate port in a 'switch'...
switch( port_index ) { case port_0: // Do stuff with P0 break; case port_1: // Do stuff with P1 break; : : etc, etc,...
It may well not be worth it - just write specific functions for the specific pins you want to twiddle; maybe with macros...?
"But in actual situation, the pin be used is not only in one port..."
Hiding requirement details until later is a no-no. We normally charge a penalty fee when you do that. We'll let it slide this time.
Ports have a position in the address space, just like bits have a position within a byte.
The 8051 architecture doesn't have any thing that can "point to" a port pin. since the OP is oblivious to that fatc it is time for some reading for him.
doing ANYTHING with a '51 without reading "the bible" first is sheer folly
Erik
here are the links to "the bible" Chapter 1 - 80C51 Family Architecture: www.nxp.com/.../80C51_FAM_ARCH_1.pdf
Chapter 2 - 80C51 Family Programmer’s Guide and Instruction Set: www.nxp.com/.../80C51_FAM_PROG_GUIDE_1.pdf
Chapter 3 - 80C51 Family Hardware Description: www.nxp.com/.../80C51_FAM_HARDWARE_1.pdf