Example #1
0
void HT1632::send_data( uint8_t data ) { // only sends bottom nibble
  write_bits_lsb( _BV(3), data );
}
Example #2
0
/* Leave pin off for time (given in microseconds) */
void IRsend::space(int time) {
  // Sends an IR space for the specified number of microseconds.
  // A space is no output, so the PWM output is disabled.
  TCCR2A &= ~(_BV(COM2B1)); // Disable pin 3 PWM output
  delayMicroseconds(time);
}
Example #3
0
uint8_t receive_UART0(void)
{
  ///Wait for a Byte to arrive
  while (!(UCSRA & _BV(RXC)));
  return UDR;
}
Example #4
0
int main(void) {
    uint8_t state = 0;

    PORTB = 0;
    DDRB = _BV(PB0);

	wdt_enable(WDTO_120MS);

    while (OSCCAL < 0x7D) {
        OSCCAL++;
        delay(10000);   
    }

    // Set PWM

    current_set = eeprom_read_byte((uint8_t*) &current_set_eeprom);

    OCR0A = eeprom_read_byte((uint8_t*) &pwm_value);
    //OCR0A = 1;

    TCCR0A = _BV(COM0A1) | _BV(WGM00) | _BV(WGM01);
    TCCR0B = _BV(CS00);
    TCNT0 = 0;
    TIMSK0 = 0;

    // Set ADC

    ADMUX = _BV(MUX1) | _BV(REFS0) | _BV(ADLAR);
    ADCSRA = _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
    DIDR0 = _BV(ADC2D) | _BV(ADC3D);

#ifdef USE_CURRENT_SENSE
    ADCSRA |= _BV(ADSC);
#endif

    sei();

//    OSCCAL = 0x7F;

	while (1) {
       	wdt_reset();

        if (OCR0A > MAX_PWM_VALUE) {
            OCR0A = MAX_PWM_VALUE;
        }
            

        // Check button state ----------

        if ((PINB & _BV(BUTTON_INC)) && (state & BUTTON_INC_ON)) {
            state &= ~(BUTTON_INC_ON);

#ifndef USE_CURRENT_SENSE
            if (OCR0A < 0xFE) {
                OCR0A++;
                eeprom_write_byte((uint8_t*) &pwm_value, OCR0A);
            }
#endif

#ifdef USE_CURRENT_SENSE
            if (current_set < MAX_CURRENT_SET) {
                current_set++;
                eeprom_write_byte((uint8_t *) &current_set_eeprom, current_set);
                eeprom_write_byte((uint8_t*) &pwm_value, OCR0A);
            }
#endif
            // INREMENT
        } 

        if (!(PINB & _BV(BUTTON_INC)) && !(state & BUTTON_INC_ON)) {
            state |= BUTTON_INC_ON;
        }

        if ((PINB & _BV(BUTTON_DEC)) && (state & BUTTON_DEC_ON)) {
            state &= ~(BUTTON_DEC_ON);

#ifndef USE_CURRENT_SENSE
            if (OCR0A > 0) {
                OCR0A--;
                eeprom_write_byte((uint8_t*) &pwm_value, OCR0A);
            }
#endif

#ifdef USE_CURRENT_SENSE
            if (current_set > 1) {
                current_set--;
                eeprom_write_byte((uint8_t *) &current_set_eeprom, current_set);
                eeprom_write_byte((uint8_t*) &pwm_value, OCR0A);
            }
#endif
            // DECREMENT
        } 

        if (!(PINB & _BV(BUTTON_DEC)) && !(state & BUTTON_DEC_ON)) {
            state |= BUTTON_DEC_ON;
        }

        // -----------------------------

        delay(10000);
	}

	return 0;
}
Example #5
0
#include "debug/debug.h"
#include "hardware/sonar.h"

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/pgmspace.h>

static PORT_t &sonar1_port = PORTD;
static PORT_t &sonar2_port = PORTE;
static const int echo1_mask = _BV(4);
static const int echo2_mask = _BV(4);
static const int trigger_mask = _BV(5);

static TC1_t &sonar_timer = TCE1;
#define TIMOVFVEC TCE1_OVF_vect
#define SIGINT0VECTD PORTD_INT0_vect
#define SIGINT0VECTE PORTE_INT0_vect

static const float sonar_scaler = 0.0345578232;

struct SonarData {
	float dist;
	float pulseStart;
};

