From c904f8ec36d21988b05102abd28f52ee10d1d50e Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sat, 1 Dec 2018 14:16:15 +0100 Subject: [PATCH] Use commands from serial rather than ADC input Use channel 0 Use "non-inverted" output --- servomotor/main.c | 57 +++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/servomotor/main.c b/servomotor/main.c index a01d657..ce151f3 100644 --- a/servomotor/main.c +++ b/servomotor/main.c @@ -29,7 +29,6 @@ #include "drivers/serial.h" #include "drivers/gpio.h" #include "extdrv/status_led.h" -#include "drivers/adc.h" #include "drivers/timers.h" @@ -43,7 +42,7 @@ * - inverted one (3), for use with a single transistor * - non inverted (40 - 3), for use with two transistors (or none). */ -#define DUTY_INVERTED 1 +#define DUTY_INVERTED 0 #if (DUTY_INVERTED == 1) #define SERVO_MED_POS_DUTY_CYCLE 3 #else @@ -62,23 +61,14 @@ const struct pio_config common_pins[] = { /* UART 0 */ { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL }, { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL }, - /* TIMER_32B0 */ - { LPC_TIMER_32B0_M1_PIO_0_19, LPC_TIMER_PIN_CONFIG }, + /* TIMER_32B1 */ + { LPC_TIMER_32B1_M0_PIO_0_23, LPC_TIMER_PIN_CONFIG }, ARRAY_LAST_PIO, }; -const struct pio_config adc_pins[] = { - { LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG }, - { LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG }, - { LPC_ADC_AD2_PIO_1_0, LPC_IO_ANALOG }, - { LPC_ADC_AD3_PIO_1_1, LPC_IO_ANALOG }, - { LPC_ADC_AD4_PIO_1_2, LPC_IO_ANALOG }, - { LPC_ADC_AD5_PIO_1_3, LPC_IO_ANALOG }, - ARRAY_LAST_PIO, -}; -const struct pio status_led_green = LPC_GPIO_1_4; -const struct pio status_led_red = LPC_GPIO_1_5; +const struct pio status_led_green = LPC_GPIO_0_28; +const struct pio status_led_red = LPC_GPIO_0_29; /***************************************************************************** */ static uint32_t servo_med_pos_cmd = 0; @@ -93,14 +83,6 @@ int servo_config(uint8_t timer_num, uint8_t pwm_chan, uint8_t uart_num) .period_chan = 3, }; - if (timer_num > LPC_TIMER_32B1) { - uprintf(uart_num, "Bad timer number\n"); - return -1; - } - if (pwm_chan >= 3) { - uprintf(uart_num, "Bad channel number\n"); - return -1; - } timer = timer_num; channel = pwm_chan; timer_conf.outputs[0] = pwm_chan; @@ -124,17 +106,13 @@ int servo_config(uint8_t timer_num, uint8_t pwm_chan, uint8_t uart_num) return 0; } -int set_servo(int adc_num, int uart_num) +int set_servo(int c, int uart_num) { uint16_t val = 0, angle = 0; uint32_t pos = servo_med_pos_cmd; - adc_start_convertion_once(adc_num, LPC_ADC_SEQ(0), 0); - msleep(10); - adc_get_value(&val, adc_num); - uprintf(uart_num, "ADC(%d): %d (raw: 0x%04x)\n", adc_num, val, val); - - angle = ((val * 10) / 56); + val = c - '0'; + angle = (val * 20); if (angle > 180) { angle = 180; } @@ -151,6 +129,12 @@ int set_servo(int adc_num, int uart_num) } +volatile int set_pos = -1; +void change_pos(uint8_t c) +{ + set_pos = c; +} + /***************************************************************************** */ void system_init() { @@ -159,7 +143,6 @@ void system_init() system_set_default_power_state(); clock_config(SELECTED_FREQ); set_pins(common_pins); - set_pins(adc_pins); gpio_on(); status_led_config(&status_led_green, &status_led_red); /* System tick timer MUST be configured and running in order to use the sleeping @@ -185,14 +168,16 @@ void fault_info(const char* name, uint32_t len) int main(void) { system_init(); - uart_on(UART0, 115200, NULL); - adc_on(NULL); - servo_config(LPC_TIMER_32B0, 1, 0); + uart_on(UART0, 115200, change_pos); + + servo_config(LPC_TIMER_32B1, 0, UART0); while (1) { chenillard(500); - /* ADC Test */ - set_servo(LPC_ADC(0), 0); + if (set_pos != -1) { + set_servo(set_pos, UART0); + set_pos = -1; + } } return 0; } -- 2.43.0