NUCLEO

Z MediaWiki SPŠ a VOŠ Písek
Skočit na navigaci Skočit na vyhledávání

soutěž:

Cortex
Nucleo KIT
Vývojový kit
Vývojový kit
Nucleo pinout

Arduino-compatible headers

Morpho headers

These headers give access to all STM32 pins.

Source code

IDE:

IDE

Compiler:

compiler

Example:

 
#include "mbed.h"
 
DigitalOut myled(LED1);
 
int main() {
    while(1) {
        myled = 1; // LED is ON
        wait(0.2); // 200 ms
        myled = 0; // LED is OFF
        wait(1.0); // 1 sec
    }
}


Blink color LED RGB

#include "mbed.h"
 
DigitalOut red(D5);
DigitalOut blue(D8);
DigitalOut green(D9);
int i;
 
int main() {
    while(1) {
        for (i=1; i<7; i++) {
            red = i & 1;
            blue = i & 2;
            green = i & 4;
            wait(0.2);
        }
    }
}

Read Button

#include "mbed.h"
 
DigitalIn mybutton(USER_BUTTON);
DigitalOut myled(LED1);
 
int main() {
  while(1) {
    if (mybutton == 0) { // Button is pressed
      myled = !myled; // Toggle the LED state
      wait(0.2); // 200 ms
    }
  }
}

PWM

#include "mbed.h"
 
PwmOut mypwm(PWM_OUT);
 
DigitalOut myled(LED1);
 
int main() {
    
    mypwm.period_ms(10);
    mypwm.pulsewidth_ms(1);
  
    printf("pwm set to %.2f %%\n", mypwm.read() * 100);
    
    while(1) {
        myled = !myled;
        wait(1);
    }
}

Nucleo read button interrupt

#include "mbed.h"
 
InterruptIn mybutton(USER_BUTTON);
DigitalOut myled(LED1);
 
float delay = 1.0; // 1 sec
 
void pressed()
{
    if (delay == 1.0)
        delay = 0.2; // 200 ms
    else
        delay = 1.0; // 1 sec
}
 
int main()
{
    mybutton.fall(&pressed);
    while (1) {
        myled = !myled;
        wait(delay);
    }
}

Nucleo_pwm2

#include "mbed.h"
 
DigitalOut  my_led(LED1);
InterruptIn my_button(USER_BUTTON);
PwmOut      my_pwm(PB_3);
 
void pressed() {
    if (my_pwm.read() == 0.25) {
        my_pwm.write(0.75);
    }
    else {
        my_pwm.write(0.25);
    }
}
 
int main()
{
    // Set PWM
    my_pwm.period_ms(10);
    my_pwm.write(0.5);
    
    // Set button
    my_button.fall(&pressed);
    
    while (1) {
        my_led = !my_led;
        wait(0.5); // 500 ms
    }
}

Nucleo_read_analog_value

#include "mbed.h"
 
AnalogIn analog_value(A0);
 
DigitalOut led(LED1);
 
int main() {
    float meas;
    
    printf("\nAnalogIn example\n");
    
    while(1) {
        meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
        meas = meas * 3300; // Change the value to be in the 0 to 3300 range
        printf("measure = %.0f mV\n", meas);
        if (meas > 2000) { // If the value is greater than 2V then switch the LED on
          led = 1;
        }
        else {
          led = 0;
        }
        wait(0.2); // 200 ms
    }
}

Nucleo_pwm3

#include "mbed.h"
 
Timeout timer;
DigitalOut my_led(LED1);
DigitalOut my_pwm(D10); // IO used by pwm_io function
 
int on_delay = 0;
int off_delay = 0;
 
void toggleOff(void);
 
void toggleOn(void) {
    my_pwm = 1;
    timer.attach_us(toggleOff, on_delay);
}
 
void toggleOff(void) {
    my_pwm = 0;
    timer.attach_us(toggleOn, off_delay);
}
 
// p_us = signal period in micro_seconds
// dc   = signal duty-cycle (0.0 to 1.0)
void pwm_io(int p_us, float dc) {
    timer.detach();
    if ((p_us == 0) || (dc == 0)) {
        my_pwm = 0;
        return;
    }
    if (dc >= 1) {
        my_pwm = 1;
        return;
    }
    on_delay = (int)(p_us * dc);
    off_delay = p_us - on_delay;
    toggleOn();
}
 
int main() {
    
    pwm_io(20000, 0.25); // 20ms - 25%
    
    while(1) {
        my_led = !my_led;
        wait(0.5);
    }
}

[]

7 segment

This is code from https://goo.gl/3BHgTp

....

void write_digit(__IO uint8_t digit){
	if (digit == 1){
		// digit 1 xuat 1; digit 2, 3, 4 xuat 0
		GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(1)); // digit 1
		GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(0)); // digit 2
		GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(0)); // digit 3
		GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(0)); // digit 4
	}
	if (digit == 2){
		// digit 2 xuat 1; digit 1, 3, 4 xuat 0
		GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(0)); // digit 1
		GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(1)); // digit 2
		GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(0)); // digit 3
		GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(0)); // digit 4
	}

}


void write_led_7_segment(__IO uint8_t number){
	
	// Muc logic 0 -> led sang, muc logic 1 -> led toi
	if (number == 0){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(0)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(1)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 1){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(1)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(1)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(1)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(1)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 2){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(1)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(0)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(1)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 3){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(1)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 4){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(1)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(1)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 5){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(1)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 6){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(1)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(0)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 7){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(1)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(1)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(1)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 8){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(0)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
	if (number == 9){
		GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(0)); // led A
		GPIO_WriteBit(GPIOB, GPIO_Pin_4, (BitAction)(0)); // led B
		GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(0)); // led C
		GPIO_WriteBit(GPIOB, GPIO_Pin_6, (BitAction)(0)); // led D
		GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)(1)); // led E
		GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)(0)); // led F
		GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(0)); // led G
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(1)); // led DP
	}
}

....

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/