PIC programming help in mikroC

Thread Starter

thebenman

Joined Jan 26, 2014
27
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,

Rich (BB code):
#if defined (__PCB__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#elif defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main()
{
while(1)
{
output_d(0x55);
delay_us(10);
output_d(0x00);
delay_us(2);
output_d(0xaa);
delay_us(10);
output_d(0x00);
delay_us(2);
}
}
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
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,

Rich (BB code):
#if defined (__PCB__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#elif defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main()
{
while(1)
{
output_d(0x55);
delay_us(10);
output_d(0x00);
delay_us(2);
output_d(0xaa);
delay_us(10);
output_d(0x00);
delay_us(2);
}
}

Where is your #endif????
 

Thread Starter

thebenman

Joined Jan 26, 2014
27
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.
 

ErnieM

Joined Apr 24, 2011
8,415
i did not program this myself i am not quite sure where the end if statement must come.
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.
 

spinnaker

Joined Oct 29, 2009
7,830
It probably goes just before the "void main()" line, but one never knows when borrowing the code exactly what anything really does, right?

.
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.
 

ErnieM

Joined Apr 24, 2011
8,415
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.
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.
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.
 

Thread Starter

thebenman

Joined Jan 26, 2014
27
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.
 

takao21203

Joined Apr 28, 2012
3,702
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.
 

CVMichael

Joined Aug 3, 2007
421
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,

Rich (BB code):
#if defined (__PCB__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#elif defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main()
{
while(1)
{
output_d(0x55);
delay_us(10);
output_d(0x00);
delay_us(2);
output_d(0xaa);
delay_us(10);
output_d(0x00);
delay_us(2);
}
}
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...
 

spinnaker

Joined Oct 29, 2009
7,830
Could you explain what exactly the statements before the void main() statement does ? That would help clear the error :)

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.
 

Thread Starter

thebenman

Joined Jan 26, 2014
27
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
Rich (BB code):
void main()
{
    PORTD=0;                   //Configure PORT D as output
    TRISD=0;
    PWM1_Init(50000);          //Set freq=50Khz
    PWM2_Init(50000);
    PWM1_Set_Duty(127);        //Set duty cycle=50%
    PWM2_Set_Duty(127);
    PWM1_Start();                       // start PWM1
    Delay_ms(200);                      //aprart each other by 200 milliseconds
    PWM2_Start();

}
 

CVMichael

Joined Aug 3, 2007
421
You have to create a loop to keep the microcontroller running, otherwise it will keep resetting.
Something like:
Rich (BB code):
void main()
{
    PORTD=0;                   //Configure PORT D as output
    TRISD=0;
    PWM1_Init(50000);          //Set freq=50Khz
    PWM2_Init(50000);
    PWM1_Set_Duty(127);        //Set duty cycle=50%
    PWM2_Set_Duty(127);
    PWM1_Start();                       // start PWM1
    Delay_ms(200);                      //aprart each other by 200 milliseconds
    PWM2_Start();

    do{
        Delay_ms(1);
    }while(1);
}
 

Papabravo

Joined Feb 24, 2006
22,111
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.
 

hexreader

Joined Apr 16, 2011
619
You have to create a loop to keep the microcontroller running, otherwise it will keep resetting.
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.
 
Top