gpt4 book ai didi

C Switch 语句 CMSIS FreeRTOS

转载 作者:行者123 更新时间:2023-11-30 19:09:52 26 4
gpt4 key购买 nike

我正在尝试制作一个使用各种功能的 C 程序,然后通过连接到 LPCXpresso 1769 的 DIP 开关,它必须选择要执行的功能(例如 00 二进制计数器 01 旋转 LED 等)。现在,我已经做到了,但想将选择要执行的程序的函数从嵌套 if 更改为 switch 语句,但它不起作用。它确实可以编译,但是,调试器会抛出一些警告(第 123 行和第 132 行的“没有效果的语句”以及第 100 行的“未使用的参数 pvParameter”),以及在将其刷新到 LPCXpresso 并为每个任务选择组合之后什么都不做。我正在使用 NXP 的 LPCXpresso IDE。

这是代码

#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>
#include "lpc17xx_gpio.h"
#include "lpc17xx_timer.h"
#include "lpc17xx_adc.h"
#include "lpc17xx_pinsel.h"
/* Library includes. */
#include "LPC17xx.h"
#include "LPC17xx_gpio.h"
#include "system_LPC17xx.h"



/* Used as a loop counter to create a very crude delay. */
IRQn_Type TIMER0;
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
/* Used in the run time stats calculations. */
/* Used in the run time stats calculations. */
static uint32_t ulClocksPer10thOfAMilliSecond = 0UL;
#define mainDELAY_LOOP_COUNT (0xfffff)
void CONFIG_GPIO(void);

static void init_adc(void);
extern int Timer0_Wait();

#define RGB_RED 0x01000000
#define RGB_BLUE 0x02000000
#define RGB_GREEN 0x04000000
void init_rgb (void);
void counter_rgb (void);

void vTaskKit( void *pvParameters );

int main( void )
{
init_adc();
init_rgb();
CONFIG_GPIO();

xTaskCreate (vTaskKit, "Kit", 240, NULL, 1, NULL );
/* Start the FreeRTOS scheduler. */
vTaskStartScheduler();

/* The following line should never execute. If it does, it means there was
insufficient FreeRTOS heap memory available to create the Idle and/or timer
tasks. See the memory management section on the http://www.FreeRTOS.org web
site for more information. */
for( ;; );
}


/*-----------------------------------------------------------*/


void CONFIG_GPIO(void)
{
GPIO_SetDir(0,0x000000FF, 1);
GPIO_ClearValue(0, 0x000000FF);
GPIO_SetDir(2,0x000000FF,0);
GPIO_ClearValue(2, 0x000000FF);
}
void init_rgb (void)
{
GPIO_SetDir (0,0x01000000, 1);
GPIO_SetDir (0,0x02000000, 1);
GPIO_SetDir (0,0x04000000, 1);
}
static void init_adc(void)
{

/*
* Init ADC pin connect
* AD0.0 on P0.23
*/
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 23;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);

/* Configuration for ADC :
* Frequency at 1Mhz
* ADC channel 0, no Interrupt
*/
ADC_Init(LPC_ADC, 100000);
ADC_IntConfig(LPC_ADC,ADC_ADINTEN0,ENABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE);
ADC_EdgeStartConfig(LPC_ADC,ADC_START_ON_FALLING);
}

void vTaskKit( void *pvParameters )
{
volatile unsigned long ul;

uint32_t var1=0x00000001;
uint32_t del =0x000000FF;
uint32_t var2=0x00000001;
uint32_t analog = 0;
uint32_t sw=0x00000000;
unsigned int var=0;
while(1)
{
sw=GPIO_ReadValue(2);
switch(sw)
{
case 0x00000001://Contador Binario
GPIO_SetValue(0,var);
var++;
vTaskDelay(100);
GPIO_ClearValue(0,0x000000FF);
break;

case 0x00000002://Auto Increible
for(var2;var2<=7;var2++)
{
GPIO_SetValue(0,var1);
var1= var1<<1;
for (ul =0; ul < mainDELAY_LOOP_COUNT; ul++ )
{
}
GPIO_ClearValue(0,del);
}
for(var2;var2>=2;var2--)
{
GPIO_SetValue(0,var1);
var1= var1>>1;
for (ul =0; ul < mainDELAY_LOOP_COUNT; ul++ )
{
}
GPIO_ClearValue(0,del);
}
break;

case 0x00000003://Contador RGB
GPIO_SetValue (0,RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_RED);
GPIO_SetValue (0,RGB_BLUE);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_BLUE);
GPIO_SetValue (0,(RGB_RED+RGB_BLUE));
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,(RGB_RED+RGB_BLUE));
GPIO_SetValue (0,RGB_GREEN);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN);
GPIO_SetValue (0,RGB_GREEN+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_RED);
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE);
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );
break;

