A2D_Uart:

   Now for something a little more complex, this time involving the interaction between  Analog to Digital Converter1, Channel 8 and  Usart1. 

Channel 8 (PortB_0) is connected to Potentiometer (R33) and Usart1 is hooked up to the PL2303HX Serial to Usb converter (U3) onboard the Stm32 Mini

We want to read the voltage on the wiper of R33 and serially send out the reading as a 32-Bit Word (Ascii formatted) to the small TTY.exe 

terminal emulator program, which is to be found as an attachment in the TTY folder.

    We begin, as usual, firstly by initialising the peripheral registers we intend to use in this routine. Looking at the Source code, we can see, that

the A2D clock is divided by 6 on Line 12 by setting Bit-15 of the Configuration Register to 1. The clock signals for the peripherals PortA, PortB, Usart1

Adc1 and  the Alternate Function IO are then enabled on Line 15. The Usart1 Tx pin is then configured as an  Alternate function Output (50 Mhz) on PortA_9

and this can be seen on Line 111. PortB_0 is then configured as an Analog Alternate function Input for Channel 8 of the A2D1 Converter on Line 88.

A2D Setup:

Line 92:      Discontinuous mode on regular channels

Line 95:      Start conversion of regular channels, External trigger conversion mode for regular channels and Enable ADC.

Line 99:      Channel 8 Sample time selection = 28.5 Cycles.

Line 101:    Channel 8 = First conversion in regular sequence. 

The Regular channel sequence length Bits are set to 0 by default.      

Usart1 Setup:

Line 115:    9600 Bps.

Line 117:    8 Data , 1 Stop , 0 Parity. 

CalA2D Routine:

    It is recommended to calibrate the A2D peripheral on power up before using it to read in any Values. The calibration takes place in 2 steps.

1:     A Reset calibration must firstly be made by setting the RSTCAL bit to 1 and waiting for it to return to 0, see Line 97.

2:    The Calibration bit Cal is set by software to start the calibration. It is reset by hardware after calibration is complete, see Line 104 .

  Main Routine:

   This routine starts the conversion by setting SWRSTR to 1 on Line 26 and at Label AA Line 29 the End of Conversion Flag (EOC)

is continuously polled until it goes High. The converted Value is read out Right Aligned (By Default) from the  ADC regular data register

and is then passed in R12 to the Serial Word Send Routine.

SerWrd Routine:

    This routine borrows heavily from the example: "Convert a 32 bit hexadecimal word to an Ascii String and output to Terminal",

 which is to be found in the already mentioned  Pdf  "ARM: Assembly Language Programming" Knaggs / Welsh ( Page 106)  

and it is so well described and commented, that I really can't add anything further to it. Just one point though; the instructions on Lines 40 + 62

push {lr} and pop {lr} might at first appear somewhat strange to AVR assembler programmers? The ARM uses a BL ( Branch with return Link)

instruction to call a subroutine. But if we have to call a nested sequence of subroutines, we must push the original return link onto the stack before

calling the next subroutine in sequence and pop it back off the stack before returning. It's different to how we ex-AVR'ers do it. And it can be a source

of irritation and confusion when debugging a seemingly recalcitrant and stubborn code problem. Just something to bear in mind ;-)

Why not read out the On-Chip Temperature Sensor Value and send it out to the TTY? The Stm32 Bible has this to say about it!

1:    Select  ADC1, Channel 16.

2:    Select a Sample time of 17.1 s ( I use a Sample time of  28.5 uS).

3:    Set the TSVREFE bit in the ADC control register 2 (ADC_CR2) to wake up the temperature sensor from power down mode.

4:    Start the ADC conversion by setting the ADON bit.

5:     Read the resulting VSENSE data in the ADC data register

6:     Obtain the temperature using the following formula:

Temperature (in C) = {(V25 - VSENSE) / Avg_Slope} + 25.

Where, V25 = VSENSE value for 25 C and Avg_Slope = Average Slope for curve between Temperature vs. VSENSE (given in mV/ C or V/ C).

 Refer to the Electrical characteristics section for the actual values of V25 and Avg_Slope.

The sensor has a startup time after waking from power down mode before it can output VSENSE at the correct level. 

The ADC also has a startup time after power-on, so to minimize the delay, the ADON and TSVREFE bits should be set at the same time.

Greets to Donald Knuth ;-)

Download