예제 #1
0
/*
  Delay by the provided number of milliseconds.
  Thus, a 16 bit value will allow a delay of 0..65 seconds
  Makes use of the _delay_loop_2
  
  _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  with f = F_CPU cycles per second,
  n = f / (1000 * 4 )
  with f = 16000000 the result is 4000
  with f = 1000000 the result is 250
  
  the millisec loop, gcc requires the following overhead:
  - movev 1
  - subwi 2x2
  - bne i 2
  ==> 7 cycles
  ==> must be devided by 4, rounded up 7/4 = 2
*/
void u8g_Delay(uint16_t val)
{
  /* old version did a call to the arduino lib: delay(val); */
  while( val != 0 )
  {
    _delay_loop_2( (F_CPU / 4000 ) -2);
    val--;
  }
}
예제 #2
0
void
us_delay(uint16_t us)
{

    // _delay_loop_2 consumes 4 cycles per count, so convert microseconds to
    // units of 4 cycles
    us *= F_CPU / 4000000UL;

    _delay_loop_2(us);
}
예제 #3
0
/*! \brief  Write a '0' to the bus(es). (Software only driver)
 *
 *  Generates the waveform for transmission of a '0' bit on the 1-Wire(R)
 *  bus.
 *
 *  \param  pins    A bitmask of the buses to write to.
 */
void OWI_WriteBit0(unsigned char pins)
{
    uint8_t intState;
    
    // Disable interrupts.
    intState = save_interrupt();
//    __disable_interrupt();
    
    // Drive bus low and delay.
    OWI_PULL_BUS_LOW(pins);
    _delay_loop_2((OWI_DELAY_C_STD_MODE) / 4);
    
    // Release bus and delay.
    OWI_RELEASE_BUS(pins);
    _delay_loop_2((OWI_DELAY_D_STD_MODE) / 4);
    
    // Restore interrupts.
    restore_interrupt(intState);
}
예제 #4
0
/*
  Delay by the provided number of microseconds.
  Thus, a 16 bit value will allow a delay of 0..65 milliseconds
  Makes use of the _delay_loop_2
  
  _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  with f = F_CPU cycles per second,
  n = f / (10000000 * 4 )
  
  
  with f = 16000000 the result is 4
  with f = 4000000 the result is 1
  
  the millisec loop, gcc requires the following overhead:
  - movev 1
  - subwi 2x2
  - bne i 2
  ==> 7 cycles
  ==> must be devided by 4, rounded up 7/4 = 2
  
  lowest two bits are ignored: 
*/
extern "C" void pff_delay_us(uint16_t val)
{
  val += 3;
  val >>= 2;
  while( val != 0 )
  {
    _delay_loop_2( (F_CPU / 1000000L ) -2);
    val--;
  }
}
예제 #5
0
파일: main.c 프로젝트: RJ/radioblink
int main(void)
{
	DDRB  = 0x01;
    PORTB = 0x01;
    for(;;){
		PORTB ^= 0x01;
		_delay_loop_2(65535);
	}
    return 0;   /* never reached */
}
예제 #6
0
파일: main.c 프로젝트: spinlockirqsave/avr
/* @brief   Flash PORTD pin 0 to signal failure. */
static void adc_signal_malloc_failure(void) {
    uint8_t n;
    while (1) {
        PORTD ^= (1 << PD0);
        n = 0;
        while (n++ < 4) { /* 1.048576s delay */
            _delay_loop_2(0);   /* 65536 loops * 4 cycles * 1microsecond = 0.262144s */
        }
    }
}
예제 #7
0
int main(void)
{
	DDRB |= (1 << 5) | (1 << 7); // Set LED as output (PIN 11 on the Mega) 
	
	PORTB |= (1 << 7);
	
	// set phase + frequency correct PWM mode, WGM1[3:0] = 8 (0b1000)	
	TCCR1B |= (1 << WGM13);
	
	// Set on compare match when up counting, clear on compare match when down counting
	TCCR1A |= (1 << COM1A1) | (1 << COM1A0); 
		
	TCCR1B |= (1 << CS11);	// Start timer at Fcpu/8
	
	uint16_t period = 30000;
	
	// this gives us 50hz signal (T=20ms) with pre-scaler set to 8
	ICR1 = period;
			
	uint16_t low = period - 1800;
	uint16_t high = period - 1200;
	
	// set compare register, this determines duty cycle (between 1.2ms and 1.8ms == [1200,1800]
	// therefore we have 1000 steps or between 9 and 10 bits of resolution for the servo!
	for(;;)	
	{		
		for(uint32_t pos = low; pos < high; ++pos)
		{
			OCR1A = pos;
			_delay_loop_2(20000);
		}
		
				
		for(uint32_t pos = high; pos > low; --pos)
		{			
			OCR1A = pos;
			_delay_loop_2(20000);
		}
		
	}
	
}
예제 #8
0
void ST7036_puts( char * string )
{
unsigned char zahl;
zahl = 0;
while (string[zahl] != 0x00)
	{
	_delay_loop_2(50000);
	ST7036_write_data_byte( string[zahl] );
	string ++;
	}
}
예제 #9
0
/*!
 * Warte 100 ms
 */
