NUCLEO

(Rozdíly mezi verzemi)
Přejít na: navigace, hledání
(Source code)
(Nejsou zobrazeny 2 mezilehlé verze od 1 uživatele.)
Řádka 2: Řádka 2:
  
 
[[Soubor:Cortex Challenge 2015.png|link=http://measure.feld.cvut.cz/soutez/soutez2015]]
 
[[Soubor:Cortex Challenge 2015.png|link=http://measure.feld.cvut.cz/soutez/soutez2015]]
 +
 +
{| class="toccolours" cellpadding="5" style="float: right; clear: right; margin: 0 0 1em 1em; font-size: 85%; width: 25em"
 +
| colspan="2" style="text-align: center; font-size: larger; background-color: lightblue;" | '''''Cortex'''''
 +
|- style="vertical-align: top;"
 +
 +
|Nucleo KIT
  
 
[[Soubor:Cortex Challenge 2015 01.png|link=https://developer.mbed.org/platforms/ST-Nucleo-F030R8/|Vývojový kit|right]]
 
[[Soubor:Cortex Challenge 2015 01.png|link=https://developer.mbed.org/platforms/ST-Nucleo-F030R8/|Vývojový kit|right]]
  
==Nucleo pinout==
+
|- style="vertical-align: top;"
 +
|Nucleo pinout  
  
 
Arduino-compatible headers
 
Arduino-compatible headers
  
[[Soubor:Nucleo pinout.png|600px]]
+
[[Soubor:Nucleo pinout.png|400px]]
  
==Morpho headers==
+
Morpho headers
  
 
These headers give access to all STM32 pins.
 
These headers give access to all STM32 pins.
  
[[Soubor:Morpho headers.png|600px]]
+
[[Soubor:Morpho headers.png|400px|right]]
  
 +
{{#widget:YouTube|id=BrMw5TNQROo|height=270|width=360|Getting started with ARM mbed Integrated Development Environment|right}}
  
{{#widget:YouTube|id=BrMw5TNQROo|height=360|width=480|Getting started with ARM mbed Integrated Development Environment|right}}
+
|}
  
 +
== Source code  ==
  
[[Soubor:mbed.png|link=https://developer.mbed.org/|IDE]]
+
IDE:
  
== Source code  ==
+
[[Soubor:mbed.png|link=https://developer.mbed.org/compiler/|IDE]]
  
 
<source lang="cpp">
 
<source lang="cpp">
Řádka 29: Řádka 38:
 
#include "mbed.h"
 
#include "mbed.h"
 
   
 
   
<source lang="asm">
 
 
 
DigitalOut myled(LED1);
 
DigitalOut myled(LED1);
 
   
 
   
Řádka 90: Řádka 97:
  
 
<source lang="cpp">
 
<source lang="cpp">
 
 
#include "mbed.h"
 
#include "mbed.h"
 
   
 
   
Řádka 109: Řádka 115:
 
     }
 
     }
 
}
 
}
 
 
</source>
 
</source>
  
 
+
=== [https://developer.mbed.org/teams/ST/code/Nucleo_read_button_interrupt/?platform=ST-Nucleo-F030R8 Nucleo read button interrupt]  ===
=== 7 segment ===
+
 
+
This is my code from https://goo.gl/3BHgTp
+
  
 
<source lang="cpp">
 
<source lang="cpp">
 
+
#include "mbed.h"
/**
+
  ******************************************************************************
+
InterruptIn mybutton(USER_BUTTON);
  * @file    main.c
+
DigitalOut myled(LED1);
  * @author  HuongLQ
+
  * @version V1.0.0
+
float delay = 1.0; // 1 sec
* @kit STM32F030R8T6 Discovery
+
  * @date    01/05/2014
+
void pressed()
  ******************************************************************************
+
 
+
  */
+
+
#include "main.h"
+
 
+
 
+
GPIO_InitTypeDef GPIO_InitStructure;
+
USART_InitTypeDef USART1_InitStructure;
+
EXTI_InitTypeDef EXTI_InitStructure;
+
NVIC_InitTypeDef NVIC_InitStructure;
+
 
+
 
+
char RxBuffer1[10];
+
uint8_t RxIndex1 = 0;
+
uint8_t led_number[4] = {0,0,0,0};
+
//uint8_t NbrOfDataToRead1 = 4;
+
uint8_t t;
+
 
+
 
+
void write_led_7_segment(__IO uint8_t number);
+
void write_digit(__IO uint8_t digit);
+
 
+
 
+
void GPIO_Configuration(void);
+
void EXTI_Configuration(void);
+
void USART_Configuration(void);
+
 
+
 
+
int main(void)
+
 
{
 
{
  GPIO_Configuration();
+
    if (delay == 1.0)
  EXTI_Configuration();
+
        delay = 0.2; // 200 ms
USART_Configuration();
+
    else
+
        delay = 1.0; // 1 sec
SysTick_Config(SystemCoreClock / 1000);
+
+
  while(1)
+
  { 
+
//USART_SendData(USART1, 'a');
+
}
+
 
}
 
}
 
+
void GPIO_Configuration(){
+
int main()
// Enable GPIOA & GPIOB Clock
+
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
+
    mybutton.fall(&pressed);
+
    while (1) {
// Cau hinh PA6,7,13-15 va PB0,1,3-7 la chan ket noi cac thanh LED, che do output push-pull.
+
        myled = !myled;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_15;
+
        wait(delay);
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+
    }
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+
  GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
+
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+
  GPIO_Init(GPIOB, &GPIO_InitStructure);
+
+
+
// Cau hinh PA0-2 la chan ket noi button, che do input floating.
+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+
GPIO_Init(GPIOA, &GPIO_InitStructure);
+
 
}
 
}
 +
</source>
  
void USART_Configuration(){
+
=== [https://developer.mbed.org/teams/ST/code/Nucleo_pwm2/?platform=ST-Nucleo-F030R8 Nucleo_pwm2] ===
// Enable peripheral USART clock
+
 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
+
<source lang="cpp">
+
#include "mbed.h"
// Cau hinh AFConfig de thuc hien chuc nang thay the
+
   
// (neu k su dung AFConfig thi cac pin chi la GPIO ma k thuc hien chuc nang USART)
+
DigitalOut my_led(LED1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
+
InterruptIn my_button(USER_BUTTON);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
+
PwmOut      my_pwm(PB_3);
+
   
/// Cau hinh USART1 TX la chan output push - pull
+
void pressed() {
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
+
    if (my_pwm.read() == 0.25) {
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
+
        my_pwm.write(0.75);
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+
    }
//  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+
    else {
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
+
        my_pwm.write(0.25);
// GPIO_Init(GPIOA, &GPIO_InitStructure);
+
    }
//
+
// // Cau hinh USART1 RX la chan input floating.
+
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
+
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
+
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+
// GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
+
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
+
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
+
  GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
+
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x02;
+
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+
  NVIC_Init(&NVIC_InitStructure);
+
+
// Cau hinh USART1
+
USART1_InitStructure.USART_BaudRate = 115200;
+
  USART1_InitStructure.USART_WordLength = USART_WordLength_8b;
+
  USART1_InitStructure.USART_StopBits = USART_StopBits_1;
+
  USART1_InitStructure.USART_Parity = USART_Parity_No;
+
  USART1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+
  USART1_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
+
USART_Init(USART1, &USART1_InitStructure);
+
+
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
+
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
+
+
// Enable UART1
+
USART_Cmd(USART1, ENABLE);
+
 
}
 
}
 +
 +
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
 +
    }
 +
}
 +
</source>
  
 +
=== [https://developer.mbed.org/teams/ST/code/Nucleo_read_analog_value/file/6d058686efbf/main.cpp Nucleo_read_analog_value]  ===
  
void EXTI_Configuration(){
+
<source lang="cpp">
// Enable SYSCFG clock
+
#include "mbed.h"
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
+
 
+
AnalogIn analog_value(A0);
  // Ket noi EXTI Line0,1 toi PA0,1
+
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
+
DigitalOut led(LED1);
 
+
 
+
int main() {
  // Cau hinh EXTI0,1
+
    float meas;
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
+
   
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
+
    printf("\nAnalogIn example\n");
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
+
   
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
+
    while(1) {
  EXTI_Init(&EXTI_InitStructure);
+
        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
  // Cau hinh uu tien ngat cho ngat EXTI
+
        printf("measure = %.0f mV\n", meas);
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
+
        if (meas > 2000) { // If the value is greater than 2V then switch the LED on
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
+
          led = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+
        }
  NVIC_Init(&NVIC_InitStructure);
+
        else {
+
          led = 0;
+
        }
EXTI_ClearITPendingBit(EXTI_Line0);
+
        wait(0.2); // 200 ms
 +
    }
 
}
 
}
 +
</source>
  
 +
=== [https://developer.mbed.org/teams/ST/code/Nucleo_pwm3/file/8d83d0909665/main.cpp Nucleo_pwm3]  ===
  
void SysTick_Handler(void)
+
<source lang="cpp">
{
+
#include "mbed.h"
static uint32_t time = 0;
+
time++;
+
Timeout timer;
if ((time > 0) && (time <= 5)){
+
DigitalOut my_led(LED1);
write_digit(1);
+
DigitalOut my_pwm(D10); // IO used by pwm_io function
write_led_7_segment(led_number[0]);
+
}
+
int on_delay = 0;
if ((time > 5) && (time <= 10)){
+
int off_delay = 0;
write_digit(2);
+
write_led_7_segment(led_number[1]);
+
void toggleOff(void);
}
+
if ((time > 10) && (time <= 15)){
+
void toggleOn(void) {
write_digit(3);
+
    my_pwm = 1;
write_led_7_segment(led_number[2]);
+
    timer.attach_us(toggleOff, on_delay);
}
+
if ((time > 15) && (time <= 20)){
+
write_digit(4);
+
write_led_7_segment(led_number[3]);
+
if (time == 20){
+
time = 0;
+
}
+
}
+
 
}
 
}
 +
 +
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);
 +
    }
 +
}
 +
</source>
  
 +
=== []  ===
  
void EXTI0_1_IRQHandler(void)
+
<source lang="cpp">
{
+
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
+
{
+
USART_SendData(USART1, 'S');
+
EXTI_ClearITPendingBit(EXTI_Line0);
+
}
+
}
+
  
 +
</source>
  
void USART1_IRQHandler(void)
+
=== 7 segment ===
{  // co the su dung static cho bien RxIndex1 de co the reset bien nay ve 0
+
//static uint8_t RxIndex1 = 0;
+
uint8_t i = 0;
+
+
// if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
+
// {
+
// if(USART_ReceiveData(USART1)== '1')
+
// USART_SendData(USART1, 'a');
+
// }
+
// USART_ClearITPendingBit(USART1, USART_IT_RXNE);
+
  
 +
This is code from https://goo.gl/3BHgTp
  
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
+
<source lang="cpp">
{
+
//RxBuffer1[RxIndex1] = USART_ReceiveData(USART1);
+
//USART_SendData(USART1, RxBuffer1[RxIndex1]);
+
//USART_SendData(USART1, RxBuffer1[0]);
+
//led_number[RxIndex1] = (uint8_t)(RxBuffer1[RxIndex1]);
+
// //USART_SendData(USART1, 'a');
+
led_number[RxIndex1] = USART_ReceiveData(USART1);
+
USART_SendData(USART1, led_number[RxIndex1]);
+
RxIndex1++;
+
// if (RxIndex1 >= 4){
+
// USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
+
// RxIndex1 = 0;
+
// }
+
}
+
+
// for (i = 0; i < 4; i++){
+
// led_number[i] = (uint8_t)RxBuffer1[i];
+
// USART_SendData(USART1, led_number[i]);
+
// }
+
+
//RxIndex1 = 0; // Khi su dung RxIndex1 = 0 khi RxIndex1 khong phai la bien static-> Du lieu moi luon duoc gan vao RxBuffer1[0]
+
if (RxIndex1 == 4){
+
// for (i = 0; i < 4; i++)
+
// {
+
// led_number[i] = (uint8_t)RxBuffer1[i];
+
// USART_SendData(USART1, led_number[i]);
+
// }
+
RxIndex1 = 0;
+
}
+
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
+
}
+
  
 +
....
  
 
void write_digit(__IO uint8_t digit){
 
void write_digit(__IO uint8_t digit){
Řádka 372: Řádka 289:
 
GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(0)); // digit 4
 
GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(0)); // digit 4
 
}
 
}
if (digit == 3){
+
 
// digit 3 xuat 1; digit 1, 2, 4 xuat 0
+
GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(0)); // digit 1
+
GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(0)); // digit 2
+
GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(1)); // digit 3
+
GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(0)); // digit 4
+
}
+
if (digit == 4){
+
// digit 4 xuat 1; digit 1, 2, 3 xuat 0
+
GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(0)); // 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)(1)); // digit 4
+
}
+
 
}
 
}
  
Řádka 494: Řádka 398:
 
}
 
}
  
 
+
....
 
