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


#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 () {
    // Включаем тактирование всех нужных портов: A(светодиоды), B, C(кнопка Select), D, F (тумблеры)
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTA | RST_CLK_PCLK_PORTB | 
                    RST_CLK_PCLK_PORTC | RST_CLK_PCLK_PORTD | 
                    RST_CLK_PCLK_PORTF, 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); 

    // Настройка портов на вход (Кнопки и тумблеры)
    PortInit.PORT_OE = PORT_OE_IN;
    PortInit.PORT_PULL_UP = PORT_PULL_UP_ON; 
    
    // Порт B (Часть X и Y)
    PortInit.PORT_Pin = PORT_Pin_0 | PORT_Pin_1 | PORT_Pin_2 | PORT_Pin_3 | PORT_Pin_4;
    PORT_Init(MDR_PORTB, &PortInit);
    
    // Порт C (Кнопка Select - Pin 0)
    PortInit.PORT_Pin = PORT_Pin_0;
    PORT_Init(MDR_PORTC, &PortInit);

    // Порт D (Часть Y - Pin 3)
    PortInit.PORT_Pin = PORT_Pin_3;
    PORT_Init(MDR_PORTD, &PortInit);

    // Порт F (Часть X - Pin 0, 1)
    PortInit.PORT_Pin = PORT_Pin_0 | PORT_Pin_1;
    PORT_Init(MDR_PORTF, &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) {
        int x = 0, y = 0;
        uint8_t hit = 0;

        /* ЧИТАЕМ X по правильной распиновке платы */
        // Если тумблер поднят (1), прибавляем вес
        if (PORT_ReadInputDataBit(MDR_PORTF, PORT_Pin_0) == 1) x += 1;
        if (PORT_ReadInputDataBit(MDR_PORTF, PORT_Pin_1) == 1) x += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_1) == 1) x += 4;
        // Знаковый тумблер: если 1, делаем отрицательным
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_0) == 1) x *= -1; 

        /* ЧИТАЕМ Y по правильной распиновке платы */
        if (PORT_ReadInputDataBit(MDR_PORTD, PORT_Pin_3) == 1) y += 1;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_2) == 1) y += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_3) == 1) y += 4;
        // Знаковый тумблер
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_4) == 1) y *= -1; 

        // Если нажата кнопка "Подтвердить" (Select на PC0, обычно замыкает на 0)
        if (PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0) {
            
            // Проверяем границы прямоугольника X[-4; 4], Y[-3; 3]
            if (x >= -4 && x <= 4 && y >= -3 && y <= 3) {
                
                // Верхняя половина графика (Y от 0 до 3)
                if (y >= 0) {
                    // Условие "ВНЕ белого полукруга": x^2 + (y-3)^2 >= R^2 (где R=2, R^2=4)
                    if ( (x*x + (y - 3)*(y - 3)) >= 4 ) {
                        hit = 1;
                    }
                }
                // Нижняя половина графика (Y от -1 до -3)
                else {
                    // Условие "ВНЕ нижнего белого полукруга": x^2 + (y+3)^2 >= 4
                    if ( (x*x + (y + 3)*(y + 3)) >= 4 ) {
                        hit = 1;
                    }
                }
            }

            if (hit == 1) Blink(1); // Попал
            else          Blink(2); // Промах

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