本鸟在测试原子哥的UCOS消息队列实验时,发现KEY0跟KEY1按键处理有类似“串扰”的现象,具体是按下不灵敏并且有时按下KEY0时执行KEY1的相关代码,头大,求大神点拨
#include "task.h"
//****************************************¿ªÊ¼ÈÎÎñ************************************
//-----,¿ªÊ¼ÈÎÎñ£¬¿ªÊ¼ÈÎÎñµÄÏà¹ØºêºÍÉùÃ÷ÔÚÍ·ÎļþÖÐ
//ÈÎÎñ¿ØÖÆ¿é
OS_TCB START_TASK_TCB;
//ÈÎÎñ¶ÑÕ»
CPU_STK START_TASK_STK[START_STK_SIZE];
//ÈÎÎñÔ­ÐÍ
void start_task(void* p_arg);
//*****************************************Óû§ÈÎÎñ***************************
////ÈÎÎñ¿ØÖÆ¿é //-----Óû§ÈÎÎñ£¬user_task
//OS_TCB USER_TASK_TCB;
////ÈÎÎñÓÅÏȼ¶
//#define USER_TASK_PRIO 4
////ÈÎÎñ¶ÑÕ»´óС
//#define USER_STK_SIZE 128
////ÈÎÎñ¶ÑÕ»
//CPU_STK USER_TASK_STK[USER_STK_SIZE];
////ÈÎÎñÔ­ÐÍ
//void user_task(void* p_arg);
//------ÈÎÎñ1
//ÈÎÎñ¿ØÖÆ¿é
OS_TCB TASK1_TASK_TCB;
//ÈÎÎñÓÅÏȼ¶
#define TASK1_TASK_PRIO 5
//ÈÎÎñ¶ÑÕ»´óС
#define TASK1_STK_SIZE 128
//ÈÎÎñ¶ÑÕ»
CPU_STK TASK1_TASK_STK[TASK1_STK_SIZE];
//ÈÎÎñÔ­ÐÍ
void task1(void* p_arg);
//------ÈÎÎñ2
//ÈÎÎñ¿ØÖÆ¿é
OS_TCB TASK2_TASK_TCB;
//ÈÎÎñÓÅÏȼ¶
#define TASK2_TASK_PRIO 6
//ÈÎÎñ¶ÑÕ»´óС
#define TASK2_STK_SIZE 128
//ÈÎÎñ¶ÑÕ»
CPU_STK TASK2_TASK_STK[TASK2_STK_SIZE];
//ÈÎÎñÔ­ÐÍ
void task2(void* p_arg);
//------ÈÎÎñ3
//ÈÎÎñ¿ØÖÆ¿é
OS_TCB TASK3_TASK_TCB;
//ÈÎÎñÓÅÏȼ¶
#define TASK3_TASK_PRIO 7
//ÈÎÎñ¶ÑÕ»´óС
#define TASK3_STK_SIZE 128
//ÈÎÎñ¶ÑÕ»
CPU_STK TASK3_TASK_STK[TASK3_STK_SIZE];
//ÈÎÎñÔ­ÐÍ
void task3(void* p_arg);
//***************************************Èí¼þ¶¨Ê±Æ÷****************************************************
OS_TMR timer1;
#define TIMER1_DLY_TICKS 50 //¶¨Ê±Æ÷½ÚÅÄΪ10ms£¬Òò´ËÑÓʱ50 * 10 = 500ms
//#define TIMER2_DLY_TICKS 200
void timer1_callback(void);
//void timer2_callback(void);
//*************************************Ä£ÄâµÄ¹²Ïí×ÊÔ´Çø£¬¼´Ò»Æ¬ÄÚ´æ¿Õ¼ä******************************************
#define SHARE_SOURCES_BLOCK_SIZE 100 //Ä£Äâ¹²Ïí×ÊÔ´Çø´óС
u8 share_sources_block[SHARE_SOURCES_BLOCK_SIZE];
OS_SEM my_sem1; //¶¨ÒåÒ»¸öÐźÅÁ¿
OS_MUTEX my_mutex1; //¶¨ÒåÒ»¸ö»¥³âÐźÅÁ¿
//**************************************ÏûÏ¢¶ÓÁÐÇø***************************************************************
#define KEY_SCAN_VAL_Q_SIZE 1 //µÚÒ»¸öÏûÏ¢¶ÓÁеĴóС
#define PRINT_MSG_Q_SIZE 20 //µÚ¶þ¸öÏûÏ¢¶ÓÁеĴóС
OS_Q key_scan_val_q; //°´¼üɨÃèÖµµÄÏûÏ¢¶ÓÁÐ
OS_Q print_msg_q; //ÐèÒª´òÓ¡µÄÐÅÏ¢µÄÏûÏ¢¶ÓÁÐ
//**********************************¿ªÊ¼ÈÎÎñ´úÂë*************************************************
//¿ªÊ¼ÈÎÎñÊÇÓÃÀ´´´½¨ÆäËûÈÎÎñµÄ,ÔÚÕâÀï´´½¨Ò»¸öÓû§½ø³ÌºÍ´ò¿ªÁ½¸ö¶¨Ê±Æ÷
void start_task(void* p_arg)
{
OS_ERR error;
CPU_SR_ALLOC();
p_arg = p_arg;
OS_CRITICAL_ENTER();
// OSSemCreate( (OS_SEM *)&my_sem1, //´´½¨ÐźÅÁ¿
// (CPU_CHAR *)"my_sem1",
// (OS_SEM_CTR )1,
// (OS_ERR *)&error);
// OSMutexCreate( (OS_MUTEX *)&my_mutex1, //´´½¨»¥³âÐźÅÁ¿
// (CPU_CHAR *)"my_mutex1",
// (OS_ERR *)&error);
OSQCreate( (OS_Q *)&key_scan_val_q, //´´½¨µÚÒ»¸öÏûÏ¢¶ÓÁÐ
(CPU_CHAR *)"key_scan_val_q",
(OS_MSG_QTY )KEY_SCAN_VAL_Q_SIZE,
(OS_ERR *)error);
OSQCreate( (OS_Q *)&print_msg_q, //´´½¨µÚ¶þ¸öÏûÏ¢¶ÓÁÐ
(CPU_CHAR *)"print_msg_q",
(OS_MSG_QTY )PRINT_MSG_Q_SIZE,
(OS_ERR *)error);
// OSTmrCreate( (OS_TMR *)&timer1, //´´½¨Ò»¸öÈí¼þ¶¨Ê±Æ÷
// (CPU_CHAR *)"timer1",
// (OS_TICK )0,
// (OS_TICK )TIMER1_DLY_TICKS,
// (OS_OPT )OS_OPT_TMR_PERIODIC,
// (OS_TMR_CALLBACK_PTR )timer1_callback,
// (void *)0,
// (OS_ERR *)&error);
OSTaskCreate( (OS_TCB *)&TASK1_TASK_TCB, //´´½¨ÈÎÎñ1
(CPU_CHAR *)"task1",
(OS_TASK_PTR )task1,
(void *)p_arg,
(OS_PRIO )TASK1_TASK_PRIO,
(CPU_STK *)TASK1_TASK_STK,
(CPU_STK_SIZE )TASK1_STK_SIZE/10,
(CPU_STK_SIZE )TASK1_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void *)0,
(OS_OPT )OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR,
(OS_ERR *)&error);
OSTaskCreate( (OS_TCB *)&TASK2_TASK_TCB, //´´½¨ÈÎÎñ2
(CPU_CHAR *)"task2",
(OS_TASK_PTR )task2,
(void *)p_arg,
(OS_PRIO )TASK2_TASK_PRIO,
(CPU_STK *)TASK2_TASK_STK,
(CPU_STK_SIZE )TASK2_STK_SIZE/10,
(CPU_STK_SIZE )TASK2_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void *)0,
(OS_OPT )OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR,
(OS_ERR *)&error);
OSTaskCreate( (OS_TCB *)&TASK3_TASK_TCB, //´´½¨ÈÎÎñ3
(CPU_CHAR *)"task3",
(OS_TASK_PTR )task3,
(void *)p_arg,
(OS_PRIO )TASK3_TASK_PRIO,
(CPU_STK *)TASK3_TASK_STK,
(CPU_STK_SIZE )TASK3_STK_SIZE/10,
(CPU_STK_SIZE )TASK3_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void *)0,
(OS_OPT )OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR,
(OS_ERR *)&error);
OS_CRITICAL_EXIT();
OSTaskDel(&START_TASK_TCB, &error); //¿ªÊ¼ÈÎÎñÍê³ÉºóÓ¦¸Ãɾ³ý½ÚÊ¡Á´±í¿Õ¼ä
}
//*******************************************Óû§ÈÎÎñ´úÂëÇø**************************
//Óû§ÈÎÎñ´úÂë
//void user_task(void* p_arg)
//{
//
//}
//************************¶¨Ê±Æ÷»Øµ÷º¯Êý´úÂëÇø*********************************************
//void timer1_callback(void)
//{
//
//}
//**************************ÆÕͨÈÎÎñ´úÂëÇø*************************************************
//ÈÎÎñ1´úÂëÇø
/*¹¦ÄÜ£º·¢²¼°´¼üɨÃèÏûÏ¢·¢²¼µ½ÏìÓ¦µÄÏûÏ¢¶ÓÁÐÖÐ
**
**
*/
void task1(void* p_arg)
{
OS_ERR error;
static u8 key_val;
p_arg = p_arg ;
while(1)
{
key_val = key_scan(0);
if(key_val)
{
//·¢²¼°´¼üɨÃèÖµµ½ÏàÓ¦µÄÏûÏ¢¶ÓÁÐÖÐ
OSQPost(&key_scan_val_q, &key_val, KEY_SCAN_VAL_Q_SIZE, OS_OPT_POST_FIFO, &error);
}
OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_PERIODIC, &error); //ÑÓʱ1ms
}
}
//ÈÎÎñ2´úÂëÇø
/*¹¦ÄÜ£º´¦Àí°´¼üɨÃèÏûÏ¢
**KEY0:led1È¡·´
**KEY1:led2È¡·´
**KEYUP:ÉêÇëÒ»¿é¶¯Ì¬ÄÚ´æÇø´æÈë¸ñʽ×Ö·û´®²¢ÇÒ·¢²¼µ½ÏàÓ¦µÄÏûÏ¢¶ÓÁÐÖÐ
*/
void task2(void* p_arg)
{
u8 *key; //°´¼üɨÃè·¢²¼µÄÏûÏ¢µØÖ·
OS_ERR error;
OS_MSG_SIZE get_q_1_size; //µÚÒ»¸ö¶ÓÁеĴóС
static u8 q_2_times = 0;
static char * print_msg_p;
p_arg = p_arg ;
while(1)
{
key = OSQPend(&key_scan_val_q, 0, OS_OPT_PEND_BLOCKING, &get_q_1_size, 0, &error); //ÇëÇó°´¼üɨÃè·¢²¼µÄÏûÏ¢
switch(*key)
{
case KEY0_PRE:
print_msg_p = mymalloc(INSRAM_BANK, PRINT_MSG_Q_SIZE);
if(print_msg_p) //Èç¹ûÉêÇëµÄµØÖ·ºÏ·¨
{
q_2_times++; //ÿ´Î·¢²¼¸ÃÏûϢʱ¼ÆÊýÆ÷¼ÓÒ»
sprintf(print_msg_p, "print message: %d", q_2_times); //½«¸ñʽ»¯×Ö·û´®´æÈëµ½¶¯Ì¬´æ´¢ÇøÖÐ
OSQPost(&print_msg_q, print_msg_p, PRINT_MSG_Q_SIZE, OS_OPT_POST_FIFO, &error); //½«ÏûÏ¢·¢²¼µ½¶ÓÁÐ2
if(error != OS_ERR_NONE) //Èç¹û·¢²¼³ö´í
{
myfree(INSRAM_BANK, print_msg_p); //ÊÍ·ÅÄÚ´æ
}
}
break;
case KEY1_PRE:
led2 = ~led2;
break;
case KEYUP_PRE: //Èç¹û°´ÏÂUP¼ü£¬½«ÉêÇëÄÚ²¿SRAMÖеÄһЩ¿Õ¼ä
led1 = ~led1;
break;
}
}
}
//ÈÎÎñÈý´úÂëÇø
/*¹¦ÄÜ£º´¦Àí·¢²¼µÄ´ý´òÓ¡ÏûÏ¢µÄº¯Êý
**
**
*/
void task3(void* p_arg)
{
OS_ERR error;
u8 * msg_q_2_p;
OS_MSG_SIZE msg_q_2_size; //¶ÓÁÐ2ÖеÄÏûÏ¢´óС
p_arg = p_arg ;
while(1)
{
msg_q_2_p = OSQPend(&print_msg_q, 0, OS_OPT_PEND_BLOCKING, &msg_q_2_size, 0, &error); //ÇëÇó¶ÓÁÐ2ÖеÄÏûÏ¢£¬Èç¹ûûÓÐÏûÏ¢ÔòÒ»Ö±µÈ´ý
if(msg_q_2_p)
{
printf("%s
", msg_q_2_p); //½«¸ÃÏûÏ¢´òÓ¡³öÀ´
myfree(INSRAM_BANK, msg_q_2_p);
}
}
}
//***********************************************************************************
呃,注释没了,我在附件中将测试工程发上去
下载寄存器版本代码,测试下。
然后说一下具体的实验过程,出错情况。
我这里测试没啥问题。
一周热门 更多>