Professional Documents
Culture Documents
D3
D2
D1
D0
I0
I1
I2
I3
PORTD7
PORTD4
PORTD2
PORTB5
PORTB4
PORTC3
PORTC2
PORTC1
//Motor derecho.
//Motor izquierdo.
}
void inicializar_puertos(void)
{
DDRD=0x6B;
//0110 1011 0,1,3,5,6 Salidas
PORTD=0x00;
DDRB=0x0A;
//0000 1010 1,3 Salidas
PORTB=0x00;
DDRC=0x01;
//0000 0001 0 Salida
PORTC=0x00;
}
void reset(void)
{
PORTD |= (1<<LEDP); //Encendemos Led en Baby Orangutan.
_delay_ms(300);
PORTD &= ~(1<<LEDP);//Apagamos Led en Baby Orangutan.
_delay_ms(300);
PORTD |= (1<<LEDP);
_delay_ms(300);
PORTD &= ~(1<<LEDP);
_delay_ms(300);
PORTD |= (1<<LEDP);
_delay_ms(300);
PORTD &= ~(1<<LEDP);
}
//Funciones para controlar la velocidad y direccin de los
//motores. PWM controla la velocidad, valor entre 0-255.
void M1_reverse(unsigned char pwm)
{
OCR0A = 0;
OCR0B = pwm;
}
void M1_forward(unsigned char pwm)
{
OCR0B = 0;
OCR0A = pwm;
}
void M2_reverse(unsigned char pwm)
{
OCR2A = 0;
OCR2B = pwm;
}
void M2_forward(unsigned char pwm)
{
OCR2B = 0;
OCR2A = pwm;
}
//Configuracin del hardware del micro que controla los motores.
void motors_init(void)
{
// configure for inverted PWM output on motor control pins:
// set OCxx on compare match, clear on timer overflow
// Timer0 and Timer2 count up from 0 to 255
TCCR0A = TCCR2A = 0xF3;
// use the system clock/8 (=2.5 MHz) as the timer clock
TCCR0B = TCCR2B = 0x02;
// initialize all PWMs to 0% duty cycle (braking)
//Motor derecho.
M2_forward(velocidad);
PORTB |= (1<<LEDV);
PORTD &= ~(1<<LEDR);
}
else if(errort<0)
{
M1_forward(velocidad);
M2_forward(velocidad + errort);
PORTD |= (1<<LEDR);
PORTB &= ~(1<<LEDV);
}
else
{
M1_forward(velocidad);
M2_forward(velocidad);
PORTB &= ~(1<<LEDV);
PORTD &= ~(1<<LEDR);
}
PORTD &= ~(1<<LEDP);
TIFR1 |= (1<<OCF1A);
}
//Motor izquierdo.
//Motor derecho.
//Motor izquierdo.