void delay_100ms(void){ 
	char counter;
	//wait (10 * 120000) cycles = wait 1200000 cycles
	counter = 0;
	while (counter != 5)
	{
	//wait (30000 x 4) cycles = wait 120000 cycles
	_delay_loop_2(30000);
	counter++;
	}
}
예제 #10
0
파일: 18S20.c 프로젝트: thomasstrand/AVRpsu
char t_reset(void) {
/* Sends a reset pulse to the temperature sensor */
/* Returns 0 if all went well, 1 if no presence were detected */
	char wait_time=0;
	clr_tsens;
	dirt_out;
	_delay_loop_2(1660);			// Delay 600us, reset pulse
	dirt_in;
	_delay_loop_2(275);				// Delay 100us
	while(tsens) {					// Wait for presence pulse
		wait_time++;
		_delay_loop_1(4);			// Delay ~1us
		if(wait_time>200)
			return 1;				// No presence pulse after 200us, return error
	}
	_delay_loop_2(1660);
	set_tsens;
	dirt_out;
	return 0;
}
예제 #11
0
/*
  Delay by the provided number of microseconds.
  Thus, a 16 bit value will allow a delay of 0..65 milliseconds
  Makes use of the _delay_loop_2
  
  _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  with f = F_CPU cycles per second,
  n = f / (10000000 * 4 )
  
  
  with f = 16000000 the result is 4
  with f = 4000000 the result is 1
  
  the millisec loop, gcc requires the following overhead:
  - movev 1
  - subwi 2x2
  - bne i 2
  ==> 7 cycles
  ==> must be devided by 4, rounded up 7/4 = 2
  
  lowest two bits are ignored: 
*/
extern "C" void pff_delay_us(uint16_t val)
{
#if defined(__AVR__)
  val += 3;
  val >>= 2;
  while( val != 0 )
  {
    _delay_loop_2( (F_CPU / 1000000L ) -2);
    val--;
  }
#endif
}
예제 #12
0
/* Main Program Routine */
int
main(void)
{
    uint16_t pgm_crc, count, cmp_crc;
	uint8_t crcgood=0;
#ifdef UART_DEBUG
    char sout[8];    
#endif

	init();
#ifdef UART_DEBUG
	uart_write("\nStart Node ", 12);
	itoa(node_id,sout, 16);
	uart_write(sout, strlen(sout));
	uart_write("\n", 1);
#endif

    /* Find the firmware size and checksum */
	count   = pgm_read_word_near(PGM_LENGTH);
    cmp_crc = pgm_read_word_near(PGM_CRC);

	/* Retrieve the Program Checksum */
	pgm_crc = pgmcrc(count);
	/* If it matches then set the good flag */
	if(pgm_crc == cmp_crc) {
	    crcgood = 1;
    }
#ifdef UART_DEBUG
    itoa(pgm_crc,sout,16);
	uart_write("Checksum ", 9);
	uart_write(sout,strlen(sout));
	uart_write("\n",1);
#endif
	/* This timer expires at roughly one second after startup */
	while(TCNT1 <= 0x2B00) /* Run this for about a second */
        bload_check();
    TCNT1 = 0x0000;
#ifdef UART_DEBUG
	uart_write("TIMEOUT\n",8);
#endif
	if(crcgood) {
	   start_app(); /* When we go here we ain't never comin' back */
    }
	
    /* If CRC is no good we sit here and look for a firmware update command
       forever. */
    PORTB |= (1<<PB0);
    while(1) { 

        bload_check();
        _delay_loop_2(0xFFFF); /* Delay */
	}	
}
예제 #13
0
파일: main.c 프로젝트: uISP/uisp-appsources
static void i2c_io_set_scl(uchar hi) {
#ifdef ENABLE_SCL_EXPAND
  _delay_loop_2(clock_delay2);
  if(hi) {
    I2C_DDR &= ~I2C_SCL;          // port is input
    I2C_PORT |= I2C_SCL;          // enable pullup

    // wait while pin is pulled low by client
    while(!(I2C_PIN & I2C_SCL));
  } else {
    I2C_DDR |= I2C_SCL;           // port is output
    I2C_PORT &= ~I2C_SCL;         // drive it low
  }
  _delay_loop_2(clock_delay);
#else
  _delay_loop_2(clock_delay2);
  if(hi) I2C_PORT |=  I2C_SCL;    // port is high
  else   I2C_PORT &= ~I2C_SCL;    // port is low
  _delay_loop_2(clock_delay);
#endif
}
예제 #14
0
파일: 18S20.c 프로젝트: thomasstrand/AVRpsu
void wr_tsens(char data) {
/* Sends a data byte to the temperature sensor */
	char a;
	for(a=0;a<8;a++) {
		_delay_loop_1(18);			// Delay 5us
		if(data&0x01) {			// Send one
			clr_tsens;
			dirt_out;				// Direction for sensor pin is output :)
			_delay_loop_1(18);		// Short pulse = '1'
			dirt_in;
			_delay_loop_2(275);		// Delay 100us
		}
		else {						// Send zero
			clr_tsens;
			dirt_out;
			_delay_loop_2(275);		// Long pulse = '0'
			dirt_in;
			_delay_loop_1(18);
		}
		data=data>>1;
	}
}
예제 #15
0
void tx_nword (unsigned int daWord) {
  setIOB(0,1);
  setIOB(1,0);
  for(cnt=0;cnt<16;cnt++) {
    setIOB(2,((daWord>>cnt)&1));
    setIOB(1,1);
    _delay_loop_2(1000);
    setIOB(1,0);
  }
  setIOB(0,0); 
  setIOB(2,0);
  setIOB(1,0);
}
예제 #16
0
void tx_nbyte (unsigned char daByte) {
  setIOB(0,1);
  setIOB(1,0);
  for(cnt=0;cnt<8;cnt++) {
    setIOB(2,((daByte>>cnt)&1));
    setIOB(1,1);
    _delay_loop_2(1000);
    setIOB(1,0);
  }
  setIOB(0,0); 
  setIOB(2,0);
  setIOB(1,0);
}
예제 #17
0
파일: main.c 프로젝트: Teknoman117/avr
/*you like the song "Wanted Dead or Alive and Copperhead Road*/
int main () {
    DDRB=0xFF;
    initPWM();
	double x=128,y;
	setCHA(128);
	setCHB(128);
	for(y=0;y<500;y++){_delay_loop_2(30000);}
	while (1) {
		while (x < 255) {
		    x++;
			//if(x==128){PORTB+=1;}
			//if(x==138){PORTB-=1;}
			setCHA(x);
			setCHB(x);
			for(y=0;y<wait_interval;y++){_delay_loop_2(30000);}
		}
		while (x > 0) {
		    x--;
			setCHA(x);
			setCHB(x);
			for(y=0;y<wait_interval;y++){_delay_loop_2(30000);}
		}
	}
}
예제 #18
0
int 
UAT::putchar(char c)
{
  uint16_t data = ((0xff00 | c) << 1);
  uint8_t bits = m_bits + m_stops + 1;
  uint16_t count = m_count;
  synchronized {
    do {
      m_tx._write(data & 0x01);
      _delay_loop_2(count);
      data >>= 1;
    } while (--bits);
  }
  return (c & 0xff);
}
예제 #19
0
int main (void) {
    char counter;
    /* set PORTB to output... whatever that means. */
    /* DDR = data direction register,
    FF = high = output, 
    00 = low = input. 
    */ 

    /* PINA contains whatever pin A contains; can be read. */

    /* PORTx is used to output data to port x. */
    /* PORTx contains 8 bits, individual bits can be accessed with
    PORTx.# where # is a number from 0 to 7. */ 

    DDRB = 0xFF; 

    while (1) {
        PORTB = 0xFF;

        counter = 0; 
        while (counter != 50) {
            _delay_loop_2(30000);
            counter++;
        }

        PORTB = 0x00;

        counter = 0; 
        while (counter != 50) {
            _delay_loop_2(30000);
            counter++;
        }
    }

    return 1;
}
예제 #20
0
파일: onewire.c 프로젝트: ninharp/ethersex
uint8_t noinline
reset_onewire(uint8_t busmask)
{
  uint8_t data1, data2;
  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  {
    /* pull bus low */
    OW_CONFIG_OUTPUT(busmask);
    OW_LOW(busmask);

    /* wait 480us */
    _delay_loop_2(OW_RESET_TIMEOUT_1);

    /* release bus */
    OW_CONFIG_INPUT(busmask);

    /* wait 60us (maximal pause) + 30 us (half minimum pulse) */
    _delay_loop_2(OW_RESET_TIMEOUT_2);

    /* sample data */
    data1 = OW_GET_INPUT(busmask);

    /* wait 390us */
    _delay_loop_2(OW_RESET_TIMEOUT_3);

    /* sample data again */
    data2 = OW_GET_INPUT(busmask);

    /* if first sample is low and second sample is high, at least one device
     * is attached to this bus */

    OW_HIGH(busmask);
    OW_CONFIG_OUTPUT(busmask);
  }
  return (uint8_t) (~data1 & data2 & busmask);
}
예제 #21
0
/* loop a few times, and see if the character is received */
static inline uint8_t wait_for_char(void)
/*{{{*/ {
    uint8_t i;
    for(i = 0; i < 100; i++) {
        _delay_loop_2(65535);

        if(_UCSRA_UART0 & _BV(_RXC_UART0)) {
            if(_UDR_UART0 == BOOTLOADER_ENTRY_CHAR) {
                    return 1;
            }
        }
    }

    /* never received the character */
    return 0;
} /* }}} */
예제 #22
0
void
owluart_outb(void *uart, int c)
{
        USART_t *port = uart;
        while (!(port->STATUS & USART_DREIF_bm));
        port->DATA = c;
        
#if BOARD == XPLAIN
        /* usb-to-serial hacks */
        if (port == &USARTC0) {
                while (!(port->STATUS & USART_TXCIF_bm));
                port->STATUS |= USART_TXCIF_bm;
                _delay_loop_2(1000);
        }
#endif
}
예제 #23
0
파일: SOFT_UART.cpp 프로젝트: Dzenik/Cosa
void 
UART::RXPinChangeInterrupt::on_interrupt(uint16_t arg)
{
  UNUSED(arg);
  if (is_set()) return;
  uint16_t count = m_uart->m_count;
  uint8_t bits = m_uart->m_bits;
  uint8_t mask = 1;
  uint8_t data = 0;
  do {
    _delay_loop_2(count);
    if (is_set()) data |= mask;
    mask <<= 1;
  } while (--bits);
  m_uart->m_ibuf->putchar(data);
}
예제 #24
0
파일: main.c 프로젝트: spinlockirqsave/avr
int
main(void) {
    struct adc          adc;
    struct adc_event    adc_e;

    ports_init();
    adc_init();
    adc_ctor(&adc);

    while(1) {
        adc_e.sample = adc_read(0);
        adc_e.super_.signal = ADC_NEW_SAMPLE_SIG;
        FSM_DISPATCH_((struct fsm*)&adc, (struct fsm_event*)&adc_e);
        _delay_loop_2(10);               /* using 16 bit counter, 10 loops, each of them takes 4 CPU cycles, so if F_CPU is 1MHz this does busy waiting for 40 microseconds */
    }
}
예제 #25
0
파일: 18S20.c 프로젝트: thomasstrand/AVRpsu
char rd_tsens(void) {
/* Reads a byte from the temperature sensor */
	char a, data=0;
	for(a=0;a<8;a++) {
		_delay_loop_1(18);
		clr_tsens;
		dirt_out;
		_delay_loop_1(18);
		dirt_in;
		_delay_loop_1(18);
		data=data>>1;
		if(tsens)
			data|=0x80;
		_delay_loop_2(275);
	}
	return data;
}
예제 #26
0
void main()
{
     unsigned char i;

   //Initialize LCD module
   LCDInit(LS_BLINK|LS_ULINE);

   //Clear the screen
   LCDClear();

   //Simple string printing
   LCDWriteString("Congrats ");

   //A string on line 2
   LCDWriteStringXY(0,1,"Loading ");

   //Print some numbers
   for (i=0;i<99;i+=1)
   {
      LCDWriteIntXY(9,1,i,3);
      LCDWriteStringXY(12,1,"% ");
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);

   }

   //Clear the screen
   LCDClear();

   //Some more text

   LCDWriteString("Hello world");
   LCDWriteStringXY(0,1,"By YourName Here");    // <--- Write ur NAME HERE !!!!!!!!!!!

   //Wait
   for(i=0;i<100;i++) _delay_loop_2(0);

   //Some More ......
   LCDClear();
   LCDWriteString("    eXtreme");
   LCDWriteStringXY(0,1,"  Electronics");

	//Wait
   for(i=0;i<100;i++) _delay_loop_2(0);

   //Custom Chars ......
   LCDClear();
   LCDWriteString("Custom Char !!!");
   LCDWriteStringXY(0,1,"%0%1%2%3%4%5%6%7");

}
예제 #27
0
파일: nRF24L01.c 프로젝트: csaleman/yavrha
/* Read data from radio.
   Since more than one radio could be present in a channel, this function check Node_Number to see if the
   Package belong to this node, if does, it update the Global DATA variables and return 1, 
   if not it retransmit the package and return 0. 
*/
uint8_t nrf_read_payload(void) 

