Saturday, June 29, 2013

To Write and Read the 32 bit Variable using 4 Byte Array

/*
 * Read_Array.c
 *
 *  Created on: Jun 29, 2013
 *      Author: Karthik
 */


/* To Write the 32 bit variable using 4 Byte Array*/

#include "stdio.h"
#include "stdlib.h"

int main()
{
    /* variable Initialization */

    int nArray[4] = {1,0,0,0};
    //int nOutVar = 0;
    unsigned int nlOutVar=0;

    printf("%d\n",nArray[0]);
    printf("%d\n",nArray[1]);
    printf("%d\n",nArray[2]);
    printf("%d\n",nArray[3]);
    nlOutVar = nArray[0];
    nlOutVar = nlOutVar << 8;
    printf("2byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[1];
    nlOutVar = nlOutVar << 8;
    printf("3byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[2];
    nlOutVar = nlOutVar << 8;
    printf("4byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[3];

    printf("4byte content looks like %u\n",nlOutVar);

    nlOutVar = nlOutVar >> 16;
    printf("MSB first 2byte content reading %u\n",nlOutVar);

    nlOutVar = 4294967295;
    printf("maximum value %u\n",nlOutVar);

    return 0;

}

Output:

1
0
0
0
2byte usage256
3byte usage65536
4byte usage16777216
4byte content looks like 16777216
MSB first 2byte content reading 256
maximum value 4294967295

Thursday, April 11, 2013

Sine Wave Generation in C Program

/*
 * SineWave.c
 *
 *  Created on: Apr 11, 2013
 *      Author: Karthik
 */

#include
<stdio.h>
#include<conio.h>
#include<math.h>
#include <windows.h>


#define pi 3.142
int main()
{
    FILE *fp;
    fp = fopen("Sinewave.xls","w");

    if(fp == NULL)
    {
        MessageBox(0,"Error","File Creation",16);
        exit(0);

    }
 int nfreq,nTime,nAmplitude=0;
 double dSample, dCycle=0;
 double dStopTime=0;
 printf("enter the desired frequency of the signal:\n");
 scanf("%d",&nfreq);
 printf("enter the desired sampling Time:\n");
 scanf("%d",&nTime);
 printf("enter the amplitude of the signal:\n");
 scanf("%d",&nAmplitude);
 printf("Enter the Stop time:\n");
 scanf("%lf",&dStopTime);

for (;dCycle <= dStopTime; )
{
    dSample = nAmplitude * sin(2*pi*nfreq*dCycle);

    fprintf(fp,"%lf\n",dSample);

    dCycle = dCycle + ((double)nTime/100);
}

    fclose(fp);
    getch();
    return 0;
 }