Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions ball_detect/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#define BALL_SENSOR_1 0
#define BALL_SENSOR_2 1
#define BALL_SENSOR_3 2
#define DIP_SWITCH_1 0
#define DIP_SWITCH_2 1
#define DIP_SWITCH_3 2
#define DIP_SWITCH_4 3
#define DIP_SWITCH_5 4
#define DIP_SWITCH_6 5
#define F_OSC F_CPU
#define ULTRASONIC_SENSOR 4
#define waitForTX() while (!(UCSR0A & 1<<UDRE0))
Expand All @@ -18,9 +24,7 @@ volatile int32_t ends_of_pulses[4];
volatile int32_t lenghts_of_pulses[4];
volatile int32_t starts_of_pulses[4];
volatile int32_t vision_result[3];
uint8_t changed_bits;
uint8_t pinstate, ct;
uint8_t portb_history = 0xFF;
volatile uint8_t pinstate, ct, changed_bits, portb_history = 0xFF, my_address;

void setup(void) {
//USART INITIALIZATION
Expand All @@ -32,10 +36,22 @@ void setup(void) {

//LED REGISTER SETUP
DDRD |= 1 << 3;

//DIP-SWITCH INITIALIZATION
DDRC = 0;
PORTC |= ((1 << DIP_SWITCH_1)
| (1 << DIP_SWITCH_2)
| (1 << DIP_SWITCH_3)
| (1 << DIP_SWITCH_4)
| (1 << DIP_SWITCH_5)
| (1 << DIP_SWITCH_6));

//INTERRUPTS INIT
DDRB = 0;
PORTB |= ((1 << BALL_SENSOR_1) | (1 << BALL_SENSOR_2) | (1 << BALL_SENSOR_3) | (1 << ULTRASONIC_SENSOR));
PORTB |= ((1 << BALL_SENSOR_1)
| (1 << BALL_SENSOR_2)
| (1 << BALL_SENSOR_3)
| (1 << ULTRASONIC_SENSOR));
PCICR |= 1;
PCMSK0 |= 0xF;

Expand All @@ -44,6 +60,17 @@ void setup(void) {
TCCR1B = 2;
TCNT1 = 0;

//DETECT ADRESS
if((PINC & (1 << DIP_SWITCH_1)) == 0) {
my_address = 1;
} else if((PINC & (1 << DIP_SWITCH_2)) == 0) {
my_address = 2;
} else if((PINC & (1 << DIP_SWITCH_3)) == 0) {
my_address = 3;
} else if((PINC & (1 << DIP_SWITCH_4)) == 0) {
my_address = 4;
}

//TURN ON INTERRUPTS
sei();
}
Expand All @@ -54,10 +81,37 @@ void sendc(uint8_t ch) {
}

ISR(USART_RX_vect) {
PORTD ^= 1 << 3;
uint32_t modulo, position;
uint8_t read_char = UDR0, i, j;
if(read_char == my_address) {
for(j = 0; j < 3; j++) {
modulo = vision_result[j];
position = 1000000;
for(i = 0; i < 7; i++) {
sendc((modulo / position) % 10);
modulo %= position;
position /= 10;
}
}
} else if((read_char - 48) == my_address) {
for(j = 0; j < 3; j++) {
modulo = vision_result[j];
position = 1000000;
sendc('0' + j);
sendc(' ');
for(i = 0; i < 7; i++) {
sendc('0' + ((modulo / position) % 10));
modulo %= position;
position /= 10;
}
sendc('\r');
sendc('\n');
}
}
}

ISR(PCINT0_vect) {
PORTD ^= 1 << 3;
pinstate = PINB;
changed_bits = pinstate ^ portb_history;
portb_history = pinstate;
Expand All @@ -76,24 +130,11 @@ ISR(PCINT0_vect) {
}

int main(void) {
uint8_t j, index = 0;
uint32_t small;
setup();
while (1) {
small = 9999999;
vision_result[0] = lenghts_of_pulses[0];
vision_result[1] = lenghts_of_pulses[1];
vision_result[2] = lenghts_of_pulses[2];
for(j = 0; j < 3; j++) {
if(small > vision_result[j]) {
small = vision_result[j];
index = j;
}
}
sendc('0' + index);
sendc('\r');
sendc('\n');
_delay_ms(200);
}
return 0;
}