.
https://www.mediafire.com/file/sjhxgn70gxshilg/protocol_spi_pwm.png/file
Diff: main.cpp
- Revision:
- 111:f11575e7c79b
- Parent:
- 110:a6d1d3525014
- Child:
- 112:478ae92cb106
diff -r a6d1d3525014 -r f11575e7c79b main.cpp --- a/main.cpp Thu Apr 07 12:13:04 2022 +0000 +++ b/main.cpp Thu Apr 07 12:30:01 2022 +0000 @@ -5,13 +5,8 @@ #include "mbed.h" #include "platform/mbed_thread.h" -#include <SPISlave.h> #include "protocol.h" - -// Blinking rate in milliseconds -#define BLINKING_RATE_MS 2000 - //Define SPI #define SPI3_MOSI D11 #define SPI3_MISO D12 @@ -22,67 +17,52 @@ #define PWM_PROP D10 #define PWM_DIR D9 -//Déclarations PWM +//Declaration PWM outputs PwmOut propulsion(PWM_PROP); PwmOut direction(PWM_DIR); -//Déclarations Liaisons +//Declaration Links static BufferedSerial serial_port(USBTX, USBRX,115200); SPISlave device(SPI3_MOSI, SPI3_MISO, SPI3_SCLK, SPI3_CS); // mosi, miso, sclk, ssel int main() { + //Declaration PWM variables uint32_t pulsewidth_direction = 1100; uint32_t pulsewidth_propulsion = 1500; - char texte[32]; + //Test Serial port + serial_port.write("Test\n\r",strlen("Test\n\r")); - sprintf(texte,"Test\n\r"); - serial_port.write(texte,strlen(texte)); - + //Init. propulsion PWM propulsion.period_us(20000); propulsion.pulsewidth_us(pulsewidth_propulsion); thread_sleep_for(2000); + //Init. Direction PWM direction.period_us(20000); direction.pulsewidth_us(pulsewidth_direction); + //Init. SPI Link device.format(8); - while (true) { + //Infinite loop + while(1) + { + //If SPI received a char, decode it then reply bullshit. if(device.receive()) { - //decodeMessage((char)device.read()); + decodeMessage((char)device.read()); + device.reply(0b10101010); } + + //If decoding has ended, get and changes PWM values. if(isDataAvailable()) { getVerifiedPWMValues(&pulsewidth_propulsion, &pulsewidth_direction); propulsion.pulsewidth_us(pulsewidth_propulsion); direction.pulsewidth_us(pulsewidth_direction); - } - - /* - - propulsion.pulsewidth_us(1450); - thread_sleep_for(BLINKING_RATE_MS); - led = !led; - propulsion.pulsewidth_us(1400); - thread_sleep_for(BLINKING_RATE_MS); - led = !led; - propulsion.pulsewidth_us(1500); - thread_sleep_for(BLINKING_RATE_MS); - led = !led; - propulsion.pulsewidth_us(1550); - thread_sleep_for(BLINKING_RATE_MS); - led = !led; - propulsion.pulsewidth_us(1600); - thread_sleep_for(BLINKING_RATE_MS); - led = !led; - propulsion.pulsewidth_us(1800); - thread_sleep_for(BLINKING_RATE_MS); - */ - - + } } }