lr 7
lr 7
Lab # 07
Ahmad Hassaan
Name
FA18-BEE-230
Registration Number
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;
}
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>
/*
* get_adc
* Return the 10bit value of the selected adc channel.
*/
uint16_t get_adc() {
uint16_t value;
return ADCW;
}
int main(void) {
uint8_t i = 0;
// 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) {
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.