static SonarData sonarData[sonar_count];

static int l_counter = 0;
static int m_counter = 0;
Example #6
0
static void initISR(timer16_Sequence_t timer)
{  
#if defined (_useTimer1)
  if(timer == _timer1) {
    TCCR1A = 0;             // normal counting mode 
    TCCR1B = _BV(CS11);     // set prescaler of 8 
    TCNT1 = 0;              // clear the timer count 
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
    TIFR |= _BV(OCF1A);      // clear any pending interrupts; 
    TIMSK |=  _BV(OCIE1A) ;  // enable the output compare interrupt  
#else
    // here if not ATmega8 or ATmega128
    TIFR1 |= _BV(OCF1A);     // clear any pending interrupts; 
    TIMSK1 |=  _BV(OCIE1A) ; // enable the output compare interrupt 
#endif    
#if defined(WIRING)       
    timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); 
#endif	
  } 
#endif  

#if defined (_useTimer3)
  if(timer == _timer3) {
    TCCR3A = 0;             // normal counting mode 
    TCCR3B = _BV(CS31);     // set prescaler of 8  
    TCNT3 = 0;              // clear the timer count 
#if defined(__AVR_ATmega128__)
    TIFR |= _BV(OCF3A);     // clear any pending interrupts;   
	ETIMSK |= _BV(OCIE3A);  // enable the output compare interrupt     
#else  
    TIFR3 = _BV(OCF3A);     // clear any pending interrupts; 
    TIMSK3 =  _BV(OCIE3A) ; // enable the output compare interrupt      
#endif
#if defined(WIRING)    
    timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service);  // for Wiring platform only	
#endif  
  }
#endif

#if defined (_useTimer4)
  if(timer == _timer4) {
    TCCR4A = 0;             // normal counting mode 
    TCCR4B = _BV(CS41);     // set prescaler of 8  
    TCNT4 = 0;              // clear the timer count 
    TIFR4 = _BV(OCF4A);     // clear any pending interrupts; 
    TIMSK4 =  _BV(OCIE4A) ; // enable the output compare interrupt
  }    
#endif

#if defined (_useTimer5)
  if(timer == _timer5) {
    TCCR5A = 0;             // normal counting mode 
    TCCR5B = _BV(CS51);     // set prescaler of 8  
    TCNT5 = 0;              // clear the timer count 
    TIFR5 = _BV(OCF5A);     // clear any pending interrupts; 
    TIMSK5 =  _BV(OCIE5A) ; // enable the output compare interrupt      
  }
