Пример #1
0
void close_pwm(char channel)
{
	   /*--------------------------------------------------------------------------------------*/
         /* Stop PWM Timer (Recommended procedure method 2)                                      */
         /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer         */          
         /* Set PWM Timer counter as 0 in Call back function                                     */                              
        /*--------------------------------------------------------------------------------------*/                          
	    char pwm_channel;
	    if(channel==1)
		{	
			pwm_channel=PWM_CH1;
			PWMA->PIER &= ~PWM_PIER_PWMIE1_Msk;
		}
		else
		{	
			pwm_channel=PWM_CH0;
			PWMA->PIER &= ~PWM_PIER_PWMIE0_Msk;
		}

        	/* Disable PWMB NVIC */
			
        	//NVIC_DisableIRQ((IRQn_Type)(PWMA_IRQn)); 

       	 /* Wait until PWMB channel 0 Timer Stop */  
		if(channel==1)
		{
			while(PWMA->PDR1!=0);
			linght_gray1=0;
		}
		else	
		{
        		while(PWMA->PDR0!=0);
			linght_gray0=0;
		}                                                                           
        /* Disable the PWM Timer */
        _PWM_DISABLE_TIMER(PWMA,pwm_channel); 
                        
        /* Disable PWM Output pin */
       _PWM_DISABLE_PWM_OUT(PWMA, pwm_channel);                         
   
   
	if(channel==1)
    {	
		SYS_UnlockReg();	
		SYS->P2_MFP |= SYS_MFP_P21_GPIO;
		SYS_LockReg();
		_GPIO_SET_PIN_MODE(P2,1,GPIO_PMD_OUTPUT);
		P21=0;
	}
	else
	{	
		SYS_UnlockReg();	
		SYS->P2_MFP |= SYS_MFP_P20_GPIO;
		SYS_LockReg();
		_GPIO_SET_PIN_MODE(P2,0,GPIO_PMD_OUTPUT);
		P20=0;
	}	
   
}
Пример #2
0
int main()
{
	int i;
	THREAD_ARG_T	arg[THREAD_NUM];
	TT_SEM_T		sem;

	/* The clock frequency of systick may be changed by user's application.
	   Please change the value of SYSTICK_CLOCK according to real situration */
#define TT_SYSTICK_CLOCK		22118400
	/* Initialize TinyThread */
	tt_init(TT_SYSTICK_CLOCK);
	
	/* Open LED GPIO for testing */
	_GPIO_SET_PIN_MODE(LED0_GPIO_GRP, LED0_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED1_GPIO_GRP, LED1_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED2_GPIO_GRP, LED2_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED3_GPIO_GRP, LED3_GPIO_BIT, GPIO_PMD_OUTPUT);

	/* Set semaphore value to 3, so that always 3 threads' LED is blinking */
	tt_sem_init (&sem, 3);

	/* Create and run thread */
	arg[0].gpio_group = LED0_GPIO_GRP;
	arg[1].gpio_group = LED1_GPIO_GRP;
	arg[2].gpio_group = LED2_GPIO_GRP;
	arg[3].gpio_group = LED3_GPIO_GRP;
	arg[0].gpio_bit = LED0_GPIO_BIT;
	arg[1].gpio_bit = LED1_GPIO_BIT;
	arg[2].gpio_bit = LED2_GPIO_BIT;
	arg[3].gpio_bit = LED3_GPIO_BIT;

	for (i = 0; i < THREAD_NUM; ++i)
	{
		arg[i].sem = &sem;
		thread[i] = tt_thread_create("thread",			/* thread Name */
									 0,					/* thread priority */
									 stack[i],			/* stack pointer */
									 sizeof(stack[i]),	/* stack size */
									 thread_entry,		/* thread entry function */
									 (void *)&arg[i]	/* argument for thread entry function */
									 );	
	}
	

	tt_thread_exit ();
	return 0;
}
Пример #3
0
int main()
{
	int i;

	/* The clock frequency of systick may be changed by user's application.
	   Please change the value of SYSTICK_CLOCK according to real situration */
#define TT_SYSTICK_CLOCK		22118400
	/* Initialize TinyThread */
	tt_init(TT_SYSTICK_CLOCK);
	
	/* Open LED GPIO for testing */
	_GPIO_SET_PIN_MODE(LED0_GPIO_GRP, LED0_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED1_GPIO_GRP, LED1_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED2_GPIO_GRP, LED2_GPIO_BIT, GPIO_PMD_OUTPUT);
	_GPIO_SET_PIN_MODE(LED3_GPIO_GRP, LED3_GPIO_BIT, GPIO_PMD_OUTPUT);

	/* Test threads lock by value[]
	   the element in value should be the same,
	   if not, there must be an error */
	tt_rmutex_init(&mutex);
	for(i = 0; i < THREAD_NUM; ++i)
		value[i] = 0;

	arg[0].gpio_group = LED0_GPIO_GRP;
	arg[1].gpio_group = LED1_GPIO_GRP;
	arg[2].gpio_group = LED2_GPIO_GRP;
	arg[0].gpio_bit = LED0_GPIO_BIT;
	arg[1].gpio_bit = LED1_GPIO_BIT;
	arg[2].gpio_bit = LED2_GPIO_BIT;

	for(i = THREAD_NUM; i-- != 0; )
	{
		arg[i].value = value;
		arg[i].mutex = &mutex;
		thread[i] = tt_thread_create("th", 
			0,
			stack[i], sizeof(stack[i]),
			thread_entry, (void *)&arg[i]);
	}
	
	tt_sleep(1024);
	
	while(1);
	return 0;
}
Пример #4
0
void CONTROL_INIT()
{
    _GPIO_SET_PIN_MODE(P0, 4, GPIO_PMD_QUASI);
    _GPIO_SET_PIN_MODE(P0, 5, GPIO_PMD_QUASI);
    _GPIO_SET_PIN_MODE(P0, 6, GPIO_PMD_QUASI);

/*
    _GPIO_SET_PIN_MODE(P0, 4, GPIO_PMD_INPUT);
    _GPIO_SET_PIN_MODE(P0, 5, GPIO_PMD_INPUT);
    _GPIO_SET_PIN_MODE(P0, 6, GPIO_PMD_INPUT);
*/


    _GPIO_ENABLE_DEBOUNCE(P0, 4);
    _GPIO_ENABLE_DEBOUNCE(P0, 5);
    _GPIO_ENABLE_DEBOUNCE(P0, 6);
//    _GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK, GPIO_DBNCECON_DBCLKSEL_32768);

}
Пример #5
0
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    SYS_Init();
    UART0_Init();
    UART1_Init();
    GPIO_Init();
    TMR0_Init();
    TMR1_Init();
    PWMA_Init();
    PWMB_Init();
    CKO_Init();

