上一次在帖子https://bbs.eeworld.com.cn/thread-105116-1-1.html玩多线程用的是标志位调度多线程,从而实现两个线程的切换。今天接着以另外一种方式来实现两个线程的切换。这里参考了photoreads的例程example-codelock和example-buffer。说起来也很简单,我一共用了两个线程,一个用于延时,一个用于IO口反转。具体实现的方法就是当延时时间到的时候退出延时线程,执行IO口反转线程,同时反转完IO口之后再重新启动延时线程,从而实现了LED的闪烁,下面是程序源代码,附件里有整个工程文件。
#include "LPC11xx.h" /* LPC11xx Peripheral Registers */
#include "gpio.h"
#include "pt.h"
char flag = 0;
static struct pt delay_pt, togio_pt;
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));
/*****************************************************************************
** 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(&delay_pt);
PT_INIT(&togio_pt);
while( 1 )
{
// PT_SCHEDULE(togio_thread(&togio_pt));
togio_thread(&togio_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*/
while(!PT_SCHEDULE(delay_thread(&delay_pt))) { //
flag = ~flag;
GPIOSetValue(PORT0,7,flag);/*IO toggle*/
PT_RESTART(&delay_pt);/*Restart the delay protothread*/
}
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*/
while(1) {
DelayMs(500);/*delay 500ms*/
PT_EXIT(pt); /*exit the delay protothread*/
}
PT_END(pt); /*declare the end of the delay protothread*/
}
gpio_photothread_2.rar
(66.45 KB, 下载次数: 37)