+
 
+
 
+
 
+
#ifdef  USE_FULL_ASSERT
+
 
+
 
+
/**
+
  * @brief  Reports the name of the source file and the source line number
+
  *  where the assert_param error has occurred.
+
  * @param  file: pointer to the source file name
+
  * @param  line: assert_param error line source number
+
  * @retval None
+
  */
+
void assert_failed(uint8_t* file, uint32_t line)
+
{
+
  /* User can add his own implementation to report the file name and line number,
+
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+
 
+
 
+
  /* Infinite loop */
+
  while (1)
+
  {}
+
}
+
#endif
+
 
+
 
+
/**
+
  * @}
+
  */
+
  
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 
</source>
 
</source>

Verze z 23. 6. 2015, 21:25

soutěž:

Cortex Challenge 2015.png

Cortex
Nucleo KIT
Vývojový kit
Nucleo pinout

Arduino-compatible headers

Nucleo pinout.png

Morpho headers

These headers give access to all STM32 pins.

Morpho headers.png

Obsah

Source code

IDE:

IDE

 
#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****/
Osobní nástroje
Jmenné prostory
Varianty
Akce
Rychlá navigace
NEJ aktivity
Nejlepší předměty
Nejlepší MCU
SW-HW
Ostatní
Utility
Nástroje
Tisk/export