Exemple #1
0
//=========================================================================
//----- (00007CD4) --------------------------------------------------------
__myevic__ void InitTimers()
{
    TIMER_Open( TIMER0, TIMER_PERIODIC_MODE, 100000 );
    TIMER_EnableInt( TIMER0 );
    TIMER_Open( TIMER1, TIMER_PERIODIC_MODE, 5000 );
    TIMER_EnableInt( TIMER1 );
    TIMER_Open( TIMER2, TIMER_PERIODIC_MODE, 1000 );
    TIMER_EnableInt( TIMER2 );
    TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 );
    TIMER_EnableInt( TIMER3 );

    NVIC_EnableIRQ( TMR0_IRQn );
    NVIC_EnableIRQ( TMR1_IRQn );
    NVIC_EnableIRQ( TMR2_IRQn );
    NVIC_EnableIRQ( TMR3_IRQn );

    TMR3Counter = 0;
    TMR2Counter = 0;
    TMR1Counter = 0;
    TMR0Counter = 0;

    TIMER_Start( TIMER0 );
    TIMER_Start( TIMER1 );
    TIMER_Start( TIMER2 );
    TIMER_Start( TIMER3 );
}
Exemple #2
0
/*---------------------------------------------------------------------------------------------------------*/
void TMR3_IRQHandler(void)
{
	// clear Timer0 interrupt flag
	TIMER_ClearIntFlag(TIMER3);
	
	if(IR_Tx_stage==TRANSFER_START)
	{
		if(IR_bit_state==1)
		{
			// Enable PWM0 CH2 output
			PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
			//Initial Timer0 to periodic mode with 222Hz(4.5ms)
			TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, START_TRAN_SECOND);
			TIMER_EnableInt(TIMER3);
			TIMER_Start(TIMER3);
			IR_bit_state=0;
			IR_Tx_stage=TRANSFER_CONT;
		}
	}
	else if(IR_Tx_stage==TRANSFER_CONT)
	{
		if(IR_TX_Byten<4)
		{
			IR_TranDATA();
		}
		else
		{
			if(IRTx_StopFlag==0)
			{
				PWM_EnableOutput(PWM0, PWM_CH_3_MASK);
				//Initial Timer0 to periodic mode with 1786Hz(560us)
				TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO);
				TIMER_EnableInt(TIMER3);
				TIMER_Start(TIMER3);
				IRTx_StopFlag=1;
			}
			else if(IRTx_StopFlag==1)
			{
				PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
				IR_TX_Bitn=0;
				IR_TX_Byten=0;
				IR_Tx_stage=TRANSFER_START;
				IRTx_StopFlag	=	0;
				IR_TxExecuteFLAG=0;
			}
		}
	}
}
Exemple #3
0
int main(void)
{
    int i = 0;
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SYS_Init();
    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    /*Initial Timer0 to periodic mode with 1Hz */
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);

    /* Enable timer wake up system */
    TIMER_EnableWakeup(TIMER0);
    /* Enable Timer0 interrupt */
    TIMER_EnableInt(TIMER0);
    NVIC_EnableIRQ(TMR0_IRQn);
    /* Start Timer0 counting */
    TIMER_Start(TIMER0);

    /* Unlock protected registers */
    SYS_UnlockReg();

    while(1)
    {
        CLK_PowerDown();
        printf("Wake %d\n", i++);

    }

}
Exemple #4
0
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    //Initial System
    SYS_Init();

    //Enable Timer0 clock and select Timer0 clock source
    CLK_EnableModuleClock(TMR0_MODULE);
    CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

    //Initial Timer0 to periodic mode with 2Hz
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 2);
    //Enable Timer0 interrupt
    TIMER_EnableInt(TIMER0);
    NVIC_EnableIRQ(TMR0_IRQn);

    //Initial 7-Segment
    Open_Seven_Segment();

    //Start Timer0
    TIMER_Start(TIMER0);

    while(1)
    {
        Show_Seven_Segment(TimerCounter / 10, 1);
        CLK_SysTickDelay(200);
        Show_Seven_Segment(TimerCounter % 10, 2);
        CLK_SysTickDelay(200);
    }
}
Exemple #5
0
/*******************************************************************************
* Description : PLC  configuration
* Syntax      : 
* Parameters I: 
* Parameters O: 
* return      : 
*******************************************************************************/
void PLC_setup(void)
{
    /* PLC Tx Pin set */
    
//    pinMode_ALL(P0, 0, OUTPUT);     //SCC
//    pinMode_ALL(P0, 1, OUTPUT);     //SCCOUT
    pinMode_ALL(P3, 6, OUTPUT);     //SCCOUT
    
    /* PLC Tx Timer default setting */
    TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 1);  //中心频率 270KHz
