.
https://www.mediafire.com/file/sjhxgn70gxshilg/protocol_spi_pwm.png/file
Diff: main.cpp
- Revision:
- 108:2fd41d299a8c
- Parent:
- 107:0965c72c798e
- Child:
- 109:4ee7ffc8f175
--- a/main.cpp Thu Mar 24 21:23:09 2022 +0000 +++ b/main.cpp Tue Apr 05 11:19:38 2022 +0000 @@ -7,79 +7,83 @@ #include "platform/mbed_thread.h" #include <string.h> #include <stdio.h> +#include <stdlib.h> +#include <SPISlave.h> // Blinking rate in milliseconds -#define BLINKING_RATE_MS 500 +#define BLINKING_RATE_MS 2000 -AnalogIn capteurIR_G(A0); -AnalogIn capteurIR_D(A2); +//Define SPI +#define SPI3_MOSI D11 +#define SPI3_MISO D12 +#define SPI3_SCLK D13 +#define SPI3_CS A3 -PwmOut propulsion(D9); -PwmOut direction(D6); +//Define PWM +#define PWM_PROP D10 +#define PWM_DIR D9 -DigitalIn BP1(D2); -DigitalIn BP2(D3); +//Déclarations PWM +PwmOut propulsion(PWM_PROP); +PwmOut direction(PWM_DIR); DigitalOut led(LED1); + +//Déclarations Liaisons static BufferedSerial serial_port(USBTX, USBRX,115200); -SPISlave device(D11, D12, D13, A3); // mosi, miso, sclk, ssel +SPISlave device(SPI3_MOSI, SPI3_MISO, SPI3_SCLK, SPI3_CS); // mosi, miso, sclk, ssel int main() { - char text[50] = {}; - uint32_t bp1,bp1_old = 1; - uint32_t bp2,bp2_old = 1; uint32_t pulsewidth_direction = 1100; - uint16_t capteurIR_g,capteurIR_d; + char texte[32]; + sprintf(texte,"Test\n\r"); + serial_port.write(texte,strlen(texte)); propulsion.period_us(20000); propulsion.pulsewidth_us(1500); + thread_sleep_for(2000); direction.period_us(20000); direction.pulsewidth_us(pulsewidth_direction); + led = 1; + device.format(8); + while (true) { - bp1 = BP1.read(); - bp2 = BP2.read(); + /* - if(!bp1 && bp1_old) - { - led = !led; - if(pulsewidth_direction >1000) - { - pulsewidth_direction -= 10; - } - direction.pulsewidth_us(pulsewidth_direction); - sprintf(text,"pulsewidth_direction = %d\n\r",pulsewidth_direction); - serial_port.write(text,strlen(text)); - capteurIR_g = capteurIR_G.read_u16(); - sprintf(text,"capteurIR G = %d\n\r",capteurIR_g); - serial_port.write(text,strlen(text)); - } - - if(!bp2 && bp2_old) - { - if(pulsewidth_direction < 1300) - { - pulsewidth_direction += 10; - } - direction.pulsewidth_us(pulsewidth_direction); - sprintf(text,"pulsewidth_direction = %d\n\r",pulsewidth_direction); - serial_port.write(text,strlen(text)); - capteurIR_d = capteurIR_D.read_u16(); - sprintf(text,"capteurIR D = %d\n\r",capteurIR_d); - serial_port.write(text,strlen(text)); - } + 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); + */ if (device.receive()) { + led = !led; int v = device.read(); // Read byte from master - v = (v + 1) % 0x100; // Add one to it, modulo 256 - device.reply(v); // Make this the next reply - sprintf(text,"valeur du caractere recu SPI : %d\n\r",v); - serial_port.write(text,strlen(text)); + if (v == 'a') + { + //device.reply('o'); + led = 1; + } + else if (v == 'e') + { + //device.reply('n'); + led = 0; + } } - - bp1_old = bp1; - bp2_old = bp2; - //thread_sleep_for(BLINKING_RATE_MS); } }