看着chenzhufly把多线程Photothreads在LPC1343上玩的有声有色,有贴为证http://bbs.**/viewthread.php?tid=103789,我也禁不住要试试。因为LPC1114同样资源紧张,呵呵 下面就一试。
同样我也参考了Photothreads的例程example-small,更多的参考了chenzhufly的photothreads的那个帖子,在帖子里chenzhufly对photothreads已经做过了介绍,这里我就不再重复了。下面是我修改的完的全部代码,附件里有我修改的工程文件。
#include "LPC11xx.h" /* LPC11xx Peripheral Registers */
#include "gpio.h"
#include "pt.h"
/* Two flags that the two protothread functions use. */
static int protothread1_flag, protothread2_flag;
char flag = 0;
static struct pt pt1, pt2;
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 int protothread1(struct pt *pt);
static int protothread2(struct pt *pt);
/*****************************************************************************
** Main Function main()
******************************************************************************/
int main (void)
{
/* 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(&pt1);
PT_INIT(&pt2);
while( 1 )
{
protothread1(&pt1);
protothread2(&pt2);
};
}
/*
*************************************************************************************************************
** 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--);
}
static int
protothread1(struct pt *pt)
{
/* A protothread function must begin with PT_BEGIN() which takes a
pointer to a struct pt. */
PT_BEGIN(pt);
/* We loop forever here. */
while(1) {
/* Wait until the other protothread has set its flag. */
PT_WAIT_UNTIL(pt, protothread2_flag != 0);
flag = ~flag;
GPIOSetValue(PORT0,7,flag);
/* We then reset the other protothread's flag, and set our own
flag so that the other protothread can run. */
protothread2_flag = 0;
protothread1_flag = 1;
/* And we loop. */
}
/* All protothread functions must end with PT_END() which takes a
pointer to a struct pt. */
PT_END(pt);
}
/**
* The second protothread function. This is almost the same as the
* first one.
*/
static int
protothread2(struct pt *pt)
{
PT_BEGIN(pt);
while(1) {
/* Let the other protothread run. */
protothread2_flag = 1;
/* Wait until the other protothread has set its flag. */
PT_WAIT_UNTIL(pt, protothread1_flag != 0);
DelayMs(500);
/* We then reset the other protothread's flag. */
protothread1_flag = 0;
/* And we loop. */
}
PT_END(pt);
} |