使用keil5实现RA4M2按键控制LED的状态

🌈个人主页:羽晨同学

💫个人格言:“成为自己未来的主人~”  

我们上一个文章有说,实现RA4M2的流水灯实验,这次,我们来看看,怎么用按钮来控制LED的状态,我们先来看一下按钮的电路图。

这上面的两个图,第一个是按钮的电路图,第二个LED灯的电路图,我们对于LED的处理还是将其赋值为低电平,这样子的话,上电之后它就不亮了。

我们先来看LED部分的代码

static void Task_LedRunning(void *pvParameters)
{
    for (;;)
    {
        // 控制三个LED循环点亮
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH);
        // ... 其他LED控制代码
        
        vTaskDelay(pdMS_TO_TICKS(200)); // 延时200ms
    }
}

这样的话,我们就实现了流水灯部分的代码。

接下来,我们来看按键部分

static void Task_KeyRunning(void *pvParameters)
{
    bsp_io_level_t key_Status[2] = {BSP_IO_LEVEL_HIGH, BSP_IO_LEVEL_HIGH};
    uint16_t key_press_cnt[2] = {0U, 0U};
    
    for (;;)
    {
        // 读取按键状态并消抖
        if (key_Status[0] == BSP_IO_LEVEL_LOW)
        {
            if (key_press_cnt[0] >= 0U)
                vTaskSuspend(Task_Led_Handle); // 暂停LED任务
            // ... 其他按键处理逻辑
        }
        
        vTaskDelay(pdMS_TO_TICKS(10)); // 延时10ms
    }
}

当按键0按下去的时候,LED任务暂停,当按键1按下去的时候,LED任务开始。

主函数部分代码:

void hal_entry(void)
{
    // 创建LED和按键任务
    Task_Led_Handle = xTaskCreateStatic(Task_LedRunning, ...);
    Task_Key_Handle = xTaskCreateStatic(Task_KeyRunning, ...);

    if (NULL != Task_Led_Handle)
        vTaskStartScheduler(); // 启动调度器

    while (1) {} // 调度器启动成功后不会执行到这里
}

这样,我们就实现了这个功能。

完整代码如下:

#include "hal_data.h"
#include "FreeRTOS.h"
#include "task.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER


/* Stack for Idle task */
static StackType_t xIdleTaskStack[512];
static StaticTask_t xIdleTaskTcb;
/* Stack for Timer task */
static StackType_t xTiemrTaskStack[512];
static StaticTask_t xTimerTaskTcb;

/* Staack for LED task */
static StackType_t xTaskLedStack[1024];
static StaticTask_t xTaskLedTcb;
static TaskHandle_t Task_Led_Handle = NULL;

/* Staack for LED task */
static StackType_t xTaskKeyStack[1024];
static StaticTask_t xTaskKeyTcb;
static TaskHandle_t Task_Key_Handle = NULL;

extern void vApplicationGetIdleTaskMemory(StaticTask_t ** ppxIdleTaskTCBBuffer,
                                          StackType_t ** ppxIdleTaskStackBuffer,
                                          uint32_t * pulIdleTaskStackSize);
extern void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
                                          StackType_t **ppxTimerTaskStackBuffer,
                                          uint32_t *pulTimerTaskStackSize);

static void Task_LedRunning(void *pvParameters);
static void Task_KeyRunning(void *pvParameters);

static void Task_LedRunning(void *pvParameters)
{
    (void)pvParameters;
	
    for (;;)
    {
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_LOW);

        /* Delay for 200ms */
        vTaskDelay(pdMS_TO_TICKS(200));

        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_HIGH);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_LOW);

        /* Delay for 200ms */
        vTaskDelay(pdMS_TO_TICKS(200));

        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW);
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_HIGH);

        /* Delay for 200ms */
        vTaskDelay(pdMS_TO_TICKS(200));
    }
}

static void Task_KeyRunning(void *pvParameters)
{
    (void)pvParameters;
	bsp_io_level_t key_Status[2] = {BSP_IO_LEVEL_HIGH, BSP_IO_LEVEL_HIGH};
	uint16_t key_press_cnt[2] = {0U, 0U};
	
	for (;;)
	{
        if (FSP_SUCCESS == R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_05, &key_Status[0]))
        {
            if (key_Status[0] == BSP_IO_LEVEL_LOW)
            {
				/* Filter */
				if (key_press_cnt[0] >= 0U)
				{
					/* Key SW0 press confirm */
					/* Suspend led task */
					vTaskSuspend(Task_Led_Handle);
				}
				else
				{
					key_press_cnt[0] ++;
				}
            }
			else
			{
				key_press_cnt[0] = 0U;
			}
        }
		
		if (FSP_SUCCESS == R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_06, &key_Status[1]))
        {
            if (key_Status[1] == BSP_IO_LEVEL_LOW)
            {
				/* Filter */
				if (key_press_cnt[1] >= 4U)
				{
					/* Key SW1 press confirm */
					/* Resume led task */
					vTaskResume(Task_Led_Handle);
				}
				else
				{
					key_press_cnt[1] ++;
				}
            }
			else
			{
				key_press_cnt[1] = 0U;
			}
        }

		vTaskDelay(pdMS_TO_TICKS(10));		
	}
}

/* Implement the Idle task memory static alocation */
void vApplicationGetIdleTaskMemory(StaticTask_t ** ppxIdleTaskTCBBuffer,
                                   StackType_t ** ppxIdleTaskStackBuffer,
                                   uint32_t * pulIdleTaskStackSize)
{
    *ppxIdleTaskTCBBuffer = &xIdleTaskTcb;
    *ppxIdleTaskStackBuffer = xIdleTaskStack;
    *pulIdleTaskStackSize = 1024;
}

/* Implement the Timer task memory static alocation */
void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
                                    StackType_t **ppxTimerTaskStackBuffer,
                                    uint32_t *pulTimerTaskStackSize)
{
	*ppxTimerTaskTCBBuffer = &xTimerTaskTcb;
	*ppxTimerTaskStackBuffer = xTiemrTaskStack;
	*pulTimerTaskStackSize = 2048;
}

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
	/* Create static task */
    Task_Led_Handle = xTaskCreateStatic(Task_LedRunning,
                        "Led",          /* Task name */
                        1024,              /* Stack */
                        NULL,              /* Task parameter */
                        4,                 /* Priority */
                        xTaskLedStack,
                        &xTaskLedTcb);            /* Task handler */
	
	Task_Key_Handle = xTaskCreateStatic(Task_KeyRunning,
						"Key",          /* Task name */
						1024,              /* Stack */
						NULL,              /* Task parameter */
						3,                 /* Priority */
						xTaskKeyStack,
						&xTaskKeyTcb);            /* Task handler */

    if (NULL != Task_Led_Handle)
	{
	    vTaskStartScheduler();
	}

	while (1) {}

#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
#endif

  好了,今天的内容就到这里,我们明天再见。 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值