void high_isr(void) #else #error "Invalid compiler selection for implemented ISR routines" #endif { /* Determine which flag generated the interrupt */ if(INTCONbits.TMR0IF == 1) { //Timer0 Servo_Switch_Flag = 0; LED3 = 1; //PORTDbits.RD0 = 1; INTCONbits.TMR0IF = 0; WriteTimer0(SERVO_PERIOD); WriteTimer1(0xFFFF - Servo_Times[0]); Servo_Id = 0; //Todo: fix defines LATC = 0xFF; } else if(PIR1bits.TMR1IF == 1) { //Timer1 //Servo_Id = (Servo_Id++) % MAX_SERVOS; WriteTimer1(0xFFFF - Servo_Times[++Servo_Id]); //Todo: Properly define this in headers //Off by one because of critical interrupt stuff above switch(Servo_Id) { case 1: LATC = LATC & ~(1 << SERVO_PIN_1); LED3 = 0; break; case 2: LATC = LATC & ~(1 << SERVO_PIN_2); break; case 3: LATC = LATC & ~(1 << SERVO_PIN_3); break; case 4: LATC = LATC & ~(1 << SERVO_PIN_4); break; case 5: LATC = LATC & ~(1 << SERVO_PIN_5); Servo_Switch_Flag = 1; break; } PIR1bits.TMR1IF = 0; } else { /* Unhandled interrupts */ } }
void InitServos(unsigned short nServos, char port) { Servo *s; int i; PORT = port; N_SERVOS = nServos; for (i = 0; i < N_SERVOS; i++) { s = &servos[i]; s->Degree = 0; s->Enable = 0; } //Interruption Enable GLOBAL_INTERRUPT = 1; PERIPHERALS_INTERRUPT = 1; TIMER_1_INTERRUPT = 1; //Configuration of Frame FrameServo = (int) FrameServo / CLOCK_MIN_TICK; //Configuration Timer1 WriteTimer1(65535 - FrameServo); TIMER_1 = 0b00100001; //Prescaler 1:4 }
void decodeIR(void) { WriteTimer1(1); invertEdge(); if (necState == invalid) return; if (necState == inactive) { necState = lead_pulse; T1CONbits.TMR1ON = 1; T3CONbits.TMR3ON = 1; return; } if (necState == lead_pulse) { if (CCPR1H > NEC_LEAD_PULSE_MIN || CCPR1H < NEC_LEAD_PULSE_MAX) necState = lead_space; else necState = invalid; return; } if (necState == lead_space) { if (CCPR1H > NEC_LEAD_SPACE_MIN && CCPR1H < NEC_LEAD_SPACE_MAX) { necState = data_pulse; withinFrame = 1; } else if (CCPR1H > NEC_REPEAT_SPACE_MIN && CCPR1H < NEC_REPEAT_SPACE_MAX) necState = repeat_pulse; else necState = invalid; return; } if (necState == repeat_pulse && withinFrame) { if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX) { ir.decoded = 1; ir.repeated = 1; WriteTimer3(0); } else { necState = invalid; } return; } if (necState == data_pulse) { if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX) necState = data_space; else necState = invalid; return; } if (necState == data_space) { // Received a 0 if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX) { capture.data = capture.data >> 1; capture.bitcount++; necState = data_pulse; // Received a 1 } else if (CCPR1H > NEC_LONG_MIN && CCPR1H < NEC_LONG_MAX) {
/******************************************************************** * Function: void Stepper_Process(void) * * PreCondition: Init_Stepper_Process(void) * * Input: None * * Output: None * * Overview: Process's the stepper information and sets or unsets the * outputs ********************************************************************/ void Stepper_Process() { int i; for (i=0;i<ENABLED_CHANNELS;i++) { if (stepper[i].status == GO_CW) { if (stepper[i].sequence == 0x00) { stepper[i].sequence = 0x06; } else if (stepper[i].sequence == 0x60) { stepper[i].sequence = 0x00; stepper[i].status == STOP; } else { stepper[i].sequence <<= 1; } } else if (stepper[i].status == GO_CCW) { if (stepper[i].sequence == 0x00) { stepper[i].sequence = 0x60; } else if (stepper[i].sequence == 0x06) { stepper[i].sequence = 0x00; stepper[i].status == STOP; } else { stepper[i].sequence >>= 1; } } } CHAN_0_A = (stepper[0].sequence & 0x04) > 0; CHAN_0_B = (stepper[0].sequence & 0x08) > 0; CHAN_0_C = (stepper[0].sequence & 0x10) > 0; CHAN_0_D = (stepper[0].sequence & 0x20) > 0; // CHAN_1_A = (stepper[1].sequence & 0x04) > 0; // CHAN_1_B = (stepper[1].sequence & 0x08) > 0; // CHAN_1_C = (stepper[1].sequence & 0x10) > 0; // CHAN_1_D = (stepper[1].sequence & 0x20) > 0; WriteTimer1(0x4000); }
void delayus(unsigned t) { OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF); while (t--) { // t x 1ms loop WriteTimer1(0); while (ReadTimer1() < SYS_FREQ / 256 / 1000000); } CloseTimer1(); }// Delayus
void timer1_isr(void) { PIR1bits.TMR1IF = 0; // 清除中斷旗標 WriteTimer1(TMR1_VAL); // 當將計數器觸發次數歸零寫入預設值 MyTimerFunction(); }
void delayms(unsigned t) // This uses Timer 1, can be changed to another timer. { OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF); while (t--) { // t x 1ms loop WriteTimer1(0); while (ReadTimer1() < SYS_FREQ / 256 / 1000); } CloseTimer1(); } // Delayms
void resetIR(void) { necState = inactive; ir.decoded = 0; ir.repeated = 0; capture.edge = 0; capture.bitcount = 0; capture.data = 1; updateEdge(); WriteTimer1(0); }
void DelayMs(int cms) { int ims; for (ims=0; ims<cms; ims++) { WriteTimer1(0); while (ReadTimer1() < cntMsDelay); } }
void Init_TMR1(void) { OpenTimer1(TIMER_INT_ON & // 使用C18編譯器timer函式庫 T1_16BIT_RW & // 初始化設定Timer1 T1_SOURCE_EXT & // 並開啟TIMER1中斷功能(PIE1bits.TMR1IE=1) T1_PS_1_1 & T1_OSC1EN_ON & T1_SYNC_EXT_ON); WriteTimer1(TMR1_VAL); // 寫入預設值 PIR1bits.TMR1IF = 0; // 清除中斷旗標 }
// A function called by the interrupt handler // This one does the action I wanted for this program on a timer1 interrupt void timer1_int_handler() { //unsigned int result; // read the timer and then send an empty message to main() //result = ReadTimer1(); //ToMainLow_sendmsg(0,MSGT_TIMER1,(void *) 0); // reset the timer WriteTimer1(0); }
void startTimer1() { // Setup the timer with a 1:1 prescaler with 16 bits resolution // Therefore the timer0 freq is 500 kHz / 4 / 4 = 31.250 kHz OpenTimer1( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4 ); // Should take a little over 2 seconds to overflow the counter from TMR0 = 0 // If you write in a different starting value for TMR0 it'll overflow sooner WriteTimer1(0); PIR1bits.TMR1IF = 0; }
/** * シリアル・ポートの Ready を待つ * 出力ポートの busy 状態を見る * timer1 を使ってオーバフローならば 0 を返す * Ready になれば 1 を返す */ int WaitToReadySerial(void) { WriteTimer1(0); PIR1bits.TMR1IF = 0; while(PIR1bits.TMR1IF==0) { // wait to ready uart if (!BusyUSART()) return 1; } return 0; }
void SetupTimerVOC() { unsigned char Timer1Config, Timer1Config1; Timer1Config = TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF; Timer1Config1 = TIMER_GATE_OFF & TIMER_GATE_TOGGLE_OFF & TIMER_GATE_1SHOT_OFF & TIMER_GATE_INT_OFF; OpenTimer1(Timer1Config,Timer1Config1); WriteTimer1(0); TGS8100Pulse=0; }
void TMR1_ISR(void) { /* * Clears the TMR1 interrupt flag to stop the program from processing the * same interrupt multiple times. */ PIR1bits.TMR1IF = 0; // Reload value for 1ms overflow WriteTimer1(tmr[1].reload); if(tmr[1].func != 0 ) (*(tmr[1].func))(); }
/* *------------------------------------------------------------------------------ * void TMR1_init(void) * Summary : Initialize timer1 for display refresh * * Input : None * * Output : None * *------------------------------------------------------------------------------ */ void TMR1_init(unsigned int reload , void (*func)()) { // Enagle TMR1 interrrupt,16 bit counter, with internal clock, No prescalar OpenTimer1(TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF ); // Reload value for 1ms overflow WriteTimer1(reload); // Make timer1 interrupt high priority IPR1bits.TMR1IP = 1; tmr[1].reload = reload; tmr[1].func = func; }
/******************************************************************************* * SERVO INTERRUPTION ******************************************************************************/ void ServoInterruption() { static char servo_number = 0; static Servo *s; s = &servos[servo_number]; if (s->Enable) { if (!Pulso) { WriteTimer1(65535 - s->Degree + CORRECTION_TIME); Output(servo_number, 1); } else { WriteTimer1((65535 - FrameServo) + s->Degree + CORRECTION_TIME); Output(servo_number, 0); servo_number = (servo_number + 1)&7; } Pulso = ~Pulso; } else { WriteTimer1(65535 - FrameServo); servo_number = (servo_number + 1)&7; } }
void timer1_int_handler() { // Encoder count for motor 1. timer1Counter++; // Reset the timer WriteTimer1(0xFFCF); DEBUG_ON(TMR1_DBG); DEBUG_OFF(TMR1_DBG); if (ticks_flag) { // Reverse counter to prevent it from going a set distance. ticks1Counter++; if (ticks1Counter >= maxTickOne && postRight == 1) { postRight = 2; // Turn right 90 motorBuffer[0] = 0x01; motorBuffer[1] = 0x03; motorBuffer[2] = 0x04; motorBuffer[3] = 0x01; motorBuffer[4] = 0x0C; motorBuffer[5] = 0x09; ToMainHigh_sendmsg(MOTORLEN, MSGT_I2C_DATA, (void *) motorBuffer); } else if (ticks1Counter >= maxTickOne && postRight == 2) { // Move forward after right turn postRight = 0; motorBuffer[0] = 0x01; motorBuffer[1] = 0x03; motorBuffer[2] = 0x01; motorBuffer[3] = 0x01; motorBuffer[4] = 0x00; motorBuffer[5] = 0x00; ToMainHigh_sendmsg(MOTORLEN, MSGT_I2C_DATA, (void *) motorBuffer); } else if (ticks1Counter >= maxTickOne && postAlignFlag) { postAlignFlag = 0; motorBuffer[0] = 0x01; motorBuffer[1] = 0x03; motorBuffer[2] = 0x01; motorBuffer[3] = 0x01; motorBuffer[4] = 0x00; motorBuffer[5] = 0x00; ToMainHigh_sendmsg(MOTORLEN, MSGT_I2C_DATA, (void *) motorBuffer); } else if (ticks1Counter >= maxTickOne) { // Stops wheels after set number of ticks WriteUSART(0x00); } } }
void timer1_int_handler() { unsigned int result; // read the timer and then send an empty message to main() #ifdef __USE18F2680 LATBbits.LATB1 = !LATBbits.LATB1; #endif result = ReadTimer1(); ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0); // reset the timer WriteTimer1(0); }
void timer1_int_handler() { //unsigned int result; // read the timer and then send an empty message to main() /* LATBbits.LATB1 = !LATBbits.LATB1; result = ReadTimer1(); ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0);*/ ConvertADC(); // reset the timer WriteTimer1(65086); }
// Timer interrupt => sample time void __attribute__((__interrupt__)) _T1Interrupt(void) { unsigned char i; WriteTimer1(0); //reset timer's value IFS0bits.T1IF = 0; //clear the interrupt flag if (current_state == TSOP40_16) // end of read turn { for (i=0 ; i<16 ; i++) { polar_response[i]=receive_flag[i]; receive_flag[i]=0; } COMPUTE_FLAG = ON; } state_machine(); }
void tratar_low_interrupt(void) { // Verifica se o motivo da chamada da interrupção foi o estouro(overflow) do Timer0 if(PIR1bits.TMR1IF) { if(++preescaler_fake_timer1 >= 2) { //Re-carrega valor inicial do contador do Timer0 WriteTimer1(INICIO_TMR1); if(barra_down_debounce == 2) barra_down_debounce = 1; preescaler_fake_timer1 = 0; } //Limpa flag da interrupção do Timer0 PIR1bits.TMR1IF = 0; }// end tratamento da interrupção do Timer0 (INTCONbits.TMR0IF) }
void interrupt global_isr(void) { // TIMER 0 if(TMR0IE && TMR0IF) { //TMR0IE=0; LED2=1; //vw_isr_tmr0(); //return; // volta antes de testar o Timer1 //TMR0IE=1; } // TIMER 1 if (TMR1IF) { //TMR1IE=0; LEDI=1; LEDI=0; if (relogio >= 60) { aferir(); // Afere, Mostra no LCD, e Trasmite relogio=0; } else relogio++; // Mostra o contador de segundos no LCD while(BusyXLCD()) ; SetDDRamAddr(0x66); //linha 4 nas duas ultimas posicoes putrsXLCD ( ltoa ( NULL , relogio , 10) ); TMR1IF=0; WriteTimer1( 0xFC00 ); //TMR1IE=1; } }
/** * @brief Setup the servos to start moving when powered * Given a particular azimuth and elevation, the servos will start continuously * moving towards the given azimuth and given elevation. */ void setupServos( void ){ // Set up the correct data directions DDR_AZIMUTH = 0; DDR_ELEVATION = 0; // Set up timer 1 : Prescaler 1 for max resolution WriteTimer1(0); ///< Clear Timer 1 T1CONbits.RD16 = 1; ///< Latch in 16 bit values for CCPR1L and CCPR1H T1CONbits.TMR1ON = 1; ///< Turn on timer 1 // Setup the initial Servo position updateCCPServoAngle( 900, 0 ); // Setup the capture Compare module CCPR1 = servoOffTime; ///< Write the initial delay CCP1CONbits.CCP1M = 0b1010; ///< Setup the compare interrupt // Enable the CCP interrupt RCONbits.IPEN = 1; ///< Enable interrupt priorites IPR1bits.CCP1IP = 0; ///< Set CCP interrupt to high priority PIE1bits.CCP1IE = 1; ///< Enable the capture compare interrupt INTCONbits.GIEH = 1; ///< Enable high priority interrupts INTCONbits.GIEL = 1; }
int main() { mJTAGPortEnable(DEBUG_JTAGPORT_OFF); mPORTFClearBits(BIT_0); // Make all lower 8-bits of PORTA as output mPORTFSetPinsDigitalOut( BIT_0 ); // Start timer1, Fpb/256, max period OpenTimer1(T1_ON | T1_PS_1_256 | T1_SOURCE_INT, 0xFFFF); // The main loop while( 1) { WriteTimer1(0); while ( TMR1 < LONG_DELAY); PORTF ^= BIT_0; } }
int main() { mJTAGPortEnable(DEBUG_JTAGPORT_OFF); mPORTFClearBits(BIT_0); // Make all lower 8-bits of PORTA as output mPORTFSetPinsDigitalOut( BIT_0 ); TRISE=0x0; PORTE = 0x0f; // Start timer1, Fpb/256, max period OpenTimer1(T1_ON | T1_PS_1_256 | T1_SOURCE_INT, 0xFFFF); // The main loop PORTSetPinsDigitalIn(IOPORT_D, BIT_5); mCNOpen(CN_ON, CN14_ENABLE, 0); // Read the port to clear any mismatch on change notice pins int dummy = PORTD; // Clear change notice interrupt flag ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2); INTEnableSystemMultiVectoredInt(); uart1_init(115200); setbuf(stdin, NULL); //no input buffer (for scanf) setbuf(stdout, NULL); //no output buffer (for printf) printf ("Hello World!\r\n"); while( 1) { putchar(getchar()); WriteTimer1(0); while ( TMR1 < LONG_DELAY); PORTE+=1; } }
/* *------------------------------------------------------------------------------ * void TMR1_init(void) * Summary : Initialize timer1 for display refresh * * Input : None * * Output : None * *------------------------------------------------------------------------------ */ void TMR1_init(unsigned int reload , void (*func)()) { UINT8 config, config1; config = (TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_FOSC_4 & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); config1 = (TIMER_GATE_OFF & TIMER_GATE_POL_LO & TIMER_GATE_TOGGLE_OFF & TIMER_GATE_1SHOT_OFF & TIMER_GATE_SRC_T1GPIN & TIMER_GATE_INT_OFF); // Enagle TMR1 interrrupt,16 bit counter, with internal clock, No prescalar OpenTimer1(config, config1 ); // Reload value for 1ms overflow WriteTimer1(reload); // Make timer1 interrupt high priority IPR1bits.TMR1IP = 1; PIE1bits.TMR1IE = 1; T1CON = 0x01; tmr[1].reload = reload; tmr[1].func = func; }
void timer1_int_handler() { unsigned int result; #ifdef __USE18F26J50 #ifdef DEBUG LATAbits.LA0 ^= 0x1;//Test to see if the handler works. #endif #endif ADCON0bits.GO_NOT_DONE = 1;////////////////////////////////////Needed to activate the A/D converter interupt // read the timer and then send an empty message to main() #ifdef __USE18F2680 LATBbits.LATB1 = !LATBbits.LATB1; #endif result = ReadTimer1(); ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0); // reset the timer WriteTimer1(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 }
void InitApp(void) { /* Setup analog functionality and port direction */ //LEDs LED1_DIR = OUT; LED2_DIR = OUT; LED3_DIR = OUT; //Control panel CTRLPOT_DIR = IN; CTRLBUTTON1_DIR = IN; CTRLSWITCH1_DIR = IN; CTRLLED1_DIR = OUT; CTRLLED2_DIR = OUT; CTRLLED3_DIR = OUT; //UART RX_DIR = IN; TX_DIR = IN; //Module will dynamically change as needed /* Initialize peripherals */ //ADC OpenADC( ADC_FOSC_RC & ADC_RIGHT_JUST & ADC_4_TAD, ADC_CH3 & ADC_REF_VDD_VSS & ADC_INT_OFF, ADC_4ANA); //Timer OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_2); WriteTimer0(0x0000); OpenTimer1(TIMER_INT_ON & T1_SOURCE_INT & T1_16BIT_RW & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); WriteTimer1(SERVO_MIN); //UART //BusPirate: m 3 6 ENTER ENTER ENTER 1 e 3 W (2)) P OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_BRGH_HIGH & USART_NINE_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, UART_BAUDRATE); baudUSART(BAUD_IDLE_CLK_LOW & BAUD_16_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF); /* Configure the IPEN bit (1=on) in RCON to turn on/off int priorities */ INTCONbits.TMR0IF = 1; //reset Interrupt Flag PIR1bits.TMR1IF = 1; RCONbits.IPEN = 1; RCSTAbits.SPEN=1; /* Enable interrupts */ ei(); }