_GPIO_SET_PIN_MODE(PC, 12, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PE, 0, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PE, 2, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PE, 4, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PA, 10, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PA, 8, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PD, 9, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PD, 11, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PD, 13, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PB, 5, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PB, 7, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PE, 8, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PB, 0, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PB, 2, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PD, 6, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PD, 14, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PC, 5, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PC, 3, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PC, 1, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PE, 6, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PB, 11, GPIO_PMD_OUTPUT);	
/*_GPIO_SET_PIN_MODE(PA, 1, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PA, 2, GPIO_PMD_OUTPUT);	
_GPIO_SET_PIN_MODE(PA, 3, GPIO_PMD_OUTPUT);
_GPIO_SET_PIN_MODE(PA, 4, GPIO_PMD_OUTPUT);*/	



	
while (1)
{


}


    __WFI();

}
Пример #6
0
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{   
	unsigned int i=0;
    SYS_Init();  
	P20=0;P21=0;
	_GPIO_SET_PIN_MODE(P3,6,GPIO_PMD_OUTPUT);
	open_capture(); //open capture funcation
	open_pwm3();
	while(1)
	{
		if(LIGHT_RESISTANCES) //day
		{
			if(light_flag!=DAY) //night turn to day need close LED 
			{
				light_flag=DAY;
				close_pwm(0);
				close_pwm(1);
				clean_capture_flag(); //day close capture
			}
		}
		else  //night
		{
			if(light_flag!=NIGHT) //day turn to night
			{
				light_flag=NIGHT;
				open_pwm(0);  //open led0
				//open_pwm(1);
				if(!capture_start_enable)
					open_capture_flag();
			}
			else
			{
				if(capture_wave_count>=CAPTURE_ALL_TIME) //capture compelet once time
				{
					
					if(calculate_capture_frequency()==0) //have someone pass by, led1 light
					{
						open_pwm(1);
						open_timer0();
						open_capture_flag();
						while(timer0_100ms_count<50) // 3s
						{
							if(capture_wave_count>=CAPTURE_ALL_TIME) 
							{
								if(calculate_capture_frequency()==0) //have peope
								{
									timer0_100ms_count=0;
								}
								open_capture_flag();
							}
							if(LIGHT_RESISTANCES) //day
							{	
								open_capture_flag();
								break;
							}
						}
						close_timer0();
						close_pwm(1);
					}
					else
						open_capture_flag();
				}
			}
			
		}
		//delay some time for check light resistance	
		for(i=0;i<1000;i++);
	}
	//return 0;  
}
Пример #7
0
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock                                                                                       */
/*---------------------------------------------------------------------------------------------------------*/

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable Internal RC clock */
    SYSCLK->PWRCON |= SYSCLK_PWRCON_IRC22M_EN_Msk;

    /* Waiting for IRC22M clock ready */
    SYS_WaitingForClockReady(SYSCLK_CLKSTATUS_IRC22M_STB_Msk);

    /* Switch HCLK clock source to Internal RC */
    SYSCLK->CLKSEL0 = SYSCLK_CLKSEL0_HCLK_IRC22M;

    /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
    SYSCLK->PLLCON |= SYSCLK_PLLCON_PD_Msk;

    /* Enable external 12MHz XTAL, internal 22.1184MHz */
    SYSCLK->PWRCON |= SYSCLK_PWRCON_XTL12M_EN_Msk | SYSCLK_PWRCON_IRC22M_EN_Msk;

    /* Enable PLL and Set PLL frequency */
    SYSCLK->PLLCON = PLLCON_SETTING;

    /* Waiting for clock ready */
    SYS_WaitingForClockReady(SYSCLK_CLKSTATUS_PLL_STB_Msk | SYSCLK_CLKSTATUS_XTL12M_STB_Msk | SYSCLK_CLKSTATUS_IRC22M_STB_Msk);

    /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
    SYSCLK->CLKSEL0 = SYSCLK_CLKSEL0_STCLK_HCLK_DIV2 | SYSCLK_CLKSEL0_HCLK_PLL;

    /* Enable IP clock */