//    TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
//    TIMER_SET_CMP_VALUE(TIMER1, 88);
    
    /* PLC Tx Timer IE enable */
    TIMER_EnableInt(TIMER1);           

    /* Enable Timer1 NVIC */
    NVIC_EnableIRQ(TMR1_IRQn);                     //中断优先级缺

    /* Clear Timer1 interrupt counts to 0 */
    TimerIntCnt[1] = 0u;

    /* Start Timer1 counting */
//    TIMER_Start(TIMER1);
}
Exemple #6
0
/*---------------------------------------------------------------------------------------------------------*/
void IR_TranDATA(void)
{
    if(IR_bit_state==0)
    {
        // Stop PWM0 CH3 output
        PWM_EnableOutput(PWM0, PWM_CH_3_MASK);
        //Initial Timer0 to periodic mode with 1786Hz(560us)
        TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_FIRST);
        TIMER_EnableInt(TIMER3);
        TIMER_Start(TIMER3);
        IR_bit_state=1;
    }
    else if (IR_bit_state==1)
    {
        if(TxIR_CODE[IR_TX_Byten] & (1 << (IR_DATA_BIT_NUMBER-IR_TX_Bitn-1)))
        {
            // Stop PWM0 CH3 output
            PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
            //Initial Timer0 to periodic mode with 595Hz(1680us)
            TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ONE);
            TIMER_EnableInt(TIMER3);
            TIMER_Start(TIMER3);
        }
        else
        {
            // Enable PWM0 CH3 output
            PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
            //Initial Timer0 to periodic mode with 1786Hz(560us)
            TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO);
            TIMER_EnableInt(TIMER3);
            TIMER_Start(TIMER3);
        }
        if(IR_TX_Bitn<7)
        {
            IR_TX_Bitn++;
        }
        else
        {
            IR_TX_Bitn=0;
            IR_TX_Byten++;
        }
        IR_bit_state=0;
    }
}
void Time::initialize()
{
	CLK_EnableModuleClock(TMR0_MODULE);
	CLK_SetModuleClock(TMR0_MODULE,CLK_CLKSEL1_TMR0_S_HCLK,CLK_CLKDIV_UART(1));
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000000/TIMER_INTERRUPT_PERIOD_MICROSECONDS);
    TIMER_EnableInt(TIMER0);
    NVIC_EnableIRQ(TMR0_IRQn);
    NVIC_SetPriority(TMR0_IRQn, TIMER_IRQ_PRIORITY);
    TIMER_Start(TIMER0);             /* Start counting */

}
Exemple #8
0
void Tim_Config(void)	//定时器配置  用于测量红外高电平的时间
{
	/* Open Timer0 frequency to 50k Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 50000);
    TIMER_EnableInt(TIMER0);
	/* Open Timer1 frequency to 10k Hz in periodic mode, and enable interrupt */
	TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 10000);
    TIMER_EnableInt(TIMER1);
	/* Open Timer2 frequency to 10 Hz in periodic mode, and enable interrupt */
	//用于按键长按休眠
	TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 10);
    TIMER_EnableInt(TIMER2);
	/* Enable Timer0、1 NVIC */
    NVIC_EnableIRQ(TMR0_IRQn);
	NVIC_EnableIRQ(TMR1_IRQn);
	NVIC_EnableIRQ(TMR2_IRQn);
