Hardware introduction:The adc12 module in MSP430 has the following features: 12-bit conversion accuracy, 1-bit non-linear error, and 1-bit Non-Linear Integral Error. Multiple clock sources are provided to the adc12 module, switch itself comes with a clock generator; built-in temperature sensor; timera/timerb hardware trigger; 8 external channels and 4 internal channels; built-in reference voltage source and 6 reference voltage combinations; four modes: Analog-to-analog conversion; 16-bit conversion cache; ultra-low power consumption supported by adc12 shutdown; fast adoption, up to 200 kbps; automatic scanning and DMA enabling. 430 the internal adc12 function is quite powerful. It can start with a timer to trigger the modulus conversion, and can also be used together with the internal DMA module to complete high-speed sampling dump and other advanced functions.
The conversion formula of this ad is as follows, and the analog voltage value of the sample can be calculated based on it:
When using AD, pay attention to the sampling time. The equivalent analog voltage input circuit of the module adc12 of the 430 microcontroller is as follows:
Among them, VS is the signal source voltage, RS is the signal source internal resistance, VI is the voltage on the ax (adc12 module simulates the input end), and Ri is the equivalent resistance of multiple switches in the single chip microcomputer, VC is the voltage on the capacitor (the voltage sampled by the adc12 module), and Ci is the capacitance value. Calculate the sampling time based on these values:
After the parameters are substituted into the single-chip microcomputer, the formula is as follows:
In my program, the sampling time is set to 4 us, so that if I use my program (without changing the sampling time), the maximum internal resistance of the signal source can be 6.8 kb. When the internal resistance of the signal source is greater, you can set the sampling time as required (in the register Settings section of the program's initialization function ).
In addition, the reference voltage is required for ADC analog-to-digital conversion. To meet this requirement, TEXAS instruments require the following circuits:
That is, all reference sources and power supplies are associated with a set of 0.1uf and 10uf capacitors.
The hardware is so much. For more details, refer to the user guide.
Program Implementation:
The program mainly implements a general initialization program, the content is as follows:
Char Adc12init (Char N, Char Channels [], Char Rep ){ If (N> 15) Return 0; // Sht0_0 Adc12ctl0 = adc12on + MSC + sht0_0 + refon + ref2_5 V; // Enable the Active Directory. The reference voltage is 2.5 V. Adc12ctl1 = SHP + adc12ssel_3; // Use sampling timer, smclk For ( Int I = 0; I <n; I ++ ){ If (Channels [I]> = 0x80)Return 0 ;*( Char *) (Adc12mctl0 _ + I) = channels [I]; // Set each mctl }*( Char *) (Adc12mctl0 _ + n-1) | = EOS; // Sequence ends If (Rep! = 0) // Multiple conversions {Adc12ctl1 | = conseq_3 ;} Else {Adc12ctl1 | = conseq_1;} adc12ie = 1 <(n-1 ); // Enable adc12ifg. n-1 Return 1 ;}
The program first checks whether the total number of N channels has exceeded the available number, then returns zero if it has exceeded, and then sets no special settings in adc12ctl0 and adc12ctl1, then set the channel mode (based on the value of the rep parameter); For Loop sets the setting of each storage register adc12mctlx ;*(Char*) (Adc12mctl0 _ + n-1) | = EOS;// Sequence endsThe end mark of the sequence is added. The interrupt register is finally set and the flag of successful settings is returned. The special one is adc12mctl0 _, which is the address value of adc12mctl0 defined in the header file provided by 430. It is used as the first address of the pointer to operate the adcmctlx register, so that the Register content can be set through loops, greatly reducedCodeNumber of rows.
The channels [] parameter is the setting of each storage register (except for the EOS bit). Its meaning is as follows:
Channels []: Specifies the channel to which the channel is located. For details, refer to source selection. For details, refer to four channels. Details: srefx Bits6-4Select reference000 VR + = avcc and VR. = avss001 VR + = vref + and VR. = avss010 VR ++ = veref + and VR. = avss011 VR + = veref + and VR. = avss100 VR + = avcc and VR. = vref. /veref.101 VR + = vref + and VR. = vref. /veref.110 VR + = veref + and VR. = vref. /veref.111 VR + = veref + and VR. = vref. /veref. inchx Bits3-0Input channel select0000 a00001 a10010 a20011 a30100 a40101 a50110 a60111 a71000 veref + 1001 vref. /veref.1010 temperature sensor1011 (avcc-AVSS)/21100 (avcc-AVSS)/21101 (avcc-AVSS)/21110 (avcc-AVSS)/21111 (avcc-AVSS)/2
This is copied from the user guide. Each bit has the same meaning as adc12mctlx (with the EOS bit removed). Therefore, you can use a macro to define this parameter, for example:
CharChannels [3]; channels [0] = sref_1 + inch_0; channels [1] = sref_1 + inch_1; channels [2] = sref_1 + inch_2; adc12init (3, channels, 1 );
This is a 3-channel A0-A2 sampling, multiple sampling.
Start the conversion function:
VoidAdc12start () {adc12ctl0 | = ENC; adc12ctl0 | = adc12sc ;}
After the ADC Initialization is complete, call this function to start the adconversion. After the conversion is complete (a sequential channel, for example, the previous 0-2), the program automatically enters the ad interruption, you need to add processing logic for your function here; only the conversion result is stored here:
# PragmaVector = adc_vector _ interruptVoidAdc12isr (Void){Static intI; Results [0] [I] = adc12mem0;// Move results, IFG is clearedResults [1] [I] = adc12mem1;// Move results, IFG is clearedResults [2] [I] = adc12mem2;// Move results, IFG is clearedI ++;If(I> 31)// Number of conversions for Multiple conversions{// Processing functions for repeated samplingAdc12ctl0 & = ~ ENC;// Stop ConversionI = 0 ;}}
The program implements 32 conversions of multiple A0-A2 and stores the results into the results array. Only one sampling (A0-A2) at a time can change the handler by yourself.
The program is complete. You must implement the processing logic or storage logic yourself during the call.