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


#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 (??????? ??????: 4, 2, 1, ????) */
        if (PORT_ReadInputDataBit(MDR_PORTF, PORT_Pin_0) == 1) x += 4;
        if (PORT_ReadInputDataBit(MDR_PORTF, PORT_Pin_1) == 1) x += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_1) == 1) x += 1;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_0) == 1) x *= -1;

        /* ?????? Y (??????? ??????: 4, 2, 1, ????) */
        if (PORT_ReadInputDataBit(MDR_PORTD, PORT_Pin_3) == 1) y += 4;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_2) == 1) y += 2;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_3) == 1) y += 1;
        if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_4) == 1) y *= -1;

        // ???? ?????? ?????? "???????????"
        if (PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0) {
            
            // ????????? ??????? ?????????????? X[-4; 4], Y[-3; 3]
            if (x >= -4 && x <= 4 && y >= -3 && y <= 3) {
                
                // ?????? 1-? ???????? (?????? ???????: X >= 0 ? Y >= 0)
                if (x >= 0 && y >= 0) {
                    if ( (x*x + (y - 3)*(y - 3)) >= 4 ) {
                        hit = 1;
                    }
                }
                // ?????? 3-? ???????? (????? ??????: X <= 0 ? Y <= 0)
                else if (x <= 0 && y <= 0) {
                    if ( (x*x + (y + 3)*(y + 3)) >= 4 ) {
                        hit = 1;
                    }
                }
                // ??? 2-? (????? ???????) ? 4-? (?????? ??????) ????????? hit ???????? 0
            }

            if (hit == 1) Blink(1); // ?????
            else          Blink(2); // ??????

            // ???? ?????????? ??????
            while(PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0);
        }
    }
}