// 	/* Start Timer0、1counting */
//      TIMER_Start(TIMER0);	
//       TIMER_Start(TIMER1);
	TIMER_Start(TIMER2);		
}
void initialize()
{
    IO::pinMode(MUX_ADR0_PIN, OUTPUT);
    IO::pinMode(MUX_ADR1_PIN, OUTPUT);
    IO::pinMode(MUX_ADR2_PIN, OUTPUT);

    IO::pinMode(V_IN_PIN, ANALOG_INPUT);
    IO::pinMode(OUTPUT_VOLTAGE_MINUS_PIN, ANALOG_INPUT);
    IO::pinMode(SMPS_CURRENT_PIN, ANALOG_INPUT);
    IO::pinMode(OUTPUT_VOLTAGE_PLUS_PIN, ANALOG_INPUT);
    IO::pinMode(DISCHARGE_CURRENT_PIN, ANALOG_INPUT);

    IO::pinMode(MUX0_Z_D_PIN, ANALOG_INPUT_DISCHARGE);
    IO::digitalWrite(MUX0_Z_D_PIN, 0);

    //initialize internal temperature sensor
    SYS->TEMPCR |= 1;

    //initialize TIMER 1 (mux ADC capacitor discharge)
    CLK_EnableModuleClock(TMR1_MODULE);
    CLK_SetModuleClock(TMR1_MODULE,CLK_CLKSEL1_TMR1_S_HCLK,CLK_CLKDIV_UART(1));
    //TODO: 50kHz ??
    TIMER_Open(TIMER1, TIMER_ONESHOT_MODE, 1000000 / ADC_CAPACITOR_DISCHARGE_DELAY_US);
    TIMER_EnableInt(TIMER1);
    NVIC_EnableIRQ(TMR1_IRQn);
    NVIC_SetPriority(TMR1_IRQn, ADC_C_DISCHARGE_IRQ_PRIORITY);

    //initialize ADC
    //init clock
    CLK_EnableModuleClock(ADC_MODULE);
    CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HCLK, CLK_CLKDIV_ADC(CLK_GetHCLKFreq()/ADC_CLOCK_FREQUENCY));
            //__HXT/ADC_CLOCK_FREQUENCY));

    /* Set the ADC operation mode as burst, input mode as single-end and enable the analog input channel 2 */
    ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_BURST, 0x1 << 2);
    ADC_SET_DMOF(ADC, ADC_ADCR_DMOF_UNSIGNED_OUTPUT);

    /* Power on ADC module */
    ADC_POWER_ON(ADC);

    /* clear the A/D interrupt flag for safe */
    ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

    /* Enable the ADC interrupt */
    ADC_EnableInt(ADC, ADC_ADF_INT);
    NVIC_EnableIRQ(ADC_IRQn);
    NVIC_SetPriority(ADC_IRQn, ADC_IRQ_PRIORITY);

    current_input_ = 0;
    startConversion();
}
Exemple #10
0
void TIMER_Init()
{
	/* Enable peripheral clock */
	CLK_EnableModuleClock(TMR2_MODULE);
	CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HIRC, 0);

	TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 100000);
	TIMER_EnableInt(TIMER2);
	NVIC_SetPriority (TMR2_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
	/* Enable Timer2 NVIC */
	NVIC_EnableIRQ(TMR2_IRQn);
	/* Start Time2 counting */
	TIMER_Start(TIMER2);
}
Exemple #11
0
void TMR0_Init(uint32_t u32Freq)
{
    /* Configure timer to operate in periodic mode and frequency = 1000Hz*/
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, u32Freq);

    /* Enable the Timer time-out interrupt */
    TIMER_EnableInt(TIMER0);

    /* Start Timer counting */
    TIMER_Start(TIMER0);

    /* Enable TMR0 Interrupt */
    NVIC_EnableIRQ(TMR0_IRQn);
}
Exemple #12
0
/**
  * @brief  Main routine. 
  * @param  None.
  * @return None.
  */
