Skip to main content

ADC EMBEDDED C CODING

#include"adc.h"

void adcconvert(unsigned int g)
{
unsigned char temp=0,i=0;
unsigned int x=10000;
if(g==0)
lcddata(temp+0x30);
else
{
while(x)
{
temp=g/x;
if((temp!=0)&&(i==0))
i++;
if((temp!=0)||(i!=0))
lcddata(temp+0x30);
g-=(x*temp);
x/=10;
}
}
}

unsigned int adc(unsigned char chn_sel)
{
unsigned char x,y,adcon_bc;
auto int s=0;
if(chn_sel == 0)
{
CHS2 = 0; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 1)
{
CHS2 = 0; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 2)
{
CHS2 = 0; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 3)
{
CHS2 = 0; CHS1 = 1; CHS0 = 1;
}
else if(chn_sel == 4)
{
CHS2 = 1; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 5)
{
CHS2 = 1; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 6)
{
CHS2 = 1; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 7)
{
CHS2 = 1; CHS1 = 1; CHS0 = 1;
}
ADON=1;
delay(100);
ADGO=1;
while(ADGO==1);
x=ADRESL;
y=ADRESH;
s+=y;s<<=8;
s+=x;
delay(100);
ADON=0;
return s;
}

unsigned int adc_getvalue(unsigned char chn_sel)
{
unsigned char i=0;
unsigned int avg = 0;
for(;i<10;i++)
{
avg += adc(chn_sel);
}
return (avg/10);
}

void adc_init()
{
TRISA = 0xFF;           // set PORTA as input
/* uncoment the below code for all analog sensors*/
// TRISE = 0x07;           // set RE0 (AN5), RE0 (AN6) and RE2(AN7) as input
// ADCON1=0x80; //For configure all ADC Channels
/*************************************************/
CMCON = CMCON | 7;     // Turn off comparators
ADCON0=0x40;
// ADCON1=0x8E; //For configure one sensor and make others Digital
ADCON1=0x82; //For configure First five sensor and last three Digital
}

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...