Пример #1
0
//
// 	Calculate Echo Pulse width (
//	return: centimeter (CM)
//
uint32_t HCSR04::distance(uint32_t timeout) {
	CTick tick;
	CTimeout to;
	to.reset();

	// trigger
	m_trig = HIGH;
	tick.delay(10); // 10us
	m_trig = LOW;

	// wait echo LOW->HIGH
	while (m_echo != HIGH) {
		if (to.isExpired(timeout))
			return 0;
	}

	// wait echo HIGH->LOW
	tick.reset();
	while (m_echo == HIGH) {
		if (to.isExpired(timeout))
			return 0;
	}

	return (tick.elapsed() * 3432 / 20000); // unit (millimeter)
}
Пример #2
0
uint32_t pulseIn(int pin, PIN_LEVEL_T value, uint32_t timeout) {
	CPin p(unoPIN[pin]);
	p.input();
	CTick    tick;
	CTimeout to;

	to.reset();
	while( p!=value ) {
		if ( timeout && to.isExpired(timeout) ) return 0;
	}
	tick.reset();

	while( p==value ) {
		if ( timeout && to.isExpired(timeout) ) return 0;
	}
	return tick.elapsed();
}
Пример #3
0
//
// Main Routine
//
int main(void) {
#ifdef DEBUG
	CSerial ser;		// declare a UART object
	ser.enable();
	CDebug dbg(ser);	// Debug stream use the UART object
	dbg.start();
#endif

	//
	// PWM (Using Timer1)
	//
	hwPWM pwm1(TIMER_1, 5, 6, 7);	// set pwm1 pins on P0.5 (CH1), P0.6 (CH2) and P0.7 (CH3)
	pwm1.period(0.0002);			// period time = 200us
	pwm1.enable();					// enable PWM module

	// update pwm2 channels duty-cycle (can be updated in any-time)
	pwm1.dutycycle(PWM_CH_1, 0.8f);	// CH1 duty-cycle = 80%
	pwm1.dutycycle(PWM_CH_2, 0.6f);	// CH2 duty-cycle = 60%
	pwm1.dutycycle(PWM_CH_3, 0.2f);	// CH3 duty-cycle = 20%

	//
	// PWM (Using Timer2)
	//
	hwPWM pwm2(TIMER_2, LED_PIN_1, LED_PIN_2);		// set pwm2 pins on LED1 (CH1) and LED2
	pwm2.period(0.0005);			// period time = 500us
	pwm2.enable();					// enable PWM module

	// update pwm2 channels duty-cycle (can be updated in any-time)
	pwm2.dutycycle(PWM_CH_1, 0.8f);	// CH1 duty-cycle = 80%
	pwm2.dutycycle(PWM_CH_2, 0.1f);	// CH2 duty-cycle = 10%

	//
	// LED
	//
	CPin led(LED_PIN_0);
	led.output();

	CTimeout tmLED;
	//
    // Enter main loop.
	//
    while(1) {
    	//
    	// FireFly loop
    	//
    	if ( tmLED.isExpired(500) ) {
    		tmLED.reset();
    		led.toggle();
    	}

    }
}
Пример #4
0
//
// Main Routine
//
int main(void) {
#ifdef DEBUG
	CSerial ser;		// declare a UART object
	ser.enable();
	CDebug dbg(ser);	// Debug stream use the UART object
	dbg.start();
#endif

	//
	// Optional: Enable tickless technology
	//
#ifndef DEBUG
	CPowerSave::tickless(true);
#endif

	//
	// Your setup code here
	//
	CButton btn(BUTTON_PIN_0);

	CBuzzer buz(15);	// buzzer on P0.15
	buz.enable();

	CPin led(LED_PIN_0);
	led.output();

	CTimeout tmLED;

	//
    // Enter main loop.
	//
    while(1) {
    	//
    	// Your loop code here
    	//
    	switch(btn.isPressed()) {
    	case BTN_PRESSED:
    		buz.post(3);	// turn on buzzer x 3
    		break;
    	case BTN_RELEASED:
    		break;
    	case BTN_NOTHING:
    		break;
    	}

    	if ( tmLED.isExpired(500) ) {
    		tmLED.reset();
    		led.toggle();
    	}
    }
}
Пример #5
0
void CButton::run() {
	CTimeout bounce;
	while(1) {
		if ( bit_chk(m_flag, KEY_FLAG_DOWN) ) {
			if ( m_pin==HIGH ){
				if ( bounce.isExpired(10)  ) {
					bit_clr(m_flag, KEY_FLAG_DOWN);
					onKeyUp();		// call virtual function "onKeyUp()"
				}
			} else {
				bounce.reset();
			}
		} else {
			if ( m_pin==LOW ){
				if ( bounce.isExpired(5)  ) {
					bit_set(m_flag, KEY_FLAG_DOWN);
					onKeyDown();	// call virtual function "onKeyDown()"
				}
			} else {
				bounce.reset();
			}
		}
	}
}
Пример #6
0
void CButtons::run() {
	CTimeout tm;
	uint32_t newval;
	while(1) {
		newval = m_pins;
		if ( newval != m_flag ) {
			if ( tm.isExpired(10) ) {						// wait for bounce time
				m_down = newval;
				for (int i=0; i<m_pins.count(); i++) {
					if ( bit_chk((m_flag ^ newval), i)  ) {	// is different? Key Even caused...
						if ( bit_chk(newval, i) ) { 		// HIGH is key up
							onKeyUp(i);
						} else {							// LOW is key down
							onKeyDown(i);
						}
					}
				}
				m_flag = newval;
			}
		} else {
			tm.reset();
		}
	}
}