Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Difference between C & Embedded C

Status
Not open for further replies.

nirmala h

Member level 1
Member level 1
Joined
Jan 18, 2011
Messages
36
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Location
Bangalore, India
Visit site
Activity points
1,530
Hi all,

What is the difference between C & Embedded C ?And also solve this program

To generate 100 Fibbonacci no's in C will write as

while(n<100)
{
r=p+q;
p=q;
q=r;
printf("%d",r);
}

How do we write in Embedded...?
 

1.h**p://www.keil.com/forum/11466/


Embedded C is Actually a Set of Extensions for the C programing language

Embedded C Miachel J pont is a good text
 
  • Like
Reactions: nirmala h

    V

    Points: 2
    Helpful Answer Positive Rating

    nirmala h

    Points: 2
    Helpful Answer Positive Rating
Basically embedded C language is used to write the programming for micro controllers of course we can programming the micro controllers using C..but the difference is some the built in functions available in embedded C that can be suitable to micro controller architecture...
 
What is the difference between C & Embedded C ?And also solve this program

To generate 100 Fibbonacci no's in C will write as

How do we write in Embedded...?

While Embedded C compiler do have extensions to aid in embedded development, Embedded C is more of a subset of the Standard C language. For instance many of the Embedded C Compilers do not support the full Standard C Library and if the functions do exist they are limited in comparison to their counterparts found in the Standard C Library. These differences are compiler dependent and vary from one compiler platform to another, which is why referencing the documentation of a particular Embedded C Compiler is critical.

Case in point, the Fibonacci sequence program, you listed above contains the printf() function which may or may not exist in a particular Embedded C Compiler's Library. And when used in a DOS, Windows or LINUX program the output would be normally directed to the console or terminal window, the Standard Output or STDOUT.

Code:
while(n<100)
{
     r=p+q;
     p=q;
     q=r;
     printf("%d",r);
}

In an embedded system, this STDOUT may also be a terminal window, LCD, GLCD or some other output device. Let us assume the STDOUT is a hyperterminal window on a Windows based PC. The embedded version of the program would look something like the following:

Code:
#include <stdio.h>
#include "serialport.h"

void main(void)
{  
    unsigned long r, p, q;      // may vary depending on compiler supported integer variable types
    
    n=1;
    p=0;
    q=1;
    r=0;

    initport(PORT1, 9600, 8, N, 1);   //initial serial port connection to PC

    while(n<100)
    {
        r=p+q;
        p=q;
        q=r;
        printf(" %u ",r);   // STDOUT defined in header file
        n++;
    }

    while(1);   // necessary to prevent program exit, no OS present

}

Obviously, the details of setting up the serial port and defining STDOUT are compiler and platform dependent. However, the general structure of this embedded example should give you a clearer picture of what is involved.

BigDog
 
Hi yanamaddinaveen,

could you elaborate this statement (built in functions available in embedded C)..
Did you mean in-built Timers, ADC's, Comparators etc..
 

Hi Nirmala,

I believe what yanamaddinaveen was speaking of is the library functions which are provided by a particular compiler. These library functions can include routines to use a LCD display, Serial Port, CAN and access various types of external devices through I2C, SPI, etc.

The availability of the functions are C compiler dependent.

BigDog
 
Hi bigdogguru,

In Embedded printf is not used, so could you tell me how to write the code..And where you are storing all the generated no's..
 

Dunno about Keil Embedded-C, but in a more generic sense there are several variants of C language (mostly a subset, with some additional pragmas) and some device specific library or set of libraries. Thus, you might find API like glcd_write(), glcd_print(), deviceprint(), deviceout(), and mostly they do not support rich formatting options like printf(). The capabilities and API would depend on specific device's abilities, and may take some device specific options, for example some API that would allow a Marquee scrolling, some that do automatic line wrap, and some which will ignore extra character at head or tail of display string. AFAIK, there is nothing standard.
 
In Embedded printf is not used, so could you tell me how to write the code..And where you are storing all the generated no's..

It would depend on your output device, LCD, Serial Port, etc. It can be quite complicated, which why most embedded programmers create their own library of functions.

Why reinvent the wheel every time you need to carry out a certain task?

If printf() were not available you would check your compiler documentation to see if it provided an alternative library function to display information on the particular device in the design.

Here's an example of a program written using Hi-Tech PICC18 C Compiler for a Microchip PIC18 MCU which displays information on a LCD screen received from a serial connection to a PC:

Code:
#include <htc.h>

void main( void )
{
         char data;
         // configure external LCD
        OpenXLCD( EIGHT_BIT & LINES_5X7 );
        // configure USART
        OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF &
        USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX,
        25);
    while(1)
    {
        while(!DataRdyUSART()); //wait for data
          data = ReadUSART(); //read data
        WriteDataXLCD(data); //write to LCD
        if(data==’Q’)
          break;
    }
   CloseUSART();
}

BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating

    veenashree89

    Points: 2
    Helpful Answer Positive Rating

    FvM

    Points: 2
    Helpful Answer Positive Rating
everything is true ... but usually we dont develop fibonacci, factorial of number and these kind of programs in embedded as embedded has programs for specific application... it purely depends on the IDE and compiler syntax on how to write them.... data are stored in flash or eeproms.. printf are usually not used unless using an UART, as the output is seen on display device and not on any specific console like pc monitor.. printf and scanf are used much.... these libraries take huge amount of memory while using stdio.h

which is usually not recommended...

---------- Post added at 11:22 ---------- Previous post was at 11:21 ----------

