arm Mbed é uma switch de ferramentas que conta com Sistema Operacional para Microcontroladores, Compilador, IDE dentre outros, mas vamos falar nesse artigo do compilador WEB para trabalhar com as placas Nucleo da STM.

iFdo5ZA.png

https://ide.mbed.com/compiler/#nav:/;


Blink:


#include "mbed.h"

DigitalOut Nucleo_GREEN_LED(LED1);
DigitalOut LED(PA_9,0);

int main() 
{
    while(1) 
    {
        Nucleo_GREEN_LED = 1; // LED is ON
        LED.write(0);
        wait(0.5); // 500 ms
        Nucleo_GREEN_LED = 0; // LED is OFF
        LED.write(1);
        wait(0.5); // 500 ms
    }
}


#include "mbed.h"

DigitalOut Nucleo_GREEN_LED(LED1);

int main() 
{
    while(1) 
    {
        Nucleo_GREEN_LED = 1; // LED is ON
        wait(0.5); // 500 ms
        Nucleo_GREEN_LED = 0; // LED is OFF
        wait(0.5); // 500 ms
    }
}


botão:


#include "mbed.h"
 
DigitalOut Nucleo_GREEN_LED(LED1);
DigitalIn button(USER_BUTTON, PullUp);
 
int main() 
{
    while(1) 
    {
        if(button == 0)                    // Button is pressed
        {
            wait_ms(500);                  // Wait 500ms to debounce
            Nucleo_GREEN_LED = !Nucleo_GREEN_LED;                    // Toggle the LED state
        }
    }
}

botão com interrupção

#include "mbed.h"
 
DigitalOut Nucleo_GREEN_LED(LED1);
InterruptIn button(USER_BUTTON, PullUp);
DigitalOut LED(PA_9,0);

void pressed()
{
    LED.write(0);
    wait(0.5); // 500 ms
    LED.write(1);
}
 
int main() 
{
    button.fall(&pressed);
    while(1) 
    {
        Nucleo_GREEN_LED = 1; // LED is ON
        wait(0.5); // 500 ms
        Nucleo_GREEN_LED = 0; // LED is OFF
        wait(0.5); // 500 ms       
    }
}


Serial:


#include "mbed.h"

//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------

DigitalOut led(LED1);
DigitalIn btn(USER_BUTTON);
Serial pc(SERIAL_TX, SERIAL_RX);
 
int main() 
{
  int i = 1;
  //pc.baud (115200);
  pc.printf("Hello World!\r\n");
  while(1) 
  {
      wait (0.95);
      pc.printf("This program runs since %d seconds.\r\n", i++);
      led = 1;
      wait (0.05);
      led = 0;
      if (!btn.read()) // Operator '!' shouldn't be needed
      {
          // User button pressed
          pc.printf("User button pressed!\r\n");
          int j = 20;
          while (j--)
          {
              led = !led;
              wait (0.05);
          }
      }
  }
}
 


Recebendo Serial via interrupção:


#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
Serial pc(USBTX, USBRX);
 
void callback() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    printf("%c\n", pc.getc());
    led2 = !led2;
}
 
int main() {
    pc.attach(&callback);
    
    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}



recebendo Serial - (não está redondo só recebe 1 char):

/**********************************************************************************
* @file    main.cpp
* @author  Petr Dousa, Daniel Toms  modeifyed by Mineiro
* @date    30-August-2020
* @brief   Nucleo UART example - sends and receives data over serial
***********************************************************************************/

/* Includes ----------------------------------------------------------------------*/
#include "mbed.h"
#include 

#define INPUT_BUFFER_SIZE 256

void flushSerialPort();

Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut myled(LED1);

int main()
{
    int i = 0;
    char text[INPUT_BUFFER_SIZE];       //input buffer
    
    //------------------------------------
    // Hyperterminal configuration
    // 9600 bauds, 8-bit data, no parity
    // --------------------------------
    // Communication settings:
    // pc.format(bits,parity,stop_bits)
    //      bits: 5-8
    //      parity: SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0
    //      stop_bits: 1 or 2
    // pc.baudrate(baud)
    //      baud: The desired baudrate (speed)
    // --------------------------------
    
    pc.format(8,SerialBase::None,1);    // This is the default setting
    pc.baud(9600);                      // This is the default setting
    
    //Sends a welcome message over UART (Serial port)
    //Checks if the port is writeable first
    if (pc.writeable())
        pc.printf("Hello World !\n-----------------\nWrite something...\n");
    
    while(1) {
        
        wait(1);
        
        myled = !myled;                             //blinking led indicates activity     

            while (pc.readable())                      //Checks if there are data to be received and stores them into the input buffer
            {    
                text[i++] = pc.getc();  
                pc.printf("readable1: %s", text);   //Note: you could use pc.scanf() instead of getc.
                                                    //pc.scanf() has the same syntax like in regular C apps.                                   
            }              
        
    }
}







Atividades recentes