25,6 → 25,7 |
#include "INTERRUPTS.h" |
#include "PWM.h" |
#include "I2C1.h" |
#include "UART.h" |
#include "CONTROLLER.h" |
|
void Pins_Init(void) { |
37,6 → 38,11 |
|
// CCP2 on RC3 |
APFCON1bits.CCP2SEL = 0; |
|
// TX/CK function on RA0 |
APFCON0bits.TXCKSEL = 1; |
// RX/DT function on RA1 |
APFCON0bits.RXDTSEL = 1; |
|
LED_1_LAT = 1; |
LED_1_TRIS = 0; |
84,6 → 90,9 |
I2C1_Init(&i2c_data); |
I2C1_Configure_Master(I2C_100KHZ); |
|
UART_DATA uart_data; |
UART_Init(&uart_data); |
|
Controller_Init(); |
|
// Initialize interrupts |
104,11 → 113,67 |
CTRL_BTN_STATUS ctrl; |
Controller_Read(&ctrl); |
|
uint8_t median; |
int16_t L_X, L_Y, R_X, R_Y; |
uint16_t pwm_left = PWM_NOMINAL; |
uint16_t pwm_right = PWM_NOMINAL; |
|
#ifdef CONTROL_FROM_UART |
uint8_t buffer[32]; |
uint8_t length; |
uint8_t uart_state = UART_STATE_READ_CMD; |
uint8_t uart_cmd; |
while(1) { |
length = UART_Read(buffer); |
for (uint8_t i = 0; i < length; i++) { |
if (uart_state == UART_STATE_READ_CMD) { |
if (buffer[i] == UART_CMD_RESET) { |
RESET(); |
} else { |
// Read and save first byte (command) |
uart_cmd = buffer[i]; |
uart_state = UART_STATE_READ_DATA; |
} |
} else if (uart_state == UART_STATE_READ_DATA) { |
uart_state = UART_STATE_READ_CMD; |
|
// Process received data |
if (uart_cmd == UART_CMD_LEFT_FORWARD) { |
pwm_left = PWM_NOMINAL + (uint16_t)(buffer[i] * 2); |
if (buffer[i] != 0) LED_2_On(); |
else LED_2_Off(); |
} else if (uart_cmd == UART_CMD_LEFT_BACKWARD) { |
pwm_left = PWM_NOMINAL - (uint16_t)(buffer[i] * 2); |
if (buffer[i] != 0) LED_2_On(); |
else LED_2_Off(); |
} else if (uart_cmd == UART_CMD_RIGHT_FORWARD) { |
pwm_right = PWM_NOMINAL + (uint16_t)(buffer[i] * 2); |
if (buffer[i] != 0) LED_1_On(); |
else LED_1_Off(); |
} else if (uart_cmd == UART_CMD_RIGHT_BACKWARD) { |
pwm_right = PWM_NOMINAL - (uint16_t)(buffer[i] * 2); |
if (buffer[i] != 0) LED_1_On(); |
else LED_1_Off(); |
} |
|
if (pwm_left > PWM_MAX) |
pwm_left = PWM_MAX; |
if (pwm_left < PWM_MIN) |
pwm_left = PWM_MIN; |
|
if (pwm_right > PWM_MAX) |
pwm_right = PWM_MAX; |
if (pwm_right < PWM_MIN) |
pwm_right = PWM_MIN; |
|
PWM_1_Set(pwm_right); |
PWM_2_Set(pwm_left); |
} |
} |
} |
#endif |
|
#ifdef CONTROL_FROM_CONTROLLER |
uint8_t median; |
int16_t L_X, L_Y, R_X, R_Y; |
while (1) { |
if (!Controller_Read(&ctrl)) { |
median = ctrl.REF / 2; |
167,4 → 232,5 |
LED_2_Off(); |
} |
} |
#endif |
} |