#endif
} 
Example #7
0
void TimerThree::initialize(long microseconds)
{
  TCCR3A = 0;                 // clear control register A 
  TCCR3B = _BV(WGM13);        // set mode as phase and frequency correct pwm, stop the timer
  setPeriod(microseconds);
}
Example #8
0
void puthex(uint8_t c) {
  c &= 0xf;
  if (c>9) c+=7;
  UDR0=c+'0';
  while(!(UCSR0A & _BV(UDRE0)));
}
Example #9
0
// This gets called to set up the board
void board_init(void) {
	/*
	 * Disable all interrupt sources
	 */
	// External pin interrupts
	EIMSK = 0;
	EIFR = _BV(INTF2) | _BV(INTF1) | _BV(INTF0);
	
	// External pin change interrupts
	PCICR = 0;
	PCIFR = _BV(PCIF3) | _BV(PCIF2) | _BV(PCIF1) | _BV(PCIF0);
	PCMSK3 = 0;
	PCMSK2 = 0;
	PCMSK1 = 0;
	PCMSK0 = 0;

	// Timer/Counter 0
	TIMSK0 = 0;
	TIFR0 = _BV(OCF0B) | _BV(OCF0A) | _BV(TOV0);

	// Timer/Counter 1
	TIMSK1 = 0;
	TIFR1 = _BV(ICF1) | _BV(OCF1B) | _BV(OCF1A) | _BV(TOV1);

	// Timer/Counter 2
	TIMSK2 = 0;
	TIFR2 = _BV(OCF2B) | _BV(OCF2A) | _BV(TOV2);

	// SPI
	SPCR &= ~(_BV(SPIE) | _BV(SPE));
	if (SPSR & _BV(SPIF)) (void)SPDR;

	// USART
	UCSR0A |= _BV(TXC0);
	UCSR0B = 0;
	UCSR1A |= _BV(TXC1);
	UCSR1B = 0;

	// I2C
	TWCR |= _BV(TWINT);
	TWCR &= ~(_BV(TWEN) | _BV(TWIE));

	// Analog Comparator
	ACSR &= ~_BV(ACIE);
	ACSR |= _BV(ACD) | _BV(ACI);

	// ADC
	ADCSRA &= ~(_BV(ADEN) | _BV(ADIE));
	ADCSRA |= _BV(ADIF);

	// SPM
	SPMCSR &= ~_BV(SPMIE);

	/*
	 * Set up IO ports (default states)
	 */
	/* Port A */
	DDRA = 0xff; // all outputs
	PORTA = 0x00; // all pulled low

	/* Port B */
	DDRB =
		_BV(PINB1) |
		_BV(PINB2) |
		_BV(PINB4) |
		_BV(PINB5) |
		_BV(PINB6) |
		_BV(PINB7);
	PORTB =
		_BV(PINB0) |
		_BV(PINB2) |
		_BV(PINB4);

	/* Port C */
	DDRC =
		_BV(PINC2) |
		_BV(PINC3) |
		_BV(PINC4) |
		_BV(PINC5);
	PORTC =
		_BV(PINC3) |
		_BV(PINC6) |
		_BV(PINC7);

	/* Port D */
	DDRD = 0x00;
	PORTD =
		_BV(PIND4) |
		_BV(PIND5) |
		_BV(PIND6) |
		_BV(PIND7);
}
Example #10
0
void TaskManager::Loop(uint8_t watchdogTimeOutFlag)
{
    uint32_t currentTick = GetTaskTime();
    uint32_t deltaTime = currentTick - _lastTick;

    if (deltaTime >= TaskTimeAccuracy)
    {
        _lastTick = currentTick; // update before calling process
        uint32_t nextWakeTime = ProcessTasks(deltaTime);

        RemoveStoppedTasks();
 
        // if the next task has more time available than the next
        // millisecond interupt, then sleep
        if (nextWakeTime > TaskTimePerMs)
        {
            // for idle sleep mode:
            // due to Millis() using timer interupt at 1 ms, 
            // the cpu will be woke up by that every millisecond 

#if defined(ARDUINO_ARCH_ESP8266)
            // the esp8266 really doesn't have an idle mode
#if defined(USE_WDT)
            // use watchdog timer for failsafe mode, 
            // total task update time should be less than watchdogTimeOutFlag
            wdt_disable();
            wdt_enable(watchdogTimeOutFlag);
#endif

#elif defined(__arm__)
                // Arm support for sleep/idle not implemented yet

#elif defined(ARDUINO_ARCH_AVR)

#if defined(USE_WDT)
            // use watchdog timer for failsafe mode, 
            // total task update time should be less than watchdogTimeOutFlag
            wdt_reset();
            wdt_enable(watchdogTimeOutFlag);
#endif

            // just sleep
            set_sleep_mode(SLEEP_MODE_IDLE);
            cli();
            sleep_enable();
#if defined(BODSE)
            // lower power trick 
            // sleep_bod_disable() - i have seen this method called, but can't find it
            MCUCR |= _BV(BODS) | _BV(BODSE);  // turn on brown-out enable select
            MCUCR &= ~_BV(BODSE);        // this must be done within 4 clock cycles of above
#endif
            sei();
            sleep_cpu(); // will sleep in this call
            sleep_disable(); 
#endif // Arduino Normal
        }
#if defined(USE_WDT)
        else
        {
#if !defined(__arm__) // no arm support for watchdog 
            wdt_reset(); // keep the dog happy
#endif
        }
#endif

    }
}
Example #11
0
void putchar(uint8_t c) {
  UDR0=c;
  while(!(UCSR0A & _BV(UDRE0)));
}
Example #12
0
bool rf_ctrl_send_message(const void* buff, const uint8_t num_bytes)
{
	nRF_WriteReg(RF_SETUP, vRF_DR_2MBPS			// data rate 
							| get_nrf_output_power());	// output power

	nRF_FlushTX();
	nRF_WriteReg(CONFIG, vEN_CRC | vCRCO | vPWR_UP);	// power up
	nRF_WriteReg(STATUS, vTX_DS | vRX_DR | vMAX_RT);	// reset the status flag
	nRF_WriteTxPayload(buff, num_bytes);
	
	bool is_sent;

	uint8_t attempts = 0;
	const uint8_t MAX_ATTEMPTS = 45;

	uint8_t ticks = 15;
	const uint8_t TICKS_INCREMENT = 20;
	
	do {
		nRF_CE_hi();	// signal the transceiver to send the packet

		// wait for the nRF to signal an event
		sleep_ticks(3);
		while (PIN(NRF_IRQ_PORT) & _BV(NRF_IRQ_BIT))
			sleep_ticks(1);

		nRF_CE_lo();

		uint8_t status = nRF_NOP();			// read the status reg
		is_sent = (status & vTX_DS) != 0;	// did we get an ACK?

		nRF_WriteReg(STATUS, vMAX_RT | vTX_DS | vRX_DR);	// reset the status flags
		
		// read the ARC
		nRF_ReadReg(OBSERVE_TX);
		arc_total += nRF_data[1] & 0x0f;
		
		++rf_packets_total;
		
		if (!is_sent)
		{
			++plos_total;
			nRF_ReuseTxPayload();		// send the last message again
			
			if (ticks >= 0xfe - TICKS_INCREMENT)
			{
				sleep_max(5);		// 63ms*5 == 0.315sec
			} else {
				sleep_ticks(ticks);
				ticks += TICKS_INCREMENT;
			}
		}

		++attempts;

	} while (!is_sent  &&  attempts < MAX_ATTEMPTS);

	nRF_WriteReg(CONFIG, vEN_CRC | vCRCO);		// nRF power down
	
	return is_sent;
}
Example #13
0
void HT1632::set_mode(Mode mode) {
  deselect();
  select();
  write_bits_msb( _BV(2), (uint8_t)mode );
  this->mode = mode;
}
Example #14
0
void HT1632::send_command( Command command ) {
  write_bits_msb( _BV(7), (uint8_t)command );
  write_bits_msb( _BV(0), 0 );
}
Example #15
0
File: motor.c Project: twm/VTMotor
static void drv2_back()
{
  DRV_DIR_PORT |= _BV(DRV2_DIR_PIN);
  m_dir2_is_forward = 0;
}
/* power up in TX mode */
void nRF24L01p_startTX()
{
	nRF24L01p_isPTX = 1;	// yes: we are in TX mode
	// config: power up, TX mode, no interrupt on TX_DS, 2 bit CRC on
	nRF24L01p_write_configByte( CONFIG, _BV( PWR_UP ) | _BV( MASK_TX_DS ) | _BV( EN_CRC ) | _BV( CRCO ));
}
Example #17
0
int main(void)
{

  #ifdef __AVR_ATtiny10__
  CCP=0xD8;		// configuration change protection, write signature
  CLKPSR=0;		// set cpu clock prescaler =1 (8Mhz) (attiny 4/5/9/10)
  #else
  CLKPR=_BV(CLKPCE);
  CLKPR=0;			// set clock prescaler to 1 (attiny 25/45/85/24/44/84/13/13A)
  #endif

  uint64_t i, j, rr, rg, rb, rR, rG, rB, k, d, l, rz;

  for (i=0; i<255; i++) {
    led[0].r = led[0].g = led[0].b = i;
    led[1].r = led[1].g = led[1].b = 255-i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  for (i=255; i>0; i--) {
    led[0].r = led[0].g = led[0].b = i;
    led[1].r = led[1].g = led[1].b = 255-i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  _delay_ms(100);

  rR = rG = rB = 0;
//  k = 100;
  d = 255;
  l = 64;

  for (i=0; i<30; i++) {
    rz = rand() % 3;

    rr = rand() % l * ((rz+2)%3);
    rg = rand() % l * ((rz+1)%3);
    rb = rand() % l * ((rz)%3);


    for (j=0; j<d;j++){
      led[0].r = ((rr * j / d) + (rR * (d-j) / d));
      led[0].g = ((rg * j / d) + (rG * (d-j) / d));
      led[0].b = ((rb * j / d) + (rB * (d-j) / d));
      led[1].r = 127-led[0].r;
      led[1].g = 127-led[0].g;
      led[1].b = 127-led[0].b;
      ws2812_setleds(led,3);
      _delay_ms(5);
    }

    rR = rr;
    rG = rg;
    rB = rb;
    _delay_ms(100);

  }
  _delay_ms(100000);

  for (i=0; i<255; i++) {
    led[0].r = i;
    led[1].r = i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  for (i=255; i>0; i--) {
    led[0].r = i;
    led[1].r = i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  for (i=0; i<255; i++) {
    led[0].r = led[0].b = i;
    led[1].r = led[1].b = i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  for (i=255; i>0; i--) {
    led[0].r = led[0].b = i;
    led[1].r = led[1].b = i;
    ws2812_setleds(led,3);
    _delay_ms(3);
  }

  for(i=0; i<100; i++) {
    led[0].r = rand() % 256;
    led[0].g = rand() % 256;
    led[0].b = rand() % 256;
    led[1].r = rand() % 256;
    led[1].g = rand() % 256;
    led[1].b = rand() % 256;
    ws2812_setleds(led,3);
    _delay_ms(30);
  }

//  while(1){
  for(i=0; i<100; i++) {
    led[0].r = 255 * rand() % 2;
    led[0].g = 255 * rand() % 2;
    led[0].b = 255 * rand() % 2;
    led[1].r = 255 * rand() % 2;
    led[1].g = 255 * rand() % 2;
    led[1].b = 255 * rand() % 2;
    ws2812_setleds(led,3);
    _delay_ms(30);
  }

  for(i=0; i<10000; i++) {
    j = 1ULL << (i % 48);
    led[0].r = ( j          ) ;
    led[0].g = ( j     >> 4 ) ;
    led[0].b = ( j     >> 8 ) ;
    led[1].r = ( j / 8 >> 0 ) ;
    led[1].g = ( j / 8 >> 8 ) ;
    led[1].b = ( j / 8 >> 16) ;
    ws2812_setleds(led,3);
    _delay_ms(10);
  }



//  while(1)
  for(i=0; i<100; i++)
  {
    led[0].r=255;led[0].g=00;led[0].b=0;    // Write red to array
    ws2812_setleds(led,2);
    _delay_ms(500);                         // wait for 500ms.

    led[0].r=0;led[0].g=255;led[0].b=0;			// green
    ws2812_setleds(led,1);
    _delay_ms(500);

    led[0].r=0;led[0].g=00;led[0].b=255;		// blue
    ws2812_setleds(led,1);
    _delay_ms(500);
  }
}
/* is data ready?
 * 1=yes, 0=no
 */
uint8_t nRF24L01p_dataReady() 
{
	uint8_t status = nRF24L01p_getStatus();
	return( status & _BV( RX_DR ));
}
Example #19
0
  Timer3.isrCallback();
}

void TimerThree::initialize(long microseconds)
{
  TCCR3A = 0;                 // clear control register A 
  TCCR3B = _BV(WGM13);        // set mode as phase and frequency correct pwm, stop the timer
  setPeriod(microseconds);
}

void TimerThree::setPeriod(long microseconds)
{
  long cycles = (F_CPU * microseconds) / 2000000;                                // the counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2
  if(cycles < RESOLUTION)              clockSelectBits = _BV(CS10);              // no prescale, full xtal
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11);              // prescale by /8
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10);  // prescale by /64
  else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12);              // prescale by /256
  else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12) | _BV(CS10);  // prescale by /1024
  else        cycles = RESOLUTION - 1, clockSelectBits = _BV(CS12) | _BV(CS10);  // request was out of bounds, set as maximum
  ICR3 = pwmPeriod = cycles;                                                     // ICR1 is TOP in p & f correct pwm mode
  TCCR3B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  TCCR3B |= clockSelectBits;                                                     // reset clock select register
}

