From 6c907068a87fdadd5f190194a4c0751495dde543 Mon Sep 17 00:00:00 2001 From: "Ishan S. Patel" Date: Sun, 19 Sep 2021 19:19:04 -0400 Subject: [PATCH] yacwc --- helpers_uart.c | 5 + main.c | 189 ++++--- nbproject/configurations.xml | 930 ++++++++++++++++++++++++++++++++++- 3 files changed, 1063 insertions(+), 61 deletions(-) diff --git a/helpers_uart.c b/helpers_uart.c index 39e0f81..a2e1e1b 100644 --- a/helpers_uart.c +++ b/helpers_uart.c @@ -8,6 +8,10 @@ void USART0_init(void) { USART0.BAUD = (uint16_t) USART0_BAUD_RATE(9600); USART0.CTRLB |= USART_TXEN_bm; + USART0.CTRLB |= USART_RXEN_bm; +// USART0.CTRLB |= USART_SFDEN_bm; +// USART0.CTRLA |= USART_RXSIE_bm; + USART0.CTRLA |= USART_RXCIE_bm; } void USART0_sendChar(char c) { @@ -20,6 +24,7 @@ void USART0_sendChar(char c) { + void USART0_sendString(const char *str) { for (size_t i = 0; i < strlen(str); i++) { USART0_sendChar(str[i]); diff --git a/main.c b/main.c index 24fab3f..c7001d1 100644 --- a/main.c +++ b/main.c @@ -7,23 +7,27 @@ #include #include #include "helpers_uart.h" - +#include #include #include "twi0_master_example.h" +#define LISTEN_EXPANDER_COMMANDS true +#define IGNORE_SOFT_LIMITS false #define delay_time 50 -// Zoom, Focus, Iris -struct PORT_struct *port_1[] = {&PORTA, &PORTB, &PORTC}; -struct PORT_struct *port_2[] = {&PORTA, &PORTB, &PORTC}; -struct PORT_struct *port_3[] = {&PORTA, &PORTC, &PORTC}; -struct PORT_struct *port_4[] = {&PORTA, &PORTC, &PORTC}; +// Zoom -> motor 0 +// Focus -> motor 1 +// Iris -> motor 2 +struct PORT_struct *port_1[] = {&PORTB, &PORTA, &PORTC}; +struct PORT_struct *port_2[] = {&PORTB, &PORTA, &PORTC}; +struct PORT_struct *port_3[] = {&PORTC, &PORTA, &PORTC}; +struct PORT_struct *port_4[] = {&PORTC, &PORTA, &PORTC}; -uint8_t pin_1[] = {2, 5, 4}; -uint8_t pin_2[] = {3, 4, 5}; -uint8_t pin_3[] = {4, 1, 6}; -uint8_t pin_4[] = {5, 0, 7}; +uint8_t pin_1[] = {5, 2, 4}; +uint8_t pin_2[] = {4, 3, 5}; +uint8_t pin_3[] = {0, 4, 6}; +uint8_t pin_4[] = {1, 5, 7}; uint8_t delay[] = {15, 15, 50}; // Multiples of "delay_time" uint16_t max_steps[] = {2994, 5180, 77}; @@ -37,28 +41,74 @@ uint8_t phase; uint8_t motor_num; uint8_t port_val; +char usart_recv_char; +bool usart_interrupt = false; + + +uint8_t read_low_usart_bit(){ + while (!(USART0.STATUS & USART_RXCIF_bm)) { + } + return USART0.RXDATAL; +} +uint8_t read_high_usart_bit(){ + while (!(USART0.STATUS & USART_RXCIF_bm)) { + } + return USART0.RXDATAH; +} + + +ISR(USART0_RXC_vect) +{ + + + usart_recv_char = read_low_usart_bit(); + USART0_sendChar(usart_recv_char); + + + +} + + +void set_ports_out() +{ + for (int i = 0; iDIRSET = _BV(pin_1[i]); + port_2[i]->DIRSET = _BV(pin_2[i]); + port_3[i]->DIRSET = _BV(pin_3[i]); + port_4[i]->DIRSET = _BV(pin_4[i]); + + port_1[i]->OUTSET = _BV(pin_1[i]); + port_2[i]->OUTSET = _BV(pin_2[i]); + port_3[i]->OUTSET = _BV(pin_3[i]); + port_4[i]->OUTSET = _BV(pin_4[i]); + + } +} void init_port_expander() { I2C0_example_write1ByteRegister(32, 0x00, 0x00); - I2C0_example_write1ByteRegister(32, 0x00, 0x00); + I2C0_example_write1ByteRegister(32, 0x09, 0x00); } void set_expanded_port_on(uint8_t num) { - port_val = I2C0_example_read1ByteRegister(32, 0x09); - port_val |= _BV(num); - I2C0_example_write1ByteRegister(32, 0x09, port_val); + if (LISTEN_EXPANDER_COMMANDS) { + port_val = I2C0_example_read1ByteRegister(32, 0x09); + port_val |= _BV(num); + I2C0_example_write1ByteRegister(32, 0x09, port_val); + } } void set_expanded_port_off(uint8_t num) { - port_val = I2C0_example_read1ByteRegister(32, 0x09); - port_val &= ~_BV(num); - I2C0_example_write1ByteRegister(32, 0x09, port_val); + if (LISTEN_EXPANDER_COMMANDS) { + port_val = I2C0_example_read1ByteRegister(32, 0x09); + port_val &= ~_BV(num); + I2C0_example_write1ByteRegister(32, 0x09, port_val); + } } - void battery_charging_on() { set_expanded_port_on(0); @@ -93,7 +143,6 @@ void power_3v3_off() PORTA.OUTCLR = _BV(6); } - void delay_n_units(uint8_t n) { for (int i = 0; i < n; i++) { @@ -133,15 +182,17 @@ void step_phased(struct PORT_struct * p1, struct PORT_struct * p2, struct PORT_s void step_motor(uint8_t i, int s) { - set_expanded_port_on(i); - for (int n = 0; i < abs(s); n++) { + + for (int n = 0; n < abs(s); n++) { phase = abs(current_step[i] % 4); motor_max_pos = (current_step[i] >= max_steps[i]) && (s > 0); motor_min_pos = (current_step[i] <= 0) && (s < 0); if (motor_max_pos || motor_min_pos) { - return; + if (!IGNORE_SOFT_LIMITS) { + return; + } } step_phased(port_1[i], port_2[i], port_3[i], port_4[i], pin_1[i], pin_2[i], pin_3[i], pin_4[i], phase); @@ -159,28 +210,28 @@ void step_motor(uint8_t i, int s) void step_iris_phased(int steps) { - set_expanded_port_on(2); + set_expanded_port_on(4); step_motor(2, steps); - set_expanded_port_off(2); -} - -void step_zoom_phased(int steps) -{ - set_expanded_port_on(1); - step_motor(1, steps); - set_expanded_port_off(1); + set_expanded_port_off(4); } void step_focus_phased(int steps) { - set_expanded_port_on(0); + set_expanded_port_on(3); + step_motor(1, steps); + set_expanded_port_off(3); +} + +void step_zoom_phased(int steps) +{ + set_expanded_port_on(2); step_motor(0, steps); - set_expanded_port_off(0); + set_expanded_port_off(2); } bool at_zoom_limit() { - if (PORTD.IN & PIN0_bm) { + if (PORTD.IN & PIN1_bm) { return false; } else { return true; @@ -190,7 +241,7 @@ bool at_zoom_limit() bool at_focus_limit() { - if (PORTD.IN & PIN1_bm) { + if (PORTD.IN & PIN0_bm) { return false; } else { return true; @@ -198,25 +249,14 @@ bool at_focus_limit() } -void home_focus() -{ - motor_num = 0; - current_step[motor_num] = max_steps[motor_num] + 200; - step_motor(motor_num, -100); - for (int i = 0; i < max_steps[motor_num]; i++) { - step_motor(motor_num, -1); - if (at_focus_limit()) { - break; - } - } - current_step[motor_num] = 0; -} - void home_zoom() { - motor_num = 1; + + motor_num = 0; + set_expanded_port_on(motor_num + 2); current_step[motor_num] = max_steps[motor_num] + 200; step_motor(motor_num, -100); + _delay_ms(100); for (int i = 0; i < max_steps[motor_num]; i++) { step_motor(motor_num, -1); if (at_zoom_limit()) { @@ -224,14 +264,34 @@ void home_zoom() } } current_step[motor_num] = 0; + set_expanded_port_off(motor_num + 2); +} + +void home_focus() +{ + motor_num = 1; + set_expanded_port_on(motor_num + 2); + current_step[motor_num] = max_steps[motor_num] + 200; + step_motor(motor_num, -100); + _delay_ms(100); + for (int i = 0; i < max_steps[motor_num]; i++) { + step_motor(motor_num, -1); + if (at_focus_limit()) { + break; + } + } + current_step[motor_num] = 0; + set_expanded_port_off(motor_num + 2); } void home_iris() { - motor_num = 3; + motor_num = 2; + set_expanded_port_on(motor_num + 2); current_step[motor_num] = max_steps[motor_num] + 200; step_motor(motor_num, -100); current_step[motor_num] = 0; + set_expanded_port_off(motor_num + 2); } void ir_filter_on() @@ -256,29 +316,38 @@ void init_pins() PORTD.DIRCLR = PIN0_bm; PORTD.DIRCLR = PIN1_bm; + PORTD.PIN0CTRL |= PORT_ISC_BOTHEDGES_gc; + PORTD.PIN1CTRL |= PORT_ISC_BOTHEDGES_gc; + // IR Filter PORTB.DIRSET = _BV(2); PORTB.DIRSET = _BV(3); - + //i2c muxing PORTMUX.TWISPIROUTEA = 0x23; + set_ports_out(); + } int main(void) { + init_pins(); - I2C0_Initialize(); - init_port_expander(); USART0_init(); + I2C0_Initialize(); + power_3v3_on(); + init_port_expander(); + sei(); + if (!LISTEN_EXPANDER_COMMANDS) { + I2C0_example_write1ByteRegister(32, 0x09, 0xFF); + } + home_zoom(); + home_focus(); + home_iris(); while (1) { - step_zoom_phased(6000); - step_iris_phased(100); - step_focus_phased(2000); - step_zoom_phased(-6000); - step_iris_phased(-100); - step_focus_phased(-2000); + } return 0; } diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 6c79971..11077d1 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -42,7 +42,7 @@ ATmega4809 - Simulator + AtmelIceTool XC8 2.32 2 @@ -200,8 +200,478 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -226,6 +696,24 @@ + + + + + + + + + + + + + + + + + + @@ -237,11 +725,451 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +