Inline assembly in XC8 compiler

Discussion in 'Embedded Systems and Microcontrollers' started by embpic, Apr 5, 2014.

  1. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    I am using XC8 compiler and when i try to implement inline assembly it gives error like syntax error. i was using
    #asm
    movwf,1
    #endasm

    likethis .
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    And what error do you get? As a hint check your command format are you sure movwf,1 is an assembler instruction that is valid
     
    Last edited: Apr 5, 2014
  3. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    main.c:51: error: syntax error

    and my code is

    Code ( (Unknown Language)):
    1. /*
    2.  * File:   main.c
    3.  *
    4.  *
    5.  * Created on April 4, 2014, 4:31 PM
    6.  */
    7. #include<xc.h>
    8. // CONFIG1L
    9. #pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
    10. #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
    11. #pragma config USBDIV = 2       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
    12.  
    13. // CONFIG1H
    14. #pragma config FOSC = HSPLL_HS  // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
    15. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
    16. #pragma config IESO = ON        // Internal/External Oscillator Switchover bit (Oscillator Switchover mode enabled)
    17.  
    18. // CONFIG2L
    19. #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
    20. #pragma config BOR = SOFT       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
    21. #pragma config BORV = 2         // Brown-out Reset Voltage bits ()
    22. #pragma config VREGEN = ON      // USB Voltage Regulator Enable bit (USB voltage regulator enabled)
    23.  
    24. // CONFIG2H
    25. #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
    26. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    27.  
    28. // CONFIG3H
    29. #pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    30. #pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
    31. #pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
    32. #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
    33.  
    34. // CONFIG4L
    35. #pragma config DEBUG = 1
    36. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    37. #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
    38. #pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
    39. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    40.  
    41. #define _XTAL_FREQ 48000000
    42.  
    43. void init_pwm(void);
    44.  
    45.  
    46. void main(void) {
    47.     init_pwm();
    48.  
    49. #asm
    50.     movwf,1
    51. #endasm
    52.     while(1);
    53. }
    54.  
    55. void init_pwm(void)
    56. {
    57.     TRISC = 0x00;
    58.     CCP2CON = 0x0C;
    59.     T2CON = 0x00;
    60.     PR2 = 0x44;         // frequency
    61.     CCPR2L = 0x0F;
    62.     TMR2 = 0x00;
    63.     TMR2ON = 1;
    64. }
    65.  
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Shouldn't it be

    MOVWF 1

    without a comma?
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You are onto something here;) Embic! Every PIC controller datasheet have a assembler command section with examples for every instruction. Use this documentation to get it correct. Also an important thing to remember is that then you enter an assembler section. You must ensure you are set to use the correct memory bank. For this you can use the BANKSEL directive. This is explained in the manual in the "MIXING C AND ASSEMBLY CODE" section
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Quote:Originally Posted by John P

    It is incidents like this that have left me a poor man.
     
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Not to be overly pendantic about it but when the OP fixes the comma problem, he will be writing an unknown value to 001h in RAM. On an 18F, this is access RAM, on enhanced midrange, it writes an indeterminate value to an indeterminate location (*FSR1), On midrange and baseline, it clobbers TMR0.

    As John P and to6afre wisely suggest, its time to get up to speed on the instruction set, memory organization and what resources are reserved by the compiler before using #asm.
     
  8. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    Thank you and sorry for silly mistake bcoz i have seen it after many days and mostly works on C so..
     
Loading...