Fa20-Bee-150 MP Lab Report - 07
Fa20-Bee-150 MP Lab Report - 07
Fa20-Bee-150 MP Lab Report - 07
“MICROPROCESSORS AND
INTERFACING”
EEE-342
CLASS: BEE-5c
REG No: FA20-BEE-150
In Lab Tasks:
Task-1 Use LM35 to sense the room temperature. Convert this data into
digital using atmega328P ADC and display temperature value on virtual
terminal. Complete the Code and simulate on Proteus.
Code:
#include <inttypes.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <string.h>
#include <math.h>
/****** Definitions for UART *******/
#define BAUD0 9600 // Baud Rate for UART
#define MYUBRR (F_CPU/8/BAUD0-1)
#define ADC_CHANNEL0 0
#define ADC_CHANNEL1 1
#define ADC_CHANNEL2 2
#define ADC_VREF 5 // Ref voltage for ADC is 5 Volts
#define ADC_RES 10 // Resolution of ADC in bits
#define ADC_QLEVELS 1024 // Quantization levels for the ADC
unsigned char ADC_Initialize();
unsigned int ADC_Read(unsigned char channel); // Reads the result of a
//single conversion from the ADC
#include "debug_prints.c"
float ADC_Convert(unsigned int);
unsigned char VinToTemp(float Vin);
unsigned char read_temp_sensor(unsigned char ADC_channel);
{
DIDR0 = 0xFF; //Disable digital I/O
DDRD = 0xFF;
UART0_init(MYUBRR);
UART0_send_char('X');
ADC_Initialize();
while(1)
{
printSerialStr("Temperature is: ");
temprature = read_temp_sensor(TEMP_SENSOR_CHANNEL);
printSerialInt(temprature);
printSerialStr("\r \n");
PORTD = temprature;
}
}
// Function Initializes the ADC for 10-Bit Single Conversion mode..
unsigned char ADC_Initialize()
{
ADMUX |=(1<<ADLAR)|(1<<REFS0);
ADCSRA = 0b11000111;
return 0;
}
/* Function reads the result of a single conversion from the ADC
channel given as an argument*/
unsigned int ADC_Read(unsigned char channel)
{
unsigned char ADC_lo;
unsigned char ADC_hi;
unsigned int result;
ADMUX &= ~(0x07); // clear previous selection of channel
ADMUX |= channel; // Select the new channel
// Delay needed for the stabilization of the ADC input voltage
_delay_us(10);
//wait for ADC to finish any ongoing operation
4 Lab Report - 07
{
unsigned int ADC_value = ADC_Read(ADC_channel); // Read the sensor
Connected at ADC_channel...
float Vin = ADC_Convert(ADC_value); // Get the value in floating point...
unsigned char temp_celsius = VinToTemp (Vin); // Convert to
temperature and return...
return temp_celsius;
}
Proteus Simulation: