比神乐 发表于 2024-12-9 17:33

【AT-START-L021测评】FREERTOS之计数信号量

代码:
#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "stdio.h"
/** @addtogroup UTILITIES_examples
* @{
*/
int cnt=0;
QueueHandle_t CountSemaphore;
/** @addtogroup FreeRTOS_demo
* @{
*/
button_type key=1;
//unsigned char const mainSENDER_1=30;
//unsigned char const mainSENDER_2=50;
button_type at32_button_press(void);
void task1(void *pvParameters)
{
    unsigned int i;
        UBaseType_t err;
    while(1)
    {
                                key=at32_button_state();
                        if(key==1)
                        {
                                        for(i=0;i<3000;i++);
                                        key=at32_button_state();
                        if(key==1)
                        {
                                while(1)
                                {
                                        key=at32_button_state();
                                        if(key==0)
                                                break;
                                }
                                err=xSemaphoreGive(CountSemaphore);
                                       
                                        if(err==pdFALSE)
                                        {
                                                printf("Sem Rele Failed!\r\n");
                                                //flag=0;
                                        }
                                        else
                                        {
                                                printf("Sem Role Succeed!\r\n");
                                                //flag=1;
                                        }
                                }
                                }/* 延时1000ticks */
                        vTaskDelay(10);
                                taskYIELD();
    }
}

/**
* @brief       task2
* @param       pvParameters : 传入参数(未用到)
* @retval      无
*/

void Keyprocess_task(void *pvParameters)
{
    unsigned int i=0;
        UBaseType_t semavalue=0;
    while(1)
    {
                       
                        if(CountSemaphore!=NULL)
                        {
                               
                                        xSemaphoreTake(CountSemaphore,portMAX_DELAY);
                                semavalue=uxSemaphoreGetCount(CountSemaphore);
                                       
                                        printf("semavalue value=%d\r\n",(int)semavalue);
                       
                               
                        }
                        else
                        {
                                vTaskDelay(10);
                        }
                                //printf("Task2 Running!\r\n");
                                vTaskDelay(1000);
}


}
/**
* @briefmain function.
* @paramnone
* @retval none
*/
int main(void)
{
system_clock_config();
        delay_init();
/* init led2 and led3 */
at32_led_init(LED2);
at32_led_init(LED3);
        at32_button_init();
/* init usart1 */
uart_print_init(115200);
        taskENTER_CRITICAL();
        CountSemaphore=xSemaphoreCreateCounting(10,10);
                if(CountSemaphore==NULL)
                {
                        printf("create CountSemaphore failed!\r\n");
                }

       
       
                /* create led2 task */
                xTaskCreate(task1,
               "vTask1",
               128,
                (void *)30,
               
                                                               2,
                NULL);
               
                xTaskCreate(Keyprocess_task,
               "vTask2",
               128,
               NULL,
               3,
               NULL);
               
       taskEXIT_CRITICAL();

                /* start scheduler */
                vTaskStartScheduler();
       
}

运行效果图:

页: [1]
查看完整版本: 【AT-START-L021测评】FREERTOS之计数信号量