case 0x00000004://Contador ADC Binario
ADC_StartCmd(LPC_ADC,ADC_START_NOW);
analog=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0);
analog=analog/16;
GPIO_SetValue(0,analog);
vTaskDelay( 100 / portTICK_RATE_MS );

GPIO_ClearValue(0,0x000000FF);
break;

case 0x00000005://Contador ADC RGB
ADC_StartCmd(LPC_ADC,ADC_START_NOW);
analog=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0);
if(analog<585)
{
GPIO_SetValue(0,RGB_RED);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_RED);
}
if(585<analog && analog<1170)
{
GPIO_SetValue (0,RGB_BLUE);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_BLUE);
}
if(1170<analog && analog<1755)
{
GPIO_SetValue (0,(RGB_RED+RGB_BLUE));
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,(RGB_RED+RGB_BLUE));
}
if(1755<analog && analog<2340)
{
GPIO_SetValue (0,RGB_GREEN);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN);
}
if(2340<analog && analog<2925)
{
GPIO_SetValue (0,RGB_GREEN+RGB_RED);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN+RGB_RED);
}
if(2925<analog && analog<3510)
{
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE);
}
if(3510<analog && analog<4095)
{
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
}
break;
}
}
}















void vMainConfigureTimerForRunTimeStats( void )
{
/* How many clocks are there per tenth of a millisecond? */
ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;
}
/*-----------------------------------------------------------*/

uint32_t ulMainGetRunTimeCounterValue( void )
{
uint32_t ulSysTickCounts, ulTickCount, ulReturn;
const uint32_t ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
volatile uint32_t * const pulCurrentSysTickCount = ( ( volatile uint32_t *) 0xe000e018 );
volatile uint32_t * const pulInterruptCTRLState = ( ( volatile uint32_t *) 0xe000ed04 );
const uint32_t ulSysTickPendingBit = 0x04000000UL;

/* NOTE: There are potentially race conditions here. However, it is used
anyway to keep the examples simple, and to avoid reliance on a separate
timer peripheral. */


/* The SysTick is a down counter. How many clocks have passed since it was
last reloaded? */
ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;

/* How many times has it overflowed? */
ulTickCount = xTaskGetTickCountFromISR();

/* Is there a SysTick interrupt pending? */
if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )
{
/* There is a SysTick interrupt pending, so the SysTick has overflowed
but the tick count not yet incremented. */
ulTickCount++;

/* Read the SysTick again, as the overflow might have occurred since
it was read last. */
ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;
}

/* Convert the tick count into tenths of a millisecond. THIS ASSUMES
configTICK_RATE_HZ is 1000! */
ulReturn = ( ulTickCount * 10UL ) ;

/* Add on the number of tenths of a millisecond that have passed since the
tick count last got updated. */
ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );

return ulReturn;
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
{
( void ) pcTaskName;
( void ) pxTask;

/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
function is called if a stack overflow is detected. */
taskDISABLE_INTERRUPTS();
for( ;; );
}
/*-----------------------------------------------------------*/

void vApplicationMallocFailedHook( void )
{
/* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails.
pvPortMalloc() is called internally by the kernel whenever a task, queue,
timer or semaphore is created. It is also called by various parts of the
demo application. If heap_1.c or heap_2.c are used, then the size of the
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
to query the size of free heap space that remains (although it does not
provide information on how the remaining heap might be fragmented). */
taskDISABLE_INTERRUPTS();
for( ;; );

}
/*-----------------------------------------------------------*/

还有一个可以工作的,但是带有嵌套的 if

#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>
#include "lpc17xx_gpio.h"
#include "lpc17xx_timer.h"
#include "lpc17xx_adc.h"
#include "lpc17xx_pinsel.h"
/* Library includes. */
#include "LPC17xx.h"
#include "LPC17xx_gpio.h"
#include "system_LPC17xx.h"



