Using Assembly in C

Discussion in 'Embedded Systems and Microcontrollers' started by johnny_sjs, Jul 26, 2011.

  1. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    Hello Friends,
    I am Johnny,
    I am using mplab, hi-tech PICC and pic16f877a microcontroller.

    Suppose in my C Code I have a variable x, I assigned it some value say 5,
    now I want to output this value on PORTB, for that I thought of 2 methods:

    1. C Code: code for dec to bcd
    2. ASM Code: assembly code and then use hex values and simply equate portb to x(I have some experience with 8051 asm programming).
    I want to know which one is better and how I can use assembly in C (like format or something)
    Thank You,
     
  2. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Here is a way to do that...
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4.   PORTB = 0x00; //Clear PORTB
    5.   TRISB = 0x00; //Config PORTB as digital output
    6.   int x = 5;
    7.   PORTB = x;
    8. }
    9.  
    Good Luck
     
  3. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    #include <htc.h>

    //device configuration bit
    //__CONFIG(0xFFBA); //LVP=ON; RB3 can't be used as an I/O PIN
    __CONFIG(0xFF3A); //LVP=OFF; RB3 can be used as an I/O PIN

    //define
    #define OUTPUT 0
    #define INPUT 1


    //main function
    //================================================================
    void main(void)
    {
    TRISB=OUTPUT;

    while(1)
    {
    #asm
    CLRF PORTB
    #endasm

    //short delay
    for(unsigned int i=0;i<30000;i+=1);

    #asm
    MOVLW 0xff
    MOVWF PORTB
    #endasm

    //short delay
    for(unsigned int i=0;i<30000;i+=1);
    }
    }
    compilation was successful, but while building its giving this error:
    Error [800] C:\Users\Johnny\AppData\Local\Temp\s61k.; 286. undefined symbol "PORTB"

    Please Help,
     
  4. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    @debjit625
    Thank you very much,
    but I would also like to know how add asm code in to C.
     
  5. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Your definition for INPUT is not proper.
    Code ( (Unknown Language)):
    1. #define INPUT 1
    Which means in binary "00000001" and that will not make all the pins of a port digital input.

    If you are using in-line assembly in HI TECH C and want to access local variables and SFR ,just use an underscore before the name of the identifier,by this way the compiler will not conflict between assembly and C identifiers under the same source code.

    Here is the way to do it...
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. //device configuration bit
    3. //__CONFIG(0xFFBA); //LVP=ON; RB3 can't be used as an I/O PIN
    4. __CONFIG(0xFF3A); //LVP=OFF; RB3 can be used as an I/O PIN
    5. //define
    6. #define OUTPUT 0x00
    7. #define INPUT 0xFF
    8.  
    9. //main function
    10. //================================================== ==============
    11. void main(void)
    12. {
    13. TRISB=OUTPUT;
    14. while(1)
    15. {
    16. #asm
    17. CLRF _PORTB
    18. #endasm
    19. //short delay
    20. for(unsigned int i=0;i<30000;i+=1);
    21. #asm
    22. MOVLW 0xff
    23. MOVWF _PORTB
    24. #endasm
    25. //short delay
    26. for(unsigned int i=0;i<30000;i+=1);
    27. }
    28. }
    29.  
    Good Luck
     
  6. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    thank you very much man!
    it worked !
    and for the INPUT one also.
    I wonder how my other programs were working though!
    Thanks again.
     
  7. blah2222

    Well-Known Member

    May 3, 2010
    554
    33
    Cool, so if you want to add in-line Assembly code into your C18 code you just have to add the #asm and #endasm tags before and after, respectively? No other pragma or config controls?
     
  8. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Their are two formats in HI TECH C to add in-line assembly in your C source code file.
    First one already discussed above using directives "#asm and #endasm",anyway here is how again
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4. int i = 0;
    5. //other C codes...
    6.  
    7. //Inline assembly starts
    8. #asm
    9. MOVLW 0x00
    10. NOP
    11. MOVLW 0xFF
    12. #endasm
    13. //end of inline assembly
    14.  
    15. //other C codes...
    16. }
    17.  
    The above method can't be used inside any C constructs such as while,if,do,for etc.
    To use inline assembly inside C constructs use this one...
    Using statement "asm()"
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4. int i = 0;
    5. //other C codes...
    6.  
    7. //Inline assembly statements
    8. asm("MOVLW 0x00");
    9. asm("NOP");
    10. asm("MOVLW 0xFF");
    11.  
    12. //other C codes...
    13. }
    14.  
    Actually if you look at the source code of your current program,then you will notice that it will work anyway, because you are not using the "INPUT" directive you are only using "OUTPUT" directives which you declared as 0 and in binary it is also zero i.e.. "00000000".

    Good Luck
     
Loading...