三合一实验板
#include "config.h"
TMbxQueue MBX_LED;
typedef struct
{
uint8 led;
uint8 delay;
}my_message_t;
#define LED0 IO_BIT_ALIAS(&GPIOD->ODR,
3)
//LED0,IO口位域别名
#define LED1 IO_BIT_ALIAS(&GPIOD->ODR,
2)
//LED1,IO口位域别名
#define LED2 IO_BIT_ALIAS(&GPIOD->ODR,
0)
//LED2,IO口位域别名
void LED_Set(uint8 led,uint8 on_off)
{
if(led==
0) LED0=on_off;
else if(led==
1) LED1=on_off;
else if(led==
2) LED2=on_off;
}
TSK_Implement(
0)
//任务0
{
my_message_t *msg;
uint8 led;
uint8 delay;
sei();
//任务开始开中断
while(
1)
{
while(MBX_CountQuery(&MBX_LED)>
20)
//限制发送消息数量,避免内存溢出
{
TSK_Delay(
10);
}
led = util_rand8()%
0x03;
//随机led,0-2
delay = util_rand8()&
0x7f;
//随机delay,0-0x7f
msg=VT_HeapAlloc(
sizeof(my_message_t));
//动态分配消息空间
if(msg!=NULL)
{
msg->led=led;
//填充消息
msg->delay=delay;
MBX_Post(&MBX_LED,msg);
//发送消息
}
}
}
TSK_Implement(
1)
//任务1
{
my_message_t *msg;
uint8 led;
uint8 delay;
sei();
while(
1)
{
msg=MBX_Pend(&MBX_LED);
//等待消息
led=msg->led;
//读取消息
delay=msg->delay;
VT_HeapFree(msg);
//释放消息存储空间
LED_Set(led,
1);
//led亮
TSK_Delay(delay);
//延时
LED_Set(led,
0);
//led灭
}
}
int main()
{
cli();
//任务开始调度前,要关中断
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_SLOW);
VT_HeapInit();
//初始化堆
VT_TicksInit();
//系统节拍初始化
TSK_Create(
0,TSK_PROC(
0),
0,
100);
//任务0初始化,优先级0
TSK_Create(
1,TSK_PROC(
1),
1,
100);
//任务1初始化,优先级1
//TSK_Create(2,TSK_PROC(2),2,100); //任务2初始化,优先级2
TSK_Start();
//任务开始调度(开始调度由空闲任务主函数切换到用户任务)
sei();
//空闲任务主函数开中断,空闲任务主函数也有自己单独的总中断标志位
while(
1)
//任务空闲时,运行这里
{
}
}
-
-
VoidSched.rar (982.77 KB)