⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 301cabec08c5001d1a38f54921afee39

📁 基于FREERtos和LPC2468的程序。里面实现了串口通信
💻
字号:

/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
//#include "mkd.h"
#include "type.h"
#include "../gui/font5_7.h"
#include "uart.h"
#include "stdio.h"
#include "package.h"
#include "keyscan.h"
#include "../app/kbd.h"
#include "../gui/GUI_StockC.h"
#include "../lcd/lcddrive.h"
/* ---------------------------------------------------------------------------------------- */


/* ---------------------------------------------------------------------------------------- */
//extern  uint8 const  DCB2HEX_TAB[8];

uint8 curCmdStat=0x00;			//用来标志系统是否忙
extern struct TURNPAGE m_TurnPage;
/*-----------------------------------------------------------
 * function : responseErrorReport
 * parameter:errcode
 * specification : if Cmd received from the upper is uncorrect ,
 *               return the errcode to the upper
 * 
 * -----------------------------------------------------------*/
void responseErrorReport(unsigned char errcode)
{
	uint8 buf;
	buf=(uint8)errcode;
	responseReport('A',&buf,1);
}

/*-------------------------------------------------------------
 * function: responseReport
 * specification:construct datas for responsing
 * parameters:flag A or I;pStr is the pointer of returning datas
 *       len=sizeof(pStr)+1
 *-------------------------------------------------------------*/
void responseReport(unsigned char flag,uint8 *pStr,uint8 len)
{
	int oLen,offset;
	char buf[200];
	unsigned char BccChr;
	offset=0;
	BccChr=0;
	buf[offset]=0x02; 					BccChr=BccChr^0x02;			offset++; 
	buf[offset]=0x2c; 					BccChr=BccChr^0x2c;			offset++;
	buf[offset]=len+1;					BccChr=BccChr^buf[offset];	offset++;
	buf[offset]=flag; 					BccChr=BccChr^buf[offset];	offset++;
	for(oLen=0;oLen<len;oLen++)
	{	
		buf[offset]=*(pStr+oLen); 		BccChr=BccChr^buf[offset];	offset++;
	}
	buf[offset]=0x03; 					BccChr=BccChr^buf[offset];	offset++;
	 
	buf[offset]=BccChr;					offset++;
	
	xUARTSend( (unsigned char *) buf,(DWORD)offset); 
}

/*-----------------------------------------------------------------------
 * function : vStartPCKTasts
 * specification : start a tast for receive Cmd from the upper
 * parameter : uxPriority is the priority of task
 * 
 * 
 * ----------------------------------------------------------------------*/

void vStartPCKTasks( unsigned portBASE_TYPE uxPriority )
{
	/* Create the task. */
	
	xTaskCreate( vPCKTask, ( signed portCHAR * ) "PCK", pckSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );		
}
/*------------------------------------------------------------------------
 * 
 * function : vPCKTask
 * specification : Crearte a task named vPCKTask
 * parameter : pvParameters 
 * 
 * ------------------------------------------------------------------------*/
static portTASK_FUNCTION( vPCKTask, pvParameters )
{
	//uint8 tmpstring[10]={0x30,0x00,0xff,0x02,0x04,0x05};
	portTickType xPCKRate, xLastPCKTime;
	int retCode;
	uint8 tmpChar[20];
	//char * pStr;
	/* The parameters are not used. */
	( void ) pvParameters;
	//GUI_PutString(10,90,"init package..");
	xUARTStartReceive();			//notice uart communication irq , may start receiving
	m_TurnPage.TurnFlag=0;
	m_TurnPage.ptr=0;
	xPCKRate = pck_RATE_BASE ;
	xPCKRate /= portTICK_RATE_MS;	//delay xPCKRate ms
	/* We need to initialise xLastPCKTime prior to the first call to vTaskDelayUntil(). */
	xLastPCKTime = xTaskGetTickCount();

	for(;;)
	{
		/* Delay for half the flash period then turn the LED on. 	*/
		vTaskDelayUntil( &xLastPCKTime, xPCKRate );
		//PollingKeyMKD(tmpstring);
		retCode=xUARTRead((unsigned char*)cmdBuff,(DWORD*)&cmdBuff_Len);   //从串口缓冲中取数据
		if(retCode==0x0f) continue;			//no data ,continue waiting
		//xUARTStartReceive();				//UART Restart
		if(retCode>0&&retCode<5)			//如果接收错误,返回接收错误帧
		{
			/*package error  */
			responseErrorReport(retCode);
			continue;	
		}
		if(cmdBuff[0]=='S')		//如果接收到一个初始化帧
		{
			responseErrorReport(0);
						//初始化系统
		}
		else if(cmdBuff[0]=='I')	//如果接收到一个查询帧
		{
			retCode=DoCommand(cmdBuff);
			if(retCode==0x02) responseErrorReport(0x02);		//device busy
			
		}else if(cmdBuff[0]=='C')	//如果接收到一个命令帧 
		{
			retCode=2;				//返回一个确认帧
			tmpChar[0]=0x00;
			tmpChar[1]=0x00;
			responseReport('A',tmpChar,retCode);
		}
		else						//无此命令标志
		{
			 responseErrorReport(0x03);	//error command 
		}
	}
} 
/*lint !e715 !e818 !e830 Function definition must be standard for task creation. */
/*----------------------------------------------------------------------------------
 * 
 * function : DoCommand
 * specificaiton : specify the received command and perform the command
 * parameters : cmdStr and  cmdLen
 * 
 * --------------------------------------------------------------------------------*/
