Digital Assignment-1
Digital Assignment-1
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.
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
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.
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.
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.
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++);
}