先要声明任务指针,因为后面需要使用。
//任务指针.
volatile TASK_TCB* volatile g_pCurrentTask = NULL;
volatile TASK_TCB* volatile g_pCurrentTask1 = NULL;
volatile TASK_TCB* volatile g_pCurrentTask2 = NULL;
接着就需要初始化这些任务栈,用下面的代码进行初始化,为了简单,全部使用内存地址操作的方式,当然后面会改成动态地分配内存的方式。代码如下:
///////////////////////////////////////////////////////////////////////////////
//函数名称: TaskInitStack
//函数功能: 分配任务的栈。
//输入参数:
//输出参数:
//返 回 值:
//开发人员: 蔡军生
//时 间: 2006/02/26
//修改说明:
//
///////////////////////////////////////////////////////////////////////////////
void TaskInitStack(void)
{
g_pCurrentTask1 = (PTASK_TCB)0x0c700000;
g_pCurrentTask1->pStackStart = (UINT*)(0x0c700000+0x200);
g_pCurrentTask1->pStackTop = g_pCurrentTask1->pStackStart + 0x100;
g_pCurrentTask2 = (PTASK_TCB)(0x0c700000 + 0x400);
g_pCurrentTask2->pStackStart = (UINT*)(0x0c700000+0x400 + 0x200);
g_pCurrentTask2->pStackTop = g_pCurrentTask2->pStackStart + 0x100;
}
接着再创建两个简单的任务,它们都是输出一行字符串,就等待一会,代码如下:
//
void TaskTest1(void)
{
for(;;)
{
Lock();
puts('TaskTest1/n');
UnLock();
SoftDelay(100);
}
}
//
void TaskTest2(void)
{
for(;;)
{
Lock();
puts('TaskTest2/n');
UnLock();
SoftDelay(100);
}
}
然后再初始化任务栈,代码如下:
void TaskStart(void)
{
//
UINT* pTemp = g_pCurrentTask1->pStackTop;
//
*g_pCurrentTask1->pStackTop = (UINT)TaskTest1;
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x14141414; /* R14 */
g_pCurrentTask1->pStackTop--;
// *g_pCurrentTask1->pStackTop = (UINT)pTemp; /* R13 */
// g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x12121212; /* R12 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x11111111; /* R11 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x10101010; /* R10 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x09090909; /* R9 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x08080808; /* R8 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x07070707; /* R7 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x06060606; /* R6 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x05050505; /* R5 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x04040404; /* R4 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x03030303; /* R3 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x02020202; /* R2 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x01010101; /* R1 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0; /* R0 */
g_pCurrentTask1->pStackTop--;
*g_pCurrentTask1->pStackTop = (UINT)0x13; /* SPSR */
//
//
//
pTemp = g_pCurrentTask2->pStackTop;
//
*g_pCurrentTask2->pStackTop = (UINT)TaskTest2;
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x14141414; /* R14 */
g_pCurrentTask2->pStackTop--;
// *g_pCurrentTask2->pStackTop = (UINT)pTemp; /* R13 */
// g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x12121212; /* R12 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x11111111; /* R11 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x10101010; /* R10 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x09090909; /* R9 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x08080808; /* R8 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x07070707; /* R7 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x06060606; /* R6 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x05050505; /* R5 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x04040404; /* R4 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x03030303; /* R3 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x02020202; /* R2 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x01010101; /* R1 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0; /* R0 */
g_pCurrentTask2->pStackTop--;
*g_pCurrentTask2->pStackTop = (UINT)0x13; /* SPSR */
//设置首先运行的任务是1.
g_pCurrentTask = g_pCurrentTask1;
}
代码初始化了两个任务栈后,接着设置第一个任务为优先运行的任务。
最后就需要进行中断任务调度任务进行运行了,代码如下:
///////////////////////////////////////////////////////////////////////////////
//函数名称: EIntTickIsr
//函数功能: 时钟中断函数。
//输入参数:
//输出参数:
//返 回 值:
//开发人员: 蔡军生
//时 间: 2006/02/26
//修改说明:
//
///////////////////////////////////////////////////////////////////////////////
上一篇:学习ARM开发(20)
下一篇:学习ARM开发(22)
推荐阅读最新更新时间:2026-03-24 11:06
- 热门资源推荐
- 热门放大器推荐
- REF196 精密微功率、低压差堆叠电压基准的典型应用电路
- LTC4367IMS8 用于滞后调节的过压电源控制器的典型应用
- CLRC663非接触式读写器IC的典型应用
- 使用 ON Semiconductor 的 CS-5621 的参考设计
- RSO-0515S 15V、67mA输出DC/DC转换器典型应用电路
- NCP51200 3 Amp VTT 终端稳压器 DDR1、DDR2、DDR3、LPDDR3、DDR4 的典型应用
- LR645大电流SMPS启动电路典型应用
- ADR420 可编程电流源的典型应用
- DC417B,使用 LT1806CS8 多尺寸单运算放大器原型的演示板
- DM300019,用于评估 dsPIC30F 和 dsPIC33F MCU 系列器件的 dsPICDEM 入门开发板



智慧门铃源码
MAX32625 PICO 嵌入式程序
现代雷达系统的信号设计
ALD2701Z
BFR340T






京公网安备 11010802033920号