RT-AICHIP-sample
ad.c
[詳解]
1 /* ------------------------------------------------------------ *
2 File ad.c
3 
4 adポート関係
5 * ------------------------------------------------------------ */
6 #include "timer.h"
7 #include "LPC13xx.h" // LPC13xx Peripheral Registers
8 
9 /*----------------------------------------------------------------------------
10  Input void
11  Output void
12  ad変換関係の設定
13  *---------------------------------------------------------------------------*/
14 void Init_ad (void)
15 {
16  LPC_SYSCON->PDRUNCFG &= ~(0x1<<4); // ADCのpower-downの設定 4bit目: 0:Powered 1:Power down
17  LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<13); // ACCのクロックのenable 13bit目: 0:disabled 1: enabled
18 
19 
20  //PIO0_11 VMOT-AD AD0
21  LPC_IOCON->R_PIO0_11 |= ( 0x1<<1 ); // Selects function AD0
22  LPC_IOCON->R_PIO0_11 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
23  LPC_IOCON->R_PIO0_11 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
24 
25  //PIO1_0 VLIPO-AD AD1
26  LPC_IOCON->R_PIO1_0 |= ( 0x1<<1 ); // Selects function AD1
27  LPC_IOCON->R_PIO1_0 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
28  LPC_IOCON->R_PIO1_0 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
29 
30  //PIO1-1 AD2
31  LPC_IOCON->R_PIO1_1 |= ( 0x1<<1 ); // Selects function AD2
32  LPC_IOCON->R_PIO1_1 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
33  LPC_IOCON->R_PIO1_1 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
34 
35  //PIO1-2 AD3
36  LPC_IOCON->R_PIO1_2 |= ( 0x1<<1 ); // Selects function AD3
37  LPC_IOCON->R_PIO1_2 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
38  LPC_IOCON->R_PIO1_2 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
39 
40  //PIO1-4 AD5
41  LPC_IOCON->PIO1_4 |= 0x1; // Selects function AD5
42  LPC_IOCON->PIO1_4 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
43  LPC_IOCON->PIO1_4 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
44 
45  //PIO1-10 AD6
46  LPC_IOCON->PIO1_10 |= 0x1; // Selects function AD6
47  LPC_IOCON->PIO1_10 &= ~( (0x1<<3)|(0x1<<4) ); // pull up pull down off
48  LPC_IOCON->PIO1_10 &= ~( 0x01<<7 ); // 7bit目 ADMODE 0:Analog input mode 1:Digital gunction mode
49 
50  //8-15bit目 CLKDIV
51  LPC_ADC->CR |= ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/4000000-1)<<8;
52 
53 
54 
55 }
56 
57 /*----------------------------------------------------------------------------
58  Input ad_value[8]
59  Output void
60  ad値の取得
61  ad_value[0] DR0
62  ad_value[1] DR1
63  ad_value[2] DR2
64  ad_value[3] DR3
65  ad_value[4] なし
66  ad_value[5] DR5
67  ad_value[6] DR6
68  ad_value[7] なし
69  *---------------------------------------------------------------------------*/
70 void storeAD2Array( int16_t *ad_value )
71 {
72 
73  //DR0
74  LPC_ADC->CR &=0xffffff00;
75  LPC_ADC->CR |= (0x1<<0); //0-7bit目 SEL
76  LPC_ADC->CR |=0x01000000; //ADC Start now
77  while(!(LPC_ADC->DR0 & 0x80000000 )); //各ピンのAD変換が終了するまで待つ.
78  ad_value[0]=(LPC_ADC->DR0 & 0x0000FFC0)>>6;
79  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
80  //DR1
81  LPC_ADC->CR &=0xffffff00;
82  LPC_ADC->CR |= (0x1<<1);
83  LPC_ADC->CR |=0x01000000; //ADC Start now
84  while(!(LPC_ADC->DR1 & 0x80000000 ));
85  ad_value[1]=(LPC_ADC->DR1 & 0x0000FFC0)>>6;
86  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
87 
88 #ifdef COMMENT_OUT
89  //DR2
90  LPC_ADC->CR &=0xffffff00;
91  LPC_ADC->CR |= (0x1<<2);
92  LPC_ADC->CR |=0x01000000; //ADC Start now
93  while(!(LPC_ADC->DR2 & 0x80000000 ));
94  ad_value[2]=(LPC_ADC->DR2 & 0x0000FFC0)>>6;
95  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
96  //DR3
97  LPC_ADC->CR &=0xffffff00;
98  LPC_ADC->CR |= (0x1<<3);
99  LPC_ADC->CR |=0x01000000; //ADC Start now
100  while(!(LPC_ADC->DR3 & 0x80000000 ));
101  ad_value[3]=(LPC_ADC->DR3 & 0x0000FFC0)>>6;
102  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
103  //DR5
104  LPC_ADC->CR &=0xffffff00;
105  LPC_ADC->CR |= (0x1<<5);
106  LPC_ADC->CR |=0x01000000; //ADC Start now
107  while(!(LPC_ADC->DR5 & 0x80000000 ));
108  ad_value[5]=(LPC_ADC->DR5 & 0x0000FFC0)>>6;
109  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
110  //DR6
111  LPC_ADC->CR &=0xffffff00;
112  LPC_ADC->CR |= (0x1<<6);
113  LPC_ADC->CR |=0x01000000; //ADC Start now
114  while(!(LPC_ADC->DR6 & 0x80000000 ));
115  ad_value[6]=(LPC_ADC->DR6 & 0x0000FFC0)>>6;
116  LPC_ADC->CR &= 0xF8FFFFFF;; //stop ADC now
117 #endif
118 
119 }
120 
121 
122 /******************************************************************************
123 ** End Of File
124 ******************************************************************************/
signed short int int16_t
Definition: type.h:22