#include "includes.h"
#include "sys.h"
#include "usart.h"		
#include "delay.h"	
#include "led.h" 
#include "key.h"
#include "lcd.h"
#include "ds18b20.h"		 
//ucos 实验

//设置任务堆栈大小
#define LED_STK_SIZE     64
#define LED1_STK_SIZE    64
#define DS18B20_STK_SIZE 256
#define START_STK_SIZE   128

//设置任务优先级
#define LED_TASK_Prio       7
#define LED1_TASK_Prio      6
#define DS18B20_TASK_Prio   8
#define START_TASK_Prio     4

//任务堆栈
OS_STK  TASK_LED1_STK[LED_STK_SIZE];
OS_STK  TASK_LED_STK[LED_STK_SIZE];
OS_STK  TASK_DS18B20_STK[DS18B20_STK_SIZE];
OS_STK  TASK_START_STK[START_STK_SIZE];
//任务申明
void TaskStart(void *pdata);
void TaskLed(void *pdata);
void TaskLed1(void *pdata);
void TaskDS18B20(void *pdata);

//系统时钟配置函数
void SysTick_Configuration(void);

			 
int main(void)
{			 	   
	Stm32_Clock_Init(9); //系统时钟设置
	delay_init(72);	     //延时初始化 
	uart_init(72,9600);	 //串口初始化为9600
	LED_Init();		  	 //初始化与LED连接的硬件接口  
	LCD_Init();
	DS18B20_Init();
	SysTick_Configuration();
	OSInit();
	OSTaskCreate( TaskStart,	//task pointer
					(void *)0,	//parameter
					(OS_STK *) & TASK_START_STK[START_STK_SIZE-1],	//task stack top pointer
					START_TASK_Prio 
				);	//task priority
	
	OSStart();
	return 0;	   
}

//开始任务
void TaskStart(void * pdata)
{
	pdata = pdata; 
	OS_ENTER_CRITICAL();   
	OSTaskCreate(   TaskLed,
	                (void * )0,
	                (OS_STK *)&TASK_LED_STK[LED_STK_SIZE-1],
				    LED_TASK_Prio
				);

	OSTaskCreate(   TaskLed1, 
	                (void * )0,
				    (OS_STK *)&TASK_LED1_STK[LED1_STK_SIZE-1],
					 LED1_TASK_Prio
			    );

	OSTaskCreate(	TaskDS18B20,
	                (void * )0,
					(OS_STK *)&TASK_DS18B20_STK[DS18B20_STK_SIZE-1],
					DS18B20_TASK_Prio
	            );


	OSTaskSuspend(START_TASK_Prio);	//suspend but not delete
	OS_EXIT_CRITICAL();
}
//任务1_控制DS0的亮灭.
void TaskLed(void *pdata)
{
	while(1)
	{
		LED0=!LED0;
	//	OSTimeDlyHMSM(0,0,0,500);
	   	OSTimeDly(100);
	}
}
//任务2_控制DS1的亮灭.
void TaskLed1(void *pdata)
{
	while(1)
	{
		LED1=!LED1;
	//	OSTimeDlyHMSM(0,0,0,1000);
		OSTimeDly(500);	
	}
}	  
 
//任务4_DS18B20
void TaskDS18B20(void *pdata)
	{ 
	   	short temp;
		POINT_COLOR=RED;//设置字体为红色	   
		LCD_ShowString(60,70,"DS18B20 TEST");	
		LCD_ShowString(60,90,"TEMPTURE");
		LCD_ShowString(60,110,"2012/10/14");
		while(DS18B20_Init())//初始化DS18B20,兼检测18B20
			{
				LCD_ShowString(60,130,"DS18B20 Check Failed!");
				OSTimeDly(500);		  
				LCD_ShowString(60,130,"Please Check!        ");
		    	OSTimeDly(500);
			}
   		LCD_ShowString(60,130,"DS18B20 Ready!     ");	 
 		POINT_COLOR=BLUE;//设置字体为蓝色	   
		LCD_ShowString(60,150, "Temperate:   . C");	 
		while(1)
			{	  
				temp=DS18B20_Get_Temp();
				if(temp<0)
					{
						temp=-temp;
						LCD_ShowChar(140,150,'-',16,0);//显示负号	
					}						 
				LCD_ShowNum(148,150,temp/10,2,16);//显示温度值	  
				LCD_ShowNum(172,150,temp%10,1,16);//显示温度值
				//printf("t1:%d\n",temp);
				OSTimeDly(500);
			}			   
	}




//系统时钟中断服务函数
void SysTick_Handler(void)
{
	OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR  */
    OSIntNesting++;
    OS_EXIT_CRITICAL();

    OSTimeTick();        /* Call uC/OS-II's OSTimeTick()               */

    OSIntExit();         /* Tell uC/OS-II that we are leaving the ISR  */
}
//系统时钟配置，设计1ms产生一次中断
void SysTick_Configuration(void)
{
 	SysTick->CTRL&=~(1<<2);//SYSTICK使用外部时钟源
	SysTick->CTRL|=1<<1;   //开启SYSTICK中断
	SysTick->LOAD=9000;    //产生1ms中断
	//bit2清空,选择外部时钟  HCLK/8
	MY_NVIC_Init(2,2,SystemHandler_SysTick,2);//组2，最低优先级 
	SysTick->CTRL|=1<<0;   //开启SYSTICK
}




















