projekty/Source/example1/src/ad.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ad.c,v 1 2004/11/25 9:22:00 BorisK Exp $
00003  *
00004  * Copyright (C) 2004 ONTRACK s.r.o.
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    
00019  * 02111-1307, USA.
00020  *
00021  * [1] ARM-based Microcontroller LPC2119/2129/2194/2292/9994, "Preliminary User Manual", February 03, 2004
00022  *
00023  * Written by Boris Kralik <kralikbo@yahoo.com>, 2004
00024  *
00025  */
00026 
00027 #include "ad.h"
00028 #include "types.h"
00029 
00030 /***********************************************************************************
00031         Name:           ADC_interrupt
00032         Description:    ADC interrupt routines
00033         Parameters:     void
00034         Return:         void
00035 ************************************************************************************/
00036 static unsigned int     inputs[7];
00037 
00038 void 
00039 ADC_int(void)
00040 {
00041         unsigned char iADchannel=0;
00042         uint32_t adVal;
00043         disableIRQ();
00044         adVal           =       AD_ADDR;
00045         iADchannel      =       ((adVal & 0x07000000) >> 24);
00046         adVal = ((adVal & 0xFFC0)>>6);
00047         adVal -= AD_OFFSET;// AD_OFFSET = Optocoupler transistor (T) saturation Voltage (cca 0.33 V)
00048         if (adVal <= 0) adVal = 0;
00049         #if 0
00050                 UARTWriteStr("\n\rADC:0x");
00051                 UARTWriteStr(itoa_16(adVal));
00052                 UARTWriteStr(" [V] Converted chanel:");
00053                 UARTWriteStr(itoa_16(iADchannel));
00054         #endif
00055         inputs[iADchannel]      =       adVal;
00056         VICVectAddr = 0;       // Acknowledge Interrupt
00057         enableIRQ();
00058 }
00059 #define NUM     10
00060 uint16_t AD_read(unsigned char channel) 
00061 {
00062 //      if (isSet == FALSE) return 0;
00063 //              else 
00064         uint16_t        i,val = 0;
00065         for (i=0;i<NUM;i++)
00066         {
00067                 AD_Start();
00068                 while((AD_ADDR & (1 << 31))) ;
00069                 val+=inputs[(channel-100)];
00070         }
00071                 return (val/NUM);
00072 }
00073 
00074 void AD_Start(void){
00075 //      if (isSet == FALSE)
00076 //              return;
00077         AD_ADCR |=      (1 << 24);
00078         //AD_ADCR       &=      0xF9FFFFFF;     //???
00079         #if 0
00080                 UARTWriteStr("\n\rAD_Start");
00081         #endif
00082 
00083 }
00084 void AD_PowerDown(void){        //Not work OK
00085         AD_ADCR |=      (1 << 21);
00086 }
00087 void AD_PowerUp(void){          //Not work OK
00088         AD_ADCR &=      (0 << 21);
00089 }
00090 void AD_Set_Reg(unsigned char uchSel){
00091         #if 0
00092                 UARTWriteStr("\n\rAD_Set_Reg");
00093         #endif
00094 //      isSet = FALSE;
00095         AD_ADCR         =       0x00000000;
00096         AD_ADDR         =       0x00000000;
00097         switch(uchSel)
00098         {
00099                 case AI0:
00100                         PCB_PINSEL0     &=      0xFF7FFFFF;//Set AI0
00101                         AD_ADCR         =       0x00200AF1;     //CLKDIV = (9+1) , Select AI[0]
00102                         #if 0
00103                                 UARTWriteStr("\n\rAD_Set_Reg(0)");
00104                         #endif
00105                         break;
00106                 case AI1:
00107                         PCB_PINSEL0     &=      0xFDFFFFFF;//Set AI1
00108                         AD_ADCR         =       0x00200AF2;//Select AI[1]
00109                         AD_ADCR         |=      (1 << 21);
00110                         break;
00111                 case AI2:
00112                         PCB_PINSEL0     &=      0xF7FFFFFF;//Set AI2
00113                         AD_ADCR         =       0x00200004;     //Select AI[2]
00114                         break;
00115                 case AI3:
00116                         PCB_PINSEL0     &=      0xDFFFFFFF;//Set AI3
00117                         AD_ADCR         =       0x00200008;     //Select AI[3]
00118                         break;
00119                 case AI4://NOT used in uPCB, uPCBNew; not tested
00120                         PCB_PINSEL2     |=      (1 << 21);//Set AI4
00121                         PCB_PINSEL2     |=      (1 << 5);//P2.30 as AI4, P3.28 as AI7
00122                         PCB_PINSEL2     |=      (1 << 4);//P2.31 as AI5, P3.29 as AI6
00123                         AD_ADCR         =       0x00200010;     //Select AI[4]
00124                         break;
00125                 case AI5://NOT used in uPCB, uPCBNew; not tested
00126                         PCB_PINSEL2     |=      (1 << 22);//Set AI5
00127                         PCB_PINSEL2     |=      (1 << 5);//P2.30 as AI4, P3.28 as AI7
00128                         PCB_PINSEL2     |=      (1 << 4);//P2.31 as AI5, P3.29 as AI6
00129                         AD_ADCR         =       0x00200020;     //Select AI[5]
00130                         break;
00131                 case AI6://NOT used in uPCB, uPCBNew; not tested
00132                         PCB_PINSEL2     |=      (1 << 6);//Set AI6
00133                         PCB_PINSEL2     |=      (1 << 5);//P2.30 as AI4, P3.28 as AI7
00134                         PCB_PINSEL2     |=      (1 << 4);//P2.31 as AI5, P3.29 as AI6   
00135                         AD_ADCR         =       0x00200040;     //Select AI[6]
00136                         break;
00137                 case AI7://NOT used in uPCB, uPCBNew; not tested
00138                         PCB_PINSEL2     |=      (1 << 7);//Set AI7
00139                         PCB_PINSEL2     |=      (1 << 5);//P2.30 as AI4, P3.28 as AI7
00140                         PCB_PINSEL2     |=      (1 << 4);//P2.31 as AI5, P3.29 as AI6
00141                         AD_ADCR         =       0x002000080;    //Select AI[7]
00142                         break;
00143         }
00144 //      isSet = TRUE;
00145         #if 0
00146                 UARTWriteStr("\n\rAD_Set_Reg(1)");
00147         #endif
00148 }

Generated on Fri Sep 21 13:41:54 2007 for example1 by  doxygen 1.4.7