0% found this document useful (0 votes)
28 views10 pages

Digital Assignment-1

The document describes how to connect an LM34 temperature sensor to an STM32F446RE microcontroller and write an embedded C program to read the ambient temperature. Key steps include: 1) Configuring the microcontroller's GPIO and ADC peripherals to interface with the temperature sensor; 2) Enabling the ADC clock and setting the ADC control and sequence registers; 3) Monitoring the ADC status and data registers to get the temperature reading after each conversion. 4) Calculating the temperature in Celsius by multiplying the ADC reading by 0.16113.

Uploaded by

Dev Shankar Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views10 pages

Digital Assignment-1

The document describes how to connect an LM34 temperature sensor to an STM32F446RE microcontroller and write an embedded C program to read the ambient temperature. Key steps include: 1) Configuring the microcontroller's GPIO and ADC peripherals to interface with the temperature sensor; 2) Enabling the ADC clock and setting the ADC control and sequence registers; 3) Monitoring the ADC status and data registers to get the temperature reading after each conversion. 4) Calculating the temperature in Celsius by multiplying the ADC reading by 0.16113.

Uploaded by

Dev Shankar Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

EMBEDDED SYSTEM DESIGN

DIGITAL ASSIGNMENT-1
NAME: DEV SHANKAR PAUL
REG NO: 19BEE0375
DATE: 29/09/2021
Question: The LM34-TO92 is a Fahrenheit temperature
sensor with the output of 10mV/oF, connect it
appropriately with an STM32F446RE and write an
embedded C program to obtain the ambient
temperature value.

CODE IS DONE AT THE END


Answer:

From the datasheet of STMF446RE, we can see that the ADC is a 12 bit
ADC. 12 bit ADC would mean a maximum of 212 = 4096 steps.

As given in the question, the LM34 produces 10mV for rise in 1oF.

From the datasheet, we can see that the TO-92 Package has a
maximum operating temperature of 300oF, so the maximum operating
voltage is 300x10mV = 3000mV or 3V, which is less than the regular
TTL logic level of 3.3V, so we can safely use that as the Vref for our
ADC.
For this task, I’ll be using GPIO Port C Pin PC0. So, the connection
diagram for the entire circuit is as shown below.
Since we’re using Port C, we need to configure GPIOC_MODER in
analog mode, i.e. we need to initialize it with (11)2 = (3)10

Accordingly we also need to enable the GPIOC Clock as follows.

We need to enable GPIOC clock, so we need to initialise the


RCC_AHB1ENR with (100)2 = (4)10

Now, the data obtained from the sensor will be an analog value, so to
obtain it as a digital value, we need to utilise the internal ADC.
I’ll be using ADC1 for the conversion. The default of 3 clock cycle for
sample time is good enough for temperature sensor LM34, so, I’ll
leave it at its default configuration.

Since we’re using PC0, we need to choose the right input channel for
the ADC. According to the datasheet, we have

The AWDCH[4:0] must be set to 0x0000000A for port pin PC0. This’ll
enable the ADC input channel for the analog input received on pin
PC0.

(Next page please)


Just like other peripherals, the ADC also needs a clock to drive the
conversion. The ADC Clock is enabled by the bits of RCC_APB2ENR.

From here, it’s clear that bit 8 is the ADC1 clock enable bit. So, to
enable ADC1, we need to initialise RCC_APB2ENR with 0x00000100.
We now need to setup the ADC control register, ADC_CR2.

So, we need to first set the ADC_CR2 ADON bit to 1 and then when we
are ready to start the conversion, we need to set the ADC_CR2
SWSTART bit to 1.

We also need to set the ADC_SQR3 register to (10)10 to configure the


conversion sequence to start from channel 10, which is the ADC
channel connected to the pin PC0.

Finally, the last two registers that need to be monitored during the
conversion of the analog data into digital are the ADC_SR (Status
Register) and the ADC_DR (Data Register). Which are given as follows
for the STM32F446RE.
The bit 1 is the EOC flag (End of Conversion) that needs to be
monitored for every conversion that the ADC performs. Once
conversion is complete, the EOC flag will be set and the converted
data will be stored in ADC_DR.

(Next page please)


The ADC_DR will contain the final converted data after the end of
conversion by the ADC.

To calculate the temperature value, we need to multiply the converted


digital

CODE
#include “stm32f4xx.h”
void delayMilisec(int n);

int main(void)
{
int result;
double temperature;
RCC->AHB1ENR |= 4;
GPIOC->MODER |= 3;
RCC->APB2ENR |= 0x00000100;
ADC1->CR2 = 0;
ADC1->SQR3 = 10;
ADC->CR1 |= 0x0000000A;
ADC->CR2 |= 1; // Set the ADON bit to enable ADC1
while(1)
{
ADC1->CR2 |= 0x40000000; // Start conversion
while(!(ADC->SR & 2)) {} // Wait for EOC = 1
result = ADC1->DR;
temperature = (double)result * 0.16113;
delayMilisec(1000);
}
}
void delayMilisec(int n)
{
int i;
for(; n>0; n--)
for(i=0; i<3195; i++);
}

You might also like