Vous êtes sur la page 1sur 5

Experiment 11

Analog Interfacing
Lab Objective
In this lab, we will learn to configure analog-to-digital converter (ADC) and sample sequencers
available in LM4F120 based Stellaris Launchpad. We will measure the temperature using onchip temperature sensor and display the result on a 7-segment display.

ADC in LM4F120
LM4F120 MCU contains two identical 12-bit ADC modules with a capability of 12 shared input
channels and hardware averaging upto 64 samples. Each module is controlled by a number of
registers and offers a variety of options. Both the modules have four sequencers. In this lab,
we will use sequencer 3 since it captures only one sample per trigger and stores the sample into
the corresponding FIFO. The sampling rate can be varied from 125 KSPS to 1 MSPS.

ADC Initialization
In this experiment, we will be using ADC0 and sample sequencer 3 to sample the data of on-chip
temperature sensor and display it on seven segment module. The initialization and configuration
instructions for the ADC module can be found on pg. 776 of the datasheet. A brief description
of these steps is given below. For further details datasheet must be consulted.
1. As with all peripherals, first initialization step is to enble the clock signal for the appropriate GPIO pin and ADC module using RCGCGPIO and RCGCUART registers
respectively. In this experiment, we are using on-chip temperature sensor so, we dont
need to enable the clock signal for any analog input (AIN) pin.
2. Enable the alternate functionality by asserting the appropriate bits in GPIOAFSEL register for the analog signal. Also configure the pin as an input by clearing the respective
bits in GPIODEN register.
3. Disable the analog isolation circuit by asserting the appropriate bits for the respective
analog input (AIN) pins in GPIOAMSEL register.
In this experiment we can skip the above mentioned steps to configure a GPIO pin as an analog
input because we will be using on-chip temperature sensor as analog input. Now, we discuss
the steps to configure sample sequencer.
101

102

CHAPTER 15. ANALOG INTERFACING

1. Set the sampling rate for the ADC by writing appropriate value to the ADCPC register.
2. Disable the sample sequencer by asserting the corresponding bit in ADCACTSS register.
We disable all the peripherals before configuration to avoid any erroneous execution which
may result in unintended results.
3. Select the trigger event from ADCEMUX register. ADC in LM4F120 MCU provides four
different options of software, analog comparator, timer or GPIO which are completely
programmable for each sample.
4. Configure the corresponding input source in the ADCSSMUXn register for each sample in
sample sequence. For example, to configure AIN3 as an input source for the first sample
in sample sequencer 3 we should write 0x03 to the lower nibble of ADCSSMUX3 register.
5. Write the corresponding nibble in ADCSSCTL register for configuring sample control bits
for each sample in sample sequencer. In the last sample, END bit must be asserted to
mark the end of analog sample otherwise ADC may exhibit unpredictable behaviour. To
use internal temperature sensor ensure that TS bit is asserted in the corresponding nibble.
6. If interrupts are to be used, corresponding MASK bit should be asserted in ADCIM
register. In this experiment, we will not use interrupts so 0 should be written to this bit.
7. After configuring the sample sequencer with the required parameters now activate the
sample sequencer by writing 1 to corresponding bit of the sample sequencer.
After configuration ADC is triggered by writing 0x0008 to ADC0PSSI register. When the
conversion is complete, bit 3 of ADC0RIS register is set to 1 automatically by the hardware.
So, by polling this bit, we will know when the conversion is done and the result is ready. Then,
the 12-bit ADC result is read from ADC0 SSFIFO3 register.

Source Code
1 // R e g i s t e r

d e f i n i t i o n s for clock enable


2 #d e f i n e SYSCTL RCGCGPIO R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x400FE608 ) )
3 #d e f i n e SYSCTL RCGCADC R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x400FE638 ) )
4
5 // R e g i s t e r
6 #d e f i n e
7 #d e f i n e
8 #d e f i n e

d e f i n i t i o n s f o r GPIO p o r t A
GPIO PORTA DATA R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0x400043FC ) )
GPIO PORTA DIR R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x40004400 ) )
GPIO PORTA DEN R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x4000451C ) )