void TimerThree::setPwmDuty(char pin, int duty)
{
  unsigned long dutyCycle = pwmPeriod;
  dutyCycle *= duty;
  dutyCycle >>= 10;
  if(pin == 5) OCR3A = dutyCycle;
  if(pin == 2) OCR3B = dutyCycle;
Example #20
0
void TWI_Start(void)
{
        TWCR |= (_BV(TWINT) | _BV(TWSTA) | _BV(TWEN));
        TWI_WAIT();
}
Example #21
0
void TimerThree::setPeriod(long microseconds)
{
  long cycles = (F_CPU * microseconds) / 2000000;                                // the counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2
  if(cycles < RESOLUTION)              clockSelectBits = _BV(CS10);              // no prescale, full xtal
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11);              // prescale by /8
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10);  // prescale by /64
Example #22
0
void TWI_Write(uint8_t data)
{
        TWDR = data;
        TWCR = _BV(TWINT) | _BV(TWEN);
        TWI_WAIT();
}
Example #23
0
uint8_t digitalRead(uint8_t pin){
	if(pin > 5 || pin < 0){return 0;}
	if(pin < 2) turnOffPWM(pin); //If its PWM pin, makes sure the PWM is off
	return !!(PINB & _BV(pin));
}
Example #24
0
void NarcolepticClass::sleep(uint8_t wdt_period,uint8_t sleep_mode) {

#ifdef BODSE
    // Turn off BOD in sleep (picopower devices only)
    MCUCR |= _BV(BODSE);
    MCUCR |= _BV(BODS);
#endif

    MCUSR = 0;
    WDTCSR &= ~_BV(WDE);
    WDTCSR = _BV(WDIF) | _BV(WDIE) | _BV(WDCE);

    wdt_enable(wdt_period);
    wdt_reset();
#ifdef WDTCSR
    WDTCSR |= _BV(WDIE);
#else
    WDTCR |= _BV(WDIE);
#endif
    set_sleep_mode(sleep_mode);

    // Disable all interrupts
    uint8_t SREGcopy = SREG;
    cli();

#ifdef EECR
    uint8_t EECRcopy = EECR;
    EECR &= ~_BV(EERIE);
#endif
#ifdef EIMSK
    uint8_t EIMSKcopy = EIMSK;
    EIMSK = 0;
#endif
#ifdef PCMSK0
    uint8_t PCMSK0copy = PCMSK0;
    PCMSK0 = 0;
#endif
#ifdef PCMSK1
    uint8_t PCMSK1copy = PCMSK1;
    PCMSK1 = 0;
#endif
#ifdef PCMSK2
    uint8_t PCMSK2copy = PCMSK2;
    PCMSK2 = 0;
#endif
#ifdef TIMSK0
    uint8_t TIMSK0copy = TIMSK0;
    TIMSK0 = 0;
#endif
#ifdef TIMSK1
    uint8_t TIMSK1copy = TIMSK1;
    TIMSK1 = 0;
#endif
#ifdef TIMSK2
    uint8_t TIMSK2copy = TIMSK2;
    TIMSK2 = 0;
#endif
#ifdef SPCR
    uint8_t SPCRcopy = SPCR;
    SPCR &= ~_BV(SPIE);
#endif
#ifdef UCSR0B
    uint8_t UCSR0Bcopy = UCSR0B;
    UCSR0B &= ~(_BV(RXCIE0) | _BV(TXCIE0) | _BV(UDRIE0));
#endif
#ifdef TWCR
    uint8_t TWCRcopy = TWCR;
    TWCR &= ~_BV(TWIE);
#endif
#ifdef ACSR
    uint8_t ACSRcopy = ACSR;
    ACSR &= ~_BV(ACIE);
#endif
#ifdef ADCSRA
    uint8_t ADCSRAcopy = ADCSRA;
    ADCSRA &= ~_BV(ADIE);
#endif
#ifdef SPMCSR
    uint8_t SPMCSRcopy = SPMCSR;
    SPMCSR &= ~_BV(SPMIE);
#endif

    sei();
    sleep_mode();
    wdt_disable();

    // Reenable all interrupts
#ifdef SPMCSR
    SPMCSR = SPMCSRcopy;
#endif
#ifdef ADCSRA
    ADCSRA = ADCSRAcopy;
#endif
#ifdef ACSR
    ACSR = ACSRcopy;
#endif
#ifdef TWCR
    TWCR = TWCRcopy;
#endif
#ifdef UCSR0B
    UCSR0B = UCSR0Bcopy;
#endif
#ifdef SPCR
    SPCR = SPCRcopy;
#endif
#ifdef TIMSK2
    TIMSK2 = TIMSK2copy;
#endif
#ifdef TIMSK1
    TIMSK1 = TIMSK1copy;
#endif
#ifdef TIMSK0
    TIMSK0 = TIMSK0copy;
#endif
#ifdef PCMSK2
    PCMSK2 = PCMSK2copy;
#endif
#ifdef PCMSK1
    PCMSK1 = PCMSK1copy;
#endif
#ifdef PCMSK0
    PCMSK0 = PCMSK0copy;
#endif
#ifdef EIMSK
    EIMSK = EIMSKcopy;
#endif
#ifdef EECR
    EECR = EECRcopy;
#endif

    SREG = SREGcopy;

#ifdef WDTCSR
    WDTCSR &= ~_BV(WDIE);
#else
    WDTCR &= ~_BV(WDIE);
#endif
}
Example #25
0
void IRsend::mark(int time) {
  // Sends an IR mark for the specified number of microseconds.
  // The mark output is modulated at the PWM frequency.
  TCCR2A |= _BV(COM2B1); // Enable pin 3 PWM output
  delayMicroseconds(time);
}
Example #26
0
File: motor.c Project: twm/VTMotor
void drv_enable()
{
  DRV_EN_PORT |= _BV(DRV_EN_PIN1);
  DRV_EN_PORT |= _BV(DRV_EN_PIN2);
  m_drv_enabled = 1;
}
Example #27
0
/*
 * Do all the startup-time peripheral initializations.
 */
