main.c
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <avr/io.h>
#include <util/delay.h>
#define CPU_FREQ 16000000L
unsigned int joystick_lr;
unsigned int joystick_ud;
void init_serial(int speed)
{
UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;
UCSR0B = (1<<TXEN0 | 1<<RXEN0);
UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
UCSR0A &= (1 << U2X0);
}
void send_serial(unsigned char c)
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}
unsigned char get_serial(void)
{
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}
void port_initio(void)
{
DDRD &= 0xFC;
DDRD |= 0x3C;
DDRB &= 0xFE;
PORTD |= 0x3C;
}
int main(void)
{
init_serial(19200);
port_initio();
unsigned char ser;
uint8_t opto0 = 0;
uint8_t opto1 = 0;
uint8_t once = 1;
while (1)
{
if((opto0 == 1) && ((PIND & 0x40) == 0x40))
{
if(once)
{
PORTD &= 0xE7;
//_delay_ms(00000);
//PORTD |= 0x18;
//once = 0;
}
}
else
{
once = 1;
PORTD |= 0x18;
}
if((opto1 == 1) && ((PIND & 0x80) == 0x80))
PORTD &= 0xDB;
else
PORTD |= 0x24;
ser = get_serial();
if(ser == 'B')
opto0 = 1;
else if(ser == 'D')
opto0 = 0;
send_serial(ser);
/*
if((PINB & 0x01) == 0x01)
send_serial('1');
else
send_serial('0');
send_serial('\n');
*/
}
return 0;
}