9
10 // R e g i s t e r
11 #d e f i n e
12 #d e f i n e
13 #d e f i n e

d e f i n i t i o n s f o r GPIO p o r t B
GPIO PORTB DATA R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0x400053FC ) )
GPIO PORTB DIR R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x40005400 ) )
GPIO PORTB DEN R
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x4000551C ) )

14
15 // R e g i s t e r

d e f i n i t i o n s f o r GPIO p o r t F

103
16 #d e f i n e
17 #d e f i n e
18 #d e f i n e

GPIO PORTF DATA R


GPIO PORTF DIR R
GPIO PORTF DEN R

( ( ( v o l a t i l e u n s i g n e d l o n g ) 0x400253FC ) )
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x40025400 ) )
( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x4002551C ) )

19
20 // R e g i s t e r

d e f i n i t i o n s f o r ADC0 and Sample S e q u e n c e r 3


( ( ( v o l a t i l e unsigned long
ADC0 SSPRI R
( ( ( v o l a t i l e unsigned long
ADC0 ACTSS R
( ( ( v o l a t i l e unsigned long
ADC0 IM R
( ( ( v o l a t i l e unsigned long
ADC0 RIS R
( ( ( v o l a t i l e unsigned long
ADC0 ISC R
( ( ( v o l a t i l e unsigned long
ADC0 SAC R
( ( ( v o l a t i l e unsigned long
ADC0 PSSI R
( ( ( v o l a t i l e unsigned long
ADC0 SSCTL3 R
( ( ( v o l a t i l e unsigned long
ADC0 SSFIFO3 R
( ( ( v o l a t i l e unsigned long

21 #d e f i n e ADC0 PC R
22 #d e f i n e
23 #d e f i n e
24 #d e f i n e
25 #d e f i n e
26 #d e f i n e
27 #d e f i n e
28 #d e f i n e
29 #d e f i n e
30 #d e f i n e

) 0x40038FC4 ) )
) 0 x40038020 ) )
) 0 x40038000 ) )
) 0 x40038008 ) )
) 0 x40038004 ) )
) 0 x4003800C ) )
) 0 x40038030 ) )
) 0 x40038028 ) )
) 0 x400380A4 ) )
) 0 x400380A8 ) )

31
32 u n s i g n e d c h a r Lookup 7Seg Disp [ 1 2 ] = {0xC0 , 0xF9 , 0xA4 , 0xB0 , 0 x99 ,

0 x92 , 0 x82 , 0xF8 , 0 x80 , 0 x90 , 0xC6 } ;

33

34 u n s i g n e d c h a r Temperature Value [ 3 ] = { 0 , 0 , 0xA } ;


