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

📄 main.c.bak

📁 uCos应用
💻 BAK
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzhou ZHIYUAN electronics Co.,LTD.
**                                     
**                                 http://www.embedtools.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name:          Main.c
** Last modified Date: 2007-04-17
** Last Version:       1.0
** Description:        The main function example template  主函数例子模版
** 
**------------------------------------------------------------------------------------------------------
** Created By:         maliang
** Created date:       2007-04-17
** Version:            1.0
** Descriptions:       The original version 初始版本
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
********************************************************************************************************/
#include <includes.h>

/********************************************************************************************************
*                                               CONSTANTS 常量                                          *
********************************************************************************************************/


/********************************************************************************************************
*                                               VARIABLES 变量                                          *
********************************************************************************************************/
static  OS_STK   Task_StartStk[TASK_START_STK_SIZE];  // The stack of start task  启动任务的堆栈
static  OS_STK 	 Task_AStk[TASK_A_STK_SIZE];
static  OS_STK 	 Task_BStk[TASK_B_STK_SIZE];
static  OS_STK 	 Task_CStk[TASK_C_STK_SIZE];
static  OS_STK 	 Task_DStk[TASK_D_STK_SIZE];


OS_EVENT *DispMsg;

/********************************************************************************************************
*                                        FUNCTION PROTOTYPES 函数声明                                   *
********************************************************************************************************/
static  void   Task_Start              (void *p_arg);  // The start task  启动任务
static  void   Task_A                  (void *p_arg);
static  void   Task_B                  (void *p_arg);
static  void   Task_C                  (void *p_arg);
static  void   Task_D                  (void *p_arg);

static  void   CreateTask              (void);
void LcdTest(void);


uint8  gDispFlg = 0;
uint32 gmVol;
/********************************************************************************************************
*                                       MAIN FUNCTION 主函数                                            *
********************************************************************************************************/
int main (void)
{
	PLLSet();
    IntDisAll();    // Disable all the interrupts 关闭所有中断
    //=============================================================================//	
    OSInit();      // Initialize the kernel of uC/OS-II 初始化uC/OS-II的内核

    OSTaskCreate ( Task_Start,       // Initialize the start task  初始化启动任务
		           (void *)0, 
				   &Task_StartStk[TASK_START_STK_SIZE-1], 
				   TASK_START_PRIO );
				     
    OSStart();                        // Start uC/OS-II  启动uC/OS-II
    return(0) ;
}

/********************************************************************************************************
*                                            Task_Start                                                 *
********************************************************************************************************/
static  void  Task_Start (void *p_arg)
{
	(void)p_arg;
    TargetInit();								// Initialize the target's MCU  初始化目标单片机
    RTC_init();
	UART1Init(38400,1);							//初始化串口0,波特率115200,中断优先级为1                         

	I2CInit(100000, 0);							// 初始化I2C驱动
	if(PCF8576_Ini(MODE_E, NOGLITTER)!=1)		// 初始化PCF8576驱动
		{ while(1); }
	//I2cInitial();								// 初始化I2C
#if OS_TASK_STAT_EN > 0
    OSStatInit();								// Enable statistics  使能统计功能
#endif

	Led_Toggle(0xff);
	OSTimeDly(OS_TICKS_PER_SEC/5);
	Led_Toggle(0xff);
	OSTimeDly(OS_TICKS_PER_SEC/5);
	Led_Toggle(0xff);
   
  	/***Create the other tasks here. 在这里创建其他任务***/
    CreateTask();
	while (1)
	{                             
        OSTaskSuspend(OS_PRIO_SELF);			// The start task can be pended here. 启动任务可在这里挂起
    }
}

/*
*********************************************************************************************************
*                                      CREATE APPLICATION TASKS
*********************************************************************************************************
*/
static  void  CreateTask (void)
{
	DispMsg = OSMboxCreate((void*)0);
    OSTaskCreate (Task_A,(void *)0, 
				   &Task_AStk[TASK_A_STK_SIZE-1], TASK_A_PRIO);

    OSTaskCreate (Task_B,(void *)0, 
				   &Task_BStk[TASK_B_STK_SIZE-1], TASK_B_PRIO);
    OSTaskCreate (Task_C,(void *)0, 
				   &Task_CStk[TASK_C_STK_SIZE-1], TASK_C_PRIO);
    OSTaskCreate (Task_D,(void *)0, 
				   &Task_DStk[TASK_D_STK_SIZE-1], TASK_D_PRIO);
}

