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


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

static PORT_InitTypeDef PortInit;
volatile uint32_t ms_tick = 0;

void SysTick_Handler() { ms_tick++; }

void delay_mss(uint32_t ms) {
    uint32_t start = ms_tick;
    while ((ms_tick - start) < ms);
}

void Blink(int count) {
    for (int i = 0; i < count; i++) {
        PORT_Write(MDR_PORTA, 0x00); // Включить зеленые
        delay_mss(400);
        PORT_Write(MDR_PORTA, 0xFF); // Потушить
        delay_mss(400);
    }
}

int main () {
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTA | RST_CLK_PCLK_PORTB | RST_CLK_PCLK_PORTC, ENABLE);
    
    // Зеленые светодиоды (PORTA)
    PortInit.PORT_OE = PORT_OE_OUT;
    PortInit.PORT_FUNC = PORT_FUNC_PORT;
    PortInit.PORT_MODE = PORT_MODE_DIGITAL;
    PortInit.PORT_SPEED = PORT_SPEED_SLOW;
    PortInit.PORT_Pin = PORT_Pin_All;
    PORT_Init(MDR_PORTA, &PortInit); 

    // Кнопки (PORTB и PORTC)
    PortInit.PORT_OE = PORT_OE_IN;
    PortInit.PORT_PULL_UP = PORT_PULL_UP_ON; // Подтяжка важна для кнопок
    PORT_Init(MDR_PORTB, &PortInit);
    PORT_Init(MDR_PORTC, &PortInit);

    SysTick->LOAD = (8000000/1000) - 1;
    SysTick->VAL = 0;
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

    PORT_Write(MDR_PORTA, 0xFF);

    while (1) {
        float x = 0, y = 0;
        int x_val = 0, y_val = 0;

        // --- СБОРКА X (PORTB 0-3). Если диод ПОГАС (==0), прибавляем вес ---
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_0) == 0) x_val += 4;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_1) == 0) x_val += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_2) == 0) x_val += 1;
        // Если нажат ПИН 3, делаем число отрицательным
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_3) == 0) x_val = -x_val;
        x = (float)x_val;

        // --- СБОРКА Y (PORTB 4-7). Если диод ПОГАС (==0), прибавляем вес ---
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_4) == 0) y_val += 4;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_5) == 0) y_val += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_6) == 0) y_val += 1;
        // Если нажат ПИН 7, делаем число отрицательным
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_7) == 0) y_val = -y_val;
        y = (float)y_val;

        // ПРОВЕРКА ПО КНОПКЕ SELECT (PORTC пин 0)
        if (PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0) {
            uint8_t hit = 0;

            // ГЕОМЕТРИЯ ВАРИАНТА №8 (включая границы):
            // Прямоугольник ограничен x от -4 до 4, y от -3 до 3
            if (x >= -4.0f && x <= 4.0f && y >= -3.0f && y <= 3.0f) {
                
                // Проверка 1-й четверти (x >= 0, y >= 0) и вне круга (0,3)
                if (x >= 0 && y >= 0) {
                    if ( (x*x + (y-3)*(y-3)) >= 4.0f ) hit = 1;
                }
                // Проверка 3-й четверти (x <= 0, y <= 0) и вне круга (0,-3)
                else if (x <= 0 && y <= 0) {
                    if ( (x*x + (y+3)*(y+3)) >= 4.0f ) hit = 1;
                }
            }

            if (hit) Blink(1); // Попал (1 раз моргает зеленым)
            else     Blink(2); // Не попал (2 раза моргает зеленым)

            while(PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0); // Ждем отпускания
        }
    }
}