Загрузка данных


#include "MDR32F9Qx_config.h"
#include "MDR32Fx.h"
#include "MDR32F9Qx_rst_clk.h"
#include "MDR32F9Qx_port.h"

#define LED1          PORT_Pin_0
#define Button_select PORT_Pin_1

static PORT_InitTypeDef PortInit;

void init_leds(void)
{
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTB, ENABLE);

    PORT_StructInit(&PortInit);

    PortInit.PORT_Pin   = LED1;
    PortInit.PORT_OE    = PORT_OE_OUT;
    PortInit.PORT_FUNC  = PORT_FUNC_PORT;
    PortInit.PORT_MODE  = PORT_MODE_DIGITAL;
    PortInit.PORT_SPEED = PORT_SPEED_FAST;

    PORT_Init(MDR_PORTB, &PortInit);

    PORT_ResetBits(MDR_PORTB, LED1);
}

void init_button(void)
{
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTE, ENABLE);

    PORT_StructInit(&PortInit);

    PortInit.PORT_Pin     = Button_select;
    PortInit.PORT_OE      = PORT_OE_IN;
    PortInit.PORT_FUNC    = PORT_FUNC_PORT;
    PortInit.PORT_MODE    = PORT_MODE_DIGITAL;
    PortInit.PORT_SPEED   = PORT_SPEED_FAST;
    PortInit.PORT_PULL_UP = PORT_PULL_UP_ON;

    PORT_Init(MDR_PORTE, &PortInit);
}

void delay_loop(uint32_t t)
{
    while (t--)
    {
        __NOP();
    }
}

int main(void)
{
    uint8_t PWM_Counter = 0;
    uint8_t PWM_A = 0;
    uint8_t PWM_B = 40;

    uint8_t btn_old_state = 1;
    uint8_t btn_state = 1;

    init_leds();
    init_button();

    while (1)
    {
        btn_state = PORT_ReadInputDataBit(MDR_PORTE, Button_select);

        if (btn_old_state == 1 && btn_state == 0)
        {
            PWM_A++;

            if (PWM_A >= PWM_B)
            {
                PWM_A = 0;
            }

            delay_loop(30000);
        }

        btn_old_state = btn_state;

        if (PWM_Counter >= PWM_B)
        {
            PWM_Counter = 0;
            PORT_SetBits(MDR_PORTB, LED1);
        }
        else if (PWM_Counter == PWM_A)
        {
            PORT_ResetBits(MDR_PORTB, LED1);
            PWM_Counter++;
        }
        else
        {
            PWM_Counter++;
        }

        delay_loop(500);
    }
}