void delay(unsigned long i)
{
	while(i--);
}



void beep(void)
{
	Buz_On();
	OSTimeDly(4);
	Buz_Off();
}


/********************************************************************************************************
*                                           The tasks A                                 *
********************************************************************************************************/
uint8 Buf[256];
uint8 TraBuf[50];
static void Task_A(void *p_arg)
{
	uint8 i,j;
    //uint8 err;
    //uint8 *msg;
    (void)p_arg;
	beep();
	for(i=0;i<8;i++)
	{
		Buf[i] = i;
	}
	EEPROMWrite(Buf, 0, 8);   // 向EEPROM的00地址单元中写入8个数据(0~7)。
	OSTimeDly(5);
	EEPROMRead(Buf, 0, 8);    // 从EEPROM的00地址单元中读取8个数据。
	for(i=0;i<8;i++)
	{
		if(Buf[i] != i)
		{
			for(j=0;j<5;j++)
				beep();
			while(1);
		}
	}
    
	i = 0;
    while(1)
    {
		while( QueueNData(UART1_Rec_Queue) >0 ) 
		{
            QueueRead(TraBuf, UART1_Rec_Queue);
            UART1Send(TraBuf, 1);
        }
    	
        while( QueueNData(Uart0_Rec_Queue) >0 ) 
	    {
            QueueRead(Buf, Uart0_Rec_Queue);
            Uart0Send(Buf, 1);
        }
        Led_Toggle(i);
     	i++;
     	if(i>4) i= 1;
     	
		OSTimeDly(5);
    }
}
/********************************************************************************************************
*                                           The tasks B                                   *
********************************************************************************************************/
static void Task_B(void *p_arg)
{
    uint8 key,keep;
    //uint8 err,msg;
    uint8 pucString[] = "welcome to http://www.zlgmcu.com\r\n";
    (void)p_arg;
	Uart0Send(pucString,sizeof(pucString));

    while(1)
    {
        key = Key_Read( );
		while(Key_Read( )!=0xFF)		// 等待按键释放,消除按键抖动
			OSTimeDly(1);
			
        switch(key)
        {
        case 0xFE:							// KEY1
        	beep();
         	gDispFlg = 1;
 	      	keep = 1;
		    while(keep)
		    {
		        key = Key_Read( );
				while(Key_Read( )!=0xFF)	// 等待按键释放,消除按键抖动
					OSTimeDly(1);
		        switch(key)
		        {
		        case 0xFE:                  // KEY1
				case 0xFD:                  // KEY2
				case 0xFB:                  // KEY3
				case 0xF7:                  // KEY4
		        	beep();
		            break;
				
				case 0xF3:                  // KEY3和KEY4的组合键
		        	beep();
		        	beep();
		        	beep();
					keep = 0;
					break;
				default:
		            break;
				}
				OSTimeDly(4);
		    }
         	gDispFlg = 0;
            break;
        
        case 0xFD:							// KEY2
        	beep();
            break;
        
        case 0xFB:							// KEY3
        	beep();
        	gDispFlg = 1;
        	keep = 1;
		    while(keep)
		    {
		        key = Key_Read( );
				while(Key_Read( )!=0xFF)	// 等待按键释放,消除按键抖动
					OSTimeDly(1);
				if(key!=0xFF)
				{
      			  	beep();
					break;
				}
				Lcd_Seg.D1 = HexToSeg(gmVol/1000)+LCD_DP;
				Lcd_Seg.D2 = HexToSeg(gmVol%1000/100);
				Lcd_Seg.D3 = HexToSeg(gmVol%100/10);
				Lcd_Seg.D4 = HexToSeg(gmVol%10);
				Lcd_Seg.Sign = LCD_V;
				Lcd_Update();
				OSTimeDly(10);
			}
         	gDispFlg = 0;
       	
            break;
        case 0xF7:                  // KEY4
        	beep();
        	gDispFlg = 1;
			LcdTest();
        	gDispFlg = 0;
            break;
            
		case 0xF3:                  // KEY3和KEY4的组合键
        	beep();
            break;
		
		default:
            break;
        }
        OSTimeDly(2);
    }
}


