Example #1
0
void init_NRF24L01(void)
{
    //init SPI
    SPCR=0x51; //set this to 0x50 for 1 mbits 
    SPSR=0x00; 
    
    //inerDelay_us(100);
    _delay_us(100);
 	cbi(PORTC,CE);//CE=0;    // chip enable
 	sbi(PORTC,CSN);//CSN=1;   // Spi disable
 	//SCK=0;   // Spi clock line init high
	SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    //
	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); //
	SPI_RW_Reg(WRITE_REG + EN_AA, 0x00);      // EN P0, 2-->P1
	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  //Enable data P0
	SPI_RW_Reg(WRITE_REG + RF_CH, 2);        // Chanel 0 RF = 2400 + RF_CH* (1or 2 M)
	SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); // Do rong data truyen 32 byte
	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   		// 1M, 0dbm
	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);   		 // Enable CRC, 2 byte CRC, Send

}
Example #2
0
void setup()
{
	cbi(TIMSK0, TOIE0);

	////////////////////////////
	pinMode(A0, OUTPUT);
	pinMode(A1, OUTPUT);

	pinMode(A2, INPUT);
	pinMode(A3, INPUT);
	pinMode(A4, INPUT);
	pinMode(A5, INPUT);

	digitalWrite(A0, LOW);
	digitalWrite(A1, HIGH);

	Serial.begin(9600);

	memset(color, 0x00, LENGTH);
	tm.sendData(color, LENGTH);
}
Example #3
0
//! Writes a byte of data to the LCD.
static void writeLcd(u08 data)
{
	//Reverse the bit order of the data, due to LCD connections to the data bus being backwards.
	//This line doesn't affect the bus, so this can be done before the interrupts are disabled.
	REVERSE(data);
	//Disable interrupts in this block to prevent the servo ISR (which shares the same data bus)
	//from interrupting in the middle of the sequence and messing with the bus.
	//ATOMIC_RESTORESTATE is used so that this function can be called from
	//user code/ISRs without unexpected side effects.
	ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
	{
		//set the LCD's E (Enable) line high, so it can fall later
		sbi(PORTD, PD6);
		//write the data to the bus
		PORTC = data;
		//brief delay to allow the data to fully propagate to the LCD
		delayUs(1);
		//set the LCD's E (Enable) line low to latch in the data
		cbi(PORTD, PD6);
	}
}
int main(void)
{    
	DDRB=0XC7;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PB.2 IS  INPUT
									//ALL OTHERS ARE OUTPUT
	DDRD=0;
	//DDRD=0XF1;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PD.1, PD.2 AND PD.3 ARE INPUT
									//ALL OTHERS ARE OUTPUT

	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 
    for (;;) 					 	//BLINK LED2
	{	
	
	
    _delay_ms(500);
	cbi(PORTB,2);			//LED OFF
	
		setstate('1','F');
		_delay_ms(1000);
		setstate('2','B');
		_delay_ms(1000);

	
	
//    _delay_ms(2000);
//	motormove('2','F');
//    _delay_ms(2000);
//	motormove('1','F');
//    _delay_ms(2000);
    }
}
Example #5
0
//brake selected motor
//full brake assumed
void brake(uint8_t motorSelect)
{
   switch(motorSelect)
   {
      case LEFT_MOTOR:
         cbi(PORTD, REVERSE_LEFT);
         cbi(PORTD, FORWARD_LEFT);
         break;
      case RIGHT_MOTOR:
         cbi(PORTD, REVERSE_RIGHT);
         cbi(PORTD, FORWARD_RIGHT);
         break;
      case BOTH:
         // both motors reverse
         cbi(PORTD, REVERSE_LEFT);
         cbi(PORTD, REVERSE_RIGHT);
         cbi(PORTD, FORWARD_LEFT);
         cbi(PORTD, FORWARD_RIGHT);
         break;
   }
   PWM_RIGHT(MAX_BRAKE);
   PWM_LEFT(MAX_BRAKE);
}
Example #6
0
//Plays the winner sounds
void play_winner(void)
{
  uint8_t x, y, z;

  //printf("You win!\n", 0);

  sbi(LED_RED_PORT, LED_RED);
  sbi(LED_YELLOW_PORT, LED_YELLOW);
  sbi(LED_BLUE_PORT, LED_BLUE);
  sbi(LED_GREEN_PORT, LED_GREEN);

  for(z = 0 ; z < 4 ; z++)
    {
      if(z == 0 || z == 2)
        {
          cbi(LED_RED_PORT, LED_RED);
          cbi(LED_YELLOW_PORT, LED_YELLOW);
          sbi(LED_BLUE_PORT, LED_BLUE);
          sbi(LED_GREEN_PORT, LED_GREEN);
        }
      else
        {
          sbi(LED_RED_PORT, LED_RED);
          sbi(LED_YELLOW_PORT, LED_YELLOW);
          cbi(LED_BLUE_PORT, LED_BLUE);
          cbi(LED_GREEN_PORT, LED_GREEN);
        }

      for(x = 250 ; x > 70 ; x--)
        {
          for(y = 0 ; y < 3 ; y++)
            {
              //Toggle the buzzer at various speeds
              sbi(BUZZER2_PORT, BUZZER2);
              cbi(BUZZER1_PORT, BUZZER1);

              delay_us(x);

              cbi(BUZZER2_PORT, BUZZER2);
              sbi(BUZZER1_PORT, BUZZER1);

              delay_us(x);
            }
        }
    }

}
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);
	}
	
}
Example #8
0
// enable and initialize the software uart
void uartswInit(void)
{
	// initialize the buffers
	uartswInitBuffers();
	// initialize the ports
	sbi(UARTSW_TX_DDR, UARTSW_TX_PIN);
	#ifdef UARTSW_INVERT
	cbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#else
	sbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#endif
	cbi(UARTSW_RX_DDR, UARTSW_RX_PIN);
	#ifdef UARTSW_INVERT
	cbi(UARTSW_RX_PORT, UARTSW_RX_PIN);
	#else
	sbi(UARTSW_RX_PORT, UARTSW_RX_PIN);
	#endif
	// initialize baud rate
	uartswSetBaudRate(9600);

	// setup the transmitter
	UartswTxBusy = FALSE;
	// disable OC0A interrupt
	cbi(TIMSK0, OCIE0A);
	// attach TxBit service routine to OC0A
	timerAttach(TIMER0OUTCOMPAREA_INT, uartswTxBitService);

	// setup the receiver
	UartswRxBusy = FALSE;
	// disable 0C0B interrupt
	cbi(TIMSK0, OCIE0B);
	// attach RxBit service routine to OC0B
	timerAttach(TIMER0OUTCOMPAREB_INT, uartswRxBitService);
	// INT1 trigger on rising/falling edge
	#ifdef UARTSW_INVERT
	sbi(EICRA, ISC11);
	sbi(EICRA, ISC10);
	#else
	sbi(EICRA, ISC11);
	cbi(EICRA, ISC10);
	#endif
	// enable INT1 interrupt
	sbi(EIMSK, INT1);

	// turn on interrupts
	sei();
}
void HardwareSerial::begin(unsigned long baud, byte config)
{
    // Try u2x mode first
    uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
    *_ucsra = 1 << U2X0;

    // hardcoded exception for 57600 for compatibility with the bootloader
    // shipped with the Duemilanove and previous boards and the firmware
    // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot
    // be > 4095, so switch back to non-u2x mode if the baud rate is too
    // low. Disable to get a more accurate baud rate at 57600
//#define EXCEPTION_FOR_57600
#if defined (EXCEPTION_FOR_57600)
    if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095))