static void
ioinit(void)
{
  uint16_t pwm_from_eeprom;

  /*
   * Set up the 16-bit timer 1.
   *
   * Timer 1 will be set up as a 10-bit phase-correct PWM (WGM10 and
   * WGM11 bits), with OC1A used as PWM output.  OC1A will be set when
   * up-counting, and cleared when down-counting (COM1A1|COM1A0), this
   * matches the behaviour needed by the STK500's low-active LEDs.
   * The timer will runn on full MCU clock (1 MHz, CS10 in TCCR1B).
   */
  TCCR1A = _BV(WGM10) | _BV(WGM11) | _BV(COM1A1) | _BV(COM1A0);
  TCCR1B = _BV(CS10);

  OCR1A = 0;			/* set PWM value to 0 */

  /* enable pull-ups for pushbuttons */
#if HAVE_ADC
  CONTROL_PORT = _BV(TRIGGER_DOWN) | _BV(TRIGGER_UP) | _BV(TRIGGER_ADC);
#else
  CONTROL_PORT = _BV(TRIGGER_DOWN) | _BV(TRIGGER_UP);
#endif

  /*
   * Enable Port D outputs: PD6 for the clock output, PD7 for the LED
   * flasher.  PD1 is UART TxD but not DDRD setting is provided for
   * that, as enabling the UART transmitter will automatically turn
   * this pin into an output.
   */
  CONTROL_DDR = _BV(CLOCKOUT) | _BV(FLASH);

  /*
   * As the location of OC1A differs between supported MCU types, we
   * enable that output separately here.  Note that the DDRx register
   * *might* be the same as CONTROL_DDR above, so make sure to not
   * clobber it.
   */
  PWMDDR |= _BV(PWMOUT);

  UCSRA = _BV(U2X);		/* improves baud rate error @ F_CPU = 1 MHz */
  UCSRB = _BV(TXEN)|_BV(RXEN)|_BV(RXCIE); /* tx/rx enable, rx complete intr */
  UBRRL = (F_CPU / (8 * 9600UL)) - 1;  /* 9600 Bd */

#if HAVE_ADC
  /*
   * enable ADC, select ADC clock = F_CPU / 8 (i.e. 125 kHz)
   */
  ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
#endif

  TIMSK = _BV(TOIE1);
  sei();			/* enable interrupts */

  /*
   * Enable the watchdog with the largest prescaler.  Will cause a
   * watchdog reset after approximately 2 s @ Vcc = 5 V
   */
  wdt_enable(WDTO_2S);

  /*
   * Read the value from EEPROM.  If it is not 0xffff (erased cells),
   * use it as the starting value for the PWM.
   */
  if ((pwm_from_eeprom = eeprom_read_word(&ee_pwm)) != 0xffff)
    OCR1A = (pwm = pwm_from_eeprom);
}
Example #28
0
File: motor.c Project: twm/VTMotor
static void drv1_back()
{
  DRV_DIR_PORT |= _BV(DRV1_DIR_PIN);
  m_dir1_is_forward = 0;
}
Example #29
0
/**
 * \brief toggle the direction LED/pin
 */
void	direction_toggle() {
	direction_led((PORTB & _BV(PORTB2)));
}
Example #30
0
void HT1632::send_address( uint8_t addr ) {
  write_bits_msb( _BV(6), addr );
}