Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Thursday, December 16, 2010

Analog to Digital Conversion in PIC18F Series

This program converts the Analog voltage signal from the photentiometer of the development board and convert in to digital. This value is then devide by 4 and result is shown in the PORTB LEDS. Turn the Photentiomter to see the LED variation.

#include <stdio.h>
#include <P18CXXX.h>    
#include <adc.h>
#include <timers.h>
 
#pragma code page  // This code make sure the program starts from 0x0004, not from 0x0000

void OpenADC( unsigned char config,unsigned char config2 );  // prototypes
void ConvertADC( void );
void OpenTimer2( unsigned char config );
void OpenPWM1( char period );
void OpenPWM2( char period );
void SetDCPWM1( unsigned int dutycycle );
void SetDCPWM2( unsigned int dutycycle );

int data; // global variables goes to here

void delay (void) // user define delay function
{
long i,C;
for (i=0;i<30000;i++)
C = C+1;
}
void main (void) // main  function 
{
TRISB = 0; // Port,pin direction configuration
PORTB = 0;
TRISC = 0;
PORTC = 0;
TRISCbits.TRISC7 = 1;  // make sure this pin is input

OpenADC (ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_5ANA_0REF, ADC_CH4 & ADC_INT_OFF );  // init ADC
OpenPWM1(0xff); // int PWM1
OpenPWM2(0xff); // int PWM1
OpenTimer2 ( TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_8 ); // int Timer2
while (1)     // this is how to make a never ending loop.
{
ConvertADC();
while( BusyADC() ); // Wait for AtoD complete.
data = ReadADC();

PORTB = data/4; // Put the AtoD value in to the PortB by deviding ADC value by 4

PORTCbits.RC0 = 1; // Set motor 1 direction
PORTCbits.RC3 = 0;

PORTCbits.RC5 = 1;  // Set motor 2 direction
PORTCbits.RC4 = 0;

SetDCPWM2(data); // Set duty cycle value for PWM2
SetDCPWM1(data); // Set duty cycle value for PWM1

delay();
}
}

Monday, December 6, 2010

Matrix Manipulations

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
int matA[n][n],matB[n][n],matAns[n][n]={0},matD[n][n];
int i,j,k;
char mat;
void input();
void sum();
void multiply();
void display();
void transpose();


void main()
{
clrscr();
int opt;
do
{
printf("~~~~~~~~~\n");
printf("MAIN MENU\n");
printf("---------\n");
printf("INPUT:1\n");
printf("SUM:2\n");
printf("PRODUCT:3\n");
printf("TRANSPOSE:4\n");
printf("EXIT:5\n");
printf("~~~~~~~~~\n");
printf("Enter your option:");
scanf("%d",&opt);
switch(opt)
{
case 1:input();break;
case 2:sum();break;
case 3:multiply();break;
case 4:transpose();break;
case 5:exit(0);break;
}
}
while(opt<7);
}


void input()
{
int row,column;
printf("\nTo which matrix, you want to INPUT(A:A,B:B): ");
cscanf("%c",&mat);


if((mat=='A')||(mat=='a'))
{
printf("\n\nMatrix A:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the number at [%d][%d] in matA: ",i,j);
scanf("%d",&matA[i][j]);
}
}
}
else if((mat=='B')||(mat=='b'))
{
printf("\n\nMatrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the number at [%d][%d] in matB: ",i,j);
scanf("%d",&matB[i][j]);
}
}
}
printf("\n\n\n");
}


void sum()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=matA[i][j]+matB[i][j];
printf("\n\nMatrix A+B:\n\n");
display();
}


void multiply()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
matAns[i][j]+=matA[i][k]*matB[k][j];
printf("\n\nMatrix AxB:\n\n");
display();
}


void transpose()
{
int temp;
printf("\nWhich matrix, you want to TRANSPOSE(A:A,B:B,Ans:any other char): ");
cscanf("%c",&mat);
if((mat=='A')||(mat=='a'))
{
for(i=0;i<n;i++)
for(j=i;j<n;j++)
matAns[j][i]=matA[i][j];
printf("\n\nMatrix A':\n\n");
}
else if((mat=='B')||(mat=='b'))
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[j][i]=matB[i][j];
printf("\n\nMatrix B':\n\n");
}
else
{
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
temp=matAns[i][j];
matAns[i][j]=matAns[j][i];
matAns[j][i]=temp;
}
}
printf("\n\nMatrix Ans':\n\n");
}
display();
}


void display()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",matAns[i][j]);
}
printf("\n");
}
printf("\n\n\n");
}

Saturday, December 4, 2010

Sine Table

#include<stdio.h>
#include<conio.h>
#define pi 3.14159262
int main()
{
float term,dr,dc,d,r;
float sintable[19][5];
int n,x,y;
clrscr();
x=0;
dr=dc=0;
while(x<19)
{
y=0;
while(y<5)
{
if((x==0)&&(y!=0))
{
sintable[x][y]=dc;
dc+=5;
printf("%0.0f\t",sintable[x][y]);
}
else if((y==0)&&(x!=0))
{
sintable[x][y]=dr;
dr+=20;
printf("%0.0f\t",sintable[x][y]);
}
else if((x==0)&&(y==0))
printf("DEGREE\t");
else
{
d=sintable[x][0]+sintable[0][y];
r=d*pi/180;
n=1;
sintable[x][y]=term=r;
while(n<100)
{
term*=((-1)*r*r/((n+1)*(n+2)));
sintable[x][y]+=term;
n+=2;
}
printf("%0.4f\t",sintable[x][y]);
}
y++;
}
printf("\n");
x++;
}
printf("\n$ No of terms we calculate: 51 $\n");
getch();
return 0;
}

Day Finder

#include<stdio.h> //pre-processor directives
#include<conio.h>
int main() //start main function
{
int y,m,d,f,a,b,f1;
clrscr();
printf("Enter the date(y,m,d):");
scanf("%d,%d,%d",&y,&m,&d); //read y,m,d
if(m<3) //if month less than 3
{
m+=12;
y--;
}
a=2*m+6*(m+1)/10; //calculation to find the day
b=y+y/4-y/100+y/400;
f1=d+a+b+1;
f=f1%7;
switch(f) //display the day
{
case 0:printf("Sunday");break;
case 1:printf("Monday");break;
case 2:printf("Tuesday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Saturday");break;
}
getch();
return 0;
}         //end main

Word Count Using C Language

#include<stdio.h>
#include<conio.h>
#include<ctype.h>


int main()
{
int i=0;
char ch,*problem_in;
FILE *fp;
clrscr();


fp=fopen("problem.in","r");
if(fp!=NULL)
{
if(isalpha(*(problem_in+i-2)))
i=1;
else
i=0;


ch=getc(fp);
while(ch!=EOF)
{
ch=getc(fp);
switch(ch)
{
case ' ':
case '!':
case '?':
case ',':
case '.':
case ';':
case ':':i++;break;
}
}
fclose(fp);
}
else
printf("Error while opening the file problem.in!...");


fp=fopen("problem.out","w");
if(fp!=NULL)
{
printf("\nproblem.out\n");
printf("Number of words: %d",i);
fprintf(fp,"Number of words: %d",i);
fclose(fp);
}
else
printf("Error while opening the file problem.out!...");


getch();
return 0;
}