mikroC and software C

Thread Starter

zak9000

Joined Jan 1, 2012
20
hi
i am completely new to programming with PIC's and at the moment i am using MikroC PRO to do my programming in C for a particular micro controller.so my question is that:

is their any difference between software C programming language and microcontroller base PIC c language because i have been having some problems with it for example the following code works with software C but does not compile on mikroC PRO for the pic micrcontroller:

int alarm[3]={1,2,3};
alarm[1]=4;

and you cant use functions like printf on it. can anyone explain this?
 

AlexR

Joined Jan 16, 2008
732
There are some differences between standard C and C for micro-controllers but all the basic C instructions should work in any micro-controller C and there is no reason why your array example should not work. Without seeing the context in which the instruction failed and any error messages it's impossible to offer a suggestion as to why it didn't work.


The main areas of difference between normal C and micro-controller C are;

Micro-controllers don't have standard input or output devices so standard C library functions such as scanf() and printf() are either left out of the compiler library or modified to access the serial port or an LCD display. there is no standard for what happens to redundant library functions and each compiler vendor does their own thing in this respect.

Many vendors provide library functions for commonly used modules such as SPI, I2C LCD displays, AtoD converts etc. Once more these functions are not covered by the ANSI standard so each vendor does it their own way and no 2 compiler are compatible with each other.

Micro-controllers do a lot of bit manipulation so many vendors provide their own extensions to the C language to allow direct bit access. Unfortunately each vendor does it their own way so there is no constancy across compilers and porting from one vendors compiler to a different compiler can be a real pain.
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
If you are used to something like C# or Visual C++, the syntax is the same, as covered above.

A new set of library routines proprietary for each different compiler are used to set and clear bits, to do conversions, to communicate with peripherals, etc.

The standard math is the same
Arrays, Unions, and structures are the same, though structures and unions aren't often used, they are available.

The two constant functions in every uC program are:

interrupt()
and
main()

Code starts running at main, and jumps into the interrupt loop when (if you set up interrupts) an interrupt occurs, such as a timer timing out, a button press, or a full UART buffer.
 

Thread Starter

zak9000

Joined Jan 1, 2012
20
ok thanks alot you have been really helpful but now i have run into another problem with regards to the array problem listed above if my code is simply:

int alarm[3]={1,2,3};
alarm[1]=4;
void main() {
}

the program will not compile and i get this error:
Compilation Started Alarm clock.c
; expected, but '=' found Alarm clock.c
'alarm' Identifier redefined Alarm clock.c
Specifier needed Alarm clock.c
Finished (with errors): 02 Jan 2012, 22:59:01 Alarm clock.mcppi

but if i change my code to:
int alarm[3]={1,2,3};
void main() {
alarm[1]=4;
}

then the program compiles succesfully

does that mean when i am using micro-controller c i can only redefine values for an identifier within the main function only?
 

bwack

Joined Nov 15, 2011
113
zak. In C, (global) declarations and definitions come first, then the runtime code.

alarm[1]=4; can mean two things, if its in the declaration part of your code (at the top), it means allocate one byte and write the value 4 to it. (but you wouldn't declare a one byte sized array). In the first code-snippet you are actually defining alarm twice and with different size, and thats why the compiler says "'alarm Identifier redefined...". This is not compiler dependent I think, its just dosn't make sence to redefine the size of the array at startup.

In your second code snippet, alarm[1]=4; is put in the runtime part of your code ( main() ) then "alarm[1]=4;" means: write the value 4 to position 1 of array alarm.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
does that mean when i am using micro-controller c i can only redefine values for an identifier within the main function only?
A code block is the area between the { and } brackets, and every function from main() to anything you define is a code block. (They are also used in other areas, such as if/else structures.)

No matter what computer you build the code for you cannot "redefine values for an identifier" outside of a code block, outside of the main() function or other any function you create.

The area outside of code blocks is meant for global definitions of symbols. Global symbols are available in that entire dot c file.

The area outside of code blocks is not to be used for assignments as it is not executed ever.
 

spinnaker

Joined Oct 29, 2009
7,830
does that mean when i am using micro-controller c i can only redefine values for an identifier within the main function only?
As Ernie says above this is true with just about every C compiler on every type of computer.

And not really just within the main function but any function. main is just a special function to the compiler.
 
Top