these programs are good for compilers and learning purpose, but not for cross compilers...
 
And where you are storing all the generated no's..


The variables containing the values would be stored in RAM, the program is stored in FLASH and there is EEPROM available for nonvolatile storage of values. Nonvolatile means the values are not lost when the microcontroller is power down.

Here is a block diagram of a typical PIC microcontroller, you can see where the various memory storage areas, RAM, FLASH and EEPROM, are located:



BigDog
 
Hi ckshivaram

everything is true ... but usually we dont develop fibonacci, factorial of number and these kind of programs in embedded as embedded has programs for specific application... it purely depends on the IDE and compiler syntax on how to write them.... data are stored in flash or eeproms.. printf are usually not used unless using an UART, as the output is seen on display device and not on any specific console like pc monitor.. printf and scanf are used much.... these libraries take huge amount of memory while using stdio.h

which is usually not recommended...

---------- Post added at 11:22 ---------- Previous post was at 11:21 ----------

these programs are good for compilers and learning purpose, but not for cross compilers...

This question is asked in Interview to write using Embedded C & i failed to answer..
So, i was searching for the answer.
 

Hi nirmala,

but these kind of questions are asked in the interview i am really surprised... Never came across anyone asking such questions in my career.. maybe your bad luck..

if you are good in concepts of storing data in RAM of controller then this program can be written. you should use indirect addressing mode and increment the RAM address after every iteration and store it in RAM location.. They might have expected to see your skills in using different addressing modes of 8051 using embedded C.
There is nothing much difference between C and embedded C except embedded C supports hardware with the help of few header files, and few changes in pointers concept like generic pointers, memory sspecific pointers and pointer abstraction and reentrant functions, bit manipulations, accessing hardware registers etc.......

Anyhow all the best for your future..
 

A important difference in a Embedded target C compiler is the optimization logic. Code logic changes that in a userland application would make no difference in operation could completely change the behavior of a program that's talking directly to hardware, so a major part of the compiler package is the 'machine dependent/independent' matrix that usually configured in the header files with the use of the 'volatile' storage keyword.

Embedded Systems/C Programming - Wikibooks, open books for an open world
**broken link removed**
 

hello I have also question about some syntax in embedded C :)

I usually declare a variable inside a ISR like this so that in every iteration, the value of a variable is not re defined.
that's my purpose of declaring like this but i dont know if it's really for this application.. :)

PHP:
static char variable;

is there any equivalent decalaration like this in standard C programming?

and also if we declare a variable like

PHP:
const char variable;

const means constant right? I read from somewhere that if we declare like this the value will not be stored in RAM but in Flash memory.. correct? I dont know.. :)

is there any such thing like this in normal C programming? hmmm
 
hello I have also question about some syntax in embedded C :)

I usually declare a variable inside a ISR like this so that in every iteration, the value of a variable is not re defined.
that's my purpose of declaring like this but i dont know if it's really for this application.. :)

PHP:
static char variable;

is there any equivalent decalaration like this in standard C programming?

Yes, it exists in ANSI C with the same outcome.

and also if we declare a variable like

PHP:
const char variable;

const means constant right? I read from somewhere that if we declare like this the value will not be stored in RAM but in Flash memory.. correct?

Correct the value contained in the variable cannot be changed. Yes, in many Embedded C Compilers constant variables are stored in Flash.

is there any such thing like this in normal C programming?

The properties of the constant variable as fair as being immutable, unchangeable, exist in ANSI C. However, on a system like a PC, constant variables are stored in protected regions of RAM, not Flash.

BigDog
 
romel why dont you use keyword "Volatile" instead of other key words.
1. Volatile is a global variable.
2. takes 1 byte of instruction
3. Compiler cannot optimise the variable.
4. type casting is not allowed.
5. Value can be modified by ports, Hardware, interrupts,serial ports only.
6. Permanent memory location is allocated for it.


When we use the word Optimise, we mean that the variable in the memory can be changed only by the compiler whenever the code is executed. Volatile modifiers cannot be optimised by the compiler, During linking process the code is allocated physical memory in the internal memory. so during the link process( generation of .lnk file during compilation) these variables are placed in the heap instead of main memory. Compiler cannot modify the variable until unless a copy is copied to RAM for execution. So the compiler allocates a different memory location for the variable. These are called un-optimised location. the variables in this location are not dependent on the compiler to change the value. instead interrupt, ports, serial, hardware are given permission to access these variables when ever they raise a request.
When a memory is optimised, then the life of that variable is limited only till the function executes then it is destroyed, but volatile are not destroyed, but keep value in it till there is any change done by external entities. Whenever these variables are accessed only the last updated value is seen in the register.

Uploading a example after this. the best example is RTC(Real Time Clock). in the PC. even when the PC is shut down , and later restarted, the clock updates the latest current time.

eg:
volatile char time;
void update(void)
{
time =time+1;
}

void main()
{
time=0;
while(time<100);
}


without volatile modifier the compiler looks at this as 2 different statements.
1. time =0;
2. while(time<100);

since time =0; 0<100, so the loop always stay in the same line till the condition is true.
Time never reach 100. So when memory is optimised then it can be changed only through execution of explicit statement which modify the value. in the above case it does not change.

if we use volatile modifier, this disables optimisation, forcing the program to fetch a new value from the variable every time variable is accessed.

hope this answers your question. if not then i can still continue my explaination in my next post. let me know if you got anything from this.

---------- Post added at 08:20 ---------- Previous post was at 08:19 ----------

volatile keyword exists in both embedded C and normal C program...
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top