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; }
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 Robot_track_end(void) { WriteTimer0(0); while((ReadTimer0()<40000) && (SeeLine.B==0)) { set_motor_speed(left, medium, 0); set_motor_speed(right, medium, 0); } if(ReadTimer0()>40000 ) { while(SeeLine.B==0) { set_motor_speed(left, rev_slow, 0); set_motor_speed(right, rev_slow, 0); } } }
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 setupGame() { game.myScore = HEALTH; game.oppScore = HEALTH; game.turn = 0; game.gameOver = 0; game.monSel = 0; game.moveSel = 0; game.myMove = NULL; game.oppMove = NULL; game.myMonster = NULL; game.oppMonster = NULL; srand(ReadTimer0()); }
void main (void) { int result; usart_init(); set_timer(); printf("Start \r\n"); WriteTimer0(0); while(1){ printf("In loop \r\n"); result = ReadTimer0(); // read timer printf("Timer is %u \r\n", result); //interrupt will run now } }
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 high_isr(void) { time = ReadTimer0(); if(INTCONbits.INT0IE && INTCONbits.INT0IF) { INTCONbits.INT0IF = 0; check_sonar(0, INTCON2bits.INTEDG0); INTCON2bits.INTEDG0 ^= 1; // On écoutera l'autre sens. } if(INTCON3bits.INT1IE && INTCON3bits.INT1IF) { INTCON3bits.INT1IF = 0; check_sonar(1, INTCON2bits.INTEDG1); INTCON2bits.INTEDG1 ^= 1; // On écoutera l'autre sens. } }
int LV_MAXSONAR_measure(void) { //timer vars unsigned int result; float temp_result; while(1) { while(SONIC_SENSOR_PW_PIN == 0); // configure timer0 OpenTimer0(TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1); WriteTimer0( 0 ); // restart timer while(PORTBbits.RB3 == 1); result = ReadTimer0(); // read timer temp_result = (float)result / (float)(TIMER_SCALE_FACTOR*147); break; } result = (int)temp_result; return result; }
void isr_high( void ) { fsk_tmr_value = ReadTimer0(); WriteTimer0(0); if ( ( fsk_tmr_value >= BIT_0_75 ) && ( fsk_tmr_value < BIT_1_25 ) ) { if ( ( RisingPosition & 1) == MIDDLE ) { WriteData ( ( ( DataWriteIndex++ ) & 0x07 ), 0 ); } else { WriteData( ( (DataWriteIndex++ ) & 0x07 ), 1 ); } } else if( ( fsk_tmr_value >= BIT_1_25 ) && ( fsk_tmr_value < BIT_1_75 ) ) { if ( ( RisingPosition & 1 ) == MIDDLE ) { WriteData( ( ( DataWriteIndex++ ) & 0x07 ), 0 ); WriteData( ( ( DataWriteIndex++ ) & 0x07 ), 1 ); } else { WriteData( ( DataWriteIndex++ ) & 0x07 , 1 ); } RisingPosition++; // change } else if ( ( fsk_tmr_value >= BIT_1_75 ) && ( fsk_tmr_value < BIT_2_25 ) ){ if ( ( RisingPosition & 1) == MIDDLE ) { WriteData( ( ( DataWriteIndex++ ) & 0x07 ), 0 ); WriteData( ( ( DataWriteIndex++ ) & 0x07 ), 1 ); } else { InitFSKData(); } RisingPosition = MIDDLE; } else { InitFSKData(); } /************* Analyze ****************/ switch ( RxState ) { case HEADER: while ( DataReadIndex != DataWriteIndex ) { temp = ReadData( ( ( DataReadIndex++ ) & 0x07 ) ); if ( temp ) { HeaderCounter++; } else { HeaderCounter = 0; } if ( HeaderCounter >= 9 ) { ParityCounter = 0; HeaderCounter = 0; DataBitCounter = 0; EmDataIndex = 0; RxState = DATA; break; } } // while break; case DATA: while ( DataReadIndex != DataWriteIndex ) { temp = ReadData( ( ( DataReadIndex++ ) & 0x07 ) ); switch ( DataBitCounter ) { case 4: if ( (temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 9: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 14: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 19: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 24: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 29: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 34: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 39: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 44: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 49: if ( ( temp & 1 ) != ( ParityCounter & 1 ) ) InitFSKData(); ParityCounter = 0; break; case 54: if ( ( temp & 1 ) == 0) { RotateLeftEmData(); RotateLeftEmData(); RotateLeftEmData(); RotateLeftEmData(); EmDataFlags.EmDataReceived = 1; } InitFSKData(); break; default: if ( ( temp & 1 ) ) ParityCounter++; RotateLeftEmData(); if ( ( temp & 1 ) == 1 ) { EmData[5] |= 0x01; } else { EmData[5] &= 0xFE; } break; } // switch databit counter DataBitCounter++; } // case DATA break; } INTCONbits.INT0IF = 0; return; }