Сделал приемлемое управление роботом.
Программа пульта:
/*******************************************
Program : transmitter
Compiler : WinAVR
Chip type : ATtiny2313
System Clock : 8 MHz
*******************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
volatile unsigned char count = 0;
// Timer 0 overflow interrupt service routine
ISR(TIMER0_OVF_vect)
{
TCNT0 = 160;
PORTD ^= 0b01000000;
count += 1;
if( count == 80 )
{
TCCR0B = 0b00000000;
}
}
int main(void)
{
// System Clock Prescaler initialization
// Clock Prescale Register
// Bits: CLKPCE - - - CLKPS3 CLKPS2 CLKPS1 CLKPS0
CLKPR = 0b10000000;
CLKPR = 0b00000000;
// Input/Output Ports initialization
// Port A initialization
DDRA = 0b00000000;
PORTA = 0b00000000;
// Port B initialization
DDRB = 0b00000000; // PB4, PB3, PB2, PB1, PB0 - button
PORTB = 0b11111111;
// Port D initialization
DDRD = 0b01000000; // PD6 - LED
PORTD = 0b00000000;
// Timer/Counter 0 initialization
// Timer/Counter 0 Control Register A
// Bits: COM0A1 COM0A0 COM0B1 COM0B0 Reserved Reserved WGM01 WGM00
TCCR0A = 0b00000000;
// Timer/Counter 0 Control Register B
// Bits: FOC0A FOC0B Reserved Reserved WGM02 CS02 CS01 CS00
TCCR0B = 0b00000000;
// Timer/Counter 0
TCNT0 = 160;
// Timer(s)/Counter(s) Interrupt(s) initialization
// Timer/Counter Interrapt Mask Register
// Bits: TOIE1 OCIE1A OCIE1B Reserved ICIE1 OCIE0B TOIE0 OCIE0A
TIMSK = 0b00000010;
// Global enable interrupts
sei();
while(1)
{
while( PINB != 0b11111111 );
_delay_ms(100);
while( PINB == 0b11111111 );
_delay_ms(100);
count = 0;
TCCR0B = 0b00000001;
while( count != 80 );
if( PINB == 0b11111110 )
{
_delay_us(500);
}
if( PINB == 0b11111101 )
{
_delay_us(2500);
}
if( PINB == 0b11111011 )
{
continue;
}
if( PINB == 0b11110111 )
{
_delay_us(4500);
}
if( PINB == 0b11101111 )
{
_delay_us(6500);
}
count = 0;
TCCR0B = 0b00000001;
while( count != 80 );
}
return 0;
}
(Добавление)
Программа робота:
/*******************************************
Program : receiver
Compiler : WinAVR
Chip type : ATtiny2313
System Clock : 2 MHz
*******************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
volatile unsigned char count = 0;
volatile unsigned char var_1000 = 0b11111111;
// Timer 0 overflow interrupt service routine
ISR(TIMER0_OVF_vect)
{
if( count == 0 )
{
PORTD |= 0b00111100;
}
if( count == 7 )
{
PORTD &= var_1000;
}
if( count == 15 )
{
PORTD &= 0b11000011;
}
count += 1;
if( count == 156 )
{
count = 0;
}
}
volatile unsigned char flag = 0;
int main(void)
{
// System Clock Prescaler initialization
// Clock Prescale Register
// Bits: CLKPCE - - - CLKPS3 CLKPS2 CLKPS1 CLKPS0
CLKPR = 0b10000000;
CLKPR = 0b00000010;
// Input/Output Ports initialization
// Port A initialization
DDRA = 0b00000000;
PORTA = 0b00000000;
// Port B initialization
DDRB = 0b00000000;
PORTB = 0b10000000;
// Port D initialization
DDRD = 0b00111100;
PORTD = 0b00000000;
// Timer/Counter 0 initialization
// Timer/Counter 0 Control Register A
// Bits: COM0A1 COM0A0 COM0B1 COM0B0 Reserved Reserved WGM01 WGM00
TCCR0A = 0b00000000;
// Timer/Counter 0 Control Register B
// Bits: FOC0A FOC0B Reserved Reserved WGM02 CS02 CS01 CS00
TCCR0B = 0b00000000;
// Timer/Counter 0
TCNT0 = 0;
// Timer(s)/Counter(s) Interrupt(s) initialization
// Timer/Counter Interrapt Mask Register
// Bits: TOIE1 OCIE1A OCIE1B Reserved ICIE1 OCIE0B TOIE0 OCIE0A
TIMSK = 0b00000010;
// Global enable interrupts
sei();
while(1)
{
flag = 0;
while( (PINB & (1<<7)) != 0 );
_delay_us(2000);
if( (PINB & (1<<7)) == 0 ) flag = 1;
_delay_us(2000);
if( (PINB & (1<<7)) == 0 ) flag = 2;
_delay_us(2000);
if( (PINB & (1<<7)) == 0 ) flag = 3;
_delay_us(2000);
if( (PINB & (1<<7)) == 0 ) flag = 4;
if( flag == 1 )
{
/* едем прямо */
var_1000 = 0b11001111;
TCCR0B = 0b00000001;
}
if( flag == 2 )
{
/* поворачиваем */
var_1000 = 0b11000011;
TCCR0B = 0b00000001;
}
if( flag == 3 )
{
/* поворачиваем */
var_1000 = 0b11111111;
TCCR0B = 0b00000001;
}
if( flag == 4 )
{
/* едем назад */
var_1000 = 0b11110011;
TCCR0B = 0b00000001;
}
_delay_ms(50);
while( (PINB & (1<<7)) != 0 );
/* стоим на месте */
while( count < 150 );
TCCR0B = 0b00000000;
_delay_ms(50);
}
return 0;
}
(Добавление)
Небольшое пояснение по функциям возложенным на кнопочки пульта:
РВ0 – вперед,
РВ1 – поворот в одну сторону,
РВ2 – стоп,
РВ3 – поворот в другую сторону,
РВ4 – назад. |