adc.c

Go to the documentation of this file.
00001 
00002 #include "t89c51cc02.h"
00003 
00004 #include "datatypes.h"
00005 #include "adc.h"
00006 
00013 void ADC_init()
00014 {
00015         ADCF=0x03;              /* set P1.0 as analog input */
00016         ADCON &= 0xF8;  /* clear A/D channel select */
00017         ADCON |= MSK_ADCON_ADEN; /* enable ADC */
00018 }
00019 
00029 WORD ADC(BYTE channel)
00030 {
00031         WORD result=0;  
00032 
00033         ADCON &= 0xF8;  /* empty channel field */
00034 
00035         ADCON |= channel; /* set channel to measure */
00036         ADCON |= MSK_ADCON_ADSST; /* start A/D input */
00037 
00038         /* wait until conversion has finished  - inline assembler is faster */
00039         _asm
00040 adcloop:
00041         mov A,_ADCON
00042         jb ACC.3,adcloop
00043         _endasm;
00044 
00045         ADCON &= 0xef;
00046 
00047         /* read A/D value */
00048         result=((ADDH<<2)+ADDL);
00049         
00050         return(result);
00051 }
00052 

Generated on Wed Oct 24 12:57:24 2007 for SensorControl by  doxygen 1.4.6