PIC programming help in mikroC

Discussion in 'Embedded Systems and Microcontrollers' started by thebenman, Mar 16, 2014.

  1. thebenman

    Thread Starter New Member

    Jan 26, 2014
    27
    0
    Hey,
    I basically want to trigger the mosfets in a DC-DC converter.I simply want to program the PIC16F877A to generate PWM signals with a certain delay for triggering the mosfets.However,when i tried to run the program i got an error-"22 304 error: End of input within #if (#ifdef) section started at line 1 } 1 error in preprocessor. D:\PIC\final.c" .I have attached the code below too,

    Code ( (Unknown Language)):
    1.  
    2. #if defined (__PCB__)
    3. #include <16f877a.h>
    4. #fuses HS,NOWDT,NOPROTECT
    5. #use delay(clock=20000000)
    6. #elif defined(__PCM__)
    7. #include <16F877a.h>
    8. #fuses HS,NOWDT,NOPROTECT,NOLVP
    9. #use delay(clock=20000000)
    10. void main()
    11. {
    12. while(1)
    13. {
    14. output_d(0x55);
    15. delay_us(10);
    16. output_d(0x00);
    17. delay_us(2);
    18. output_d(0xaa);
    19. delay_us(10);
    20. output_d(0x00);
    21. delay_us(2);
    22. }
    23. }
    24.  
     
    Last edited by a moderator: Mar 16, 2014
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990

    Where is your #endif????
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    And this should be in the embedded forum.
     
  4. bertus

    Administrator

    Apr 5, 2008
    15,646
    2,345
    I have moved the thread

    Bertus
     
  5. thebenman

    Thread Starter New Member

    Jan 26, 2014
    27
    0
    Hey,
    Thanks for the quick reply,as i did not program this myself i am not quite sure where the end if statement must come.Could you please elaborate.

    Thanks.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    It probably goes just before the "void main()" line, but one never knows when borrowing the code exactly what anything really does, right?

    That's why people don't study how to borrow code, they study how to write code.
     
    Papabravo likes this.
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    Well said.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    Actually it probably does not go there either. What I would do is to remove the #if statement. That technique is used to avoid loading the header file more than once for a given source file. Obvious this is not a header file or at least it better not be.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    After careful consideration of your comments I still stand by mine.

    The technique you think you see is not being used here. The code is being conditionally compiled for two different platforms.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    You might be right. I was assuming #if defined (__PCB__) was the header file but after looking at it again I think you're correct.
     
  11. thebenman

    Thread Starter New Member

    Jan 26, 2014
    27
    0
    Yes,i do believe that it is being compiled for two different conditions follow the if statement and the elif statement.However,even as i read the mikroC documentation i am unable to find any reference to such statement as "defined" explained in the documentation.
     
  12. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Isn't so dramatic most C compilers support it this way.

    After all just a LVP fuse is set or not- how about to remove it alltogether?

    In the MPLABX documentation it actually can be found explained. I think even the assembler supports it.

    Not aware of any modern programming language (C related) that DOES NOT support it. It's similar to #include.
     
  13. thebenman

    Thread Starter New Member

    Jan 26, 2014
    27
    0
    Could you explain what exactly the statements before the void main() statement does ? That would help clear the error :)
     
  14. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    According to example here: http://en.wikipedia.org/wiki/C_preprocessor#Conditional_compilation

    #ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
    # include <unistd.h>
    #elif defined _WIN32 /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
    # include <windows.h>
    #endif

    You are missing #endif
     
  15. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    This code is not for MikroC...

    In MikroC, you include libraries from the pane on the right, you just checkmark them. The clock speed you select in the settings (or tools?) menu (I am at work now, don't remember exactly). There is not such thing as "#fuses" in mikroC...
    For "output_d", you will have to find the equivalent... is the microcontroller connected to a display? If you want to write a byte to the 8 pins on port D, then you just do:
    PORTD = 0x55;

    So you can remove ALL the pre-compiler code there (the code that starts with #), and you just leave the void main(), then it will work (it will compile at least), but you still need the correct settings for the microcontroller to actually make it run...
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990

    If you are asking a question like that then it is obvious you know absolutely nothing about C programming and answering that question will only lead to more questions. It is way beyond the scope of this forum to teach basic C programming.

    My suggestion is forget about micro controllers for now and lean C. There are many tutorials on the internet. Once you understand C then you can tackle micros.

    Here is are halfway decent lessons

    http://www.mikroe.com/products/view/285/book-pic-microcontrollers-programming-in-c/

    and in Mikro C.
     
  17. thebenman

    Thread Starter New Member

    Jan 26, 2014
    27
    0
    Hey,
    Yeah,the code is not for mikroC although i am still wondering on which compiler it might work.However,i had written an equivalent code on mikroC where i want the desiered PWM output from the 19,20th pin of 16F877A-each apart from each other by a delay of 200 milliseconds.I was not able to achieve my goal as both the PWM are generated at the same time when i ran it in proteus simulation.I am attaching the code below
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4.     PORTD=0;                   //Configure PORT D as output
    5.     TRISD=0;
    6.     PWM1_Init(50000);          //Set freq=50Khz
    7.     PWM2_Init(50000);
    8.     PWM1_Set_Duty(127);        //Set duty cycle=50%
    9.     PWM2_Set_Duty(127);
    10.     PWM1_Start();                       // start PWM1
    11.     Delay_ms(200);                      //aprart each other by 200 milliseconds
    12.     PWM2_Start();
    13.  
    14. }
     
  18. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    You have to create a loop to keep the microcontroller running, otherwise it will keep resetting.
    Something like:
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4.     PORTD=0;                   //Configure PORT D as output
    5.     TRISD=0;
    6.     PWM1_Init(50000);          //Set freq=50Khz
    7.     PWM2_Init(50000);
    8.     PWM1_Set_Duty(127);        //Set duty cycle=50%
    9.     PWM2_Set_Duty(127);
    10.     PWM1_Start();                       // start PWM1
    11.     Delay_ms(200);                      //aprart each other by 200 milliseconds
    12.     PWM2_Start();
    13.  
    14.     do{
    15.         Delay_ms(1);
    16.     }while(1);
    17. }
    18.  
     
  19. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    Removing the #if would be just as deadly because of the #elif still hanging in the breeze. With apologies to Bill Maher:

    New Rules:

    Don't borrow someone else's code, screw up the transcribing, and come here whining for help.
     
  20. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    This is true for many compilers, but not true for MikroC Pro, which automatically inserts "while(1);" or some equivalent at the end of every program.

    Still good practice to do it anyway, in case of porting to another compiler.

    Sorry to be pedantic :)

    ... but you make a good point, that the first 7 lines of code starting with '#' have no relevance to mikroC pro and need to be deleted.

    Luckily the OP is asking the same questions on the mikroE forum, where the ME experts are.
     
Loading...