You are on page 1of 5

#include <inttypes.

h>

#define F_CPU 8000000

#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/sleep.h>

#include <util/delay.h>

void ADC_init()

//Output adjust =right//

ADMUX &=~(1<<ADLAR);

//Voltage Reference =AVCC//

ADMUX |= (1<<REFS0);

ADMUX &=~ (1<<REFS1);

//Frecuency divisor =128 ->> 16000/128 =125KHz

ADCSRA |= (1<<ADPS0);

ADCSRA |= (1<<ADPS1);

ADCSRA |= (1<<ADPS2);

int AdcGet(int canal)

//Seleccion del canal ADC//

ADMUX &=~ 0x0F; //0B00001111

ADMUX |= canal;

//Encendemos en ADC

ADCSRA |= (1<<ADEN);
_delay_us(10);

// Mandamos el muestreo

ADCSRA |= (1<<ADSC);

//Esperamos a que muestree, leyendo el flag

while( !(ADCSRA & (1<<ADIF)) );

ADCSRA |= (1<<ADIF); //reiniciamos el flag

//apagamos el ADC

ADCSRA &=~ (1<<ADEN);

return ADC;

int main(void)

ADC_init();

uint16_t valor1=0;

uint16_t valor2=0;

uint16_t valor3=0;

uint16_t valor4=0;

float pwm1=0;

float pwm2=0;

float pwm3=0;

float pwm4=0;

//uint8_t cnt=0;

DDRD |=0b01101000; //Puerto como salida PD3; PD%; PD6

DDRB |= 0b00001000; //salida PB3;


TCCR2A = 0b10100011; // PWM para 0C2A y 0C2B; no invertido, modo rapido

TCCR2B = 0b00000001;

TCCR0A= 0b10100011; // PWM para 0C0A y 0C0B, no invertido, modo rapido

TCCR0B= 0b00000001;

OCR0B=0;

OCR2B=0;

OCR0A=0;

OCR2A=0;

while(1)

valor1 =AdcGet(0);

valor2 =AdcGet(1);

valor3 =AdcGet(2);

valor4 =AdcGet(3);

pwm1=(float)valor1/4;

OCR0B= pwm1; //pwm: 0-255

_delay_ms(1);

pwm2=(float)valor2/4;

OCR0A= pwm2;

_delay_ms(1);

pwm3=(float)valor3/4;

OCR2B=pwm3;

_delay_ms(1);

pwm4=(float)valor4/4;

OCR2A=pwm4;
_delay_ms(1);

#include <avr/io.h>

#include <avr/interrupt.h>

#define F_CPU 1000000UL

#include <util/delay.h>

int main(void)

int8_t cnt=0;

if (cnt==0);

_delay_ms(1000);

cnt=1;

if (cnt==1);

_delay_ms(1000);

cnt=2;

if (cnt==2);

_delay_ms(1000);

cnt=3;

if (cnt==3);

_delay_ms(1000);

cnt=4;

if (cnt==4);
_delay_ms(1000);

DDRB = 0XFF;

DDRD = ((1<<DDD0)|(1<<DDD1));

ADMUX |= (1<<REFS0)|(1<<MUX1)|(1<<MUX0); // ADC3, Vref=5v

ADCSRA |= (1<<ADPS0)|(1<<ADPS1)|(1<<ADEN); //PRE ESCALADOR DE 8 //ADEN-

DIDR0 = (1<<ADC3D); //ADC3

// Write your code here

while (1)

ADCSRA |= (1<<ADSC);

while (ADCSRA & (1<<ADSC));

PORTB= ADCL;

PORTD = ADCH;

_delay_ms(100);

return 0;

cnt=0;

You might also like