You are on page 1of 7

EMBEDDED C ASSIGNMENT

1, WRITE A SAMPLE PROGRAM USING EMBEDDED C

#INCLUDE<STDIO.H>

void main(void)
{
printf(“hai welcome to cross compiler”)
}

2, WRITE A PROGRAM TO SIMULATE SERIAL INTERRUPT

//serial interrupt//
#include<reg51.sfr>
#include<stdio.h>

_xdat int i _at(0x5000);

void main (void)


{
IE=0x90;
TI=1;
while(1);
}

_interrupt(4) _using(2) void serial()


{
i=0x34;
TI=0;
}

3, WRITE A PROGRAM TO PASS ARGUMENT USING REGISTERS

#include<stdio.h>
#include<reg51.sfr>

_regparm func(int);

void main (void)


{
func(5);
}

_regparm func(int i)
{
printf("%d",i);
}

4, try nesting of interrupt

#include<reg51.sfr>
#include<stdio.h>

_xdat int i _at(0x5000);


_xdat int j _at(0x5001);

void main (void)


{
IE=0x98;
IP=0x90
TF1=1;
while(1);
}

_interrupt(4) _using(2) void serial()


{
i=0x34;
TI=0;
}

_interrupt(3) _using(3) void timer1()


{
j=0x45;
TI=1;
}

5, try intrinsic function using embedded c

/* void _nop(void);
* bit _testclear(bit);
* unsigned char _da(unsigned char);
* unsigned char _rol(unsigned char,unsigned char);
* unsigned char _ror(unsigned char,unsigned char);
*/

static bit b;
static unsigned char c,d;
static int i;

main()
{
_nop(); /* insert a nop instruction */

if ( _testclear( b ) ) /* jump bit and clear: jbc


instruction */
c = 1;

c = _da( c + 1 ); /* decimal adjust after addition */

c = _rol( c, i ); /* rotate left, using variable


expression */

c = _ror( c, 2 ); /* rotate right, using constant */


c = _ror( c, 3 );
c = _ror( c, 7 );
}

6, DEMO.C

#include <string.h>

#pragma romstring /* equivalent with -S option */


#pragma optimize t /* equivalent with -Ot option */
#pragma optimize D /* equivalent with -OD option */
/* prevent optimizing 'sum' in main() */

typedef enum color_e


{
red, yellow, blue
}
color_type;

typedef struct rec_s


{
short a;
char b[16];
long c;
color_type color;
}
rec_type;

/*
* 'optimize t' forces 'initval' to be allocated directly
after 'table'.
* Needed for the deliberate bug, to be discovered in the
tutorial.
*/
_idat unsigned int table[7]; /* table of factorial values
*/
_idat int initval; /* initialization value for
calculation */

#define INPUT_LEN 2
#define OUTPUT_LEN 35
char inbuf[INPUT_LEN]; /* input buffer for one
character */
char outbuf[OUTPUT_LEN]; /* output buffer for a line
*/

static rec_type recordvar = {-1, "TASKING, Inc.",


987654321L, blue};
_rom static rec_type statrec = {57, "hello,world",
123456789L, red };
_rom static char bell[2] = { '\007', '\0' };

/*
* FUNCTION PROTOTYPES
*
* These functions are present in this module.
*/
void myputs( _rom char * );
void mygetchar( char * );
unsigned int factorial( unsigned char );

/*
* The function addone() is present in the module
"addone.src".
* This function is deliberately treated as an assembly
routine
* (although it has been created by CC51) to demonstrate
* assembly level debugging.
*/
_regparm unsigned int addone( unsigned int );
/*
* The simulated I/O functions _simi() and _simo() are
present in the
* module "_simio.c". Prototypes are present in the
include file
* "simio.h". These functions are used by CrossView Pro
for temporary
* breakpoints, when performing simulated I/O. You can
have up to 8 I/O
* channels (called streams) in your application. This
demo uses the
* default streams 0 for input and 1 for output. In
CrossView Pro you
* can connect these streams to a window, file or
keyboard.
*/
#include <simio.h>
#define INPUT_STREAM 0
#define OUTPUT_STREAM 1

void
main(void)
{
unsigned char loopvar; /* the loop counter
*/
char cvar; /* sample char variable
*/
unsigned int sum; /* 17+sum of factorials from 0
to 6 */

initval = 17;

if (initval > recordvar.a)


{
sum = 0;
}

/* This loop has an upper limit which is too high */


/* As a result, initval will get clobbered */

for (loopvar = 0; loopvar <= 7; ++loopvar)


{
table[ loopvar ] = factorial(loopvar);
sum += table[ loopvar ];
}
sum += initval;

if (sum == 891)
{
myputs("pass\n");
}
else
{
myputs("fail\n");
}

recordvar.color = red;
recordvar.a = strlen(recordvar.b);

/* An infinite loop to be discovered using CrossView


Pro*/
while (loopvar)
{
sum = sum + 1;
}

recordvar.color = blue;

/* A call to a function without HLL debug info */


sum = addone( 5 );

myputs( "Do you want to ring the bell?\n" );


mygetchar( &cvar );

if (cvar == 'y')
{
myputs( bell );
}
myputs( "End of demo\n" );
}

/* Compute the factorial of a number */

/*
* Iteration performs better than recursion.
*/
unsigned int
factorial( unsigned char num )
{
char locvar = 'x'; /* an unused local variable */
unsigned int tmp;

if (num < 2)
return 1;
else
{
for (tmp = num--; num; num--)
tmp *= num;
}
return tmp;
}

/* Put a string in ROM to an output port */

void
myputs( _rom char *string )
{
char *p = outbuf;

while( *p++ = *string++ )


;
_simo( OUTPUT_STREAM, outbuf, OUTPUT_LEN );
}

/* Read a line from an input port */

void
mygetchar( char *c )
{
_simi( INPUT_STREAM, inbuf, 2);
*c = inbuf[0];
}

You might also like