{
	
    uint8_t ReturnFlag  = 0;

    CSN_LOW                    // Pull down chip select
    spi_fast_shift( R_RX_PAYLOAD ); // Send Read Payload Command
    spi_transfer_sync(buffer,buffer, PAYLOAD_WIDTH);   // Read payload
    CSN_HIGH                    // Pull up chip select
	
	// clear status register flag IRQ, write 1 to clear bit.
	nrf_config_register(STATUS,(1<<RX_DR) | (1<<TX_DS) | (1<<MAX_RT)); 
	
	
	// Verify the the Received Node Number before accepting the data.

	if (NODE_NUMBER == buffer[5]){
		DATA0 = buffer[0];
		DATA1 = buffer[1];
		DATA2 = buffer[2];
		DATA3 = buffer[3];
		RECV_MSGID = buffer[4];
        ReturnFlag = 1;
	}
	// Else forward the message	
	else {
		// short delay to avoid collitions		
		_delay_loop_2(NODE_NUMBER * 100);

		// configure radio in TX
		nrf_tx_config();

		// send package
		nrf_send(buffer);

		// configure radio as rx again.
		nrf_rx_config();

	}


// Return 1 if received packaged pertained to this node, else return 0.
    return ReturnFlag;
}
예제 #28
0
uint8_t SoftI2CWriteByte(uint8_t data)
{

	uint8_t i;

	for(i=0;i<8;i++)
	{
		SCLDDR|=((1<<SCL));
		_delay_loop_2(3);

		if(data & 0x80)
			SDADDR&=(~(1<<SDA));
		else
			SDADDR|=((1<<SDA));

		_delay_loop_2(5);

		SCLDDR&=(~(1<<SCL));
		_delay_loop_2(5);

		while((SCLPIN & (1<<SCL))==0);//pinA and PA0

		data=data<<1;
	}

	//The 9th clock (ACK Phase)
	SCLDDR|=((1<<SCL));
	_delay_loop_2(3);

	SDADDR&=(~(1<<SDA));
	_delay_loop_2(5);

	SCLDDR&=(~(1<<SCL));
	_delay_loop_2(5);

	uint8_t ack=!(SDAPIN & (1<<SDA));
	//set ack == 0 if error
	//means can't be congruent

	SCLDDR|=((1<<SCL));
	_delay_loop_2(5);

	return ack;

}
예제 #29
0
파일: adc.c 프로젝트: jmesmon/avrbits
void adc_init() {
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\nadc: init"));
	#endif
	power_adc_enable();

	//Set Voltage to AVCC with external capacitor at AREF pin
	ADMUX|= (uint8_t)(1<<REFS0);
	ADMUX&=(uint8_t)~(1<<REFS1);
	//ADMUX&=~(1<<ADLAR); // Default disabled
	
	// Enable ADC, Inturupt, Trigger mode and set prescaler
	//ADCSRA=(((1<<ADEN)|(1<<ADIE)|(1<<ADATE))&0b11111000)|(ADC_PRESCALE);
	ADCSRA|= (uint8_t)(1<<ADEN)|(1<<ADIE)|(1<<ADATE);
	ADCSRA = (uint8_t)(ADCSRA & 0b11111000)|((uint8_t)ADC_PRESCALE);
	
	// Enable Free Running Mode 
	ADCSRB|= (1<<7); //reserved bit.
	ADCSRB&= (uint8_t)~(0b111); //(ADTS2:0)=0
	
	// Disable Digital reads from analog pins
	DIDR0 |= (uint8_t)((1<<ADC4D)|(1<<ADC5D)|(1<<ADC6D)|(1<<ADC7D));
	
	set_sleep_mode(SLEEP_MODE_ADC);
	#if DEBUG_L(3)
	fprintf_P(stderr,PSTR("\nadc: init: setup convertions"));
	#endif
	adc_set_channel(curr_ch);
	//Start the convertions
	ADCSRA|= (1<<ADSC);

	// Wait one adc clock cycle and change the channel, done by interupt later.
	_delay_loop_2(ADC_CYCLE_DELAY);
	adc_set_channel(++curr_ch);
	
	// Wait for one set of convertions to complete.
	//_delay_loop_2(ADC_CYCLE_DELAY*26);
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\t[done]"));
	#endif
}
예제 #30
0
파일: main.c 프로젝트: davidgraeff/oldstuff
int	main(void) {
  wdt_enable(WDTO_1S);

  /* all outputs except INT0 and RxD/TxD */
  USBDDR = ~USBMASK;	   /* all outputs except USB data */
  USBOUT = 0;
  usbDeviceDisconnect();

  /* USB Reset by device only required on Watchdog Reset */
  _delay_loop_2(40000);   // 10ms

  usbDeviceConnect();
  usbInit();

  sei();
  for(;;) {	/* main event loop */
    wdt_reset();
    usbPoll();
  }
  return 0;
}