/* * Don't forget to call this function from the right interrupt vector. */ void InterruptServo() { if (INTCONbits.TMR0IE && INTCONbits.TMR0IF) { if (CurrentServo > 0) { // Set previous pin (CurrentServo - 1) to 0. PORTB &= ~(1 << (CurrentServo - 1)); } if (CurrentServo < CountServo) { // Set current (CurrentServo) pin to 1. PORTB |= 1 << CurrentServo; WriteTimer0(65534 /* Max int */ /* Maximum width of 2.4ms. */ - AngleServo[CurrentServo] * 2 * ((2400 - 544) / 180) /* Minimum width of 544µs. */ - 544 * 2 /* 2 = 8MHz / 4 (proc. freq) / 1µs */ ); // Between 0.9ms and 2.1ms. } else { WriteTimer0(65535 - 20000 * 2); // Minimum of 20ms between pulses. } CurrentServo++; if (CurrentServo > CountServo) CurrentServo = 0; // When CurrentServo == CountServo, the next interrupt sets every pin to 0. INTCONbits.TMR0IF = 0; } }
void timer0_int_handler() { unsigned int val; int length, msgtype; LATBbits.LATB6 = !LATBbits.LATB6; // toggle an LED // LATBbits.LATB0 = !LATBbits.LATB0; // reset the timer #if defined (MOTOR_PIC) WriteTimer0(TIMER0_START); ticks_right++; if ( executingEncode && ticks_right_C > 0) ticks_right_C--; if ( executingEncodeLong && ticks_right_C_Long > 0) ticks_right_C_Long--; if ( ticks_left_C <= 1 && ticks_right_C <= 1 && executingEncode ) { if ( motor_state == RoverMsgMotorForward) RoverForward(); else if ( motor_state == RoverMsgMotorLeft2 || motor_state == RoverMsgMotorRight2 ) { motor_encode_lthread(RoverMsgMotorForwardCMDelim+10); } else RoverStop(); executingEncode = 0; } else if ( ticks_left_C_Long <= 1 && ticks_right_C_Long <= 1 && executingEncodeLong ) { if ( motor_state == RoverMsgMotorLeft2 ) { motor_encode_lthread(RoverMsgMotorRight2); } else if ( motor_state == RoverMsgMotorRight2 ) { motor_encode_lthread(RoverMsgMotorLeft2); } else { RoverStop(); } motor_state = RoverMsgMotorStop; executingEncodeLong = 0; } #else WriteTimer0(0); #endif // try to receive a message and, if we get one, echo it back //length = FromMainHigh_recvmsg(sizeof(val), (unsigned char *)&msgtype, (void *) &val); //if (length == sizeof (val)) { // ToMainHigh_sendmsg(sizeof (val), MSGT_TIMER0, (void *) &val); //} }
void SendIRImpl(void) { WORD t,wait; BYTE hilo; WORD pos; WORD byteOrWord; // 0:未設定 WriteTimer0(0); INTCONbits.TMR0IF = 0; hilo = 1; pos = 0; byteOrWord = 0; while (1) { wait = ReadBuffer(&pos,&byteOrWord); if (wait==BUFF_EOF) break; if (wait==MAX_WAIT_CYCLE) break; do { LED1_PORT = hilo; IRLED_PORT = hilo; DelayIRFreqHi(); t = ReadTimer0(); if (t >= wait) break; LED1_PORT = 0; IRLED_PORT = 0; DelayIRFreqLo(); t = ReadTimer0(); } while(t < wait); WriteTimer0(0); hilo = !hilo; LED1_PORT = hilo; IRLED_PORT = hilo; } // wait 10msec [1cycle==0.083333333us] Delay10KTCYx(120); LED1_PORT = 0; IRLED_PORT = 0; PutsString("OK\r\n"); LED1_PORT = 0; IRLED_PORT = 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) { unsigned char i; initLEDs(); // Initialize the arrays for (i=0; i<sizeof(USB_Out_Buffer); i++) { USB_Out_Buffer[i] = 0; } ReadMessageFromEEPROM(); testLEDs(); NextUSBOut = 0; LastRS232Out = 0; lastTransmission = 0; nextProcessState = P_VERSION; RS232cp = 0; // POV space = 0; POV_curPos = 0; POV_curLetter = 0; OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_256); WriteTimer0(POV_TIMER); }//end UserInit
void timer0_int_handler(unsigned char *adcbuffer) { WriteTimer0(0); // unsigned int val; // int length, msgtype; // toggle an LED #ifdef __USE18F2680 LATBbits.LATB0 = !LATBbits.LATB0; #endif // reset the timer // try to receive a message and, if we get one, echo it back // length = FromMainHigh_recvmsg(sizeof(val), (unsigned char *)&msgtype, (void *) &val); // if (length == sizeof (val)) { // ToMainHigh_sendmsg(sizeof (val), MSGT_TIMER0, (void *) &val); // } ConvertADC(); //while( BusyADC()) { //LATBbits.LATB1 = 1; //} //LATBbits.LATB1 = 0; }
void TMR0_ISR(void) { /* * Clears the TMR0 interrupt flag to stop the program from processing the * same interrupt multiple times. */ INTCONbits.TMR0IF = 0; ++heartBeatCount; ++appUpdateCount; ++keypadUpdate_count; ++comUpdateCount; --timeStampUpdateCount; if( timeStampUpdateCount <= 0 ) { AppTimestamp++; timeStampUpdateCount = TIMESTAMP_DURATION; } if(tmr[0].func != 0 ) (*(tmr[0].func))(); // Reload value for 1ms overflow WriteTimer0(tmr[0].reload); }
void delay(unsigned int c) { INTCONbits.TMR0IF = 0; WriteTimer0(c); while (INTCONbits.TMR0IF == 0) ; }
void timer0_isr(void) { INTCON1bits.TMR0IF = 0; WriteTimer0(6); ms_ticks++; }
void main(void) { //timer vars int result; float final_result; Delay100TCYx(10); //let the device startup usart_init(); i2c_init(); printf("Starting\r\n"); // let sonar initialize, delay 250 ms Delay1KTCYx(625); TRISBbits.TRISB3 = 1; // data pin input while(1) { while(PORTBbits.RB3 == 0); // configure timer0 OpenTimer0(TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1); WriteTimer0( 0 ); // restart timer while(PORTBbits.RB3 == 1); //Delay1TCY(); result = ReadTimer0(); // read timer printf("Timer is %u \r\n", result); final_result = (float)result / (float)(2.5*147); printf("X / (4*147) = %i \r\n", (int)final_result); } }
void main(void) { TRISBbits.RB0 = 1; // KEY1 is input TRISBbits.RB1 = 1; // KEY2 is input SSEG_Init(); // Initialize SSEGs // Configure Timer0 OpenTimer0(TIMER_INT_ON & // Enable TIMER Interrupt T0_16BIT & // Timer0 is configured as an 16-bit timer/counter T0_SOURCE_INT & // Internal instruction cycle clock (CLKO) acts as source of clock T0_PS_1_16); // 1:16 Prescaler value WriteTimer0(TMR0_value); // Write Timer0 value to Timer0 to count INTCONbits.INT0E = 1; // Enable Interrupt 0 (RB0 as interrupt) INTCON2bits.INTEDG0 = 1; // Interrupt 0 occurs at rising edge INTCONbits.INT0F = 0; // Clear Interrupt 0 flag INTCON3bits.INT1E = 1; // Enable Interrupt 1 (RB1 as interrupt) INTCON2bits.INTEDG1 = 1; // Interrupt 1 occurs at rising edge INTCON3bits.INT1F = 0; // Clear Interrupt 0 flag INTCONbits.GIE = 1; // Enables all unmasked interrupts while(1) { Delayms(250); if(direction_flag == 0) { counter++; // Increase counter by one if(counter > 99) counter = 0; } else if (direction_flag == 1) { counter--; if(counter < 0) counter = 99; } } }
void interrupt ISR(void) { if(INTCONbits.TMR0IF==1) { // if Timer0 Interrupt is occurred INTCONbits.TMR0IF = 0; // Clear the interrupt flag WriteTimer0(TMR0_value); // Update TMR0 value if(timer_flag == 0) { // if it is first 10ms SSEG_Print(1,counter%10); // Shows ones of counter at first sseg timer_flag = 1; } else { // if it is second 10ms SSEG_Print(2,counter/10); // Shows tens of counter at second sseg timer_flag = 0; } } if(INTCONbits.INT0F == 1) { // if External Interrupt 0 is occurred (KEY1) INTCONbits.INT0F = 0; // Clear Interrupt 0 Flag direction_flag = 1; } if(INTCON3bits.INT1F == 1) { // if External Interrupt 1 is occurred (KEY2) INTCON3bits.INT1F = 0; // Clear Interrupt 1 Flag direction_flag = 0; } }
void timer0_int_handler() { WriteTimer0(0); // unsigned int val; // int length, msgtype; // toggle an LED #ifdef __USE18F2680 LATBbits.LATB0 = !LATBbits.LATB0; #endif // reset the timer // try to receive a message and, if we get one, echo it back // length = FromMainHigh_recvmsg(sizeof(val), (unsigned char *)&msgtype, (void *) &val); // if (length == sizeof (val)) { // ToMainHigh_sendmsg(sizeof (val), MSGT_TIMER0, (void *) &val); // } UART_timeout++; //unsigned char motorcomm[2] = {0x9F, 0x1F}; // unsigned char motorcomm[2] = {0x00, 0x00}; // uart_trans(2, motorcomm); //ConvertADC(); //while( BusyADC()) { //LATBbits.LATB1 = 1; //} //LATBbits.LATB1 = 0; }
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 */ } }
/////////////////////////////////////////////////////////////////////// // Hardware and variable init. /////////////////////////////////////////////////////////////////////// void init(void) { // Oscillator selection OSCCONbits.IRCF0 = 1; OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF2 = 1; // All digital pins on porta ADCON1 = 0x0F; // Set initial port state PORTA = 1; PORTB = 0; PORTC = 0x04; TRISA = PortAConfig; TRISB = PortBConfig; TRISC = PortCConfig; // PortB pullups enable INTCON2bits.NOT_RBPU = 0; // Enable the main 1-sec timer that will interrupt every second OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_32); WriteTimer0(TMR0_VAL); // Load initial timer value // Serial interface init (9600 @ 8 MHz, BRGH = 1 => 51) OpenUSART(USART_ASYNCH_MODE & USART_TX_INT_OFF & USART_RX_INT_ON & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 51); uart_state = IDLE; rx_pointer = 0; // Enable interrupts GIE and PEIE INTCON |= 0xC0; // Clear the clock clock_clear(); // Clear possible existing interrupt flags INTCONbits.INT0IF = 0; INTCONbits.TMR0IF = 0; PIR1bits.TMR1IF = 0; output = 0; switchpoint_init(); }
// Timer0 interrupt handler void timer0_int_handler() { // reset the timer WriteTimer0(0); // Send low priority message ToMainLow_sendmsg(0, MSGT_TIMER0, 0); }
void interrupt ISR(void) { if(INTCONbits.TMR0IF==1) { // if Timer0 Interrupt is occurred INTCONbits.TMR0IF = 0; // Clear the interrupt flag WriteTimer0(TMR0_value); // Update TMR0 value flag = 1; } }
void ReadIR(void) { WORD t; BYTE hilo; BYTE exit; WORD byteOrWord; // 0:未設定 WORD pos; if (enableReadIR==0) return; if (IR_PORT == 1) return; // なにか信号があった WriteTimer0(0); INTCONbits.TMR0IF = 0; hilo = 0; pos = 0; byteOrWord = 0; exit = 0; while (exit==0) { LED1_PORT = !hilo; do { t = ReadTimer0(); if (INTCONbits.TMR0IF || (t > MAX_WAIT_CYCLE)) { t = MAX_WAIT_CYCLE; INTCONbits.TMR0IF = 0; exit=1; break; } } while (IR_PORT == hilo); WriteTimer0(0); exit += WriteBuffer(t,&pos,&byteOrWord); hilo = !hilo; } LED1_PORT = 0; WriteEOF(pos); if (!WaitToReadySerial()) return; PrintIRBuffer("received,"); }
void low_isr(void) { if(INTCONbits.TMR0IE && INTCONbits.TMR0IF) { WriteTimer0(57724); led=led^1; INTCONbits.TMR0IF = 0; } }
void pausa (unsigned int segundos) { if (segundos <= 0) return; OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_PS_1_32 & T0_SOURCE_INT & T0_EDGE_FALL ); //Enable TIMER Interrupt (Int_On) /* Calculo do Timer0 * * (1 seg * 8000000) / (4 * 32) ... 32 = 1/32 prescaler * 8.000.000 / 128 = 62500 * * Timer0 = 65536 - 62500 = 3035 ou 0xBDB */ WriteTimer0( 0xBDC ); LED_AMAR=1; LED_VERM=0; T0CONbits.TMR0ON = 1; // Ligue o Timer 0 //T0CONbits.PSA = 0; // Use valores atraves do Pre-Scaler //T0CONbits.T0CS=0; // A fonte clock eh Interna (CLKO/FOSC) T0_SOURCE_INT // Sem Interrupcoes INTCONbits.GIE = 0; // Interrupcoes Globais desligadas INTCONbits.PEIE = 0; // Interrupcoes dos Perifericos desligadas INTCONbits.TMR0IE = 0; // Interrupcoes do Timer0 desligadas INTCONbits.TMR0IF = 0; // Zera o contador do Timer0 while (segundos > 0) { while (!TMR0IF) ; // enquando o Timer0 nao tiver overflow, espere LED_AMAR=~LED_AMAR; // Led Amarelo pisca a cada segundo //segundos--; //WriteTimer0( 0xBDC ); INTCONbits.TMR0IF=0; // Zera o contador do Timer0 segundos--; } CloseTimer0(); // Feche o Timer0 //TMR0ON=0; LED_AMAR=0; LED_VERM=0; LED_VERD=0; }
int main(void) { //-------------- // Initialization char LCDinit[] = {0x33,0x32,0x28,0x01,0x0c,0x06,0x00}; //Array holding initialization string for LCD char Msg1[] = {0x84,'C','U','N','T','\0'}; char Msg2[] = {0xC5,'R','P','S','\0'}; char Msg3[10]; // Msg for displaying RPS to LCD (size 10 in case dealing with large numbers) InitApp(); // Initialize Ports DisplayLCD(LCDinit,1); // Initialize LCD //-------------- // Message on LCD DisplayLCD(Msg1,0); // Display message on LCD DisplayLCD(Msg2,0); // Display message 2 //-------------- // Initialize encoder variables CHA = PORTBbits.RB5; // Initialize channel A CHB = PORTBbits.RB4; // Initialize channel B OLD_ROT = 0; // Initialize state of rotation CCWTurn = 0; // Initialize CCW count CWTurn = 0; // Initialize CW count RPS = 0.0; // Initialize RPS value CHAcount = 0; // Channel A counter //-------------- // Setup PWM cycle to motor PR2 = 0x9B; // Open pwm1 at period = 1 ms CCP1CONbits.DC1B1 = 0; // Set duty cycle of pwm1 CCP1CONbits.DC1B0 = 0; // ... CCPR1L = 0b00000100; // ... //------------- // Set timer and interrupts TMR0count = 0; // Set counter for TMR0 WriteTimer0(EncoderCount);// Load Timer0 InitInterrupts(); // Initialize timer interrupts for Port B encoder //-------------- // Loop phase: Display RPS on LCD while(1) { WaitHalfSec(); WriteLCD(0xC0,5,RPS,Msg3); // Display RPS on LCD } //-------------- // Exit main CloseTimer0(); return (EXIT_SUCCESS); }
/* *------------------------------------------------------------------------------ * void TMR0_init(void) * Summary : Initialize timer0 for schedular base * * Input : None * * Output : None * *------------------------------------------------------------------------------ */ void TMR0_init(UINT16 reload , void (*func)(void)) { // Enable the TMR0 interrupt,16bit counter // with internal clock,No prescalar. OpenTimer0(TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT & T0_PS_1_1); // Reload value for 5ms overflow WriteTimer0(reload); tmr[0].reload = reload; tmr[0].func = func; }
void interrupt low_isr(void){ /* Check if overflow interrupt for Timer 0 occures */ if(INTCONbits.TMR0IF == 1){ /* Cyclic change state from ON to OFF and so on */ LED_2 = ~LED_2; /* Clear timer interrupt flage - it's necessary */ INTCONbits.TMR0IF = 0; /* Upload default value to timer */ WriteTimer0(TIMER0_DEF_VALUE); } }
void startTimer0() { // Setup the timer with a 1:1 prescaler with 16 bits resolution // Therefore the timer0 freq is 500 kHz / 4 / 4 = 31.250 kHz OpenTimer0( 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 WriteTimer0(0); INTCONbits.TMR0IF = 0; }
void interrupt ISR(void) { if(INTCONbits.TMR0IF==1) { // if Timer0 Interrupt is occurred INTCONbits.TMR0IF = 0; // Clear the interrupt flag WriteTimer0(TMR0_value); // Update TMR0 value if(PORTBbits.RB0 == 0) { // if KEY1 is pressed while(PORTBbits.RB0 == 0); // Trap until KEY1 is released flag = 1; // Change flag to toogle LED1 } } }
void InterruptHandlerHigh () { //Every 0.026 secs if (INTCONbits.TMR0IF) { //check for TMR0 overflow INTCONbits.TMR0IF = 0; //clear interrupt flag WriteTimer0(0); //Code used by the interrupt goes here } printf("In interrupt handler \r\n"); }
void timer0_int_handler() { // Encoder count for motor 0. timer0Counter++; // Reset the timer WriteTimer0(0xCF); DEBUG_ON(TMR0_DBG); DEBUG_OFF(TMR0_DBG); if (ticks_flag) { // Reverse counter to prevent it from going a set distance. ticks0Counter++; // Going thru right hand turn if (ticks0Counter >= maxTickZero && postRight == 1) { postRight = 2; WriteUSART(0x00); // 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 (ticks0Counter >= maxTickZero && 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 (ticks0Counter >= maxTickZero && 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 (ticks0Counter >= maxTickZero) { //Stops wheels after set number of ticks WriteUSART(0x00); } } }
void InterruptHandlerLow (void) { unsigned int t0; if(INTCONbits.TMR0IF) //check for TMR0 overflow interrupt flag { INTCONbits.TMR0IF = 0; //clear interrupt flag t0=ReadTimer0(); WriteTimer0(t0 +18661); //Nastavi na 1.0 Sek UTicTac++; // Povecaj idle števec LATAbits.LATA3 = !LATAbits.LATA3; } }
void timer0_int_handler() { unsigned int val; int length, msgtype; // toggle an LED LATBbits.LATB0 = !LATBbits.LATB0; // reset the timer WriteTimer0(0); // try to receive a message and, if we get one, echo it back length = FromMainHigh_recvmsg(sizeof(val), (unsigned char *)&msgtype, (void *) &val); if (length == sizeof (val)) { ToMainHigh_sendmsg(sizeof (val), MSGT_TIMER0, (void *) &val); } }
void timer0_handler (void) { INTCONbits.TMR0IF = 0; WriteTimer0( TIMER_START_SEC ); if(LED==1) LED = 0; else LED=1; transmit = 1; /* if(ledr==1){ LED = 0; ledr=0; } else { LED=1; ledr=1; }*/ }
void tratar_high_interrupt(void) { // Verifica se o motivo da chamada da interrupção foi o estouro(overflow) do Timer0 if(INTCONbits.TMR0IF) { //Re-carrega valor inicial do contador do Timer0 WriteTimer0(INICIO_TMR0); if(barra_up_debounce == 2) barra_up_debounce = 1; //Limpa flag da interrupção do Timer0 INTCONbits.TMR0IF = 0; } }