pass address of SFR through parameter in func

Jenny
2007-03-16
2013-03-12
  • Jenny

    Jenny - 2007-03-16

    I want to know how address of SFR register can be passed as parameter to a function.

     
    • Todd Brown

      Todd Brown - 2007-03-27

      This worked for me:

      void doSomethingToSFR( __sfr* pSFR ) {
        *pSFR = 0xF;
      }

      void main() {
        doSomethingToSFR( P0 );
      }

      Note that there's no support for pointers to __sbits, so you can't do the above with, say, P0_0.

      -- Todd

       
    • Jenny

      Jenny - 2007-03-27

      Hi Todd,

      Thanks for your response. It is not working for me ..... I guess I'm doing it correctly.

      First the version of SDCC I'm using is "2.6.0 #4309 (Jul 28 2006) (UNIX)".

      I found out from the assembly code that it is loading the address of the SFR register to DPTR in the main. Inside the function the value of dpl and dph is loaded into r2 and r3, but there is no MOV instruction to with direct addressing.

      I have attached the C code and assembly code below. If you are able to look at it and find any problem, let me know.

      Thanks for your help.

      -Jenny.

      My code looks as below:
      C Code
      ----------
      void doSomethingToSFR (__sfr *pSFR) {
      *pSFR = 0x0f;
      }

      void main(){
      .
      .
      doSomethingToSFR(&ACAL_COUNTER_REG);
      /* without ampersand the compiler gives errors */
      .
      .
      }

      ACAL_COUNTER REG is defined as 'SFR(ACAL_COUNTER_REG, 0xC9)'

      Assembly Code (part of main)
      ----------------------------

                                 2458 ;       cal_fsm.c:580: doSomethingToSFR(&ACAL_COUNTER_REG);
                                 2459 ;       genCall
                                 2460 ;       Peephole 182.b  used 16 bit load of dptr
         05A7 90 00 C9           2461         mov     dptr,#0x00C9
         05AA 75 F0 00           2462         mov     b,#0x00
         05AD C0 02              2463         push    ar2
         05AF C0 04              2464         push    ar4
         05B1 12 06 30           2465         lcall   _doSomethingToSFR
         05B4 D0 04              2466         pop     ar4
         05B6 D0 02              2467         pop     ar2
                                 2468 ;       cal_fsm.c:581: func_acal_inc();
                                 2469 ;       genCall
         05B8 C0 02              2470         push    ar2
         05BA C0 04              2471         push    ar4
         05BC 12 07 91           2472         lcall   _func_acal_inc
         05BF D0 04              2473         pop     ar4
         05C1 D0 02              2474         pop     ar2

      Assembly Code (function)
      ----------------------------
         0630                    2686 _doSomethingToSFR:
                          0002   2687         ar2 = 0x02
                          0003   2688         ar3 = 0x03
                          0004   2689         ar4 = 0x04
                          0005   2690         ar5 = 0x05
                          0006   2691         ar6 = 0x06
                          0007   2692         ar7 = 0x07
                          0000   2693         ar0 = 0x00
                          0001   2694         ar1 = 0x01
                                 2695 ;       genReceive
                                 2696 ;       cal_fsm.c:654: *pSFR = 0x0f;
                                 2697 ;       genPointerSet
                                 2698 ;       genGenPointerSet
         0630 AA 82              2699         mov     r2,dpl
         0632 AB 83              2700         mov     r3,dph
         0634 AC F0              2701         mov     r4,b
                                 2702 ;       Peephole 238.d  removed 3 redundant moves
         0636 74 0F              2703         mov     a,#0x0F
                                 2704 ;       cal_fsm.c:655: return;
                                 2705 ;       genRet
                                 2706 ;       Peephole 253.b  replaced lcall/ret with ljmp
         0638 02 17 DA           2707         ljmp    __gptrput

       
    • Todd Brown

      Todd Brown - 2007-03-27

      Jenny,

      Sorry, I forgot the address-of operator (&) in my listing, but it looks like you figured out to add that.  I'll try this out again when I get home; something is definitely fishy about that assembler, but I can't put my finger on it.  It may be beyond my assembler ability, actually, so if anyone else wants to chime in, please do....

      -- T

       
    • Jenny

      Jenny - 2007-03-28

      Thanks Todd, appreciate your help a lot!

       
    • Maarten Brock

      Maarten Brock - 2007-03-28

      Jenny,

      If you had stated right away that you're using mcs51 I could've responded earlier that what you want is impossible. Using pointers is by definition using indirect accessing. SFR's only live in direct space on the 8051.

      What worries me is that the compiler did not generate a warning or error when it converted the sfr pointer to a generic pointer.

      Maarten

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks