1456 views|0 replies

3836

Posts

19

Resources
The OP
 

ADC12 module [Copy link]

The msp430 contains an ADC12 module that can complete 12-bit analog-to-digital conversion. When the accuracy or other indicators are not required, the ADC12 inside the 430 microcontroller can be used to complete the analog-to-digital conversion. Here, a relatively general ADC12 module initialization program is mainly implemented. The specific data storage and processing need to be added in the interrupt processing function. Hardware Introduction: The features of the ADC12 module in the msp430 microcontroller are as follows: 12-bit conversion accuracy, 1-bit nonlinear error, 1-bit nonlinear integral error; multiple clock sources for the ADC12 module, and its own 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; 4 modes of analog-to-digital conversion; 16-bit conversion cache; ADC12 shutdown supports ultra-low power consumption; fast speed, up to 200Kbps; automatic scanning and DMA enable. The ADC12 inside the 430 is quite powerful. It can have a timer to trigger the start of analog-to-digital conversion, and can also be used together with the internal DMA module to complete advanced functions such as high-speed sampling and dumping. The conversion formula of AD is as follows, and the sampled analog voltage value can be calculated based on it: When using AD, you must also pay attention to the sampling time. The equivalent analog voltage input circuit of the analog-to-digital ADC12 module of the 430 microcontroller is as follows: Where VS is the signal source voltage, RS is the internal resistance of the signal source, VI is the voltage on Ax (ADC12 module analog input terminal), RI is the equivalent resistance of the multi-way switch in the microcontroller, VC is the voltage on the holding capacitor (the voltage sampled by the ADC12 module), and CI is the value of the capacitor. The sampling time needs to be calculated based on these values: After substituting the parameters on the microcontroller, the formula is as follows: The sampling time in my program is set to 4us. It can be calculated that if my program is used (without changing the sampling time), the maximum signal source internal resistance can be 6.8k. When the signal source internal resistance is larger, you can set the sampling time as required (in the register setting part in the initialization function of the program). Also, the ADC analog-to-digital conversion requires the reference voltage to be very stable. In order to achieve this requirement, Texas Instruments requires the circuit of this part to be as follows: That is: all reference sources and power supplies are connected in parallel with a set of 0.1uF and 10uF capacitors. That's all for the hardware part; if you need a more detailed explanation, refer to the user guide. char ADC12Init(char n,char channels[],char rep){if(n>15)return 0;//SHT0_0 ADC12CTL0 = ADC12ON + MSC + SHT0_0 + REFON + REF2_5V;// Turn on ad, reference voltage 2.5v ADC12CTL1 = SHP + ADC12SSEL_3; //Use sampling timer, SMCLK for(int i = 0;i < n;i++) { if(channels >= 0x80) return 0; *(char*)(ADC12MCTL0_ + i) = channels; //Each MCTL setting } *(char*)(ADC12MCTL0_ + n - 1) |= EOS; //End of sequence 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 determines whether the total number of n channels exceeds the available number. If it exceeds, it returns zero and then sets the parts of ADC12CTL0 and ADC12CTL1 that do not require special settings, and then sets the channel mode (according to the value of the rep parameter); the for loop sets the settings of each storage register ADC12MCTLx; *(char*)(ADC12MCTL0_ + n - 1) |= EOS; //The end of the sequence is added to this sentence. The end of the sequence flag is added; finally, the interrupt register is set and the successful setting flag is returned. Among them, the more special one is ADC12MCTL0_, which is the address value of ADC12MCTL0 defined in the header file provided by 430. It is used as the pointer address to operate the ADCMCTLx register, thereby using the loop to set the contents of the register, which greatly reduces the number of lines of code. The parameter channels[] is the setting of each storage register (except the EOS bit), and the meaning is as follows: channels[]: corresponding channel setting, the upper four bits are reference source selection; the lower four bits are channel selection. The details are as follows: SREFx Bits 6-4 Select reference 000 VR+ = AVCC and VR. = AVSS 001 VR+ = VREF+ and VR. = AVSS 010 VR+ = VeREF+ and VR. = AVSS 011 VR+ = VeREF+ and VR. = AVSS 100 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 Bits 3-0 Input channel select 0000 A0 0001 A1 0010 A2 0011 A3 0100 A4 0101 A5 0110 A6 0111 A7 1000 VeREF+ 1001 VREF./VeREF. 1010 Temperature sensor 1011 (AVCC – AVSS) / 2 1100 (AVCC – AVSS) / 2 1101 (AVCC – AVSS) / 2 1110 (AVCC – AVSS) / 2 1111 (AVCC – AVSS) / 2 This is copied from the user guide. Each bit has the same meaning as ADC12MCTLx (minus the EOS bit), so you can use a macro definition to specify this parameter, such as: char channels[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 the sampling of 3 channels A0-A2, multiple sampling. Start the conversion function: void ADC12Start() { ADC12CTL0 |= ENC; ADC12CTL0 |= ADC12SC; } After ADC initialization, call this function to start AD conversion. After the conversion is completed (a sequence channel, such as 0-2 just now), the program automatically enters the AD interrupt. The user needs to add processing logic for his own function here; only the conversion result is stored here: #pragma vector=ADC_VECTOR __interrupt void ADC12ISR (void) { static int i; results[0] = ADC12MEM0; // Move results, IFG is cleared results[1] = ADC12MEM1; // Move results, IFG is cleared results[2] = ADC12MEM2; // Move results,IFG is cleared i++; if(i>31) //Conversion times for multiple conversions { //When sampling repeatedly, process the function here ADC12CTL0 &=~ ENC; //Stop conversion i=0; } } This program implements multiple A0-A2 32 conversions and stores the results in the results array. For single conversion, only one sample (A0-A2) is sampled. You can change the processing function yourself. The program part is completed. When calling, pay attention to implementing the processing logic or storage logic yourself. Usage example: This program is still used by adding C files and including H files; however, unlike the previous program, you need to implement the interrupt processing logic yourself. For an example of usage, see ADC12 in the program library. #include #include "ADC12.h" void main( void ) { / / Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; ClkInit(); char channels[3 ]; channels[0] = SREF_1+INCH_0; channels[1] = SREF_1+INCH_1; channels[2 ] = SREF_1+INCH_2; ADC12Init(3,channels,1); _EINT(); ADC12Start(); LPM0; } Here is a 3-channel multiple conversion, the reference voltage is the internal reference Voltage. For the self-implemented processing logic, please refer to the last part of the previous program implementation.

This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list