int32_t main(void)                 
{
    SYS_UnlockReg();
    SYS->P5_MFP = (SYS->P5_MFP & 0x00FFFCFC) | 0x03;  /* P5.1 -> XTAL2, P5.0 -> XTAL1 */
    CLK->PWRCON = CLK_PWRCON_XTL12M | 4 | 8 ;

    SYS_Init();
  
    /* SPI test */   
    LCD_Init();   
    LCD_EnableBackLight();
    LCD_ClearScreen();
    LCD_Print(0, "Welcome! Nuvoton");
    LCD_Print(1, "This is LB board");
    LCD_Print(2, "Mini51");
    LCD_Print(3, "TEST"); 
  
    // backlight control pin P5.4
    GPIO_SetMode(P5,1<<4,GPIO_PMD_OUTPUT);
  
    /* INT button triggers P3.2 */
    GPIO_SetMode(P3,(1<<2),GPIO_PMD_OPEN_DRAIN);  
    GPIO_EnableInt(P3, 2, GPIO_INT_FALLING);
    NVIC_EnableIRQ(EINT0_IRQn);
  
    /* Enable interrupt de-bounce function and select de-bounce sampling cycle time */
    GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK,GPIO_DBNCECON_DBCLKSEL_16);  
    GPIO_ENABLE_DEBOUNCE(P3,1<<2);  

    /* Reset and stop TIMER0, TIMER1 counting first */
    TIMER1->TCSR = TIMER_TCSR_CRST_Msk;
  
    /* Enable TIMER0, TIMER1, NVIC */
    NVIC_EnableIRQ(TMR1_IRQn);
    
    /* To Configure TCMPR values based on Timer clock source and pre-scale value */
    TIMER_SET_PRESCALE_VALUE(TIMER1,0);
  
    
    /* Start TIMER1 counting and setting*/
    TIMER_Open(TIMER1,TIMER_PERIODIC_MODE,SystemCoreClock/1000); 
    
    TIMER_EnableInt(TIMER1);

    while(1) ;    // loop forever
    
}
int Timer_InterTimerTriggerMode(void)
{
    int volatile i;

    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SysInit();

    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    /* This sample code demonstrate inter timer trigger mode using Timer0 and Timer1
     * In this mode, Timer0 is working as counter, and triggers Timer1. Using Timer1
     * to calculate the amount of time used by Timer0 to count specified amount of events.
     * By dividing the time period recorded in Timer1 by the event counts, we get
     * the event frequency.
     */
    printf("Inter timer trigger mode demo code\n");
    printf("Please connect input source with Timer 0 counter pin PB.8, press any key to continue\n");
    UART_GetChar();

    // Give a dummy target frequency here. Will over write prescale and compare value with macro
    TIMER_Open(TIMER0, TIMER_ONESHOT_MODE, 100);

    // Update prescale and compare value. Calculate average frequency every 1000 events
    TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
    TIMER_SET_CMP_VALUE(TIMER0, 1000);

    // Update Timer 1 prescale value. So Timer 1 clock is 1MHz
    TIMER_SET_PRESCALE_VALUE(TIMER1, 11);

    // We need capture interrupt
    NVIC_EnableIRQ(TMR1_IRQn);

    while(1) {
        complete = 0;
        // Count event by timer 0, disable drop count (set to 0), disable timeout (set to 0). Enable interrupt after complete
        TIMER_EnableFreqCounter(TIMER0, 0, 0, TRUE);
        while(complete == 0);
    }

}
int Timer_FreeCountingMode(void)
{
    int volatile i;

    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SysInit();

    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    printf("\nThis sample code demonstrate timer free counting mode.\n");
    printf("Please connect input source with Timer 0 capture pin PD.11, press any key to continue\n");
    UART_GeyChar();

    // Give a dummy target frequency here. Will over write capture resolution with macro
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000000);

    // Update prescale to set proper resolution.
    // Timer 0 clock source is 12MHz, to set resolution to 1us, we need to
    // set clock divider to 12. e.g. set prescale to 12 - 1 = 11
    TIMER_SET_PRESCALE_VALUE(TIMER0, 11);

    // Set compare value as large as possible, so don't need to worry about counter overrun too frequently.
    TIMER_SET_CMP_VALUE(TIMER0, 0xFFFFFF);

    // Configure Timer 0 free counting mode, capture TDR value on rising edge
    TIMER_EnableCapture(TIMER0, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_RISING_EDGE);

    // Start Timer 0
    TIMER_Start(TIMER0);

    // Enable timer interrupt
    TIMER_EnableCaptureInt(TIMER0);
    NVIC_EnableIRQ(TMR0_IRQn);

    while(1);

}
Exemple #15
0
int32_t main (void)
{

    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SYS_Init();

    /* Init UART0 for printf */
    UART_Open(UART0, 115200);

    printf("\nThis sample code demonstrate Timer 0 trigger DAC channel 0 function.\n");

    // Enable DAC channel 0, trigger by Timer 0.
    DAC_Open(DAC, 0, DAC_TIMER0_TRIGGER);


    // Enable DAC0 interrupt. Enable interrupt for one channel is sufficient in group mode.
    DAC_ENABLE_INT(DAC, 0);
    NVIC_EnableIRQ(DAC_IRQn);

    // Wait 'til both channels are ready
    while(DAC_IS_BUSY(DAC, 0) == 1);

    // Set timer frequency
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 100);

    // Enable Timer timeout event trigger DAC
    TIMER_SetTriggerTarget(TIMER0, TIMER_CTL_DAC_TEEN_Msk);

    // Write first data out. timer will 0 will trigger DAC update
    DAC_WRITE_DATA(DAC, 0, a16Sine[index0]);
    index0 = (index0 + 1) % SINE_ARRAY_SIZE;

    // Start Timer 0
    TIMER_Start(TIMER0);

    while(1);

}
Exemple #16
0
void Timer1Init(uint16_t freq)
{
	/* Enable peripheral clock */
	CLK_EnableModuleClock(TMR1_MODULE);
	
	/* Peripheral clock source */
	CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_LIRC, 0);
	
  /* Enable Timer0 NVIC */
  NVIC_EnableIRQ(TMR1_IRQn);	
	
	/* Open Timer0 time-out frequency to (freq) Hz in periodic mode */
  TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, freq);
	
	/* Enable Timer0 time-out interrupt and wake-up function */
  TIMER_EnableInt(TIMER1);
  TIMER_EnableWakeup(TIMER1);
	
	/* Start Timer0 counting */
  TIMER_Start(TIMER1);
}
/* ----------------------- Start implementation -----------------------------*/
BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
{

	uint16_t PrescalerValue = 0;

   CLK_EnableModuleClock(TMR0_MODULE);
   CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK, 0);
    // Set timer frequency to 20KHZ	 (50us)
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, usTim1Timerout50us);

    // Enable timer interrupt
    TIMER_EnableInt(TIMER0);
	NVIC_SetPriority(TMR0_IRQn, 10 ); //定时器的中断优先级应该比串口的低
    NVIC_EnableIRQ(TMR0_IRQn);

	TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
    // Stop Timer 0
    TIMER_Stop(TIMER0);
	
	return TRUE;
}
Exemple #18
0
int main(void)
{
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SYS_Init();

    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    printf("\nThis sample code use timer to wake up system every 1 second \n");

    /*Initial Timer0 to periodic mode with 1Hz */
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);

    /* Enable timer wake up system */
    TIMER_EnableWakeup(TIMER0);
    /* Enable Timer0 interrupt */
    TIMER_EnableInt(TIMER0);
    NVIC_EnableIRQ(TMR0_IRQn);
    /* Start Timer0 counting */
    TIMER_Start(TIMER0);

    /* Unlock protected registers to let CPU enter power down mode */
    SYS_UnlockReg();

    while(1)
    {
        printf("Sleep 1 second\n");
        // Wait 'til UART FIFO empty to get a cleaner console out
        while(!UART_IS_TX_EMPTY(UART0));
        CLK_PowerDown();
    }

}
Exemple #19
0
/*---------------------------------------------------------------------------------------------------------*/
void TMR3_IRQHandler(void)
{
    uint8_t LearnedDataByten;							//LearnedDataByten transfered from IR_Tx_LearnedDataByten
    // clear Timer0 interrupt flag
    TIMER_ClearIntFlag(TIMER3);

    if(IR_Tx_stage==TRANSFER_START && IR_bit_state==1)
    {
        // Enable PWM0 CH2 output
        PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
        //Initial Timer0 to periodic mode with 222Hz(4.5ms)
        TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, START_TRAN_SECOND);
        TIMER_EnableInt(TIMER3);
        TIMER_Start(TIMER3);
        IR_bit_state=0;
        IR_Tx_stage=TRANSFER_CONT;
    }
    else if(IR_Tx_stage==TRANSFER_CONT)
    {
        if(IR_LearnedFlag==0)
        {
            if(IR_TX_Byten<4)
            {
                IR_TranDATA();
            }
            else
            {
                if(IRTx_StopFlag==0)
                {
                    PWM_EnableOutput(PWM0, PWM_CH_3_MASK);
                    //Initial Timer0 to periodic mode with 1786Hz(560us)
                    TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO);
                    TIMER_EnableInt(TIMER3);
                    TIMER_Start(TIMER3);
                    IRTx_StopFlag=1;
                }
                else if(IRTx_StopFlag==1)
                {
                    PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
                    IR_TX_Bitn = 0;
                    IR_TX_Byten = 0;
                    IR_Tx_stage=TRANSFER_START;
                    IRTx_StopFlag = 0;
                    IR_TxExecuteFLAG = 0;
                }
            }
        }
        else if(IR_LearnedFlag==1)
        {
            if(IRTx_StopFlag==1)
            {
                PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
                IR_Tx_stage=TRANSFER_START;
                IR_TX_Bitn=0;
                IR_byte_state = 0;
                IRTx_StopFlag	=	0;
                IR_TxExecuteFLAG = 0;
            }
            else if(IR_TX_Bitn%2==0)
            {
                LearnedDataByten = IR_Tx_LearnedDataByten*2+IR_byte_state;
                PWM_EnableOutput(PWM0, PWM_CH_3_MASK);
                if(IR_LearnedData[LearnedDataByten] & (1 << (IR_LearnedDataLeng[IR_Tx_LearnedDataByten]/2 - IR_TX_Bitn - 1)))//IR_TX_Bitn*2
                    //Initial Timer0 to periodic mode with 1786Hz(560us)
                    TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_FallingMaxFreq);
                else
                    TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_FallingMinFreq);
                TIMER_EnableInt(TIMER3);
                TIMER_Start(TIMER3);
                IR_TX_Bitn++;
            }
            else if(IR_TX_Bitn%2==1)
            {
                LearnedDataByten = IR_Tx_LearnedDataByten*2+IR_byte_state;
                PWM_DisableOutput(PWM0, PWM_CH_3_MASK);
                if(IR_LearnedData[LearnedDataByten] & (1 << (IR_LearnedDataLeng[IR_Tx_LearnedDataByten]/2-IR_TX_Bitn-1)))//IR_TX_Bitn*2
                    //Initial Timer0 to periodic mode with 1786Hz(560us)
                    TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_RisingMaxFreq);
                else
                    TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_RisingMinFreq);
                TIMER_EnableInt(TIMER3);
                TIMER_Start(TIMER3);
                IR_TX_Bitn++;
            }
            if(IR_TX_Bitn==32 && IR_byte_state == 0)
            {
                IR_TX_Bitn=0;
                IR_byte_state=1;
            }
            else if((IR_TX_Bitn+31==IR_LearnedDataLeng[IR_Tx_LearnedDataByten]) && (IR_byte_state == 1))
            {
                IRTx_StopFlag = 1;
            }
        }
    }
}
Exemple #20
0
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    volatile uint32_t u32InitCount;

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
    printf("+-------------------------------------------------+\n");
    printf("|    Timer0 Power-down and Wake-up Sample Code    |\n");
    printf("+-------------------------------------------------+\n\n");

    printf("# Timer Settings:\n");
    printf("  Timer0: Clock source is LIRC(10 kHz); Toggle-output mode and frequency is 1 Hz; Enable interrupt and wake-up.\n");
    printf("# System will enter to Power-down mode while Timer0 interrupt count is reaching 3;\n");
    printf("  And be waken-up while Timer0 interrupt count is reaching 4.\n\n");

    /* To program PWRCON register, it needs to disable register protection first. */
    SYS_UnlockReg();

    /* Open Timer0 frequency to 1 Hz in toggle-output mode */
    TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1);

    /* Enable Timer0 interrupt and wake-up function */
    TIMER_EnableInt(TIMER0);
    TIMER_EnableWakeup(TIMER0);

    /* Enable Timer0 NVIC */
    NVIC_EnableIRQ(TMR0_IRQn);

    /* Start Timer0 counting */
    TIMER_Start(TIMER0);

    u32InitCount = g_u8IsTMR0WakeupFlag = g_au32TMRINTCount[0] = 0;
    while(g_au32TMRINTCount[0] < 10) {
        if(g_au32TMRINTCount[0] != u32InitCount) {
            printf("Timer0 interrupt count - %d\n", g_au32TMRINTCount[0]);
            if(g_au32TMRINTCount[0] == 3) {
                PowerDownFunction();

                /* Check if Timer0 time-out interrupt and wake-up flag occurred */
                while(1) {
                    if(((CLK->PWRCON & CLK_PWRCON_PD_WU_STS_Msk) == CLK_PWRCON_PD_WU_STS_Msk) && (g_u8IsTMR0WakeupFlag == 1))
                        break;
                }
                printf("System has been waken-up done. (Timer0 interrupt count is %d)\n\n", g_au32TMRINTCount[0]);
            }
            u32InitCount = g_au32TMRINTCount[0];
        }
    }

    /* Stop Timer0 counting */
    TIMER_Stop(TIMER0);

    printf("*** PASS ***\n");

    while(1);
}
Exemple #21
0
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    volatile uint32_t u32InitCount;
    uint32_t au32CAPValus[10];

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
    printf("+---------------------------------------------------+\n");
    printf("|    Timer External Capture Function Sample Code    |\n");
    printf("+---------------------------------------------------+\n\n");

    printf("# Timer Settings:\n");
    printf("  Timer0: Clock source is 12 MHz; Toggle-output mode and frequency is 500 Hz.\n");
    printf("  Timer3: Clock source is 12 MHz; Toggle-output mode and frequency is 1 Hz.\n");
    printf("  Timer2: Clock source is HCLK(72 MHz); Continuous counting mode; TCMP is 0xFFFFFF;\n");
    printf("          Counter pin enable; Capture pin and capture interrupt enable;\n");
    printf("# Generate 500 Hz frequency from TM0 and connect TM0 pin to Timer2 counter pin.\n");
    printf("# Generate 1 Hz frequency from TM3 and connect TM3 pin to TM2_EXT capture pin.\n");
    printf("# Get 500 event counts from Timer2 counter pin when each TM2_EXT pin interrupt occurred.\n\n");

    /* Initial Timer0 and Timer3 default setting */
    TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
    TIMER_Open(TIMER3, TIMER_TOGGLE_MODE, 2);

    /* Initial Timer2 default setting */
    TIMER_Open(TIMER2, TIMER_CONTINUOUS_MODE, 1);

    /* Configure Timer2 setting for external counter input and capture function */
    TIMER_SET_PRESCALE_VALUE(TIMER2, 0);
    TIMER_SET_CMP_VALUE(TIMER2, 0xFFFFFF);
    TIMER_EnableEventCounter(TIMER2, TIMER_COUNTER_FALLING_EDGE);
    TIMER_EnableCapture(TIMER2, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
    TIMER_EnableCaptureInt(TIMER2);

    /* Enable Timer2 NVIC */
    NVIC_EnableIRQ(TMR2_IRQn);

    /* Clear Timer2 interrupt counts to 0 */
    u32InitCount = g_au32TMRINTCount[2] = 0;

    /* Start Timer0, Timer2 and Timer3 counting */
    TIMER_Start(TIMER0);
    TIMER_Start(TIMER2);
    TIMER_Start(TIMER3);

    /* Check TM2_EXT interrupt counts */
    while(1) {
        if(g_au32TMRINTCount[2] != u32InitCount) {
            au32CAPValus[u32InitCount] = TIMER_GetCaptureData(TIMER2);
            printf("[%2d] - %4d\n", g_au32TMRINTCount[2], au32CAPValus[u32InitCount]);
            if(u32InitCount > 1) {
                if((au32CAPValus[u32InitCount] - au32CAPValus[u32InitCount - 1]) != 500) {
                    printf("*** FAIL ***\n");
                    while(1);
                }
            }
            u32InitCount = g_au32TMRINTCount[2];
        }
        
        if(u32InitCount == 10)
            break;
    }

    /* Stop Timer0, Timer2 and Timer3 counting */
    TIMER_Close(TIMER0);
    TIMER_Close(TIMER2);
    TIMER_Close(TIMER3);

    printf("*** PASS ***\n");

    while(1);
}
Exemple #22
0
int main()
{
	CLEAR_WRITE();
	SET_STOPMOTOR();
	CLEAR_SCANMEDIA();
	CLEAR_MEDIASET();
	CLEAR_READY();
	
	//setup led and button gpio
    GPIO_SetMode(LED_G_PORT, LED_G_PIN, GPIO_PMD_OUTPUT);
    GPIO_SetMode(LED_R_PORT, LED_R_PIN, GPIO_PMD_OUTPUT);
    GPIO_SetMode(SWITCH_PORT, SWITCH_PIN, GPIO_PMD_INPUT);
    GPIO_SetMode(IRDATA_PORT, IRDATA_PIN, GPIO_PMD_INPUT);
	LED_GREEN(0);
	LED_RED(1);

	detect_board_version();

    /* Unlock protected registers */
    SYS_UnlockReg();

    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    UART0_Init();
	SPI_Init();

	TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 6000000);
	TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, TRANSFER_RATE * 2);
	TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, TRANSFER_RATE * 2);
	TIMER_EnableInt(TIMER1);
	TIMER_EnableInt(TIMER3);

	/* Open USB controller */
    USBD_Open(&gsInfo, HID_ClassRequest, NULL);

    /* Init Endpoint configuration for HID */
    HID_Init();

    /* Start USB device */
    USBD_Start();

    /* Enable USB device interrupt */
    NVIC_EnableIRQ(USBD_IRQn);

	LED_GREEN(1);
	LED_RED(0);
    printf("\n\nnuc123-fdsemu v%d.%02d build %d started.  Compiled on "__DATE__" at "__TIME__"\n",version / 100,version % 100,BUILDNUM);
    printf("--CPU @ %0.3f MHz\n", (double)SystemCoreClock / 1000000.0f);
    printf("--SPI0 @ %0.3f MHz\n", (double)SPI_GetBusClock(SPI0) / 1000000.0f);
    printf("--SPI1 @ %0.3f MHz\n", (double)SPI_GetBusClock(SPI1) / 1000000.0f);
    printf("--Detected board version: %d (config = %d %d %d)\n", boardver,PA12,PA13,PA14);
	
	NVIC_SetPriority(USBD_IRQn,2);
	NVIC_SetPriority(TMR1_IRQn,1);
	NVIC_SetPriority(TMR2_IRQn,0);
	NVIC_SetPriority(TMR3_IRQn,0);
	NVIC_SetPriority(GPAB_IRQn,0);
	NVIC_SetPriority(EINT0_IRQn,0);

	flash_init();
	sram_init();

	fds_init();

	print_block_info(0);
	while(1) {
		if(havepacket) {
			havepacket = 0;
//			process_send_feature(epdata,64);
		}
		console_tick();
		fds_tick();
	}
}
Exemple #23
0
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    volatile uint32_t u32InitCount;

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
    printf("+-------------------------------------------------+\n");
    printf("|    Timer1 External Counter Input Sample Code    |\n");
    printf("+-------------------------------------------------+\n\n");

    printf("# Timer Settings:\n");
    printf("  Timer1: Clock source is HCLK(50 MHz); Continuous counting mode; Interrupt enable;\n");
    printf("          External counter input enable; TCMP is 56789.\n");
    printf("# Connect P2.0 to T1 pin and pull P2.0 High/Low as T1 counter input source.\n\n");

    /* Configure P2.0 as GPIO output pin and pull pin status to Low first */
    GPIO_SetMode(P2, 0, GPIO_PMD_OUTPUT);
    P20 = 0;

    /* Initial Timer1 default setting */
    TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);

    /* Configure Timer1 setting for external counter input function */
    TIMER_SELECT_TOUT_PIN(TIMER1, TIMER_TOUT_PIN_FROM_TX_PIN);
    TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
    TIMER_SET_CMP_VALUE(TIMER1, 56789);
    TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE);
    TIMER_EnableInt(TIMER1);

    /* Enable Timer1 NVIC */
    NVIC_EnableIRQ(TMR1_IRQn);

    /* Clear Timer1 interrupt counts to 0 */
    g_au32TMRINTCount[1] = 0;

    /* Start Timer1 counting */
    TIMER_Start(TIMER1);

    /* To check if TDR of Timer1 must be 0 as default value */
    if(TIMER_GetCounter(TIMER1) != 0)
    {
        printf("Default counter value is not 0. (%d)\n", TIMER_GetCounter(TIMER1));

        /* Stop Timer1 counting */
        TIMER_Close(TIMER1);
        while(1);
    }

    /* To generate one counter event to T1 pin */
    GeneratePORT2Counter(0, 1);

    /* To check if TDR of Timer1 must be 1 */
    while(TIMER_GetCounter(TIMER1) == 0);
    if(TIMER_GetCounter(TIMER1) != 1)
    {
        printf("Get unexpected counter value. (%d)\n", TIMER_GetCounter(TIMER1));

        /* Stop Timer1 counting */
        TIMER_Close(TIMER1);
        while(1);
    }

    /* To generate remains counts to T1 pin */
    GeneratePORT2Counter(0, (56789 - 1));

    while(1)
    {
        if((g_au32TMRINTCount[1] == 1) && (TIMER_GetCounter(TIMER1) == 56789))
        {
            printf("Timer1 external counter input function ... PASS.\n");
            break;
        }
    }

    /* Stop Timer1 counting */
    TIMER_Close(TIMER1);

    while(1);
}
void HardwareTimer::open(uint32_t mode, uint32_t freq) { 
  TIMER_Open(dev, mode, freq);		
}
Exemple #25
0
//=========================================================================
// Additional initialisations
//-------------------------------------------------------------------------
__myevic__ void CustomStartup()
{

//-------------------------------------------------------------------------
// EADC test

	if ( 0 )
	{
		uint32_t s1, s2, s3;

		SetADCState( 0, 1 );
		SetADCState( 4, 1 );

		do
		{
			ClearScreenBuffer();

			CLK_SysTickDelay( 10 );
			s3 = ADC_Read( 4 );

			CLK_SysTickDelay( 10 );
			s1 = ADC_Read( 18 );

			CLK_SysTickDelay( 10 );
			s2 = ADC_Read( 0 );

			DrawValue( 8,  0, s1, 0, 0x29, 4 );
			DrawValue( 8, 20, s2, 0, 0x29, 4 );
			DrawValue( 8, 40, s3, 0, 0x29, 4 );

			DisplayRefresh();

			WaitOnTMR2( 1000 );
		}
		while ( PD3 );
	}


//-------------------------------------------------------------------------
// Timer test 1

	if ( 0 )
	{
		TIMER_Stop( TIMER3 );
		TIMER_Close( TIMER3 );

		MemClear( gPlayfield.uc, 256 );

		CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 );

		gPlayfield.ul[1] =
		TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 );
		TIMER_EnableInt( TIMER3 );
		TIMER_Start( TIMER3 );
	}