int DoCommand(uint8 * ptr)
{
		uint8 tmpData;
		//uint8 * ptr;
		if(curCmdStat) return 2;		//device busy
		//ptr=curCmdStat;
		tmpData=*(ptr+1);				//获得Cmd
		portENTER_CRITICAL();
		{
			curCmdStat=0x02;			//设置忙标志位
			switch(tmpData)
			{
				case 0x30:				//开关背景灯
					//CurrCmdStatMKD.curCmdNob=0;
					SwitchlightMKD(ptr+2);
					break;
				case 0x32:				//区域清屏
					//CurrCmdStatMKD.curCmdNob=2;
					ClearScreenMKD(ptr+2);
					break;
				case 0x33:				//显示内容
					//CurrCmdStatMKD.curCmdNob=3;
					DisplayStringMKD(ptr+2,cmdBuff_Len-5);		//cmdBuff_Len-5是显示内容占的字节
					break;
				case 0x34:				//设置显示模式
					//CurrCmdStatMKD.curCmdNob=4;
					SetDisplayMode(ptr+2);
				case 0x35:				//读取键值
					//CurrCmdStatMKD.curCmdNob=5;
					PollingKeyMKD(ptr+2);
					break;
			}
			curCmdStat=0x00;		//清楚忙标志位
		}
		portEXIT_CRITICAL();
		return 0;
}
/*-----------------------------------------------------------------------
 * 
 * 区域清屏
 * 
 * 
 * 
 * ----------------------------------------------------------------------*/
int  ClearScreenMKD(uint8 *ptr)
{
	uint8 x,y,w,h,tmpChar[20];
	uint8  fillData=0x00;
	ptr++;					//跳过pm
	x=(*ptr)*12-12;		ptr++;	//计算第x行顶点的像素坐标
	y=(*ptr)*6-6;		ptr++;		//计算第y列顶点的像素坐标
	w=(*ptr)*6;			ptr++;	//计算w列宽的像素值
	h=(*ptr)*12;		ptr++;		//计算h行高占的像素值
	if(*ptr==0x31)
			fillData=0xff;	
	LCD_FillDat(x,y,w,h,fillData);	//填充指定区域
	tmpChar[0]=0x00;				//返回数据
	tmpChar[1]=0x00;
	responseReport('R',tmpChar,3);
	return NORMAL;	
}
/*---------------------------------------------------------------------------
 * 
 * 开或关背景灯
 * 
 * 
 * --------------------------------------------------------------------------*/
int  SwitchlightMKD(uint8 * ptr)
{
	uint8 tmpChar[20];
	uint8 * buf;
	buf=ptr;			//pm
	if(*buf==0x30)
		LCD_BackLight(1);
	else
		LCD_BackLight(0);
	tmpChar[0]=0x00;
	tmpChar[1]=0x00;
	responseReport('R',tmpChar,3);
	return NORMAL;	
}

/*-------------------------------------------------------------------------------
 * 
 * 
 * function : display a string on appointing location.
 * 
 * parameter : ptr 从显示坐标开始,len为显示内容所占的长度
 * 
 * 
 * ------------------------------------------------------------------------------*/
int  DisplayStringMKD(uint8 * ptr,uint8 len)  
{
	//uint8 num;
	uint8 tmpChar[20];
	uint32 x,y;
	int tmp;
	//uint8 string[254];
	ptr++;						/* Skip the PM */ 
	x=(*ptr);		ptr++;			// get the x
	y=(*ptr);		ptr++;			//get the y
	tmp=len+10*x+y-110;				//是否需要翻页
	if(tmp)
	{
		PutString(x,y,ptr,110-10*x-y);
		m_TurnPage.TurnFlag=tmp;
		m_TurnPage.ptr=ptr+110-10*x-y;
		DisplayFlag(2);
	}
	PutString(x,y,ptr,len);
	tmpChar[0]=0x00;
	tmpChar[1]=0x00;
	responseReport('R',tmpChar,3);
	return NORMAL;	
}
/*-------------------------------------------------------------------------------
 * 
 * function : SetDisplayMode 
 * specification : 设置显示方式,0x00白底黑字;0x01黑底白字 
 * 
 * parameter : ptr-- the pointer to the command 
 * 
 * ------------------------------------------------------------------------------*/
int SetDisplayMode(uint8 *ptr)
{
	uint8 tmp,tmpChar[20];
	tmp=*(ptr+1);				// skip pm
	if(tmp==0x00)
		GUI_SetColor(1,0);
	else
		GUI_SetColor(0,1);
	tmpChar[0]=0x00;
	tmpChar[1]=0x00;
	responseReport('R',tmpChar,3);
	return 0;
}
/*-----------------------------------------------------------------------------------
 * 
 * 显示一个字符串
 * 在x行,y列显示一个字符串,宽度len
 * 
 * ---------------------------------------------------------------------------------*/
void PutString(uint8 x,uint8 y,uint8 *ptr,uint8 len)
{
	uint32 m_x,m_y;
	m_x=(uint32)(x-1)*12;				//计算行像素坐标
	m_y=(uint32)(y-1)*6;				//计算列像素坐标
	GUI_PutString(m_y,m_x,ptr,len);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -