예제 #1
0
unsigned char check_store_button(void) 
{
	// check store button:
	cbi(PORTB,PB2);
	kbd_wait();
	if (bit_is_clear(PINB,PINB3)){
		return(1);
	}
	return(0);
}
예제 #2
0
uint8_t check_store_button(void) 
{
	// check store button:
	PORTB&=~(1<<PORTB2); // to gnd
	kbd_wait();
	if (bit_is_clear(PINB,PINB3)){
		return(1);
	}
	return(0);
}
예제 #3
0
void HardwareSerial::flush()
{
  // If we have never written a byte, no need to flush. This special
  // case is needed since there is no way to force the TXC (transmit
  // complete) bit to 1 during initialization
  if (!_written)
    return;

  while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) {
    if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0))
	// Interrupts are globally disabled, but the DR empty
	// interrupt should be enabled, so poll the DR empty flag to
	// prevent deadlock
	if (bit_is_set(*_ucsra, UDRE0))
	  _tx_udr_empty_irq();
  }
  // If we get here, nothing is queued anymore (DRIE is disabled) and
  // the hardware finished tranmission (TXC is set).
}
void getstartbit(void)
{
	unsigned char count;
	TCNT0 = 0;
	while(bit_is_clear(PIND,2));
	while(!bit_is_clear(PIND,2));
	count=TCNT0;
	if(count>25)
	{
		a=1;
		
	}
	else
	{
	a=0;
	}
	
	
}
예제 #5
0
void motor_reset() {

    /* Begin with resetting axis Z if no reset is already in progress. It is
    * imperative to first retract on axis Z separately from the others. Once
    * that has been dealt with, axes X and Y may follow. */
    if(bit_is_clear(motor_status, MTR_RESET)) {
        motor_stop();
        motor_status   |=  _BV(MTR_RESET) | _BV(MTR_IS_Z);
        setup_axis(AXIS_Z, MTR_INC);
        LOCK_DISABLE();
        motor_start();

    /* Axis Z has been reset. Now, axes X and Y may follow. */
    } else if(bit_is_set(motor_status, MTR_RESET_Z_DONE)) {
        /* Remove flag denoting axis Z is resetting. */
        motor_status   &= ~(_BV(MTR_IS_Z) | _BV(MTR_RESET_Z_DONE));

        /* Reset axes X and Y. */
        setup_axis(AXIS_Y, MTR_DEC);
        setup_axis(AXIS_X, MTR_DEC);
        LOCK_DISABLE();     /* Manually enable PWM propagation. */
        motor_start();

    /* All resetting stages have been completed. Reset #cur_pos and flags. */
    } else if(bit_is_set(motor_status, MTR_RESET_X_DONE)
           && bit_is_set(motor_status, MTR_RESET_Y_DONE)) {

        cur_pos.x       =  0;
        cur_pos.y       =  0;
        cur_pos.z       =  max_pos.z - 1;
        motor_stop();

        LOCK_ENABLE();

        /* If this resetting cycle was initiated as a response to a limit being
        * engaged while under normal motor operation, retry reaching #new_pos
        * anew. */
        if(bit_is_set(motor_status, MTR_LIMIT)) {
            if(motor_update()) {
                motor_stop();
                MTR_CALL(cur_pos, MTR_EVT_OK);
            }
        } else {
            new_pos     =  cur_pos;
            MTR_CALL(cur_pos, MTR_EVT_OK);
        }

        motor_status   &= ~(_BV(MTR_RESET) | _BV(MTR_LIMIT)
                          | _BV(MTR_RESET_X_DONE) | _BV(MTR_RESET_Y_DONE));
        motor_status   |=  _BV(MTR_IS_RST_FRESH);

    /* Reset is in progress. */
    } else {
    }
}
예제 #6
0
void adcReadRun(void)
{
  uint8_t low, high;
  static uint32_t oldTime;

  switch (a2dState)
  {
    case a2d_idle:
      if (itIsTimeToStartScanning())
      {
        a2dPin = 0;
        a2dState = a2d_SetMux;
      }
      break;

    case a2d_SetMux:
      if (pinToRead[a2dPin])
      {
        ADMUX = (DEFAULT << 6) | (a2dPin & 0x07);
        oldTime = micros();
        a2dState = a2d_InputSettleWait;
      }
      else
      {
        a2dPin++;
      }
      break;

    case a2d_InputSettleWait:
      if (timeDiff(micros(), oldTime) >= 500)
      {
        // start the conversion
        sbi(ADCSRA, ADSC);
        a2dState = a2d_AwaitConversionComplete;
      }
      break;

    case a2d_AwaitConversionComplete:
      if (bit_is_clear(ADCSRA, ADSC))
      {
        low  = ADCL;
        high = ADCH;
        adcValue[a2dPin] = (high << 8) | low;
        a2dPin++;
        a2dState = a2d_SetMux;
      }
      break;
  }

  if (a2dPin >= MAX_CHANNELS)
  {
    a2dState = a2d_idle;
  }

}
예제 #7
0
void keyscanner_main(void)
{
    /* TODO: low power mode:
     *   When all keys reported up:
     *     DDR_PP = 0x11; PORT_PP = 0x00;
     *     Guarantee wake on TWI / any PORT_OD pin FALLING
     *     Sleep
     */
    for (uint8_t pp = 0; pp < 8; ++pp) {
        uint8_t pp_bitmask = _BV(pp);

        _delay_ms(0.5);

        DDR_PP = 0x00 ^ pp_bitmask;
        PORT_PP = 0xFF ^ pp_bitmask;

        _delay_ms(0.5);

        uint8_t od_bits = PIN_OD;
        /*
         * Rollover conditions exist if:
         *  * Multiple OD pins are pulled low AND
         *  * Multiple PP pins are pulled low
         */
        uint8_t nPp = popCount(~PIN_PP);
        uint8_t nOd = popCount(~od_bits);
        // Most of the time the keyboard will not be a rollover state
        if (__builtin_expect(nPp > 1 && nOd > 1, 0)) {
            continue;
        }

        // Debounce key state
        uint8_t changes = debounce(od_bits, db + pp);
        // Most of the time there will be no new key events
        if (__builtin_expect(changes == 0, 1)) {
            continue;
        }

        DISABLE_INTERRUPTS({
            key_t key;
            key.dataNumber = 0; // Set by I²C code (ringbuf.count != 0)
            key.pp = pp;

            for (int8_t od = 0; od < 8; od++) {
                // Fewer than half the keys are expected to be down for each scanline
                if (__builtin_expect(bit_is_set(changes, od), 0)) {
                    key.keyState = bit_is_clear(db[pp].state, od);
                    key.od = od;
                    ringbuf_append(key.val);
                }
            }

            SET_INT(0);
        });
    }
static bool rx_char(uint8_t *c){
#if defined(UDR0)
  if (bit_is_clear(UCSR0A, UPE0)) {
    *c = UDR0;
    return true;
  } else {
    *c = UDR0;
    return false;
  };
#elif defined(UDR)
  if (bit_is_clear(UCSRA, PE)) {
    *c = UDR;
    return true;
  } else {
    *c = UDR;
    return false;
  };
#else
  #error UDR not defined
#endif
}
예제 #9
0
void capture_wave (int16_t *buffer, uint16_t count)
{
	ADMUX = _BV(REFS0)|_BV(ADLAR)|_BV(MUX2)|_BV(MUX1)|_BV(MUX0);	// channel

	do {
		ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADFR)|_BV(ADIF)|_BV(ADPS2)|_BV(ADPS1);
		while(bit_is_clear(ADCSRA, ADIF));
		*buffer++ = ADC - 32768;
	} while(--count);

	ADCSRA = 0;
}
예제 #10
0
파일: lab2_skel.c 프로젝트: Lars139/ECE473
//******************************************************************************
//                            chk_buttons                                      
//Checks the state of the button number passed to it. It shifts in ones till   
//the button is pushed. Function returns a 1 only once per debounced button    
//push so a debounce and toggle function can be implemented at the same time.  
//Adapted to check all buttons from Ganssel's "Guide to Debouncing"            
//Expects active low pushbuttons on PINA port.  Debounce time is determined by 
//external loop delay times 12. 
//
uint8_t chk_buttons(uint8_t button) {
	//******************************************************************************
//	static uint16_t state[8] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; //holds present state
//	TODO: I swear the array was the problem. Not sure why...
//	TODO: and the ! in front of the bit_is_clear
	static uint16_t state = 0; //holds present state
//	state[button] = (state[button] << 1) | (! bit_is_clear(PINA, button)) | 0xE000;
	state = (state << 1) | ( bit_is_clear(PINA, button)) | 0xE000;
	if (state == 0xF000) return 1;
	return 0;

}
예제 #11
0
파일: main.c 프로젝트: gokulvanan/Learning
int main(void)
{
	DDRB = 0b01111111; // portB all registers are output except for PINB7
	DDRD = 0b01111111; // portD all registers are output except for PIND7
	PORTB = 0b10000000; // keep PINB7 as high
	PORTD = 0b10000000; /// keep PIND7 as high
	
	while(1)
	{
		if(bit_is_clear(PINB,7)){
			processPressedButton(0);
		}else{
			processReleasedButton(0);
		}
		if(bit_is_clear(PIND,7)){
			processPressedButton(1);
		}else{
			processReleasedButton(1);
		}
	}
}
예제 #12
0
void    ReadKn(void)
    {
    unsigned    char R0;
    R0=0;

    if(bit_is_clear(PINA,PA3))
    R0=R0 | 1;

    if(bit_is_clear(PINA,PA4))
    R0=R0 | 2;

    if(bit_is_clear(PINA,PA5))
    R0=R0 | 4;

    if(R0==RegSTemp)
		{

    RegS=R0;
		}
    RegSTemp=R0;
    }
//************** MAIN PROGRAM ********************
int main(void)
{
	//char tbl[8]={'s','F','S','B','s','L','S','R'};
//	int m=0;
	
	DDRB=0xC7;						//SET DATA DIRECTION REGISTER
	DDRD=0xF1;						//SET DATA DIRECTION REGISTER
	
	sbi(PORTD,2);					//ENABLE PULL UP FOR SWITCH INT0
	//sbi(PORTD,3);					//ENABLE PULL UP FOR SWITCH INT1
    GICR = _BV(INT0);           	// enable external int0
    MCUCR = _BV(ISC01);          	// falling egde: int0*/
    sei();                       	// enable interrupts 
									
    TCCR1B = TMC16_CK1024;       		// use CLK/1024 prescale value
    //TCNT0  = 0x00;        			// reset TCNT0
    TIMSK  = _BV(TOIE1);        	// enable TCNT0 overflow 
 
	navflag = 'S';
	_delay_ms(1000);				//DELAY IN MILLISECONDS 
	robotmove('F');
	TCNT1=TIMER_1_CNT;
	while(1)						//INFINITE LOOP
	{
		if(navflag=='O')
		{
			if (bit_is_clear(PIND,2))	//IF Sensor1 senses obstacle
			{
				robotmove('l');
				cli();                       	// disable interrupts 
				_delay_ms(500);
				sei();                       	// enable interrupts 
				        			
				//count = 0;
				//navflag='S';
				TCNT1 = TIMER_1_CNT;			// reset TCNT0
			}
			else
			{
				//if(move=='F')
				
					cbi(PORTB,2);
					robotmove('F');
			}
		}
		
//		m++;
//		m=m & 7;
			
	}
	return(0);
}
void obstacle(void)
{
            direction_motor(4);
			_delay_ms(800);
			
			direction_motor(7);
			_delay_ms(4000);
			if(bit_is_clear(PINC,0))
			{
				obstacleL();
			}
			navflag='O';
}
예제 #15
0
int main() {
  DDRA = ALL_INPUT;
  PORTA = ALL_GND_PULL_OFF;
  DDRB = ALL_INPUT;
  PORTB = ALL_POS_PULL_ON;

  // set prescaler to 1024
  TCCR1B = _BV(CS12) | _BV(CS10);
  
  while (1) {
    for (uint8_t n = 0; n < NUM_ANIMATIONS; n++) {
      uint8_t next_animation = 0;
      uint16_t frame_count = 0;
      memcpy_P(&frame_count, &ANIMATIONS[n]->num_frames, sizeof(frame_count));
      while (1) {
        for (uint16_t f = 0; f < frame_count; f++) {
          anim_frame cur_frame;
          memcpy_P(&cur_frame, &ANIMATIONS[n]->frames[f], sizeof(cur_frame));
          for (TCNT1 = 0; TCNT1 < cur_frame.dur; /*nothing*/) {
            for (uint8_t i = 0; i < 27; i++) {
              uint8_t tmp_port = ALL_GND_PULL_OFF, tmp_ddr = ALL_INPUT;
              if (cur_frame.leds[i]) {
                turn_led_on(&tmp_ddr, &tmp_port, i);
              }
              DDRA = tmp_ddr;
              PORTA = tmp_port;
            }
            DDRA = ALL_INPUT;
            PORTA = ALL_POS_PULL_ON;
            DDRB = ALL_INPUT;
            PORTB = ALL_POS_PULL_ON;

            // TODO: make sure the button comes up
            // to make this less time-dependant
            if (bit_is_clear(MODE_PIN, MODE_BIT)) {
              next_animation = 1;
              _delay_ms(1000);
              break;
            }
          }
          if (next_animation) break;
        }
        if (next_animation) break;
      }
    }
  }
  DDRA = ALL_INPUT;
  PORTA = ALL_GND_PULL_OFF;
  return 0;
}
예제 #16
0
/*
 * Block current thread until work is available for it.
 */
void
taskset_thread_wait4start( taskset_t *ts, int thr_idx )
{
    hb_lock( ts->task_cond_lock );
    while ( bit_is_clear( ts->task_begin_bitmap, thr_idx ) )
        hb_cond_wait( ts->task_begin, ts->task_cond_lock );

    /*
     * We've been released for one run.  Insure we block the next
     * time through the loop.
     */
    bit_clear( ts->task_begin_bitmap, thr_idx );
    hb_unlock( ts->task_cond_lock );
}
예제 #17
0
uint8_t tw_start(void)
{
	RELEASE_SDA();
	SCL_1();
	_delay_loop_1(TWI_SW_DELAY);
	if(bit_is_clear(TWI_PIN_SDA,bSDA)) {
		return RC_FAIL;
	}
	SDA_0(); 
	_delay_loop_1(TWI_SW_DELAY);
	SCL_0(); 
	_delay_loop_1(TWI_SW_DELAY);
	return RC_SUCCESS;
}
void detect_control(void)
{
	TCNT0=0;
	while(TCNT0<30);
	if(bit_is_clear(PIND,2))
	{
		sbi(PORTB,2);
		
	}
	else
	{
		cbi(PORTB,2);
	}
}
예제 #19
0
uint16_t ReadAD(uint8_t chan){


    if(curr_chnl == chan){

        while(bit_is_clear(ADCSRA,ADIF));

        curr_chnl = NO_CH;

        return ADC;
    }
    else return BAD_CH;

}
void path_follower(void)
{
	
	if((bit_is_clear(PINC,2)) && (bit_is_clear(PINC,3)))
	{
		sbi(PORTD,0);
		cbi(PORTD,1);	//move left
		//robotmove('L');
		sbi(PORTD,7);
		cbi(PORTB,0);
	}
	if((bit_is_clear(PINB,1)) && (bit_is_clear(PINC,5)))
	{
		sbi(PORTD,1);
		cbi(PORTD,0);	//move right
		//robotmove('R');
		sbi(PORTB,0);
		cbi(PORTD,7);
	}
	if((bit_is_clear(PINC,3)) && (bit_is_clear(PINC,4)) && (bit_is_clear(PINC,5)))
	{
		sbi(PORTD,0);
		sbi(PORTD,1);	//move forward
		//robotmove('F');
		sbi(PORTD,7);
		sbi(PORTB,0);
	}
	if((!bit_is_clear(PINC,2)) && (!bit_is_clear(PINC,3)) && (!bit_is_clear(PINC,4)) && (!bit_is_clear(PINC,5)) && (!bit_is_clear(PINB,1)))
	{
		cbi(PORTD,0);
		cbi(PORTD,1);	//stop
		//robotmove('S');
		cbi(PORTD,7);
		cbi(PORTB,0);
	}
	
}
예제 #21
0
void Settings1()
{
	displayMode = SETTINGS1;
	displayValue = (uint16_t)(savedParametersList[HIGH_LIM] * 100);
	
	uint8_t pressCounter = 0;
	
	while(bit_is_set(PINC, MODE_BUTTON))
	{}
	
	_delay_ms(100);
	
	while(bit_is_clear(PINC, MODE_BUTTON))
	{
		if (bit_is_set(PINC, SET_BUTTON))
		{
			pressCounter++;
			if(savedParametersList[HIGH_LIM] < 0.99)
				savedParametersList[HIGH_LIM] += 0.01;
			else
				savedParametersList[HIGH_LIM] = 0.00;
		}
		else
		{
			pressCounter = 0;
		}
		
		if (pressCounter < 10)
		{
			_delay_ms(500);
		} 
		else
		{
			_delay_ms(100);
		}
		
		displayValue = (uint16_t)(savedParametersList[HIGH_LIM] * 100);
	}
		
	if (eeprom_read_float((float*)(HIGH_LIM * 4)) != savedParametersList[HIGH_LIM])
	{
		eeprom_write_float((float*)(HIGH_LIM * 4), savedParametersList[HIGH_LIM]);
	}
		
	Settings2();
	
	while(bit_is_set(PINC, MODE_BUTTON))
	{}
}
예제 #22
0
int main(void)
{
	// DONE: Attach LED on pin0 and pin1 of port B
	DDRB |= (1 << PINB0) | (1 << PINB1);		// Set the direction on these pin as output
	DDRB &= ~(1 << PINB2);						// Set the direction on this pin as input
	
	PORTB |= (1 << PINB0);						// Set the default state of pinb0 to high
	PORTB &= ~(1 << PINB1);						// and the default state of pinb1 to low
	
	// DONE: Attach the button on Pin 2 of Port B
	PORTB |= (1 << PINB2);						// Set the default state of pin b2 to high
	
	uint8_t Pressed = 0;						// Declare the current state of the button 0 means that
	// it is not pressed
	int intPressedConfidenceLevel = 0;
	int intReleasedConfidenceLevel = 0;
	
    while(1)
    {
        //DONE: When the button is pressed and release switch the LED which is turned on
        if (bit_is_clear(PINB, PINB2))  // Check if the button is pressed
        {
			intPressedConfidenceLevel++;
	        // check if already not pressed
			if (intPressedConfidenceLevel > 200)
			{
				if (Pressed == 0)
				{
					// If pressed is 0 then blink the led connected to pin b0
					PORTB ^= (1 << PINB0);
					PORTB ^= (1 << PINB1);
					// if pressed and released then blinked the led connected to pin b1
					Pressed = 1;
				}
				intPressedConfidenceLevel = 0;
			}
        }
        else
        {
			intReleasedConfidenceLevel++;
			if (intReleasedConfidenceLevel > 200)
			{
				// When button is not pressed
				Pressed = 0;
				intReleasedConfidenceLevel = 0;
			}
        }
    }
}
예제 #23
0
Button::Button(void)
{
	set_sleep_mode(SLEEP_MODE_PWR_DOWN);

	//pull ups for buttons
	BUTTON_MAIN_PORT |= 1<<BUTTON_MAIN_PINX;
	BUTTON_DOWN_PORT |= 1<<BUTTON_DOWN_PINX;
	BUTTON_UP_PORT |= 1<<BUTTON_UP_PINX;
	
	uint8_t d = LONG_TIME / 100;
	while ((bit_is_clear(BUTTON_MAIN_PIN, BUTTON_MAIN_PINX)) && d)
	{
		#ifdef USE_RFM_CLOCK
			_delay_ms(100 * 1000000UL / F_CPU); //CPU runs with 1 MHz here (RFM12 not yet initialized)
		#else
			_delay_ms(100); //CPU runs with internal osc
		#endif
		d--;
	}
	
	if ((d < (LONG_TIME / 100)) && d)
		Shutdown(false); //button was released before LONG_TIME, or button was not pushed at all, i.e. power via USB
		
	//check battery voltage 
	rfm12.Trans(0x820C);
	rfm12.Trans(0xC000 | VBAT_MIN_VAL);
	_delay_ms(50);
	if (rfm12.Trans(0x0000) & 0x0400)
		Shutdown(0); //battery emtpy
	
	//switch on (PWEN)
	//PWEN_DDR |= 1<<PWEN_PINX;
	PWEN_PORT |= 1<<PWEN_PINX;
	
	msg = 0;
	firstrun = true;
	counter = 1;
	
	//Pin change interrupt for main button
	PCICR = 1<<PCIE2;
	PCMSK2 = 1<<PCINT23 | 1<<PCINT19;
	PCMSK0 |= 1<<PCINT7;

	state = 0xF8;
	state |= (BUTTON_MAIN_PIN & 1<<BUTTON_MAIN_PINX) >> BUTTON_MAIN_SHIFT;
	state |= (BUTTON_DOWN_PIN & 1<<BUTTON_DOWN_PINX) >> BUTTON_DOWN_SHIFT;
	state |= (BUTTON_UP_PIN & 1<<BUTTON_UP_PINX) >> BUTTON_UP_SHIFT;
	timer.SetTime(DEBOUNCE_TIME);
}
예제 #24
0
파일: gpio.c 프로젝트: haoyangyu/RIOT
int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup)
{
    int res;

    if (dir == GPIO_DIR_OUT) {
        _SFR_MEM8(_ddr_addr(pin)) |= (1 << _pin_num(pin));
        res = bit_is_set(_SFR_MEM8(_ddr_addr(pin)), _pin_num(pin));
    }
    else {
        _SFR_MEM8(_ddr_addr(pin)) &= ~(1 << _pin_num(pin));
        res = bit_is_clear(_SFR_MEM8(_ddr_addr(pin)), _pin_num(pin));
    }

    return (res == 0) ? -1 : 0;
}
예제 #25
0
void    ReadKn(void)
{


	unsigned    char R0;

	R0=RegSTemp;//Temp;
	if(bit_is_clear(PINB,PB2))
	R0 |=0x1;
	else
	R0 &=0xfe;
	if(bit_is_clear(PINB,PB3))
	R0 |=0x2;
	else
	R0 &=0xfd;
	if(bit_is_clear(PINB,PB4))
	R0 |=0x4;
	else
	R0 &=0xfb;

	if(R0==RegSTemp)
	RegS=R0;
	RegSTemp=R0;	
}
예제 #26
0
int ilosc_petli()
{
    int wait = TAK;
    int ile_petli = 0;
    char ch_liczba[3]; //przechowywana jest liczba int w formie stringu

    wyswietl_LCD("ilosc petli:");
    _delay_ms(200);


    wait = TAK;
    do
    {
        if(bit_is_clear(PIN_SWITCH, SWITCH_UP))
        {
            ile_petli += 5;
            gen_char(ch_liczba, &ile_petli);
            wyswietl_LCD(ch_liczba);
        }
        else if(bit_is_clear(PIN_SWITCH, SWITCH_DOWN))
        {
            ile_petli -= 5;
            gen_char(ch_liczba, &ile_petli);
            wyswietl_LCD(ch_liczba);
        }
        else if(bit_is_clear(PIN_SWITCH, SWITCH_OK))
        {
            wait = NIE;
        }
        _delay_ms(200);
    }
    while(wait == TAK);


    return ile_petli;
}
예제 #27
0
파일: portIn.c 프로젝트: nagi/geekfit
void getInput()
{
    static uchar xx, yy, buttons;
    //read joystick
    if (bit_is_clear(JOY_IN,JUP))
    {
        yy = 0;
    }
    else if (bit_is_clear(JOY_IN,JDOWN))
    {
        yy = 2;
    }
    else
    {
        yy = 1;
    }

    if (bit_is_clear(JOY_IN,JLEFT))
    {
        xx = 0;
    }
    else if (bit_is_clear(JOY_IN,JRIGHT))
    {
        xx = 2;
    }
    else
    {
        xx = 1;
    }
    //read buttons
    buttons = ~(BTN_IN | 0b11000000);
    // file report
    reportBuffer[0] = xx;
    reportBuffer[1] = yy;
    reportBuffer[2] = buttons;
}
예제 #28
0
파일: Lab_3.c 프로젝트: Lars139/Archive
/**
 * Plays a sequence of notes when the user taps the piano.
 * 
 * @param sequence pointer to the start of a char array which represents
 * the sequence of a song.
 * @param length length of the sequence
 * 
 */