//    SYSCLK->APBCLK = SYSCLK_APBCLK_PWM01_EN_Msk | SYSCLK_APBCLK_PWM23_EN_Msk | SYSCLK_APBCLK_TMR2_EN_Msk;
    /* IP clock source */
//    SYSCLK->CLKSEL1 = SYSCLK_CLKSEL1_PWM01_HCLK | SYSCLK_CLKSEL1_PWM23_HCLK | SYSCLK_CLKSEL1_TMR2_HCLK;
    /* IP clock source */
    // SYSCLK->CLKSEL2 = SYSCLK_CLKSEL2_PWM01_XTAL|SYSCLK_CLKSEL2_PWM23_XTAL;

    /* Reset PWMA channel0~channel3 */
//    SYS->IPRSTC2 = SYS_IPRSTC2_PWM03_RST_Msk;
//    SYS->IPRSTC2 = 0;

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
    //SystemCoreClockUpdate();
    PllClock        = PLL_CLOCK;            // PLL
    SystemCoreClock = PLL_CLOCK / 1;        // HCLK
    CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

    // Init LCD GPIO
    _GPIO_SET_PIN_MODE(P3, 0, GPIO_PMD_OUTPUT);
    _GPIO_SET_PIN_MODE(P3, 1, GPIO_PMD_OUTPUT);
    _GPIO_SET_PIN_MODE(P3, 2, GPIO_PMD_OUTPUT);
    _GPIO_SET_PIN_MODE(P3, 3, GPIO_PMD_OUTPUT);
    _GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_OUTPUT);
    _GPIO_SET_PIN_MODE(P3, 5, GPIO_PMD_OUTPUT);

    // Init Encoder GPIO
//    _GPIO_SET_PIN_MODE(P0, 2, GPIO_PMD_INPUT);
//    _GPIO_SET_PIN_MODE(P0, 3, GPIO_PMD_INPUT);
    /*
    _GPIO_ENABLE_DEBOUNCE(P0, 2);
    _GPIO_ENABLE_DEBOUNCE(P0, 3);
    _GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK, GPIO_DBNCECON_DBCLKSEL_16);
    */
//    GPIO_EnableInt(P0, 2, GPIO_INT_RISING);
//    GPIO_EnableInt(P0, 3, GPIO_INT_RISING);

