前两个帖子里分别以不同的方式实现的LED的闪烁,今天再以信号量的方式来实现LED的闪烁。程序里一共用到了两个信号量,分别为io_sem和delay_sem,这两个信号量就类似一个互斥的变量,使得程序交替运行延时和IO反转的进程。下面上传源代码,同样附件里放工程文件。
#include "LPC11xx.h" /* LPC11xx Peripheral Registers */
#include "gpio.h"
#include "pt.h"
#include "pt-sem.h"
char flag = 0;
static struct pt_sem delay_sem,io_sem;
typedef unsigned char uint8; //defined for unsigned 8-bits integer variable
typedef signed char int8; //defined for signed 8-bits integer variable
typedef unsigned int uint16; //defined for unsigned 16-bits integer variable
typedef signed int int16; //defined for signed 16-bits integer variable
typedef unsigned long uint32; //defined for unsigned 32-bits integer variable
typedef signed long int32; //defined for signed 32-bits integer variable
typedef float fp32; //single precision floating point variable (32bits)
typedef double fp64; //double precision floating point variable (64bits)
void Delay(uint16 dly);
void DelayMs(uint16 dly);
static PT_THREAD(togio_thread(struct pt *pt));//
static PT_THREAD(delay_thread(struct pt *pt));
static PT_THREAD(schedule_thread(struct pt *pt));
/*****************************************************************************
** Main Function main()
******************************************************************************/
int main (void)
{
struct pt schedule_pt;
/* Basic chip initialization is taken care of in SystemInit() called
* from the startup code. SystemInit() and chip settings are defined
* in the CMSIS system_<part family>.c file.
*/
GPIOInit();
/* use port0_7 as output event*/
GPIOSetDir( PORT0, 7, 1 );
/* Initialize the protothread state variables with PT_INIT(). */
PT_INIT(&schedule_pt);
while(PT_SCHEDULE(schedule_thread(&schedule_pt)) )
{
;
};
return 0;
}
/*
*************************************************************************************************************
** Function name : Delay
** Descriptions : Delay
** Global variables:
** Input parameters:
** Returned value :
** Calling modules :
** Example :
** Created by : LiXiaohai
** Created date : 2010-04-29
**-----------------------------------------------------------------------------------------------------------
** Modified by :
** Modified date :
**-----------------------------------------------------------------------------------------------------------
*************************************************************************************************************
*/
void DelayMs(uint16 dly)
{
while(dly--)
{
Delay(2678);
}
}
/*
*************************************************************************************************************
** Function name : void Delay(uint16 dly)
** Descriptions : Delay
** Global variables:
** Input parameters:
** Returned value :
** Calling modules :
** Example :
** Created by : LiXiaohai
** Created date : 2010-04-29
**-----------------------------------------------------------------------------------------------------------
** Modified by :
** Modified date :
**-----------------------------------------------------------------------------------------------------------
*************************************************************************************************************
*/
void Delay(uint16 dly)
{
while(dly--);
}
/**
* Declare the first protothread function-toggleIO protothread
*/
static PT_THREAD(togio_thread(struct pt *pt))
{
PT_BEGIN(pt);/*start of toggleIO protothread*/
PT_SEM_WAIT(pt,&delay_sem);
flag = ~flag;
GPIOSetValue(PORT0,7,flag);/*IO toggle*/
PT_SEM_SIGNAL(pt,&io_sem);
PT_END(pt); /*declare the end of the toggleIO protothread*/
}
/**
* Declare the second protothread function-delay protothread
*/
static PT_THREAD(delay_thread(struct pt *pt))
{
PT_BEGIN(pt); /*start of delay protothread*/
PT_SEM_WAIT(pt,&io_sem);
DelayMs(500);/*delay 500ms*/
PT_SEM_SIGNAL(pt,&delay_sem);
PT_END(pt); /*declare the end of the delay protothread*/
}
/**
* Declare the third protothread function-schedule protothread
*/
static PT_THREAD(schedule_thread(struct pt *pt))
{
static struct pt delay_pt, togio_pt;
PT_BEGIN(pt); /*start of delay protothread*/
PT_SEM_INIT(&io_sem,0);
PT_SEM_INIT(&delay_sem,1);
PT_INIT(&delay_pt);
PT_INIT(&togio_pt);
while(1)
{
PT_WAIT_THREAD(pt,delay_thread(&delay_pt)&togio_thread(&togio_pt));
}
PT_END(pt); /*declare the end of the delay protothread*/
}
gpio_photothread_3.rar
(72.98 KB, 下载次数: 23)
[
本帖最后由 lixiaohai8211 于 2010-5-19 21:45 编辑 ]