void playSequence (char * sequence, length) {
	
	while (bit_is_clear(PINB, 1)) //while the button is pushed, remain in "zelda mode"
	{
		cli(); // disable interrupts
		while(ADCH < 10) // while the piano is not touched, dont play anything
			_delay_ms(20);
		sei(); //turn interrupts back on once the piano is pushed again
		OCR1A = sequence[i]; //cycle to next note in the song
		while(ADCH >= 10) // while piano is pushed, keep playing the current note
			_delay_ms(20);
		i++; //move index to next note
		i = i % length; //keep index value in the bounds of our song array
	}
	i = 0; //reset index after "zelda mode" button is released
}
예제 #29
0
int main(void) {
  char byte;
  uint16_t timerValue;

  // -------- Inits --------- //

  initUSART();
  initTimer1();
  LED_DDR = 0xff;                               /* all LEDs for output */
  BUTTON_PORT |= (1 << BUTTON);             /* enable internal pull-up */

  printString("\r\nReaction Timer:\r\n");
  printString("---------------\r\n");
  printString("Press any key to start.\r\n");

  // ------ Event loop ------ //
  while (1) {

    byte = receiveByte();                             /* press any key */
    printString("\r\nGet ready...");
    randomDelay();

    printString("\r\nGo!\r\n");
    LED_PORT = 0xff;                                     /* light LEDs */
    TCNT1 = 0;                                        /* reset counter */

    if (bit_is_clear(BUTTON_PIN, BUTTON)) {
            /* Button pressed _exactly_ as LEDs light up.  Suspicious. */
      printString("You're only cheating yourself.\r\n");
    }
    else {
      // Wait until button pressed, save timer value.
      loop_until_bit_is_clear(BUTTON_PIN, BUTTON);
      timerValue = TCNT1 >> 4;
      /* each tick is approx 1/16 milliseconds, so we bit-shift divide */

      printMilliseconds(timerValue);
      printComments(timerValue);
    }

    // Clear LEDs and start again.
    LED_PORT = 0x00;
    printString("Press any key to try again.\r\n");

  }                                                  /* End event loop */
  return (0);                            /* This line is never reached */
}
예제 #30
0
파일: i2c.c 프로젝트: Genoil/smartwatch
void i2c_stop()
{
	// STOP
	TWCR = _BV(TWINT)|_BV(TWEN)|_BV(TWSTO);

	// Wait for STOP
	// WARNING: A bad I2C line can cause this loop to hang
	//loop_until_bit_is_set(TWCR, TWSTO);
	byte counter = 255; // 255 * 5us = 1.275ms timeout
	while(bit_is_clear(TWCR, TWSTO) && counter--)
		delay_us(5);

	// Disable I2C
	bit_clr(TWCR, TWEN);

	power_twi_disable();
}