Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <avr/io.h>
int main (void)
{
DDRE |= (1 << 2); // Set LED1 as output
DDRG |= (1 << 0); // Set LED2 as output
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
// No MUX values needed to be changed to use ADC0
ADCSRA |= (1 << ADFR); // Set ADC to Free-Running Mode
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D Conversions
for(;;) // Loop Forever
{
// TODO: Test ADC Value and set LEDs
}
}
DDRE |= (1 << 2); // Set LED1 as output
DDRG |= (1 << 0); // Set LED2 as output
#define ADSC 6
#define ADIF 4
void adc_init (void);
unsigned char adc_read (unsigned char ch_select);
void adc_init (void)
{
ADCSRA = 0x83;
}
unsigned char adc_read (unsigned char ch_select)
{
unsigned char data;
ADMUX = ch_select;
ADCSRA.ADSC = 1;
while (ADCSRA.ADIF == 0);
data = ADCH;
ADCSRA.ADSC = 0;
return data;
}
#include<stdio.h>
#include<string.h>
#include<avr/io.h>
#include<util/delay.h>
#include "uart.h"
void ADC_Init();
int read_adc();
unsigned int ADC_calculating();
unsigned int array[10];
unsigned int ADC_VALUE;
unsigned int value,temp;
void main()
{
_delay_ms(50);
UART_Init(25);
UART_TransmitString("Testing the room temperature:\r\n");
DDRB = 0x01;
PORTB= 0x01;
DDRB = 0x00;
PORTB= 0x00;
ADC_Init();
while(1)
{
if(bit_is_clear(PINB,0))
{
ADC_calculating();
while(bit_is_clear(PINB,0));
}
}
}
void ADC_Init()
{
ADCSRA = 0x00;
ADMUX = 0x40;
ADCSRA = 0x86;
}
int read_adc()
{
//select the ADC channel
ADMUX |= 0x01;
//START THE ADC CONVERATION
ADCSRA |= (1<<ADSC);
//WAIT FOR CONVERATION TO COMPLETE
while(!(ADCSRA) & (1<< ADIF));
//CLEAR ADIF BY WRITING ONE TO IT
ADCSRA |= 0x01;
return(ADC);
}
unsigned int ADC_calculating()
{
ADC_VALUE = read_adc();
itoa(ADC_VALUE, array, 10);
UART_TransmitString("ADC:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
value = ADC_VALUE * 4.8875;
itoa(value, array, 10);
UART_TransmitString("Ref:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
temp = value / 10;
itoa(temp, array, 10);
UART_TransmitString("temp:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
UART_TransmitString("\n");
}
Testing the room temperature:
ADC:0
Ref:0
temp:0
ADC:63
Ref:307
temp:30
ADC:62
Ref:303
temp:30
ADC:62
Ref:303
temp:30
ADC:62
Ref:303
temp:30
ADC:62
Ref:303
temp:30
ADC:62
Ref:303
temp:30
ADC:62
Ref:303
temp:30
ADC:63
Ref:307
temp:30
ADC:62
Ref:303
temp:30