/********************************************************************************************
    任务C
    实时时钟显示。默认起始时间为2008年8月8日8时8分0秒
    按 年--- 月日 --- 时分 --- 秒,来回显示,每秒切换一次。
********************************************************************************************/
static void Task_C (void *p_arg)
{
    uint8 disp=3;
    (void)p_arg;
	time.year = 2008;
	time.month = 8;
	time.date = 8;
	time.hour = 8;
	time.minute= 8;
	time.second= 0;
	
    while(1)
    {
    	while(gDispFlg)					// 如果其他任务有LCD,等待
    		OSTimeDly(2);
    		
        switch(disp)
        {
        case 0:                  // 年
			Lcd_Seg.D1 = HexToSeg(time.year/1000);
			Lcd_Seg.D2 = HexToSeg(time.year%1000/100);
			Lcd_Seg.D3 = HexToSeg(time.year%100/10);
			Lcd_Seg.D4 = HexToSeg(time.year%10)+LCD_DP;
			Lcd_Seg.Sign = LCD_YEAR;
			Lcd_Update();
            break;
            
        case 1:                  // 月日
			Lcd_Seg.D1 = HexToSeg(time.month/10);
			Lcd_Seg.D2 = HexToSeg(time.month%10)+LCD_DP;
			Lcd_Seg.D3 = HexToSeg(time.date/10);
			Lcd_Seg.D4 = HexToSeg(time.date%10)+LCD_DP;
			Lcd_Seg.Sign = LCD_MON+LCD_DAY;
			Lcd_Update();
            break;
        case 2:                  // 时分
			Lcd_Seg.D1 = HexToSeg(time.hour/10);
			Lcd_Seg.D2 = HexToSeg(time.hour%10)+LCD_DP;
			Lcd_Seg.D3 = HexToSeg(time.minute/10);
			Lcd_Seg.D4 = HexToSeg(time.minute%10)+LCD_DP;
			Lcd_Seg.Sign = LCD_PM+LCD_DOT;
			Lcd_Update();
            break;
        case 3:                  // 秒
			Lcd_Seg.D1 = 0;
			Lcd_Seg.D2 = 0;
			Lcd_Seg.D3 = HexToSeg(time.second/10);
			Lcd_Seg.D4 = HexToSeg(time.second%10)+LCD_DP;
			Lcd_Seg.Sign = 0;
			Lcd_Update();
			OSTimeDly(20);
			break;
            
        default:
            break;
        }
    	disp++;
    	disp = disp & 0x03;
        OSTimeDly(20);
    }
}

/********************************************************************************************
    任务D
    采集电压数据
********************************************************************************************/
static void Task_D (void *p_arg)
{
	(void)p_arg;
	ADCInit();
    while(1)
    {
    	gmVol = GetADC();
        OSTimeDly(20);
    }
	
}

/*
************************************************************************************************
** 函数名称:LcdTest()
** 函数功能:一段一段的显示LCD,用于测试LCD。
** 入口参数:无
** 出口参数:无
*************************************************************************************************
*/ 
void LcdTest(void)
{
	uint8 *pDat;
	uint16 *pSign;
	uint8 i,j;
	pDat = &Lcd_Seg.D1;
	
	Lcd_Seg.D1 = 0;
	Lcd_Seg.D2 = 0;
	Lcd_Seg.D3 = 0;
	Lcd_Seg.D4 = 0;
	Lcd_Seg.Sign = 0;
	for(j=0;j<4;j++)					// 前面4个数字
	{
		for(i=0;i<8;i++)
		{
			*pDat |= (1<<i);
			Lcd_Update();
			OSTimeDly(5);
		}
		pDat++;
	}
	
	pSign = (uint16*)&Lcd_Seg.Sign;		// 符号 为uint16
	for(i=0;i<16;i++)					
	{
		*pSign |= (1<<i);
		Lcd_Update();
		OSTimeDly(5);
	}
    while(Key_Read( )==0xff)
		OSTimeDly(5);
}


⌨️ 快捷键说明

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