This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

different port manipulation in one function

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

Parents
  • 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...?

Reply
  • 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...?

Children
No data