0% found this document useful (0 votes)
17 views

lr 7

microprocessor lab report 7

Uploaded by

ahmad hassaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

lr 7

microprocessor lab report 7

Uploaded by

ahmad hassaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MP

Lab # 07

Ahmad Hassaan
Name

FA18-BEE-230
Registration Number

Instructor’s Name Adeel Javed


Pre lab task:
Explain the working of five analog sensors. Explain what physical quantity do they measure and
provide the mapping function for the output voltage.

Quantity being Input Device Output voltage


Measured

Light Level V*I=0.406*I


Solar Cell

Temperature Thermistor Vt=R/R+Rt * Vdd

Strain Gauge
Force/Pressure GF*ἐ/2

Potentiometer
Position Vi=Vr1+Vr2

Tacho-generator
Speed Rl*Ka*Ws/(Rl+Ra)

Sound V=P*g*t
Piezo-electric Crystal
IN-LAB TASK:
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:
/*
* lab_7.c
*
* Created: 20/11/2020 9:09:30 pm
* Author: Junaid Computers
*/

#include <avr/io.h>
#include<stdlib.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <string.h>
#include <math.h>
#include "debug_prints.c"
#define BAUD0 9600
#define MYUBRR (F_CPU/8/BAUD0-1)
#define ADC_CHANNEL0 0
#define ADC_CHANNEL1 1
#define ADC_CHANNEL2 2
#define ADC_VREF 5
#define ADC_RES 10
#define ADC_QLEVELS 1024
unsigned char ADC_Intailize();
unsigned int ADC_Read(unsigned char channel);
float ADC_convert(unsigned int);
unsigned char VintoTemp(float Vin);
unsigned char read_temp_sensor(unsigned char ADC_channel);
#define TEMP_SENSOR_CHANNEL ADC_CHANNEL0
int main(void)
{
ADC_Intailize();
DIDR0=0xFF;
DDRD=0xFF;
UART0_init(MYUBRR);
printSerialStrln("lab 8:");
unsigned char temperature;
while (1)
{
printSerialStr("temperature is :");
temperature=read_temp_sensor(TEMP_SENSOR_CHANNEL);
printSerialInt(temperature);
printSerialStr("\r\n");
PORTD=temperature;
}

}
unsigned char ADC_Intailize()
{
ADMUX|=(1<<ADLAR);
ADMUX|=(1<<REFS0);
ADCSRA|=(1<<ADPS1);
ADCSRB|=(1<<ADPS2);
ADCSRA|=(1<<ADEN);
return 0;
}
unsigned int ADC_Read(unsigned char channel)
{
unsigned char ADC_lo;
unsigned char ADC_hi;
unsigned int result;
ADMUX &=~(0x07);
ADMUX |=channel;
_delay_ms(10);
while ((ADCSRA & (1<<ADSC)) !=0);
ADCSRA |=(1<<ADSC);
while ((ADCSRA &(1<<ADIF) ==0));
ADCSRA |=(1<<ADIF);
result=(ADCH<<8)|(ADCL & 0xC0);
ADC_lo=ADCL;
ADC_hi=ADCH;
result = (ADC_hi<<2) | (ADC_lo>>6);
return result;
}

float ADC_convert(unsigned int ADC_value)


{
float Vin;
Vin=ADC_VREF*((float)ADC_value/ADC_QLEVELS);
return Vin;
}
unsigned char VintoTemp(float Vin)
{
unsigned char temperature=0;
float voltspercentrigrade = 0.01;
temperature = (unsigned char) floor(Vin/voltspercentrigrade);
return temperature;
}
unsigned char read_temp_sensor(unsigned char ADC_channel)
{
unsigned int ADC_value= ADC_Read(ADC_channel);
float Vin= ADC_convert(ADC_value);
unsigned char temp_celsius= VintoTemp(Vin);
return temp_celsius;
}

Proteus simulation:

In lab task 2:
Interface any analog sensor assigned by your lab instructor, Find mapping function for its output
and implement ADC for the sensor using ATmega328p.
Code:
/*
* lab_7.c
*
* Created: 20/11/2020 9:09:30 pm
* Author: Junaid Computers
*/

#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>

#define LED_BIT PD4

/*
* get_adc
* Return the 10bit value of the selected adc channel.
*/
uint16_t get_adc() {

uint16_t value;

// warm up the ADC, discard the first conversion


ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
value = ADCW;

ADCSRA |= (1 << ADSC); // start single conversion


while (ADCSRA & (1 << ADSC)); // wait until conversion is done

return ADCW;
}

int main(void) {

uint8_t i = 0;

DDRD |= 0x1c; // PD2-PD3: col 6-7, PD4: debug LED

// select channel
ADMUX = 5;

// ADC setup
ADCSRA =
(1 << ADEN) | // enable ADC
(1 << ADPS1) | (1 << ADPS0); // set prescaler to 8

// say hello
for (i = 0; i < 5; i++) {
PORTD |= (1 << LED_BIT);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
PORTD &= ~(1 << LED_BIT);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
}
_delay_ms(10);
_delay_ms(10);

while (1) {

if (get_adc() > 180) {


PORTD |= (1 << LED_BIT);
_delay_ms(10);
PORTD &= ~(1 << LED_BIT);
}

return 0;

}
Critical analysis:
We have learnt how to use the ADC function of the Atmega328p. we have learnt how to monitor
the temperature of our surrounding and the detection of sound in our surrounding using the
Atmega328p.

You might also like