1) WHAT ARE THE DIFFERENT DATATYPES COMMONLY USED IN EMBEDDED C?
Below are the major data types that are commonly used in embedded C-
int main()
{
typedef signed char sint8; //Size is -128 to 127
typedef unsigned char uint8; //Size is 0 to 255
typedef signed short sint16; //Size is -32768 to 32767
typedef unsigned short uint16; //Size is 0 to 65535
typedef signed long sint32; //Size is -2147483648 to 2147483647
typedef unsigned long uint32; //Size is 0 to 4,294,967,295
typedef signed long long sint64; //Size is -((2^64)/2)-1) to ((2^64)/2)-1)
typedef unsigned long long uint64; //Size is 0 to (2^64)-1
typedef float float32; //Size is -3.4e+38 to 3.4e+38.
typedef double float64; //Size is -1.7e+308 to +1.7e+308.
return 0;
}
2) WHAT IS A FUNCTION POINTER?
The function just like a variable has some address and therefore it is possible to declare a pointer to a function. Thus, a function pointer is nothing but a pointer to a function.
A simple example of function pointer is as shown below-
#include <stdio.h>
int sum(int x, int y)
{
int z = x+y; // Function to perform addition
return z;
}
void main()
{
int (*fptr)(int,int);
fptr = ∑ // or fptr=∑ // Defining Function pointer
int result = (*fptr)(40,20);
printf("The sum is %d",result); // Output is 60
}
3) WHAT IS THE USE OF FUNCTION POINTER?
The typical example where the function pointer can be used in embedded systems is for the implementation of State Machines. The function pointer can be used to invoke particular function for a given state in the state machine. Below is a simple example for state machine implementation using function pointer –
// State Machine States
typedef enum{
ST_INIT,
ST_RUN,
ST_DEINIT
}states;
// State Machine functions
void func1(){
//Do nothing
}
void func2(){
//Do nothing
}
void func3(){
//Do nothing
}
typedef struct{
states curr_state;
states next_state;
}state_and_function;
static state_and_function SM[]={
{ ST_INIT, ST_RUN},
{ ST_RUN , ST_DEINIT},
{ ST_DEINIT, ST_INIT}
};
typedef struct{
const char *name;
void (*fptr) (void);
}state_fptr;
static state_fptr statefunctionptr[]={
{ "ST_INIT", &func1},
{ "ST_RUN", &func2},
{ "ST_DEINIT", &func3}
};
typedef struct{
states curr_state;
}state_machine;
void main()
{
state_machine *stateMachine;
for(int i=0;i<3;i++)
{
// Transition from current state to next state
stateMachine->curr_state = SM[i].next_state;
// Call the function related to the state
statefunctionptr[stateMachine->curr_state].fptr();
}
}
4) WHAT ARE BIT FIELDS IN EMBEDDED C?
Using bit fields, we can specify the size in bits for structure and union members to be more memory efficient. Below is a simple example for structure definition using bit fields –
//Structure Bit field example
struct date{
unsigned int d:5; // Size is 5 bits
unsigned int m:4; // Size is 4 bits
unsigned int y:3; // Size is 3 bits
}
5) WHAT IS THE USE OF BIT FIELDS IN EMBEDDED C?
The registers of microcontroller can be accessed using the bitfields. Below is a simple example showing the use of bit fields-
#include "stdio.h"
typedef unsigned long uint32;
uint32 addr;
#define ADDR_OF_REG &addr //Define address of register
typedef union
{
uint32 reg;
struct
{
uint32 regfield1:3; //bit 0..2
uint32 regfield2:1; //bit 3
uint32 regfield3:2; //bit 4..5
uint32 regfield4:2; //bit 6..7
};
}special_reg;
//Assign register to its address
volatile special_reg *reg = (volatile special_reg *) ADDR_OF_REG;
//Fill the register with certain value
void main()
{
reg->regfield1= 0x7;
reg->regfield2=0x1;
reg->regfield3=0x3;
reg->regfield4=0x2;
printf("%d",reg->regfield1); // Output is 0x7
printf("\n%d",reg->regfield2); // Output is 0x1
printf("\n%d",reg->regfield3); // Output is 0x3
printf("\n%d",reg->regfield4); // Output is 0x2
}
6) What is the difference between Structure and Union?
Structure | Union |
Each Member of the structure is assigned unique storage area. | Memory location is shared between individual members of the Union. |
Altering value of a member will not affect other members of the structure. | Altering value of any member will affect other members values. |
Individual members can be accessed at a time. | Only one member can be accessed at a time. |
Several members of a structure can be initialized at once. | Only first member of a union can be initialized. |