35 u n s i g n e d c h a r i , v a l u e =0;
36 u n s i g n e d i n t ADC value = 0 , t e m p e r a t u r e = 0 ;
37
38 v o i d ADC Init ( ) {
39
40

v o l a t i l e unsigned long delay ;

41
42

// Enable c l o c k

43
44
45
46
47
48
49

// Enable c l o c k f o r GPIO p o r t A, B and F [ pg . 3 1 4 ]


SYSCTL RCGCGPIO R |= 0 x23 ;
// Enable c l o c k f o r ADC0 [ pg . 3 2 6 ]
SYSCTL RCGCADC R |= 0 x01 ;
// Delay f o r c l o c k s i g n a l t o s e t t l e down
d e l a y = SYSCTL RCGCADC R ;

50
51

//GPIO A, B, F e n a b l e

52
53
54
55
56

// S e t t h e l a t c h e n a b l e p i n s o f 7 segment a s output [ pg . 6 2 2 ]
GPIO PORTA DIR R |= 0 x38 ;
// S e t t h e d i g i t a l o p e r a t i o n f o r GPIO p o r t A [ pg . 6 4 1 ]
GPIO PORTA DEN R |= 0 x38 ;

57
58
59
60

GPIO PORTB DIR R |= 0xFF ;


GPIO PORTB DEN R |= 0xFF ;
GPIO PORTB DATA R |= 0xFF ;

61
62
63

GPIO PORTF DIR R |= 0 x08 ;


GPIO PORTF DEN R |= 0 x08 ;

104
64

CHAPTER 15. ANALOG INTERFACING

GPIO PORTF DATA R |= 0 x08 ;

65
66

//ADC0 c o n f i g u r e

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

// C l e a r t h e sample r a t e [ pg . 8 4 7 ]
ADC0 PC R &= 0 x00 ;
// S e t sample r a t e e q u a l t o 125 k s p s
ADC0 PC R |= 0 x01 ;
// S e t t h e p r i o r i t y o f sample s e q u e n c e r s .
// Sample s e q u e n c e 0 has h i g h e s t and SS3 has l o w e s t p r i o r i t y [ pg . 7 9 8 ]
ADC0 SSPRI R |= 0 x3210 ;
// D i s a b l e sample s e q u e n c e 3 b e f o r c o n f i g u r a t i o n [ pg . 7 8 0 ]
ADC0 ACTSS R &= 0 x08 ;
// Enable TS0 , IE0 and END0 b i t s [ pg . 8 3 3 ]
ADC0 SSCTL3 R |= 0x0E ;
// Enable 16 x hardware o v e r s a m p l i n g [ pg . 8 0 4 ]
ADC0 SAC R |= 0 x4 ;
// D i s a b l e I n t e r r u p t by w r i t i n g 0 t o c o r r e s p o n d i n g b i t [ pg . 7 8 4 ]
ADC0 IM R &= 0 x08 ;
// A c t i v a t e sample s e q u e n c e r
ADC0 ACTSS R |= 0 x08 ;

85
86 }
87
88 v o i d ADC Temperature ( v o i d ) {
89
90
91

// S o f t w a r e t r i g g e r [ pg . 8 0 2 ]
ADC0 PSSI R |= 0 x08 ;

92
93
94
95
96
97
98
99

// P o l l t h e s t a t u s o f c o r r e s p o n d i n g RIS b i t t o w a i t
// f o r t h e end o f c o n v e r s i o n [ pg . 7 8 2 ]
w h i l e ( ( ADC0 RIS R & 0 x08 )==0) ;
// A f t e r c o n v e r s i o n f e t c h t h e data from t h e FIFO [ pg . 8 1 7 ]
ADC value = ( ADC0 SSFIFO3 R & 0xFFF) ;
// C a l c u l a t e t h e v a l u e o f t e m p e r a t u r e [ pg . 7 7 1 ]
t e m p e r a t u r e = ( u n s i g n e d c h a r ) ( 1 4 7 . 5 ( ADC value 3 . 3 7 5 ) / 4 0 9 6 ) ;

100
101
102

Temperature Value [ 1 ] = t e m p e r a t u r e %10;


Temperature Value [ 0 ] = t e m p e r a t u r e / 1 0 ;

103
104
105

// C l e a r RIS t o s t a r t c o v e r s i o n a g a i n [ pg . 7 8 7 ]
ADC0 ISC R |= 0 x08 ;

106 }
107
108 // Function t o i n t r o d u c e d e l a y
109 v o i d Delay ( u n s i g n e d l o n g count ) {
110
111

int i ;
f o r ( i = 0 ; i < count ; i ++)

105
112

{ }

113 }
114
115 i n t main ( ) {
116
117
118

// I n i t i a l i z e ADC and GPIO p o r t s f o r s e v e n segment d i s p l a y


ADC Init ( ) ;

119
120
121
122
123
124
125

while (1)
{
f o r ( i = 0 ; i <= 2 ; i ++)
{
v a l u e = Temperature Value [ i ] ;
GPIO PORTB DATA R = Lookup 7Seg Disp [ v a l u e ] ;

126

// S e l e c t t h e d i g i t on Port A
GPIO PORTA DATA R = ( 0 x38 ( 1 << ( i +3) ) ) ;
Delay ( 1 0 0 0 ) ;

127
128
129

130

ADC Temperature ( ) ;

131
132
133 }

Vous aimerez peut-être aussi