/* Used as a loop counter to create a very crude delay. */
IRQn_Type TIMER0;
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
/* Used in the run time stats calculations. */
/* Used in the run time stats calculations. */
static uint32_t ulClocksPer10thOfAMilliSecond = 0UL;
#define mainDELAY_LOOP_COUNT (0xfffff)
void CONFIG_GPIO(void);

static void init_adc(void);
extern int Timer0_Wait();

#define RGB_RED 0x01000000
#define RGB_BLUE 0x02000000
#define RGB_GREEN 0x04000000
void init_rgb (void);
void counter_rgb (void);

void vTaskKit( void *pvParameters );

int main( void )
{
init_adc();
init_rgb();
CONFIG_GPIO();

xTaskCreate (vTaskKit, "Kit", 240, NULL, 1, NULL );
/* Start the FreeRTOS scheduler. */
vTaskStartScheduler();

/* The following line should never execute. If it does, it means there was
insufficient FreeRTOS heap memory available to create the Idle and/or timer
tasks. See the memory management section on the http://www.FreeRTOS.org web
site for more information. */
for( ;; );
}


/*-----------------------------------------------------------*/


void CONFIG_GPIO(void)
{
GPIO_SetDir(0,0x000000FF, 1);
GPIO_ClearValue(0, 0x000000FF);
GPIO_SetDir(2,0x000000FF,0);
GPIO_ClearValue(2, 0x000000FF);
}
void init_rgb (void)
{
GPIO_SetDir (0,0x01000000, 1);
GPIO_SetDir (0,0x02000000, 1);
GPIO_SetDir (0,0x04000000, 1);
}
static void init_adc(void)
{

/*
* Init ADC pin connect
* AD0.0 on P0.23
*/
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 23;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);

/* Configuration for ADC :
* Frequency at 1Mhz
* ADC channel 0, no Interrupt
*/
ADC_Init(LPC_ADC, 100000);
ADC_IntConfig(LPC_ADC,ADC_ADINTEN0,ENABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE);
ADC_EdgeStartConfig(LPC_ADC,ADC_START_ON_FALLING);
}

void vTaskKit( void *pvParameters )
{
volatile unsigned long ul;

uint32_t var1=0x00000001;
uint32_t del =0x000000FF;
uint32_t var2=0x00000001;
uint32_t analog = 0;
char var=0;
char sw=0x000000000;
char bin=0x00000001;
char inc=0x00000002;
char rgb=0x00000003;
char adcbin=0x00000004;
char adcrgb=0x00000005;

while(1)
{

sw=GPIO_ReadValue(2);
if(sw==bin)
{
GPIO_SetValue(0,var);
var++;
vTaskDelay(100);
GPIO_ClearValue(0,0x000000FF);




}


if(sw==inc)
{
for(var2;var2<=7;var2++)
{
GPIO_SetValue(0,var1);
var1= var1<<1;
for (ul =0; ul < mainDELAY_LOOP_COUNT; ul++ )
{
}

GPIO_ClearValue(0,del);
}

for(var2;var2>=2;var2--)
{
GPIO_SetValue(0,var1);
var1= var1>>1;
for (ul =0; ul < mainDELAY_LOOP_COUNT; ul++ )

{
}
GPIO_ClearValue(0,del);

}
}
if(sw==rgb)
{
GPIO_SetValue (0,RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_RED);
GPIO_SetValue (0,RGB_BLUE);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_BLUE);
GPIO_SetValue (0,(RGB_RED+RGB_BLUE));
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,(RGB_RED+RGB_BLUE));
GPIO_SetValue (0,RGB_GREEN);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN);
GPIO_SetValue (0,RGB_GREEN+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_RED);
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE);
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );

GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE+RGB_RED);
vTaskDelay( 200 / portTICK_RATE_MS );


}

if(sw==adcbin)
{
ADC_StartCmd(LPC_ADC,ADC_START_NOW);
analog=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0);
analog=analog/16;
GPIO_SetValue(0,analog);
vTaskDelay( 100 / portTICK_RATE_MS );

