打印
[微控制器/MCU]

我也来玩玩多线程

[复制链接]
1731|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
米其林r|  楼主 | 2010-6-27 14:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
看着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);
}

相关帖子

沙发
LPC300| | 2010-6-27 14:28 | 只看该作者
好东西,不过现在我还领会不了啊!

使用特权

评论回复
板凳
米其林r|  楼主 | 2010-6-27 14:29 | 只看该作者
小提示:以前不知道怎么导入不压缩的工程文件,今天我又研究了一下,居然还真让我发现了,呵呵。下面就说说方法。


0.bmp (553.22 KB)
2010-5-16 22:00



点击如上图中的红圈表示的Import existing projects,会弹出如下的对话框


1.bmp (843.15 KB)
2010-5-16 22:00



点击红圈表示后面的Browse按钮找到对应的工程文件夹即可,如下图示

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

340

主题

1587

帖子

3

粉丝