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


#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);
}

// ??????? ??? ?????? ???????? ????????? (3 ???? ???????? + 1 ??? ????)
int Get_Coord(uint8_t raw) {
    int val = raw & 0x07; // ?????? 3 ????
    if (raw & 0x08) {     // 4-? ??? - ????????
        return -val;
    }
    return val;
}

// ??????? ???????? (?????????, ??? 0x00 - ?????, 0xFF - ????????)
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 () {
    // 1. ????????????
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTA | RST_CLK_PCLK_PORTC, ENABLE);
    
    // 2. ????????? ??????????? (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); 
    
    // 3. ????????? ?????? (PORTC - ????)
    // ???? 0-7 ??? ?????????, ??? 10 ??? SELECT
    PortInit.PORT_OE = PORT_OE_IN;
    PortInit.PORT_Pin = 0xFF | PORT_Pin_10; 
    PORT_Init(MDR_PORTC, &PortInit);
    
    // 4. ?????? SysTick
    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); // ????? ??? ?????

    float x = 0, y = 0;

while (1) {
    // ?????????? X (KEY1-KEY4 ?? PORTE)
    // ??????????? ????????, ????? ????????? ? 2-?, ? 4-? ????????
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_0) == 0) x = -4.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_1) == 0) x = -2.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_2) == 0) x = 2.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_3) == 0) x = 4.0f;

    // ?????????? Y (KEY5-KEY8 ?? PORTB)
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_4) == 0) y = -3.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_5) == 0) y = -1.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_6) == 0) y = 1.0f;
    if (PORT_ReadInputDataBit(MDR_PORTB, PORT_Pin_7) == 0) y = 3.0f;

    // ??????? SELECT (PORTC ??? 0)
    if (PORT_ReadInputDataBit(MDR_PORTC, PORT_Pin_0) == 0) {
        
        // ???? ?????????? (??????? ?8):
        // 1. ??? ????? R=2: (x*x + y*y >= 4.0f)
        // 2. ?????? 2-? (x<0, y>0) ? 4-? (x>0, y<0) ????????: (x*y <= 0)
        
        if ( ((x*x + y*y) >= 4.0f) && (x * y <= 0) ) 
        {
            Blink(1); // ?????
        } 
        else 
        {
            Blink(2); // ?? ?????
        }

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