O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

256 8. ANALOG PERIPHERALS
overflow interrupt, and (2) when another conversion is requested before the current conversion is
not completed, called a conversion time overflow.
Each input channel has these two interrupts, making the total number of interrupts associated
with each analog input channel three.When a conversion result is written, the conversion completion
flag (ADC12IFG0 - ADC12IFG15) is set in the ADC12_A Interrupt Flag Register (ADC12IFG).
If the local interrupt enable bit, ADC12IEx, in the ADC12 Interrupt Enable Register (ADC12IE)
along with the GIE bit in the status register are set, an interrupt is triggered. For the overflow and
the time overflow interrupts, the ADC12OVIE bit (bit 3) and ADC12TOVIE bit (bit 2) in the
ADC12CTL0 register must be enabled. The interrupt vector register (ADC12IV) contains the
source of the interrupt as shown in Table 8.2.
Table 8.2:
Contents Interrupt Source Flag Priority
0000h no interrupt no flag
0002h overflow interrupt no flag Highest
0004h time overflow interrupt no flat
0006h ADC12MEM0
ADC12IFG0
0008h ADC12MEM1 ADC12IFG1
000Ah
ADC12MEM2 ADC12IFG2
000Ch ADC12MEM3 ADC12IFG3
000Eh ADC12MEM4 ADC12IFG4
0010h ADC12MEM5 ADC12IFG5
0012h ADC12MEM6 ADC12IFG6
0014h ADC12MEM7 ADC12IFG7
0016h ADC12MEM8 ADC12IFG8
0018h ADC12MEM9 ADC12IFG9
001Ah ADC12MEM10 ADC12IFG10
001Ch ADC12MEM11 ADC12IFG11
001Eh ADC12MEM12
ADC12IFG12
0020h ADC12MEM13 ADC12IFG13
0022h ADC12MEM14
ADC12IFG14
0024h ADC12MEM15 ADC12IFG15 Lowest
8.5.3 PROGRAMMING THE ADC12 CONVERTER
To successfully configure and program the converter, the following steps should be followed:
1. Either connect appropriate reference voltage sources to ADC12 converters pins A8 and A9
and/or program ADC12SREFx bits in ADC12MCTLx registers.
2. Connect analog signal(s) for conversion to appropriate input pins (A0-A7, A12-A15).
8.5. MSP430 ANALOG-TO-DIGITAL CONVERTER 257
3. Turn on the ATD converter: ADC12ON in ADTCTL0.
4. Select the clock source and the sampling mode.
5. Configure the converter for proper operation: single channel, single conversion; single chan-
nel, multiple conversion; multiple channels, single conversion; or multiple channels, multiple
conversions using ADC12CONSEQx bits in ADCTL1.
6. Initiate conversion: ADC12ENC bit in ADC12CTL0.
7. Monitor for conversion completion using the ADC12IFG flags.
8. Use the results of the conversion located in the corresponding result registers (ADC12MEM0-
ADC12MEM15).
9. Repeat the process starting at step 6.
Example:In this example, we show how to configure the analog-to-digital converter to accept an
analog signal on an input pin (A0) and repeat conversion.
In assembly:
;************************************************************************
;MSP430F54x Demo - ADC12, Using the Internal Reference
;
;Description: This example shows how to use the internal reference of
;ADC12. It uses the internal 2.5V reference and performs a single
;conversion on channel A0. The conversion results are stored in
;ADC12MEM0. Test by applying a voltage to channel A0, then setting and
;running to a break point at the "_NOP()" instruction. To view the
;conversion results, open an ADC12 register window in debugger and view
;the contents of ADC12MEM0.
;
;M. Morales
;Texas Instruments Inc.
;September 2008
;Built with Code Composer Essentials v3.x
;************************************************************************
.cdecls C,LIST,"msp430x54x.h"
;------------------------------------------------------------------------
.global _main
.text ;Assemble to Flash memory
258 8. ANALOG PERIPHERALS
;------------------------------------------------------------------------
_main
RESET mov.w #0x5C00,SP ;Initialize stackpointer
mov.w #WDTPW + WDTHOLD,&WDTCTL;Stop WDT
bis.b #0x01,&P6SEL ;Enable A/D channel A0
mov.w #ADC12ON + ADC12SHT02 + ADC12REFON +
ADC12REF2_5V,&ADC12CTL0
;Turn on ADC12, set sampling
;time and turn on reference
;generator and set to 2.5V
mov.w #ADC12SHP,&ADC12CTL1 ;Use sampling timer
mov.b #ADC12SREF_1,&ADC12MCTL0;Vr+=Vref+ and Vr-=AVss
mov.w #3600,R4 ;Initialize delay loop
;counter
delay_loop dec.w R4 ;Delay for reference startup
jne delay_loop
bis.w #ADC12ENC,&ADC12CTL0 ;Enable conversions
while_loop bis.w #ADC12SC,&ADC12CTL0 ;Start conversion
poll_ifg bit.w #BIT0,&ADC12IFG
jnc poll_ifg
nop ;SET BREAKPOINT HERE
jmp while_loop
;------------------------------------------------------------------------
; Interrupt Vectors
;------------------------------------------------------------------------
.sect ".reset" ;POR, ext.
Reset
.short RESET
.end
;------------------------------------------------------------------------
In C:
//***********************************************************************
//MSP430F54x Demo - ADC12, Using the Internal Reference
//
//Description: This example shows how to use the internal reference of
//ADC12. It uses the internal 2.5V reference and performs a single
//conversion on channel A0. The conversion results are stored in
8.5. MSP430 ANALOG-TO-DIGITAL CONVERTER 259
//ADC12MEM0. Test by applying a voltage to channel A0, then setting and
//running to a break point at the "__no_operation()" instruction.
To view
//the conversion results, open an ADC12 register window in debugger and
//view the contents of ADC12MEM0
//
//M Smertneck / W. Goh
//Texas Instruments Inc.
//September 2008
//Built with CCE Version: 3.2.2 and IAR Embedded Workbench Version: 4.11B
//***********************************************************************
#include "msp430x54x.h"
void main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW+WDTHOLD; //Stop watchdog timer
P6SEL |= 0x01; //Enable A/D channel A0
ADC12CTL0 = ADC12ON+ADC12SHT02+ADC12REFON+ADC12REF2_5V;
//Turn on ADC12, Sampling time
//On Reference Generator and set
//to 2.5V
ADC12CTL1 = ADC12SHP; //Use sampling timer
ADC12MCTL0 = ADC12SREF_1; //Vr+=Vref+ and Vr-=AVss
for (i=0; i<0x30; i++); //Delay for reference start-up
ADC12CTL0 |= ADC12ENC; //Enable conversions
while (1)
{
ADC12CTL0 |= ADC12SC; //Start conversion
while (!(ADC12IFG & BIT0));
__no_operation(); //SET BREAKPOINT HERE
}
}
//***********************************************************************

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required