#else
    if (baud_setting >4095)
    {
        *_ucsra = 0;
        baud_setting = (F_CPU / 8 / baud - 1) / 2;
    }
#endif

        // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
        *_ubrrh = baud_setting >> 8;
    *_ubrrl = baud_setting;

    _written = false;

    //set the data bits, parity, and stop bits
#if defined(__AVR_ATmega8__)
    config |= 0x80; // select UCSRC register (shared with UBRRH)
#endif
    *_ucsrc = config;

    sbi(*_ucsrb, RXEN0);
    sbi(*_ucsrb, TXEN0);
    sbi(*_ucsrb, RXCIE0);
    cbi(*_ucsrb, UDRIE0);
}
Example #10
0
bool resetXBeeSoft(void)
{
    uint8_t messageState = 0;   /* Progress in message reception */
    rxFrameType rxMessage;
    uint8_t count = 0;
    bool reset = false;
    while (! reset)
    {
        sendATFrame(2,"FR");

/* The frame type we are handling is 0x88 AT Command Response */
        uint16_t timeout = 0;
        messageState = 0;
        uint8_t messageError = XBEE_INCOMPLETE;
/* Wait for response. If it doesn't come, try sending again. */
        while (messageError == XBEE_INCOMPLETE)
        {
            messageError = receiveMessage(&rxMessage, &messageState);
            if (timeout++ > 30000) break;
        }
/* If errors occur, or frame is the wrong type, just try again */
        reset = ((messageError == XBEE_COMPLETE) && \
                 (rxMessage.message.atResponse.status == 0) && \
                 (rxMessage.frameType == AT_COMMAND_RESPONSE) && \
                 (rxMessage.message.atResponse.atCommand1 == 'F') && \
                 (rxMessage.message.atResponse.atCommand2 == 'R'));
#ifdef TEST_PORT
        if (! reset)
        {
            _delay_ms(200);
            sbi(TEST_PORT,TEST_PIN);    /* Set pin on */
            _delay_ms(200);
            cbi(TEST_PORT,TEST_PIN);    /* Set pin off */
        }
#endif
        if (count++ > 10) break;
    }
    return reset;
}
void HardwareSerial::begin(unsigned long baud)
{
    uint16_t baud_setting;
    bool use_u2x = true;

#if F_CPU == 16000000UL
    // hardcoded exception for compatibility with the bootloader shipped
    // with the Duemilanove and previous boards and the firmware on the 8U2
    // on the Uno and Mega 2560.
    if (baud == 57600) {
        use_u2x = false;
    }
#endif

try_again:

    if (use_u2x) {
        *_ucsra = 1 << _u2x;
        baud_setting = (F_CPU / 4 / baud - 1) / 2;
    } else {
        *_ucsra = 0;
        baud_setting = (F_CPU / 8 / baud - 1) / 2;
    }

    if ((baud_setting > 4095) && use_u2x)
    {
        use_u2x = false;
        goto try_again;
    }

    // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
    *_ubrrh = baud_setting >> 8;
    *_ubrrl = baud_setting;

    sbi(*_ucsrb, _rxen);
    sbi(*_ucsrb, _txen);
    sbi(*_ucsrb, _rxcie);
    cbi(*_ucsrb, _udrie);
}
Example #12
0
void BiscuitSerial::begin(unsigned long baud)
{
  uint16_t baud_setting;
  bool use_u2x = true;

try_again:
  
  if (use_u2x) {
    UCSR0A = 1 << U2X0;
    baud_setting = (F_CPU / 4 / baud - 1) / 2;
  } else {
    UCSR0A = 0;
    baud_setting = (F_CPU / 8 / baud - 1) / 2;
  }
  
  if ((baud_setting > 4095) && use_u2x)
  {
    use_u2x = false;
    goto try_again;
  }

  // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  UBRR0H = baud_setting >> 8;
  UBRR0L = baud_setting;

  sbi(UCSR0B, RXEN0);
  sbi(UCSR0B, TXEN0);
  sbi(UCSR0B, RXCIE0);  
  cbi(UCSR0B, UDRIE0);

  // setup 9bit
  /*
  sbi(UCSR0C, UCSZ00);
  sbi(UCSR0C, UCSZ01);
  sbi(UCSR0B, UCSZ02);
  */
  // setup even parity
  //sbi(UCSR0C, UPM01);
}
Example #13
0
void uartswSendByte(u08 data)
{
	// wait until uart is ready
	while(UartswTxBusy);
	// set busy flag
	UartswTxBusy = TRUE;
	// save data
	UartswTxData = data;
	// set number of bits (+1 for stop bit)
	UartswTxBitNum = 9;
	
	// set the start bit
	#ifdef UARTSW_INVERT
	sbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#else
	cbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#endif
	// schedule the next bit
	outb(OCR2, inb(TCNT2) + UartswBaudRateDiv);
	// enable OC2 interrupt
	sbi(TIMSK, OCIE2);
}
Example #14
0
//------------------------------------------------------------------------------
//     				===== Uart_Init =====
//             		: 희망하는 속도로 시리얼 통신을 초기화 한다.
//------------------------------------------------------------------------------
void Init_Uart(U08 Com, U32 Uart_Baud)
{
	U16 Temp_UBRR;

	Temp_UBRR = AVR_CLK/(16L * Uart_Baud) - 1;   	// 통신 보레이트 계산식
													// U2X = 0 일때 (일반모드)
	
    //---------------------------- UART0 초기화 --------------------------------
	if( Com == UART0 )                           	
	{
		UBRR0H = (Temp_UBRR >> 8);              // 통신속도 설정
		UBRR0L = (Temp_UBRR & 0x00FF);
		
		UCSR0A = (0<<RXC0)  | (1<<UDRE0);		// 수신,송신 상태비트 초기화
        UCSR0B = (1<<RXEN0) | (1<<TXEN0);  		// 수신,송신 기능 활성화
		UCSR0C = (3<<UCSZ00);				// START 1비트/DATA 8비트/STOP 1비트
		
		cbi( DDRE, 0 );                         // RXD0 핀 입력으로 설정
		sbi( DDRE, 1 );                         // TXD0 핀 출력으로 설정
		
		UCSR0B |=  (1<<RXCIE0);	             	// 수신인터럽트0 활성화
	}
Example #15
0
void DoAConversion(void)
{
   if (ShwattDaqState & WaitingForXaxis)
   {
      lastDaqTime_ = currentDaqTime_;
      ADMUX = XaxisPinMuxValue; //set pin to read
      sbi(ADCSRA, ADSC);        //start ADC
   } else if (ShwattDaqState & WaitingForZaxis)
   {
      ADMUX = ZaxisPinMuxValue;
      sbi(ADCSRA, ADSC);
   } else if (ShwattDaqState & WaitingForYrate)
   {
      ADMUX = YratePinMuxValue;
      sbi(ADCSRA, ADSC);
   } else
   {
      cbi(ADCSRA, ADEN);   //disable ADC
      ShwattDaqComplete(); //handler function for the end of the DAQ
   }

}
Example #16
0
void twi_close()
{	
	// de-activate internal pull-up resistors
	cbi(PORTD, 0);
	cbi(PORTD, 1);
	
	
	sbi(TWSR, TWPS0);
	sbi(TWSR, TWPS1);
	
  // disable twi module, acks, and twi interrupt
	sbi(TWCR,TWINT);
	sbi(TWCR,TWSTO);
	cbi(TWCR,TWEA);
	cbi(TWCR,TWSTA);
	cbi(TWCR,TWWC);
	sbi(TWCR,TWEN);
	cbi(TWCR,TWIE);
	cbi(PRR0,PRTWI);
}
Example #17
0
//! initializes software PWM system
void servoInit(void)
{
	u08 channel;
	// disble the timer1 output compare A interrupt
	cbi(TIMSK, OCIE1A);
	// set the prescaler for timer1
	timer1SetPrescaler(TIMER_CLK_DIV256);
	// attach the software PWM service routine to timer1 output compare A
	timerAttach(TIMER1OUTCOMPAREA_INT, servoService);
	// enable and clear channels
	for(channel=0; channel<SERVO_NUM_CHANNELS; channel++)
	{
		// set minimum position as default
		ServoChannels[channel].duty = SERVO_MIN;
		// set default port and pins assignments
		ServoChannels[channel].port = _SFR_IO_ADDR(SERVO_DEFAULT_PORT);
		//ServoChannels[channel].port = (unsigned char)&SERVO_DEFAULT_PORT;
		ServoChannels[channel].pin = (1<<channel);
		// set channel pin to output
		// THIS IS OBSOLETED BY THE DYNAMIC CHANNEL TO PORT,PIN ASSIGNMENTS
		//outb(SERVODDR, inb(SERVODDR) | (1<<channel));
	}
	// set PosTics
	ServoPosTics = 0;
	// set PeriodTics
	ServoPeriodTics = SERVO_MAX*9;
	// set initial interrupt time
	u16 OCValue;
	// read in current value of output compare register OCR1A
	OCValue =  inb(OCR1AL);		// read low byte of OCR1A
	OCValue += inb(OCR1AH)<<8;	// read high byte of OCR1A
	// increment OCR1A value by nextTics
	OCValue += ServoPeriodTics; 
	// set future output compare time to this new value
	outb(OCR1AH, (OCValue>>8));			// write high byte
	outb(OCR1AL, (OCValue & 0x00FF));	// write low byte
	// enable the timer1 output compare A interrupt
	sbi(TIMSK, OCIE1A);
}
int main(void)
{
	
	DDRA=0xF0;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PA.4, PA.5, PA.6 AND PA.7 ARE OUTPUT
									//ALL OTHERS ARE INPUT
									
	DDRB=0XFB;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PB.2 IS  INPUT
									//ALL OTHERS ARE OUTPUT
	
	DDRD=0XF1;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PD.1, PD.2 AND PD.3 ARE INPUT
									//ALL OTHERS ARE OUTPUT

	sbi(PORTA,4);					//LED1 ON (INDICATION FOR READY TO USE)
	
	
	sbi(PORTB,2);					//ENABLE PULL UP FOR SWITCH INT2
	sbi(PORTD,1);					//ENABLE PULL UP FOR SW1
	sbi(PORTD,2);					//ENABLE PULL UP FOR SWITCH INT0
	sbi(PORTD,3);					//ENABLE PULL UP FOR SWITCH INT1
    

    for (;;)								/* loop forever */ 
	{                           
		sbi(PORTA,5);
        wait_debounce();				/* wait until push button sw1 is pressed */
		cbi(PORTA,5);
		wait_debounce();				/* wait until push button sw1 is pressed */
    }
}
Example #19
0
static int SpiMemInit(uint8_t cs, uint16_t *pages, uint16_t *pagesize)
{
    uint8_t fs;
    
    /* Init SPI memory chip select. */
    if (cs == SPIMEM_CS_BIT) {
        cbi(SPIMEM_CS_PORT, cs);
    } else {
        sbi(SPIMEM_CS_PORT, cs);
    }
    sbi(SPIMEM_CS_DDR, cs);
    
    /* Initialize the SPI interface. */
    SpiInit();
    
    /* Read the status register for a rudimentary check. */
    fs = SpiMemStatus(cs);
    if(fs & 0x80) {
        fs = (fs >> 2) & 0x0F;
        *pagesize = 264;
        if(fs == 3) {
            *pages = 512;
            return 0;
        }
        else if(fs == 5) {
            *pages = 1024;
            return 0;
        }
        else if(fs == 7) {
            *pages = 2048;
            return 0;
        }
        else if(fs == 13) {
            *pagesize = 528;
            *pages = 8192;
            return 0;
        }
    }
Example #20
0
size_t FTDI_FT245::write(uint8_t c)
{
  // check state of TXE line
  // TXE = L  => Tx fifo is ready to be written
  // TXE = H  => Tx fifo full or not ready
  while(*_cont_pin & (1<<_txe))
    ;	

  sbi(*_cont_port, _wr);
  *_data_ddr = 0xFF;
  *_data_port = c;
  
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("nop");
  
  cbi(*_cont_port, _wr);
  *_data_ddr = 0x00;  // Put Port DDR Back to Input
  *_data_port = 0xFF;  // Enable The Pull-Ups

  return 1;
}
Example #21
0
// Fonctions spécifiques au fonctionnement en mode PWM
void timer1pwm_init(T1_presc prescaler, T1_mode_t mode) {		// Configuration initiale en mode PWM

	// Configuration en entrée des ports (PWM désactivée par défaut)
	cbi(OC1A_DDR, OC1A_PIN);
	cbi(OC1B_DDR, OC1B_PIN);
	cbi(OC1C_DDR, OC1C_PIN);

	// Configuration à l'état bas par défaut
	cbi(OC1A_PORT, OC1A_PIN);
	cbi(OC1B_PORT, OC1B_PIN);
	cbi(OC1C_PORT, OC1C_PIN);

	timer1_setpresc(prescaler);
	timer1_setmode(mode);
	timer1_compare_setmodeA(T1_OC_MODE_CLEAR_ON_COMPARE);
	timer1_compare_setmodeB(T1_OC_MODE_CLEAR_ON_COMPARE);
	timer1_compare_setmodeC(T1_OC_MODE_CLEAR_ON_COMPARE);
}
Example #22
0
/*---------------------------------------------------------------------------*/
void init_hardware()
{
    /* init interrupt pin */
    pinMode(B,2,INPUT);

    /* init led ... */
    led0_init();
    led0_low();

    /* init hardware pins */
    nrf24_init();
    
    /* disable the analog comparator */
    ACSR = (1<<ACD); 

    /* channel #2 , payload length: 4 */
    nrf24_config(2,32);

    /* get the rx address from EEPROM */
    rx_address[0] = eeprom_read_byte((uint8_t*)10);
    rx_address[1] = eeprom_read_byte((uint8_t*)11);
    rx_address[2] = eeprom_read_byte((uint8_t*)12);
    rx_address[3] = eeprom_read_byte((uint8_t*)13);
    rx_address[4] = eeprom_read_byte((uint8_t*)14);

    /* set the device addresses */
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);    

    /* initial power-down */
    nrf24_powerDown();

    /* disable digital buffer at the adc pin */
    cbi(DIDR0,ADC1D);

    PRR = (1<<PRTIM1)|(1<<PRTIM0)|(0<<PRADC)|(0<<PRUSI);
}
void one_byte_w(uint8_t cmd, uint8_t msb)
{
	TWCR = 0x00;
	TWBR = 64;
	//TWSR = (1 << TWPS1);
	cbi(TWCR, TWEA);	
	sbi(TWCR, TWEN);
	delay_us(10);
	
	//Send start condition 
	i2cSendStart();		
    i2cWaitForComplete();
	delay_us(10);	
	printf("TWSR is: %x\n", (TWSR & 0xFC));
	
	
	// send slave device address with write
	i2cSendByte(SLA_W);	
	i2cWaitForComplete();
	delay_us(10);
	printf("TWSR is: %x\n", (TWSR & 0xFC));
	
	TWDR = cmd;
	TWCR = (1<<TWINT)|(1<<TWEN);	
	i2cWaitForComplete();
	delay_us(10);
	
	TWDR = msb;
	TWCR = (1<<TWINT)|(1<<TWEN);	
	i2cWaitForComplete();
	delay_us(10);

	
	delay_us(10);
    i2cSendStop();
	
}
void init_acc(void)
{
	//Initializing the various ports
	//setting as input
	cbi(ACC_DDR,PIN_X);
	cbi(ACC_DDR,PIN_Y);
	cbi(ACC_DDR,PIN_Z);
	
	//and pulling them low
	cbi(ACC_PORT,PIN_X);
	cbi(ACC_PORT,PIN_Y);
	cbi(ACC_PORT,PIN_Z);
	
	//initializing the adc
	init_adc();

	return;
}
Example #25
0
void WriteToCC1020Register(unsigned char addr, unsigned char data)
{
  cbi(PORT_CS,CC1020);		        // Clear SS_bar to enable slave
  //char array1[25];
  unsigned char dummy;
  dummy=SPDR;

  SPDR=(addr<<1)|0x01;			// Write address to CC1020, left shift,write bit is always 1
	
  // Wait until data is written 
  while (! (SPSR & (1<<SPIF)));
  if(WCOL==1)
    {
      //sprintf(array1,"\nCollision detected!!!");
      //usart_transmit(array1);
    }
	
  dummy=SPDR;  
  SPDR=data;

  while (! (SPSR & (1<<SPIF)));
  //PORTD |=0x01;			//********1******
  sbi(PORT_CS,CC1020); 			//PSEL must be made high for atleast 50ns after each read/write operation	
}
void HardwareSerial::writeBuffered(uint8_t c) {

	unsigned char tmphead = (_tx_buffer->head + 1) & TX_BUFFER_MASK;

	// Wait for free space in buffer
	while (tmphead == _tx_buffer->tail);

	cbi(*_ucsrb, _txcie);
//	cli();

	bool empty = (_tx_buffer->head==_tx_buffer->tail) && ((*_ucsra) & (1 << _udre));

	if(empty ) {
		sbi(*_ucsrb, _txcie);
//		sei();
		*_udr = c;
	} else {
		_tx_buffer->buffer[_tx_buffer->head] = c; // Store data in buffer
		_tx_buffer->head = tmphead; // Store new index
//		sei();
		sbi(*_ucsrb, _txcie);
	}

}
Example #27
0
void i2cMasterSendDiag(u08 deviceAddr, u08 length, u08* data)
{
	// this function is equivalent to the i2cMasterSendNI() in the I2C library
	// except it will print information about transmission progress to the terminal

	// disable TWI interrupt
	cbi(TWCR, TWIE);

	// send start condition
	i2cSendStart();
	i2cWaitForComplete();
	rprintf("STA-");

	// send device address with write
	i2cSendByte( deviceAddr&0xFE );
	i2cWaitForComplete();
	rprintf("SLA+W-");
	
	// send data
	while(length)
	{
		i2cSendByte( *data++ );
		i2cWaitForComplete();
		rprintf("DATA-");
		length--;
	}
	
	// transmit stop condition
	// leave with TWEA on for slave receiving
	i2cSendStop();
	while( !(inb(TWCR) & BV(TWSTO)) );
	rprintf("STO");

	// enable TWI interrupt
	sbi(TWCR, TWIE);
}
Example #28
0
void init(void)
{
  // Enable outputs
  DDRC = 0b11111111;
  PORTC = 0xFF;
  // Turn on PWR Led
  cbi(PORTC, PORTC0);

  InitLeds();
  	
  uart0_init();
  uart0_setbaud(HOST_BAUD_RATE);

  rs485_init();
  rs485_setbaud(BUS_BAUD_RATE);

  ctable_init();
  adc_init();
  
  //timer1_init();
  //timer1_set_overflow_callback(timer1_callback);

  sei();
}
Example #29
0
static void pwm1Pins(_t_pwm_pol pol)
{
	if (pol==PWM_NORMAL_A)
	{
		cbi(TCCR1A, COM1A0);
		sbi(TCCR1A, COM1A1);
		write16bit(0, &OCR1AH, &OCR1AL);
		sbi(DDRB, DDB1);
		cbi(PORTB, PORTB1); /* out low */
		pwmPol1A = pol;
	}
	else if (pol==PWM_INVERT_A)
	{
		sbi(TCCR1A, COM1A0);
		sbi(TCCR1A, COM1A1);
		write16bit(0, &OCR1AH, &OCR1AL);
		sbi(DDRB, DDB1);
		cbi(PORTB, PORTB1); /* out low */
		pwmPol1A = pol;
	}
	if (pol==PWM_NORMAL_B)
	{
		cbi(TCCR1A, COM1B0);
		sbi(TCCR1A, COM1B1);
		write16bit(0, &OCR1BH, &OCR1BL);
		sbi(DDRB, DDB2);
		cbi(PORTB, PORTB2); /* out low */
		pwmPol1A = pol;
	}
	else if (pol==PWM_INVERT_B)
	{
		sbi(TCCR1A, COM1B0);
		sbi(TCCR1A, COM1B1);
		write16bit(0, &OCR1BH, &OCR1BL);
		sbi(DDRB, DDB2);
		cbi(PORTB, PORTB2); /* out low */
		pwmPol1B = pol;
	}
	else
	{
		/* Usi futuri: funzionalità di toggle di OC0A*/
	}
}
Example #30
0
//! enable and initialize the software uart
void uartswInit(void)
{
    // initialize the buffers
	uartswInitBuffers();
	// initialize the ports
	sbi(UARTSW_TX_DDR, UARTSW_TX_PIN);
	#ifdef UARTSW_INVERT
	cbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#else
	sbi(UARTSW_TX_PORT, UARTSW_TX_PIN);
	#endif
	cbi(UARTSW_RX_DDR, UARTSW_RX_PIN);
	cbi(UARTSW_RX_PORT, UARTSW_RX_PIN);
	// initialize baud rate
	uartswSetBaudRate(9600);
	
	// setup the transmitter
	UartswTxBusy = FALSE;
	// disable OC2 interrupt
	cbi(TIMSK, OCIE2);
	// attach TxBit service routine to OC2
	timerAttach(TIMER2OUTCOMPARE_INT, uartswTxBitService);
		
	// setup the receiver
	UartswRxBusy = FALSE;
	// disable OC0 interrupt
	cbi(TIMSK, OCIE0);
	// attach RxBit service routine to OC0
	timerAttach(TIMER0OUTCOMPARE_INT, uartswRxBitService);
	// INT2 trigger on rising/falling edge
	#ifdef UARTSW_INVERT
	sbi(MCUCSR, ISC2);	// rising edge
	#else
	cbi(MCUCSR, ISC2);	// falling edge
	#endif
	// enable INT2 interrupt
	sbi(GICR, INT2);

	// turn on interrupts
	sei();
}