You are on page 1of 3

Eka Kakalashvili

RED ID: 823419435

Group 2

LAB REPORT 9
“A/D Conversion”
Description of program
In the nineth lab, called A/D Conversion, we should have written a code that uses timers
and interrupts to convert analog input signals in the range 0 to the reference voltage. This lab is
continuation of lab 7 – Interrupts. After executing code, LED should dim and bright up according
to the value read by the ADC Pin. Brightness should be proportional to the position of the
wiper. To do so, I used the solderless breadboard (MB-102), and jumper cables to connect
ATmega 328PB with the potentiometer, one is in VCC ( ADMUX |= (1<<REFS0) | (1 << MUX1) | (1

<< MUX0)), one in PC3 (DDRC &= ~(1<<PINC3);) and the last one is in ground. After setup, I
declared two timers, from which one controls brightness and other one processes the
conversion. For this lab, I Set up a timer to generate an interrupt every (X+1) ms (for me X was 4
so period is 5ms and OCR0A = 78,125), and PWM Frequency as (Z+1)*100 Hz (Z for me is 9), I
set the input as PINC3, because for me y=3 and y%8=3. I saved the data in global short variable,
and as requested, used infinite loop (while()).

Description of the result

The purpose of this assignment is to send a character on the serial I/O port corresponding
to the value read by the ADC in percent and light up the LED on Atmega board accordingly. As
you can see in the provided videos, all of these requirements and check points are fulfilled. LED
lights up brighter while wiper is moved to the right, and it turns off when wiper is position on
the left side. Therefore, everything was set up appropriately. It also should be mentioned, that
as requested, I used ADC Conversion done by busy wait instead of interrupts.

Source code
/*
* GccApplication23.c
*
* Created: 12/8/2020 5:01:55 PM
* Author : eka
*/

//REDID: 823419435
//XYZ=435; X=4;Y=3;Z=5;
#define F_CPU 16000000UL
#define LEDOFF PORTB &= ~(1<<PORTB5);
#define LEDON PORTB |= (1<<PORTB5);
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

void init_Adc();
void t0();
void t2();

short int dc=0;

int main(void)
{
t0();
t2();
init_Adc();
sei();
DDRB|=(1<<5);
while (1)
{
}
}
//timer 0 initialization, OCR0A is 78,125 for me
void t0(){
TCCR0A|=(1<<1);
TCCR0B |= (1<<2)|(1<<0);
OCR0A = 78.125;
TIMSK0|=(1<<1);
}
//timer 2 initialization, OCR2A for me is 25
void t2(){
TCCR2A|=(1<<1);
TCCR2B|=(1<<0)|(1<<1)|(1<<2);
OCR2A = 25;
TIMSK2 |= (1<<1)|(1<<2);
}
void init_Adc(){
//Y for me is 3; 3%8=3 therefore I used PINC3
DDRC &= ~(1<<PINC3); //my input
ADCSRA |= (1<<ADEN)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADPS2);
//// Enable ADC Auto Trigger & Conversion Complete Interrupt
ADCSRB |= (1<<ADTS1)|(1<<ADTS0); //comparison
ADMUX |= (1<<REFS0) | (1 << MUX1) | (1 << MUX0); //vcc
}
ISR(ADC_vect){
uint16_t dc = ADC;
OCR2B =OCR2A*(dc/1023.0);
// PINC ^= (1<<3); //toggle if needed
}
ISR(TIMER2_COMPA_vect){
LEDON;
}
ISR (TIMER0_COMPA_vect){
ADCSRA|=(1<<6);
}

ISR(TIMER2_COMPB_vect){
if(OCR2B!=OCR2A){
LEDOFF;
}
}

You might also like