Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
adc.c
Go to the documentation of this file.
1 #include <adc.h>
2 
9 int adc_read(unsigned int ch){
10 
11  volatile int delay;
12 
13  #ifdef __MSP430F2274__
14  switch(ch){
15  case 0:
16  ADC10CTL1 = INCH_0;
17  break;
18  case 1:
19  ADC10CTL1 = INCH_1;
20  break;
21  case 2:
22  ADC10CTL1 = INCH_2;
23  break;
24  case 3:
25  if((ADC10CTL0 & SREF_6))
26  return -EPERM; /* Operation not permitted */
27  ADC10CTL1 = INCH_3;
28  break;
29  case 4:
30  if((ADC10CTL0 & SREF_6))
31  return -EPERM; /* Operation not permitted */
32  ADC10CTL1 = INCH_4;
33  break;
34  case 12:
35  ADC10CTL1 = INCH_12;
36  break;
37  case 13:
38  ADC10CTL1 = INCH_13;
39  break;
40  case 14:
41  ADC10CTL1 = INCH_14;
42  break;
43  case 15:
44  ADC10CTL1 = INCH_15;
45  break;
46  default:
47  return -EPERM; //Operation not permitted
48  }
49 
50  #elif defined(__MSP430G2553__)
51  switch(ch){
52  case 0:
53  ADC10CTL1 = INCH_0;
54  break;
55  case 1:
56  ADC10CTL1 = INCH_1;
57  break;
58  case 2:
59  ADC10CTL1 = INCH_2;
60  break;
61  case 3:
62  if((ADC10CTL0 & SREF_6))
63  return -EPERM; /* Operation not permitted */
64  ADC10CTL1 = INCH_3;
65  break;
66  case 4:
67  if((ADC10CTL0 & SREF_6))
68  return -EPERM; /* Operation not permitted */
69  ADC10CTL1 = INCH_4;
70  break;
71  case 5:
72  ADC10CTL1 = INCH_5;
73  break;
74  case 6:
75  ADC10CTL1 = INCH_6;
76  break;
77  case 7:
78  ADC10CTL1 = INCH_7;
79  break;
80  default:
81  return -EPERM; /* Operation not permitted */
82  }
83  #else
84  #error "No such device!"
85  return -ENXIO;
86  #endif
87 
88  if(!( ADC10CTL0 && ADC10ON))
89  adc_eref_off() ;
90 
91  for(delay=35;delay==0;delay--);
92 
93  ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
94  __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
95  ADC10CTL0 |= ~ENC;
96 
97  return ADC10MEM;
98 }
99 
107 {
108  #if defined(__MSP430G2553__) || defined(__MSP430F2274__)
109 
110  ADC10AE0 = 0xff; /* Seleção das portas como OUTPUT */
111 
112  ADC10CTL0 |= ~ENC;
113 
114  ADC10CTL0 =
115  ADC10ON +
116  SREF_0+ /* Referencia VCC */
117  ADC10SHT_3 +
118  ADC10IE;
119  return (ADC10CTL0 - (ADC10ON + SREF_0 + ADC10SHT_3 + ADC10IE));
120  #else
121  #error "No such device!"
122  return -ENXIO; /* No such device */
123  #endif
124 
125 
126  return -ENXIO; /* No such device */
127  }
128 
129 
137 {
138  #if defined(__MSP430F2274__) || defined(__MSP430G2553__)
139 
140 /* if( ADC10CTL0 && ADC10ON)
141  return -EPERM; /* Operation not permitted */
142 
143  // Modo com referencia externa nos pinos 6 (-) e 7 (+)
144 
145  volatile int delay;
146  ADC10AE0 |= 0x3ff; /* seleção de todas as portas exceto as que correspondem a eref+/- */
147 
148  ADC10CTL0 |= ~ENC;
149  ADC10CTL0 |=
150  ADC10ON +
151  SREF_6+ /* Referencia VCC */
152  ADC10SHT_3 +
153  ADC10IE;
154 
155  for(delay=35;delay==0;delay--);
156  return SUCCESS;
157 
158 
159  #else
160  #error "No such device!"
161  #endif
162  return -ENXIO;
163 }
164 
171 void adc_close()
172 {
173  #if defined(__MSP430G2553__) || defined(__MSP430F2274__)
174 
175  ADC10CTL1 |= ~ADC10ON;
176 
177  #endif
178 
179 }
180 
181 // *************************************************************************************************
182 // @fn ADC12ISR
183 // @brief Store ADC12 conversion result. Set flag to indicate data ready.
184 // @param none
185 // @return none
186 // *************************************************************************************************
187 //Rotina de interrupção
188 #if defined(__MSP430G2553__) || defined(__MSP430F2274__)
189 
190  /* pfs wrapped the following to accommodate mspgcc compiler */
191  #ifdef __GNUC__
192  #include <legacymsp430.h>
193  interrupt (ADC10_VECTOR) ADC10ISR (void)
194  #else
195  #pragma vector=ADC10_VECTOR
196  __interrupt void ADC10ISR (void)
197  #endif
198 {
199  __bic_SR_register_on_exit(LPM0_bits + GIE);
200 }
201 #else
202  #error "No such device!"
203 
204 #endif
205 
#define EPERM
Definition: adc.h:6
int adc_eref_off()
It will hold the configuration of the ADC, and change it according to the OP variable.
Definition: adc.c:106
int adc_eref_on()
Reconfigure the parameters of the ADC to enable Veref+ and Veref-.
Definition: adc.c:136
int adc_read(unsigned int ch)
Definition: adc.c:9
#define ENXIO
Definition: adc.h:8
void adc_close()
It will stop ADC, free the ports, and reset the OP configuration.
Definition: adc.c:171
#define SUCCESS
Definition: adc.h:4