示例#1
0
文件: CC1101.cpp 项目: epatel/Cosa
bool
CC1101::begin(const void* config)
{
  // Reset the device
  m_cs.pulse(30);
  DELAY(30);
  strobe(SRES);
  DELAY(300);

  // Upload the configuration. Check for default configuration
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  write_P(IOCFG2, 
	  config ? (const uint8_t*) config : CC1101::config, 
	  CONFIG_MAX);
  spi.end();

  // Adjust configuration with instance specific state
  uint16_t sync = hton(m_addr.network);
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  write(PATABLE, 0x60);
  write(CHANNR, m_channel);
  write(ADDR, m_addr.device);
  write(SYNC1, &sync, sizeof(sync));
  spi.end();

  // Initiate device driver state and enable interrupt handler
  m_avail = false;
  spi.attach(this);
  m_irq.enable();
  return (true);
}
示例#2
0
文件: io.c 项目: jtn0514/SNESoIP
void sendOutput(snesIO port0, snesIO port1) {
    int i;

    // Initialise transmission.
    Port0LatchPORT |= (1 << Port0Latch);
    Port1LatchPORT |= (1 << Port1Latch);
    _delay_us(12);
    Port0LatchPORT &= ~(1 << Port0Latch);
    Port1LatchPORT &= ~(1 << Port1Latch);


    for (i = 0; i < 16; i++) {
        loop_until_bit_is_clear(Port0ClockPIN, Port0Clock);
        loop_until_bit_is_clear(Port1ClockPIN, Port1Clock);

        if ((port0 & (1 << i)) == 0)
            Port0DataPORT &= ~(1 << Port0Data);
        else
            Port0DataPORT |= (1 << Port0Data);

        if ((port1 & (1 << i)) == 0)
            Port1DataPORT &= ~(1 << Port1Data);
        else
            Port1DataPORT |= (1 << Port1Data);

        loop_until_bit_is_set(Port0ClockPIN, Port0Clock);
        loop_until_bit_is_set(Port1ClockPIN, Port1Clock);
    }
}
void calibration(uint8_t *measurement)
{
	
	uint8_t i=0;
	
	//Add button to calibrate every axis at once
	while((PINB & (1<<2))){}
	while(!(PINB & (1<<2))){}
		
	for (i=0;i<=1;i++)
	{
		
		//setting the channel to measure the corresponding angle
		ADMUX &= 0b11110000;
		ADMUX = ADMUX | i;
		//start the conversion. Then wait until conversion is completed. After redo this procedure in roder to make it more reliable
		ADCSRA |= (1<<ADSC);
		loop_until_bit_is_clear(ADCSRA,ADSC);
		ADCSRA |= (1<<ADSC);
		loop_until_bit_is_clear(ADCSRA,ADSC);
		//copy result into array
		measurement[i]=ADCH;
		
	}
	
	measurement[2] =(measurement[0]+measurement[1])/2;
			
}
示例#4
0
文件: CC1101.cpp 项目: epatel/Cosa
int 
CC1101::send(uint8_t dest, uint8_t port, const iovec_t* vec)
{
  // Sanity check the payload size
  if (vec == NULL) return (-1);
  size_t len = iovec_size(vec);
  if (len > PAYLOAD_MAX) return (-1);

  // Wait for the device to become idle before writing the frame
  await(IDLE_MODE);

  // Write frame header(length, dest, src, port)
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  write(TXFIFO, len + 3);
  write(TXFIFO, dest);
  write(TXFIFO, m_addr.device);
  write(TXFIFO, port);
  spi.end();

  // Write frame payload
  for (const iovec_t* vp = vec; vp->buf != NULL; vp++) {
    spi.begin(this);
    loop_until_bit_is_clear(PIN, Board::MISO);
    write(TXFIFO, vp->buf, vp->size);
    spi.end();
  }

  // Trigger the transmit
  strobe(STX);
  return (len);
}
示例#5
0
文件: adc.c 项目: di9it/therminator
uint16_t adcRead(uint8_t channel)
{
	// disable interrupt and wait for end of conversion
	ADCSRA = (0xFF & ~_BV(ADIE));
	loop_until_bit_is_clear(ADCSRA, ADSC);
	
	ADMUX = channel;		// set channel to read from
	ADCSRA |= _BV(ADSC);	// start conversion
	loop_until_bit_is_clear(ADCSRA, ADSC);	// wait to complete
	ADCSRA = 0b11011111;	// reenable interrupt
	return ADC;
}
示例#6
0
uint16_t 
AnalogPin::sample(Board::AnalogPin pin, Board::Reference ref)
{
  if (sampling_pin != NULL) return (0xffffU);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  ADMUX = (ref | (pin & 0x1f));
#if defined(MUX5)
  bit_write(pin & 0x20, ADCSRB, MUX5);
#endif
  bit_mask_set(ADCSRA, _BV(ADEN) | _BV(ADSC));
  loop_until_bit_is_clear(ADCSRA, ADSC);
  return (ADCW);
}
示例#7
0
int Get_Light()
{
	long Light;
	uint16_t x;							// variable to store the ADC result
	float reading;
	float freading;
	float fresult;
	ADCSRA = (1<<ADEN) | (1<<ADPS2);	// Enable ADC hardware i. e. ADEN=1 and
	// set ADC Pre-scaler to 16 -->
	// 1Mhz CPU clock/16 = 62.5kHz, i. e. ADPS2=1
	// (ADPS0 and ADPS1 default to 0)
	ADMUX = ADMUX | (0<<REFS1) | (1<<REFS0);   // Reference voltage = AVCC = (?V) in our case
	ADMUX = ADMUX & ~(1 << ADLAR);		// Right adjust the result registers
	// Select channel, e.g. pin 0 (which is the 1st pin at port A)
	ADMUX = ADMUX & 0b11111110;			// initialize channel bits (0)
	ADMUX = ADMUX | 0;					// set channel 0
	PORTA = PORTA | 0b00000001;			// switch off pull up resistor 0
	
	// 1st conversion:
	ADCSRA = ADCSRA | (1<<ADSC);    // start ADC
	loop_until_bit_is_clear(ADCSRA,ADSC);    // wait for completion of ADC
	// 2nd conversion:
	ADCSRA = ADCSRA | (1<<ADSC);    // start ADC
	loop_until_bit_is_clear(ADCSRA,ADSC);    // wait for completion of ADC
	reading=ADCW;    // store result in 16bit-variable
	//x=(x*0.0048828125)	
	reading = 1023-reading;
	freading = reading;	
	
	if(reading>=0 && reading<=155) {  // Darkness
		fresult = 0.0;
	}
	if(reading>155 && reading<=350) {
		// y = 0,0042273988x2 - 1,0130028488x + 55,4403759239
		fresult = 0.0042273988 * freading * freading - 1.0130028488 * freading + 55.4403759239;
	}
	if(reading>350 && reading<=650) {
		// y = 11,7717399221e0,0083003710x
		fresult = 11.7717399221 * pow(EULERNUMBER, (0.0083003710 * freading));
	}
	if(reading>650 && reading<=936) {
		// y = 0,3373539789e0,0134529914x
		fresult = 0.3373539789 * pow(EULERNUMBER, (0.0134529914 * freading)) + 448.5;
	}
	if(reading>980) {                 // Upper limit of measurement
		fresult = 100000.6;
	}	
		// Needs to be calibrated, after which the factor can be introduced here
	return fresult;
}
示例#8
0
文件: CC1101.cpp 项目: epatel/Cosa
int 
CC1101::recv(uint8_t& src, uint8_t& port, void* buf, size_t len, uint32_t ms)
{
  // Check if we need to wait for a message
  uint8_t size;
  if (!m_avail) {
    // Fix: Use wakeup on radio to reduce power during wait
    uint32_t start = RTC::millis();
    if (read_status().mode == IDLE_MODE) {
      strobe(SFRX);
      strobe(SRX);
    }
    do {
      while (!m_avail && ((ms == 0) || (RTC::since(start) < ms))) yield();
      if (!m_avail) return (-2);
      spi.begin(this);
      loop_until_bit_is_clear(PIN, Board::MISO);
      size = read(RXBYTES);
      spi.end();
    } while ((size & RXBYTES) == 0);
  }
  m_avail = false;

  // Read the payload size and check against buffer length
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  size = read(RXFIFO) - 3;
  if (size > len) {
    spi.end();
    strobe(SIDLE);
    strobe(SFRX);
    return (-1);
  }

  // Read the frame (dest, src, payload)
  m_dest = read(RXFIFO);
  src = read(RXFIFO);
  port = read(RXFIFO);
  read(RXFIFO, buf, size);
  spi.end();

  // Read the link quality status
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  read(RXFIFO, &m_recv_status, sizeof(m_recv_status));
  spi.end();

  // Fix: Add possible address checking for robustness
  return (size);
}
示例#9
0
//for manually reading adc channels
uint16_t adc_read(uint8_t channel)
{
  adc_start_conversion(channel);
  loop_until_bit_is_clear(ADCSRA, ADSC);

  return ADCW;
}
示例#10
0
文件: sensors.c 项目: abarbu/axon
//PING Sonar
//equations were taken from here and not independently verified:
//http://www.societyofrobots.com/member_tutorials/node/174
//http://www.societyofrobots.com/robotforum/index.php?topic=5123.msg40008
//http://www.societyofrobots.com/robotforum/index.php?topic=4656.30
//uses timer0
int sonar_Ping()
{
#define PINGPIN    0          // assign a pin to the Ping Sensor
#undef DDR
#undef PIN
#define DDR        DDRC
#define PORT       PORTC
#define PIN        PINC

  PORT_ON(DDR, PINGPIN);   // Switch PingPin to OUPUT
  // ------Trigger Pulse--------------
  PORT_OFF(PORT, PINGPIN);   // Bring PingPin low before starting trigger pulse
  delay_us(2);        //  Wait for 2 microseconds
  PORT_ON(PORT, PINGPIN);    // Bring PingPin High for 5us according to spec sheet.
  delay_us(5);       // Wait for 5 microseconds
  PORT_OFF(PORT, PINGPIN);; //  Bring PingPin Low and standby
  //--------End Trigger Pulse---------------------
  FLIP_PORT(DDR, PINGPIN);   // Switch PingPin to INPUT
  loop_until_bit_is_set(PIN, PINGPIN);     // Loop until the the PingPin goes high  (macro found in sfr_def.h)
  //clears timer, reset overflow counter
  reset_timer0();       //reset timer 0
  loop_until_bit_is_clear(PIN, PINGPIN);     // Loop until the the PingPin goes low  (macro found in sfr_def.h)
  //read timer0's overflow counter
  //255 is count before overflow, dependent on clock

  return (get_timer0_overflow()*255+TCNT0) * 2.068965517;//elapsed time x conversion
}
示例#11
0
int main()
{
	uint16_t potentiometerValue;		//Compare Potentiometer Value to certain number ranges
	initADC0();							//Initialize ADC

	DDRD=0x00;		//Port D pins as input
	PORTD=0xFF;		//Enable internal pull ups
	DDRB=0xFF;		//Set PORTB1 pin as output
	
	//Output compare OC1A 8 bit non inverted PWM
	//Clear OC1A on Compare Match, set OC1A at TOP
	ICR1=20000;	//ICR1=20000 defines 50Hz PWM, TOP=ICR1;
	TCCR1A|=(0<<COM1A0)|(1<<COM1A1)|(0<<COM1B0)|(0<<COM1B1)|(0<<FOC1A)|(0<<FOC1B)|(1<<WGM11)|(0<<WGM10);
	TCCR1B|=(0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);	//Start timer with prescaler 8

	OCR1A = 0;

	for (;;)
	{
		ADCSRA |= (1 << ADSC);					//Start ADC conversion
		loop_until_bit_is_clear(ADCSRA, ADSC);	//Wait until ADC conversion is done
		potentiometerValue = ADC;				//Read ADC in


		OCR1A = 1200 + (potentiometerValue*3.4);	//Every turn = 180 degrees of servo
	}
}
static uint8_t sampleADC(uint8_t ch)
{
   ADMUX = B(REFS0) | B(ADLAR) | ch;         // ADC Reference is AVcc; left adjusted result; select analog input
   ADCSRA |= B(ADSC);
   loop_until_bit_is_clear(ADCSRA, ADSC);
   return ADCH;
}
示例#13
0
uint16_t readADC( uint8_t channel )
{
	ADMUX = ( 0xF0 & ADMUX ) | channel;
	ADCSRA |= ( 1 << ADSC );
	loop_until_bit_is_clear( ADCSRA, ADSC );
	return ( ADC );
}
示例#14
0
static void
echo(double *ping_value,const uint8_t pingpin)
{
    /*Round-trip-time, unit microseconds*/
    uint16_t elapsed_time;
    /* ------Trigger Pulse--------------------------*/
    PORT_ON(DDR, pingpin);   
    PORT_OFF(PORT, pingpin);   
    _delay_us(2);       
    PORT_ON(PORT, pingpin);    
    _delay_us(5);       
    PORT_OFF(PORT, pingpin); 
    /*--------End Trigger Pulse---------------------*/
    /*--------Meassure pulse------------------------*/
    FLIP_PORT(DDR, pingpin);   
    loop_until_bit_is_set(PIN, pingpin);      
    reset_timer_0();       
    loop_until_bit_is_clear(PIN, pingpin);
    /*--------Stop Meassure pulse-------------------*/
    
    /* MAXCOUNTER is dependent on timer */
    elapsed_time = (tot_overflow * MAXCOUNTER) + TCNT0;

    /*Simple band-pass filter*/
    if (elapsed_time >= ping_distance_max)
    	*ping_value = 0;
    else if (elapsed_time <= ping_distance_min)
    	*ping_value = 0;
    else
		*ping_value = (elapsed_time * TO_CM);
    _delay_us(250);
    
}
示例#15
0
文件: spiro.c 项目: tommay-avr/spiro
static uint8_t
read_adc(void)
{
  ADCSRA |= _BV(ADSC);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  return ADCH;
}
/**
 * Gets the distance that chico has from an heat source.  It sends a sonar signal and calculates
 * the elapsed time between the send of the signal and the time when the signal comes back to chico.
 * It then converts the time into centimeters.
 *
 * Returns the distance in centimeters.
 */