GPIO_ClearValue(0,0x000000FF);


}
if(sw==adcrgb)
{
ADC_StartCmd(LPC_ADC,ADC_START_NOW);
analog=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0);
if(analog<585)
{
GPIO_SetValue(0,RGB_RED);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_RED);
}
if(585<analog && analog<1170)
{
GPIO_SetValue (0,RGB_BLUE);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_BLUE);
}
if(1170<analog && analog<1755)
{
GPIO_SetValue (0,(RGB_RED+RGB_BLUE));
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,(RGB_RED+RGB_BLUE));
}
if(1755<analog && analog<2340)
{
GPIO_SetValue (0,RGB_GREEN);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN);
}
if(2340<analog && analog<2925)
{
GPIO_SetValue (0,RGB_GREEN+RGB_RED);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN+RGB_RED);
}
if(2925<analog && analog<3510)
{
GPIO_SetValue (0,RGB_GREEN+RGB_BLUE);
vTaskDelay( 50 / portTICK_RATE_MS );
GPIO_ClearValue (0,RGB_GREEN+RGB_BLUE);
}
if(3510<analog && analog<4095)
{
GPIO_SetValue
(0,RGB_GREEN+RGB_BLUE+RGB_RED);

vTaskDelay( 50 / portTICK_RATE_MS );

GPIO_ClearValue

(0,RGB_GREEN+RGB_BLUE+RGB_RED);

}


}
}
}















void vMainConfigureTimerForRunTimeStats( void )
{
/* How many clocks are there per tenth of a millisecond? */
ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;
}
/*-----------------------------------------------------------*/

uint32_t ulMainGetRunTimeCounterValue( void )
{
uint32_t ulSysTickCounts, ulTickCount, ulReturn;
const uint32_t ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
volatile uint32_t * const pulCurrentSysTickCount = ( ( volatile uint32_t *) 0xe000e018 );
volatile uint32_t * const pulInterruptCTRLState = ( ( volatile uint32_t *) 0xe000ed04 );
const uint32_t ulSysTickPendingBit = 0x04000000UL;

/* NOTE: There are potentially race conditions here. However, it is used
anyway to keep the examples simple, and to avoid reliance on a separate
timer peripheral. */


/* The SysTick is a down counter. How many clocks have passed since it was
last reloaded? */
ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;

/* How many times has it overflowed? */
ulTickCount = xTaskGetTickCountFromISR();

/* Is there a SysTick interrupt pending? */
if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )
{
/* There is a SysTick interrupt pending, so the SysTick has overflowed
but the tick count not yet incremented. */
ulTickCount++;

/* Read the SysTick again, as the overflow might have occurred since
it was read last. */
ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;
}

/* Convert the tick count into tenths of a millisecond. THIS ASSUMES
configTICK_RATE_HZ is 1000! */
ulReturn = ( ulTickCount * 10UL ) ;

/* Add on the number of tenths of a millisecond that have passed since the
tick count last got updated. */
ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );

return ulReturn;
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
{
( void ) pcTaskName;
( void ) pxTask;

/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
function is called if a stack overflow is detected. */
taskDISABLE_INTERRUPTS();
for( ;; );
}
/*-----------------------------------------------------------*/

void vApplicationMallocFailedHook( void )
{
/* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails.
pvPortMalloc() is called internally by the kernel whenever a task, queue,
timer or semaphore is created. It is also called by various parts of the
demo application. If heap_1.c or heap_2.c are used, then the size of the
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
to query the size of free heap space that remains (although it does not
provide information on how the remaining heap might be fragmented). */
taskDISABLE_INTERRUPTS();
for( ;; );

}
/*-----------------------------------------------------------*/

最佳答案

引用未使用的参数警告:实现 FreeRTOS tasks 的函数必须具有相同的原型(prototype),并且原型(prototype)包含一个参数。但是,并非所有任务实际上都想使用该参数,但如果该参数未使用,编译器将生成您看到的警告。该警告是良性的,您无法通过删除参数来修复它,因此为了保持编译器安静,只需通过将以下代码添加到任务来执行参数的无效读取:

/* 删除有关未使用参数的编译器警告。 */( void ) pv参数;

Ref 语句对第 123 行没有影响。无法发表评论,因为我不知道哪一行是第 123 行。

关于C Switch 语句 CMSIS FreeRTOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com