Example #1
0
/******************************************************************************
 * Function:        void UserInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine should take care of all of the demo code
 *                  initialization that is required.
 *
 * Note:            
 *
 *****************************************************************************/
void UserInit(void)
{
	// Real time clock start
	RtccInitClock();
	RtccWrOn();                             
	
	{
		rtccTimeDate initd;
		initd.f.year=0x10;
		initd.f.mon=0x01;
		initd.f.mday=0x01;
		initd.f.wday=5;   // 2010.01.01 is friday
		initd.f.hour=0;
		initd.f.min=0;
		initd.f.sec=0;
		RtccWriteTimeDate(&initd,FALSE);
		RtccWriteAlrmTimeDate(&initd);
	}
	mRtccOn();
        mRtccAlrmEnable();
	RtccSetAlarmRpt(RTCC_RPT_MIN,TRUE);
	// Mtouch init
	mTouchInit();
	mTouchCalibrate();
	
	cmdstr[0]=0;
	screen=0;
	screenvalid=0;
	position=0;
	buttonstate.Val=0;
	buttonpressed.Val=0;
    devicereset=0;  
    clockss=0;
	resetcounter=0;
	alarmcnt=0;

        TRISBbits.TRISB1=0;

        PPSUnLock();
        iPPSOutput(OUT_PIN_PPS_RP4,OUT_FN_PPS_CCP1P1A);            //Configre RP24 as C1OUT pin
        PPSLock();

//----Configure pwm ----
    period = 0xFF;
    OpenPWM1( period);            //Configure PWM module and initialize PWM period

//-----set duty cycle----
        duty_cycle = 256;
        SetDCPWM1(duty_cycle);        //set the duty cycle

//----set pwm output----
    outputconfig = HALF_OUT ;
    outputmode = PWM_MODE_1;
    SetOutputPWM1( outputconfig, outputmode);    //output PWM in respective modes
	ADCON0bits.CHS=4;
	/* Make sure A/D interrupt is not set */
	PIR1bits.ADIF = 0;
	/* Begin A/D conversion */
}//end UserInit
Example #2
0
void configTimers (void)
{
    //TIMER0
    OpenTimer0 (    TIMER_INT_ON &
            T0_8BIT &
            T0_EDGE_FALL &
            T0_PS_1_256 &
            T0_SOURCE_INT
            );              // Clock Interno, contador de 8 bits e Prescaler 1/256
    WriteTimer0 ( 0x3D );   // Gera um intervalo de 0,1 segundo a 4 mhz

    //TMR0ON=1;               // Liga o Timer0
    //extern volatile __bit TMR0ON              @ (((unsigned) &T0CON)*8) + 7;
    //#define               TMR0ON_bit          BANKMASK(T0CON), 7

    TMR0IF=0;               // Zera o Overflow do Timer0
    TMR0IE=1;               // Habilita a Interrupcao para o Overflow do Timer0

    //////////////////////////////////////////////////////////////////////

    // TIMER1
    OpenTimer1(TIMER_INT_ON &
               T1_SOURCE_EXT &
               T1_SYNC_EXT_OFF &
               T1_PS_1_1 &
               T1_OSC1EN_ON &
               T1_16BIT_RW
               );
    //WriteTimer1( 0xBE5  );   // equivalemnte a 1/2 segundo em 4,00 mhz
    WriteTimer1( 0x8000 );   // equivalente a 1 segundo em 32,768 khz

    //TMR1ON = 1;   //  Liga o Timer1 -> Igual ao TIMER_INT_ON
    TMR1IF = 0;     // Zera o Overflow para o Timer1
    TMR1IE=1;       // Habilita Interrupcao para o Overflow do Timer1
    //////////////////////////////////////////////////////////////////////

     // PWM TIMER2
     OpenPWM1(0x7C);    // Inicializa o PWM com intervalo de 2khz e PS_1/16
                        // num clock de 4 mhz
     SetDCPWM1(0x000F);  // Configura o Duty Cycle Inicial

     OpenTimer2(
             TIMER_INT_OFF &
             T2_PS_1_16
             );             // Para o PWM funcionar corretamente, a Interrupcao
                            // TIMER_INT_OFF (ou TMR2IE) deve ser desabilitada

     // obs: como o PWM funciona como um "temporizador", nao precisa executar
     // interrupcao ou acao; a propria porta de saida PWM1 ja executa

     //TMR2=0;    // Clear Timer2 -> Nao necessario para este exemplo
     //T2CKPS1=1; // Pre-Scaler 16 (ja setado no T2_PS1_16), redundante

     SetOutputPWM1( SINGLE_OUT , PWM_MODE_1 );    // Configura o CCP1CON
     // apenas o PWM1: P1A modulated; P1B, P1C, P1D assigned as port pins
     // no modo PxA,PxC active high, PxB,PxD active high */

     //CCP1IE=1;  // Habilita Interrupcao no modulo CCP1, Nao Necessario
     //TMR2IF=0;  // Limpando o flag de overflow do Timer2, Nao Necessario
     TMR2ON=1;  // Ligando o Timer2
     //TMR2IE=0;    // -> TIMER_INT_OFF , redundante

     // Pisca Verde/Vermelho 2 vezes para sinalizar inicio do PWM
    __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0;
    __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0;
    __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0;
    __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0;

    PEIE=1; //  Habilita As Interrupcoes dos Perifericos
    GIE=1;  //  Habilita as Interrupcoes Globais
}