int getDistance() {
	DDRA |= 0b00000001; //output
	PORTA &= 0b11111110;  // make sure pin A0 is LOW
	_delay_us(2);
	PORTA |= 0b00000001; // pulse HIGH
	_delay_us(5);
	PORTA &= 0b11111110; //pin A0 LOW
	DDRA &= 0b11111110; //input

	usart_fprintf_P(USART_0, PSTR("1\n"));

	unsigned long count = 0;

	while(!bit_is_set(PINA, PA0)) {
		_delay_us(5);
		count++;
		if(count > 200000){
			usart_fprintf_P(USART_0, PSTR("NOOOO CHICO!!!!!\n"));
			return -1;
		}
	}

	unsigned long start = time_in_microseconds();
	usart_fprintf_P(USART_0, PSTR("2\n"));
	loop_until_bit_is_clear(PINA, PA0);
	unsigned long end = time_in_microseconds();
	usart_fprintf_P(USART_0, PSTR("3\n"));
	long elapsedTime = end - start;

	int distance = elapsedTime/29/2;
	usart_fprintf_P(USART_0, PSTR("Dist: %lu \n"), end);
	return distance;
}
示例#17
0
int main(void) {

    uart_init();
    adc0_init();

    uint16_t adc_value;
    char adc_value_str[5];

    while(1) {

        // Get the ADC value
        ADCSRA |= (1 << ADSC);                 // Start ADC conversion
        loop_until_bit_is_clear(ADCSRA, ADSC); // Wait until done
        adc_value = ADC;                       // Read ADC in

        // Send the value to UART
        snprintf((char *) &adc_value_str, 5, "%d", adc_value);
        uart_send_string(adc_value_str);       // Send a string to UART
        uart_send_string("\r\n");              // Send a string to UART

        // Wait
        _delay_ms(1000);      // Wait 1000ms

    }

    return(0);
}
示例#18
0
文件: range.c 项目: gabm/Wetter
uint16_t range_get(void)
{

	//send out one pulse
	sbi(RANGE_PORT, RANGE_TRIG);
	_delay_us(15);
	cbi(RANGE_PORT,RANGE_TRIG);

	//wait for result
	TCNT1 = 0x0000;
	//wait for high val
	//while(!(timeout_event() || bit_is_set(RANGE_PIN, RANGE_ECHO)));
	loop_until_bit_is_set(RANGE_PIN, RANGE_ECHO);
	//timeout_stop();

	//if (timeout_event())
		//return 0xFFFF;


	//start timer with 1/8 prescaler
	sbi(TCCR1B, CS11);
	//timeout_start_ms(500);
	//while(!(timeout_event() || bit_is_clear(RANGE_PIN, RANGE_ECHO)));
	loop_until_bit_is_clear(RANGE_PIN, RANGE_ECHO);
	cbi(TCCR1B, CS11);
	//timeout_stop();

	//if (timeout_event())
		//return 0xFFFF;
	//the factor 1.3 is from 1/6.441MHz*8
	//58 comes from 58cm per µs
	return TCNT1*1.3/58.0;
}
示例#19
0
int main()
{

  uint8_t dir = CW;
  
  initPorts();
  initADC();

// accel section
  halfStep(dir);
  _delay_us(10000);
  halfStep(dir);
  _delay_us(6000);
  halfStep(dir);
  _delay_us(4667);
  halfStep(dir);
  _delay_us(3949);

  while (1)
  {
    stepperVal = MOTORPORT;
    switch (stepperVal)
    {
      case 1:
	ADMUX = (ADMUX & 0b1110000) | 0x1;
	adctested = 1;
	break;
      case 2:
	ADMUX = (ADMUX & 0b1110000) | 0x2;
	adctested = 1;
	break;
      case 4:
	ADMUX = (ADMUX & 0b1110000) | 0x3;
	adctested = 1;
	break;
      case 8:
	ADMUX = (ADMUX & 0b1110000) | 0x0;
	adctested = 1;
	break;
      default:
	ADMUX = (ADMUX & 0b1110000) | 0x31; // GND
	adctested = 0;
    }


    if (adctested)
    {
      // trigger ADC sample
      ADCSRA |= _BV(ADSC);
      // poll delay until sample done
      loop_until_bit_is_clear( ADCSRA, ADSC );
      DEBUGPORT = ADCH;
    }
    halfStep(dir);
    _delay_us(3500);

  } // main loop
  
}
示例#20
0
char* sysAdcStart(void){
	ADCSRA |= (1 << ADSC); //ADC Start Conversion
	loop_until_bit_is_clear (ADCSRA, ADSC);
	tempRead = ADCH - 20.5128;
	itoa(tempRead, ADCValue, 10);
	sysReverseArray(ADCValue, sizeof(ADCValue));
	return ADCValue;
}
示例#21
0
uint16_t read (uint8_t channel)
{
	ADMUX = _ref | (channel & 0x0f);
	ADCSRA |= _BV (ADSC);
	loop_until_bit_is_clear (ADCSRA, ADSC);
	ADCSRA |= _BV (ADIF);
	return ADC;
}
示例#22
0
uint16_t readADC(uint8_t channel) {
  //read ADC value from channel (ADC0 to ADC 5)
  //works for attiny85
  ADMUX = (0b11110000 & ADMUX) | channel;
  ADCSRA |= (1 << ADSC);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  return (ADC);
}
示例#23
0
文件: CC1101.cpp 项目: epatel/Cosa
void
CC1101::strobe(Command cmd)
{
  spi.begin(this);
  loop_until_bit_is_clear(PIN, Board::MISO);
  m_status = spi.transfer(header_t(cmd, 0, 0));
  spi.end();
}
示例#24
0
uint16_t adc_v_read()
{
	// AREF (no ext. cap), right adjust, single ended input (ADC7), 1X gain
	ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
	_delay_us(200);
	ADCSR |= _BV(ADSC);
	loop_until_bit_is_clear(ADCSR, ADSC);
	return (ADC);
}
示例#25
0
uint16_t adc_a_read()
{
	// AREF (no ext. cap), right adjust, differential input (ADC8-ADC9), 20X gain
	ADMUX = _BV(REFS1) | _BV(MUX4) | _BV(MUX3) | _BV(MUX0);
	_delay_us(200);
	ADCSR |= _BV(ADSC);
	loop_until_bit_is_clear(ADCSR, ADSC);
	return (ADC);
}
示例#26
0
uint16_t 
AnalogPin::bandgap(uint16_t vref)
{
  loop_until_bit_is_clear(ADCSRA, ADSC);
  ADMUX = (Board::AVCC_REFERENCE | Board::VBG);
#if defined(MUX5)
  bit_clear(ADCSRB, MUX5);
#endif
  bit_set(ADCSRA, ADEN);
  DELAY(1000);
  bit_set(ADCSRA, ADSC);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  uint16_t sample;
  synchronized {
    sample = ADCW;
  }
  return ((vref * 1024L) / sample);
}
示例#27
0
uint16_t readADC(uint8_t channel)
{
  // Clear old input channel selections, then set to this channel
  ADMUX = (0b11110000 & ADMUX) | channel;

  // Start conversion
  ADCSRA |= (1 << ADSC);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  return ADC;
}
示例#28
0
// Polling method
uint16_t adc_read(int channel)
{
  ADMUX = (ADMUX & (_BV(REFS1)|_BV(REFS0)|_BV(ADLAR))) | channel;

  // Start conversion
  ADCSRA |= _BV(ADSC);
  loop_until_bit_is_clear(ADCSRA, ADSC);

  return ADCW;
}
示例#29
0
void ADCInitilize(void)
{
  PORTC = 0x00;
  DDRC = 0x00;

  ADMUX |= 0 << REFS0 | 0 << REFS1;
  ADMUX |= 1 << ADLAR;
  ADCSRA |= 1 << ADEN | 1 << ADPS2 | 1 << ADPS1 | 0 << ADPS0 | 1 << ADSC;
  loop_until_bit_is_clear(ADCSRA,ADSC);
}
示例#30
0
文件: adc.c 项目: 1234tester/ethersex
uint16_t
adc_get_setref(uint8_t ref, uint8_t channel)
{
  /* select reference and channel */
  ADMUX = (ref & 0xc0) | (channel & 0x1f);
  if (last_ref != ref)
  {
    ADCSRA |= _BV(ADSC);
    loop_until_bit_is_clear(ADCSRA, ADSC);
    last_ref = ref;
  }

  /* Start adc conversion */
  ADCSRA |= _BV(ADSC);
  /* Wait for completion of adc */
  loop_until_bit_is_clear(ADCSRA, ADSC);

  return ADC;
}