0% found this document useful (0 votes)
15 views3 pages

ADC and Timer Configuration Code

The document contains code for initializing an ADC, USART, timers, and PWM on an Arduino. The ADC_init() function configures the ADC for right-adjusted output, a 5V reference voltage, and a frequency divider of 128 for a sample rate of 125kHz. The GetData() function selects an ADC channel, starts a conversion, waits for it to complete, and returns the result. The USART_config() function configures the USART for 8-bit UART communication at 9600 baud by setting control and baud rate registers. The Timer1_init() and Timer1_1seg() functions initialize Timer1 for general use and 1 second
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)
15 views3 pages

ADC and Timer Configuration Code

The document contains code for initializing an ADC, USART, timers, and PWM on an Arduino. The ADC_init() function configures the ADC for right-adjusted output, a 5V reference voltage, and a frequency divider of 128 for a sample rate of 125kHz. The GetData() function selects an ADC channel, starts a conversion, waits for it to complete, and returns the result. The USART_config() function configures the USART for 8-bit UART communication at 9600 baud by setting control and baud rate registers. The Timer1_init() and Timer1_1seg() functions initialize Timer1 for general use and 1 second
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

void ADC_init()

{
// Output ajuste a la derecha
ADMUX &=~ (1<<ADLAR);

//Configuración del Voltaje de referencia AV cc=5V


ADMUX |= (1<<REFS0);
ADMUX &=~ (1<<REFS1);

// Divisor de Frecuencia = 128 -> 16000000/128 = 125 KHz (entre 50 y 200 KHz)
ADCSRA |= (1<<ADPS0);
ADCSRA |= (1<<ADPS1);
ADCSRA |= (1<<ADPS2);
}

int GetData(int canal) //Función que retorna un valor


{
// Select MUX verificar la variable canal!
ADMUX &=~ 0x0F;
ADMUX |= canal;

ADCSRA |= (1<<ADEN); // Activación del ADC


_delay_us(10); // Tiempo de calentamiento

ADCSRA |= (1<<ADSC); // Start conversion

// Read flag (Indica cuando el muestreo ha terminado)


while( !(ADCSRA & (1<<ADIF)) );
ADCSRA |= (1<<ADIF); // Reset flag

//Desactivamos el ADC
ADCSRA &=~ (1<<ADEN);

return ADC; // Retorna el valor de la conversión(guarda relación con la


configuración del ADLAR)
}

void USART_config()
{
//Config as UART

UCSR0C &=~ (1<<UMSEL00);


UCSR0C &+~ (0<<UMSEL01);

//Parity disable

UCSR0C &=~ (1<<UPM00);


UCSR0C &+~ (0<<UPM01);

//Stop bit -1bit

UCSR0C &=~ (1<<USBS0);

//8 bits data


UCSR0C |= (1<<UCSZ00);
UCSR0C |= (1<<UCSZ01);
UCSR0B &=~(1<<UCSZ02);

//Baud rate

UCSR0A |= (1<<U2X0);
UBRR0 = (F_CPU/8/9600)-1;

UCSR0B |= (1<<TXEN0);
UCSR0B |= (1<<RXEN0);

void Timer1_init()
{
//Timer1 settings
TCCR1A = (0<<COM1A1)|(0<<COM1A0)|(0<<WGM10)|(0<<WGM11);
TCCR1B = (0<<WGM12)|(0<<WGM13)|(1<<CS10)|(0<<CS11)|(1<<CS12);
TCNT1 = 34286;
TIMSK1 |= (1<<TOIE1);
TIFR1 = 0;
PORTB &=~ (1<<DDB5);
}

void Timer1_1seg()
{
TCCR1A = 0;
TCCR1B = (1<<CS12);
TCNT1 = 46005;
TIMSK1 = (1<<TOIE1);
TIFR1 = 0;
PORTB &=~ (1<<DDB5);
}

void PWM_init(int freq, int duty)

//Pin configuration

DDRB |= (1<<DDB1);
//Output active non inverting mode

TCCR1A &=~ (1<<COM1A0);

TCCR1A |= (1<<COM1A1);

//FAST PWM

TCCR1A &=~ (1<<WGM10);

TCCR1A |= (1<<WGM11);

TCCR1B |= (1<<WGM12);

TCCR1B |= (1<<WGM13);

//Set frequency and duty cycle

ICR1 = (F_CPU/1024/freq)-1;

OCR1A =(((F_CPU/1024/freq)-1)*duty)/100;

//PWM on

TCNT1 = 0;

//PrescaleR 1024

TCCR1B|= (1<<CS10);

TCCR1B|= (1<<CS12);

FCPU
F out =
2 N ( 1+OCR )
FCPU
OCR= −1
2 N Fout

You might also like