Lecture du joystick

This commit is contained in:
Club robotique de Riom 2024-01-12 18:53:51 +01:00
parent 20481afb70
commit 71b225f477
2 changed files with 53 additions and 6 deletions

View File

@ -20,6 +20,7 @@ target_link_libraries(Mon_Projet
hardware_i2c hardware_i2c
hardware_uart hardware_uart
hardware_pwm hardware_pwm
hardware_adc
pico_stdlib pico_stdlib
pico_multicore pico_multicore
) )

58
main.c
View File

@ -6,11 +6,14 @@
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "hardware/pwm.h" #include "hardware/pwm.h"
#include <stdio.h> #include <stdio.h>
#include "hardware/adc.h"
#define LED_VERTE 25 #define LED_VERTE 25
#define PIN_VITESSE_M1 2 #define PIN_VITESSE_M1 2
#define PIN_SENS_A_M1 0 #define PIN_SENS_A_M1 0
#define PIN_SENS_B_M1 1 #define PIN_SENS_B_M1 1
uint16_t result1 = 0;
uint16_t result0 = 0;
int M1_INITIALISE() int M1_INITIALISE()
{ {
gpio_init(PIN_VITESSE_M1); gpio_init(PIN_VITESSE_M1);
@ -20,6 +23,10 @@ int M1_INITIALISE()
gpio_set_dir(PIN_VITESSE_M1, GPIO_OUT); gpio_set_dir(PIN_VITESSE_M1, GPIO_OUT);
gpio_set_dir(PIN_SENS_A_M1, GPIO_OUT); gpio_set_dir(PIN_SENS_A_M1, GPIO_OUT);
gpio_set_dir(PIN_SENS_B_M1, GPIO_OUT); gpio_set_dir(PIN_SENS_B_M1, GPIO_OUT);
gpio_set_function(PIN_VITESSE_M1, GPIO_FUNC_PWM);
pwm_set_wrap(PIN_VITESSE_M1, 100);
pwm_set_chan_level(PIN_VITESSE_M1, PWM_CHAN_B, 50);
} }
@ -44,11 +51,33 @@ int M1_RECULE()
} }
int Adc_Init()
{
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
}
int AdcRead0()
{
adc_select_input(0);
uint16_t resultadc = adc_read();
return resultadc;
}
int AdcRead1()
{
adc_select_input(1);
uint16_t resultadc= adc_read();
return resultadc;
}
void main() void main()
@ -61,16 +90,33 @@ void main()
pwm_set_chan_level(4, PWM_CHAN_B, 25); pwm_set_chan_level(4, PWM_CHAN_B, 25);
pwm_set_enabled(4, true); pwm_set_enabled(4, true);
//Moteur 1 // ADC
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
while (1)
{
// Voie Y
result0 = AdcRead0();
printf(">result:%d\n",result0-2048);
// Voie X
result1 = AdcRead1();
printf(">result1:%d\n",result1-2048);
printf("test\n",result0);
sleep_ms(100);
}
M1_INITIALISE(); M1_INITIALISE();
while(1) while(1)
{ {
M1_AVANCE(); M1_AVANCE();
sleep_ms(3000); sleep_ms(1000);
M1_RECULE(); M1_RECULE();
sleep_ms(3000); sleep_ms(1000);
} }