//-------------------------------------------------------------------------
// Timer test 2

	if ( 0 )
	{
		TIMER_Close( TIMER2 );
		TIMER_Close( TIMER3 );

		MemClear( gPlayfield.uc, 256 );

		CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HXT, 0 );
		CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 );

		CLK_EnableModuleClock( TMR2_MODULE );
		CLK_EnableModuleClock( TMR3_MODULE );

		__set_PRIMASK(1);

		TIMER3->CTL |= TIMER_CTL_RSTCNT_Msk;
		TIMER2->CTL |= TIMER_CTL_RSTCNT_Msk;

		TIMER3->CMP  = 1000;

		TIMER3->CTL  = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE;
		TIMER2->CTL  = TIMER_CTL_CNTEN_Msk | TIMER_CONTINUOUS_MODE;
		while(!(TIMER3->INTSTS & TIMER_INTSTS_TIF_Msk));
		TIMER2->CTL = 0;

		gPlayfield.ul[0] = TIMER2->CNT;

		__set_PRIMASK(0);

		TIMER_Close( TIMER2 );
		TIMER_Close( TIMER3 );

		CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HIRC, 0 );
		CLK_EnableModuleClock( TMR2_MODULE );
		TIMER_Open( TIMER2, TIMER_PERIODIC_MODE, 1000 );
		TIMER_EnableInt( TIMER2 );
		TIMER_Start( TIMER2 );

		CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_HXT, 0 );
		CLK_EnableModuleClock( TMR3_MODULE );
		TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 );
		TIMER_EnableInt( TIMER3 );
		TIMER_Start( TIMER3 );
	}

	return;
}