//    NVIC_EnableIRQ(GPIO_P0P1_IRQn);
//    NVIC_EnableIRQ(TMR2_IRQn);
//    NVIC_EnableIRQ(PWMA_IRQn);

    // Test Output
//    _GPIO_SET_PIN_MODE(P0, 4, GPIO_PMD_OUTPUT);


/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function                                                                                 */
/*---------------------------------------------------------------------------------------------------------*/
    /* Set P3 multi-function pins for UART0 RXD and TXD  */
    // SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
    /* Set P2 multi-function pins for PWMB Channel0~3  */
//    SYS->P2_MFP = SYS_MFP_P20_PWM0;
//    SYS->P4_MFP = SYS_MFP_P40_T2EX;
    /* Lock protected registers */
    SYS_LockReg();
}
Пример #8
0
int32_t main()
{

/***************************************************************************
*       ClkInit()
****************************************************************************/
	ClkInit();

/***************************************************************************
*       GPIOInit();
****************************************************************************/
//	DrvGPIO_InitFunction(E_FUNC_GPIO);
	outpw(&SYS->P0_MFP, 0);
	outpw(&SYS->P1_MFP, 0);
	outpw(&SYS->P2_MFP, 0);
	outpw(&SYS->P3_MFP, 0);
	outpw(&SYS->P4_MFP, 0);

	_GPIO_SET_PIN_MODE(MODEM_ON_PORT, MODEM_ON_PIN, GPIO_PMD_OUTPUT);
	ModuleOn(TRUE);

	_GPIO_SET_PIN_MODE(MODEM_POWER_PORT, MODEM_POWER_PIN, GPIO_PMD_OUTPUT);
	ModulePowerOn(FALSE);

	_GPIO_SET_PIN_MODE(LOCK_POWER_PORT, LOCK_POWER_PIN, GPIO_PMD_OUTPUT);
	LockPower(FALSE);

	_GPIO_SET_PIN_MODE(KEY_HELP_PORT, KEY_HELP_PIN, GPIO_PMD_QUASI);
	_GPIO_SET_PIN_MODE(KEY_BAT_PORT, KEY_BAT_PIN, GPIO_PMD_QUASI);

	_GPIO_SET_PIN_MODE(SLEEP_PORT, SLEEP_PIN, GPIO_PMD_OUTPUT);
	ModemSleep(FALSE);

	_GPIO_SET_PIN_MODE(LED_PORT, LED_PIN, GPIO_PMD_OUTPUT);
	
	LedDark();
	
	_GPIO_SET_PIN_MODE(INT_PORT, INT_PIN, GPIO_PMD_INPUT);

/***************************************************************************
*       TimerInit();
****************************************************************************/
	SysTick_Config(SYS_TICK);
//	SYS_LockReg();

/***************************************************************************
*       UartInit();
****************************************************************************/
	UartInit();
//	debug(VERSION);
//	DrvSYS_GetPLLClockFreq();

/***************************************************************************
*       WatchdogInit();
****************************************************************************/	
	WatchdogInit();

//	udpTest();
	NvInit();



 	WhatToDo();
	AdcInit();
	
	
	
	WaitLockPower();

  LockPower(!custermParam.param.lockState);


	InitMsgDebug();	
	
	if( Communication(10) == FALSE )
		SoftReset();

	if( isCheckingBattery )
	{
		delay_50ms(40);
		MeasurePower(4);
	}
	else
	{
		MeasurePower(2);
	}
					
	Flag_ModuleOn = TRUE;
	//Flag_ModuleOn = TRUE;
	ModemVolumeInit();

	///--------------------24.11.2015----------------------------------------------
	////////////////////////////////////////////////////////////////////////////////////////
while(PressHelp()&&(tmp_my<200)&&(PressBatter())){
	tmp_my++;
		
	delay_50ms(1);
if (tmp_my>=150) {
	  LedLight(144);
	    if (custermParam.param.lockState) {
														LockPower(TRUE);
														custermParam.param.lockState = FALSE;
												    WriteToNv( &custermParam ); //- save to eeprom
  													PlayVoice("\"unitUnlock.wav\",2\r");
														delay_ms(2000);
			                      LockPower(FALSE);
			
			} else {
	                          //  debug("ins");
															while(PressBatter()){};
														 tmp_my=0;
												while (tmp_my<25){               
																											tmp_my++;
																											delay_ms(150);
																											if (PressBatter()){counterPress++;
																																				while(!PressBatter()){};
																																					}
																										  if (counterPress>2){ 
																																					PlayVoice("\"unitLock.wav\",2\r");
																																					delay_ms(2000);
																																					custermParam.param.lockState = TRUE;
																																					WriteToNv( &custermParam ); //- save to eeprom
																																		      tmp_my=0;
																																					delay_ms(2000);
																																					LockPower(DISABLE);

																																				}
																											}
													LedBlink();
										} 
					///////				
			tmp_my=0;
			}

}

while(custermParam.param.lockState){}
 
///////////////////////////////////////////////////////////////////////////////////////////////													
#ifdef DELAY_TEST
	DelayTest();
#endif
	PowerOn();
	
	

	
	if(state==STATE_POWERING_DOWN || Flag_Power_Down)
	{
		Flag_Power_Down = FALSE;
		EnterPowerDown();
		while( TimerWait )
			FeedWatchdog();
		PowerDown();
		//不会返回
	}
	
	InitVariables();
	  
	if(state == STATE_NULL)
	{
		state = STATE_1ST_CALL;
		TimerWait = 100;	// 5 s
	}
	
	TimerTestMode = 6000;
	//debug("ent main loop");
	while(1)
	{  
			
		///--------------------
				if (!TimerTestMode && state==STATE_TEST)
										{ 
										PlayMusic(MUSIC_PD);
										EnterPowerDown();
										}
		
		///--------------------
		
		if( Flag_Incomming )
		{
	#if 1
			TimerWait = 200;
			Flag_Incomming = FALSE;
			TimerStandby = custermParam.param.delay;
			//if(state != STATE_ACTIVE)
			{
				ModemSleep(FALSE);
				state = STATE_INCOMMING;
			}
	#else
			Flag_Incomming = FALSE;
			if(state != STATE_INCOMMING)
			{
				state = STATE_INCOMMING;
				ModemSleep(FALSE);
				EnterAnswer();
			}
	#endif
		}
		
		if(TimerWait == 0)
		{
			switch(state)
			{
									
			case STATE_POWERING_DOWN:
				PowerDown();
				break;

			case STATE_1ST_CALL:
				RegisterWait();
				break;

			case STATE_REGISTER_OK:	
				GetCpsiInfo();
				if( Flag_ObtainCpsi==TRUE)
				{
					state = STATE_GSM_STEADY;
					if( Flag_SimExist )
					{
						//delay_50ms(200);
						SockOpen();
						TimerWait = 400;
					}
					else
					{
						TimerWait = 60;
					}
				}
				else
				{
					TimerWait = 70;
				}
				break;

			case STATE_GSM_STEADY:	
				if( gPhoneNumOk==PHONE_NUM_READY || Flag_SimExist==FALSE)
					EnterCall();
				else
					TimerWait = 40;
				break;
				
			case STATE_CALL_PROMPT:
				Call();
				break;

			case STATE_ALERT:
				EnterCallFail();
				break;
			
			case STATE_NEXT_CALL:
				Redial();
				break;				

			case STATE_BATT_DISPLAY:
				EnterPowerDown();
				break;

			case STATE_INCOMMING:
				TimerStandby = custermParam.param.delay;
				EnterStandby();
				break;
			}
		}

		if( TimerSock==0 && Flag_SimExist
			&& ( (state>=STATE_GSM_STEADY && state<STATE_STANDBY) || progress>=TEST_GPS ) )
		{
			switch(sockState)
			{
			//case SOCKSTATE_NULL:
			case SOCKSTATE_CONFIG:				
			case SOCKSTATE_OPENNING:
				SockOpen();	// 打开失败,从新打开
				break;

			case SOCKSTATE_OPENED:
				SendAtWaitRsq(50, 1, FALSE, "AT+CIPOPEN=0,\"UDP\",,,%s\r\n", custermParam.param.local_port);
				TimerSock = 80;
				break;
	#if 0
			case SOCKSTATE_CONNECTING:
				SockConnect();	// 连接失败,从新连接
				break;
	#endif
			case SOCKSTATE_CONNECT_OK:		
				if( witchApn == 1)
					ClientInit();
				break;

			default:
				break;
			}
		}

		KeyHandle();

		SecondHandle();
		//SignalCheck();
		BatterCheck();
		PowerDownHandle();
		MsgHandle();
											
		if( Flag_SimExist )
		{
			UdpHandle();
			SmsHandle();
			PowerDownHandle();
		}
		
		FeedWatchdog();
	}
}