differences in coding for blinking of LED in mikro c and mplabx

Thread Starter

peter65

Joined Dec 4, 2016
18
please i want to know why the coding (e.g booking of led is different in mikro c and mplabx. I just bought a pickit3 that uses mplabx only to find out that some of d tutorials I've been reading on blinking code in mikro c cannot work on mplabx. I'm entirely a novice. thanks
 

spinnaker

Joined Oct 29, 2009
7,830
please i want to know why the coding (e.g booking of led is different in mikro c and mplabx. I just bought a pickit3 that uses mplabx only to find out that some of d tutorials I've been reading on blinking code in mikro c cannot work on mplabx. I'm entirely a novice. thanks

Differences are negligible at that level.
 

ErnieM

Joined Apr 24, 2011
8,377
Differences are tremendous at that level.

As someone just starting out you have a very limited bag of tricks to use. While both of these are good compilers they still have differences in how they reference things, such as the way they name bits in registers. Other details like MikroC hiding some important files to make it simpler to start off (which I find annoying but workable) will insure MikroC code cannot work in whatever Microchip compiler you are using.

Try to find a set of tutorials where there is a complete match: Learning material for the compiler you use with the same IDE (Integrated Development Environment) for the same chip you use, with a pre-built and tested circuit too.

It sounds like I am asking a lot, but if ANY link in the chain is broken you don't have a circuit, you have a brick.
 

spinnaker

Joined Oct 29, 2009
7,830
Differences are tremendous at that level.
Really???

Micro C example to blink LED

Code:
void main()
        {
        TRISB = 0 ;     // set PORTB as OUTPUT

        for(;;)         // forever
                {
                PORTB = 0xff ;          // turn all LEDs ON
                Delay_ms(500) ;         // wait 500 ms
                PORTB = 0 ;             // turn all LEDs OFF
                Delay_ms(500) ;         // wait 500 ms
                }
        }

XC8 example

Code:
TRISB = 0 ;     // set PORTB as OUTPUT

        for(;;)         // forever
        {
            PORTB = 0xff ;          // turn all LEDs ON
            __delay_ms(500) ;         // wait 500 ms
            PORTB = 0 ;             // turn all LEDs OFF
            __delay_ms(500) ;         // wait 500 ms
         }

If you want to call that "tremendous " then OK.
 

Thread Starter

peter65

Joined Dec 4, 2016
18
Differences are tremendous at that level.

As someone just starting out you have a very limited bag of tricks to use. While both of these are good compilers they still have differences in how they reference things, such as the way they name bits in registers. Other details like MikroC hiding some important files to make it simpler to start off (which I find annoying but workable) will insure MikroC code cannot work in whatever Microchip compiler you are using.

Try to find a set of tutorials where there is a complete match: Learning material for the compiler you use with the same IDE (Integrated Development Environment) for the same chip you use, with a pre-built and tested circuit too.

It sounds like I am asking a lot, but if ANY link in the chain is broken you don't have a circuit, you have a brick.
No u aren't asking a lot...thanks, at least you have given me a kinda stepping stone. just dat I used to think since it is the same embedded c language then all IDE's should not be different. I would just focus on mplabx tutorial for now. thanks
 

spinnaker

Joined Oct 29, 2009
7,830
Thanks for this one. pls is it the xc8 that has the LAT or the mikro c??

I am not that familiar with MikroC but I believe they do have LAT.

Your differences will be one you move on to accessing bits in the various registers.

For example in XC8

LATCbits.LATC1 = 1 will set the C1 register.

But both can set bit the "hard way".

For example LATC = 0x0000010 sets C1 too. It of course clears all of the other bits. You would need to get creative with OR to avoid that but you get the point.
 

ErnieM

Joined Apr 24, 2011
8,377
If you want to call that "tremendous " then OK.
I'll stick with "tremendous" as I am keeping the poor overwrought user in mind.

The delay calls or macros have different names. This will only compile with one not the other.

The XC compiler needs an include for the device, while MikroC hides this step in the IDE. This the code will not compile in XC.

Simple fixes for those with much experience with these compilers, show stoppers for newbies.

A devil lurks in these details.

@Peter: MPLAB is an environment that can accept many different compilers. Can you tell which compiler you are using here?
 

Thread Starter

peter65

Joined Dec 4, 2016
18
I'll stick with "tremendous" as I am keeping the poor overwrought user in mind.

The delay calls or macros have different names. This will only compile with one not the other.

The XC compiler needs an include for the device, while MikroC hides this step in the IDE. This the code will not compile in XC.

Simple fixes for those with much experience with these compilers, show stoppers for newbies.

A devil lurks in these details.

@Peter: MPLAB is an environment that can accept many different compilers. Can you tell which compiler you are using here?
well...i'm using the xc8 compiler
 

Thread Starter

peter65

Joined Dec 4, 2016
18
I'll stick with "tremendous" as I am keeping the poor overwrought user in mind.

The delay calls or macros have different names. This will only compile with one not the other.

The XC compiler needs an include for the device, while MikroC hides this step in the IDE. This the code will not compile in XC.

Simple fixes for those with much experience with these compilers, show stoppers for newbies.

A devil lurks in these details.

@Peter: MPLAB is an environment that can accept many different compilers. Can you tell which compiler you are using here?
The target chip is pic16f877A (just in case more info is needed) thanks
 

spinnaker

Joined Oct 29, 2009
7,830
well...i'm using the xc8 compiler

See this thread.

https://forum.allaboutcircuits.com/...ger-express-need-help-getting-started.141363/

Microchip has a really nice demo kit with lessons on how to use the kit.

The problem is the examples are in the older version of C from microchip. I am pretty certain the thread starter of that thread converted all of the examples over ti XC8. You might want to buy the kit and contact the starter of that thread for the converted examples.
 

Thread Starter

peter65

Joined Dec 4, 2016
18
I am not that familiar with MikroC but I believe they do have LAT.

Your differences will be one you move on to accessing bits in the various registers.

For example in XC8

LATCbits.LATC1 = 1 will set the C1 register.

But both can set bit the "hard way".

For example LATC = 0x0000010 sets C1 too. It of course clears all of the other bits. You would need to get creative with OR to avoid that but you get the point.
OK...I will try this out right away.
thanks
 

Thread Starter

peter65

Joined Dec 4, 2016
18
See this thread.

https://forum.allaboutcircuits.com/...ger-express-need-help-getting-started.141363/

Microchip has a really nice demo kit with lessons on how to use the kit.

The problem is the examples are in the older version of C from microchip. I am pretty certain the thread starter of that thread converted all of the examples over ti XC8. You might want to buy the kit and contact the starter of that thread for the converted examples.
wow! this is a great thread for starters...thanks very much spinnaker.let me just follow it up closely
 

Thread Starter

peter65

Joined Dec 4, 2016
18
I am not that familiar with MikroC but I believe they do have LAT.

Your differences will be one you move on to accessing bits in the various registers.

For example in XC8

LATCbits.LATC1 = 1 will set the C1 register.

But both can set bit the "hard way".


For example LATC = 0x0000010 sets C1 too. It of course clears all of the other bits. You would need to get creative with OR to avoid that but you get the point.
i just googled and i saw something like TRISB, TRISC ... and further reading on it shows that it can be used to set the registers too. this is somewhat confusing to me( i mean the multiple ways to give a particular instruction). please which is the most efficient??
 

spinnaker

Joined Oct 29, 2009
7,830
Your TRIS registers are to select if the pin is going to be an input or output. If the pin can also be an analog input then you may need to take the additional step of making it digital.

You really should try to read the datasheet for your Pic.
 

be80be

Joined Jul 5, 2008
2,072
I'll stick with "tremendous"
That depends with what chip you use.
People don't like change!!
It's like this setting up a chip in Mplab x can be and is harder then Mikro C
you can basically pic a chip add code posted in #5 and it will blink

Now head over to MPlab X
pick the same chip add the same code the one for MPlab X

nothing happens nothing nothing.

Why is code wrong No code is good
problem is you have to set up the chips osc which is a crystal by default
mikro C uses the inosc if the chip has one so it will blink.

Next you got dig out all the pin names and see what needs turned off or on.

pic16f877A doesn't have a LAT.

I really like this one Mplab X own examples half the time don't work because why?
I have 4.05 they most all are for older 3.xx below 3.65
 

be80be

Joined Jul 5, 2008
2,072
Try this out will work with the 16f877a out off the box in mplab x
Code:
*
* File:   blink.c
* Author: burt
*
* Created on December 5, 2017, 7:12 PM
*/
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 4000000
#include <xc.h>

void main(void) {
    TRISB0 = 0; //RB0 as Output PIN
 
  while(1)
  {
    RB0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    RB0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  }
    return;
}
 

Thread Starter

peter65

Joined Dec 4, 2016
18
Try this out will work with the 16f877a out off the box in mplab x
Code:
*
* File:   blink.c
* Author: burt
*
* Created on December 5, 2017, 7:12 PM
*/
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 4000000
#include <xc.h>

void main(void) {
    TRISB0 = 0; //RB0 as Output PIN
 
  while(1)
  {
    RB0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    RB0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  }
    return;
}
@be80be On adding- int main (void)I just tried this your code and it worked perfectly! . thanks very much
 
Top