Exemple #1
0
/***********************************************************
* 	   
* startDownHandler ISR
*  
***********************************************************/
void startDownHandler(void)
{
	  //maybe most of this stuff should happen in loop? (speed ok..but maybe better outside?)
	  unsigned long currentTime = millis();

		#ifdef TT_SHIELD
				 PCintPort::detachInterrupt(START_BUTTON);
			     PCintPort::attachInterrupt(START_BUTTON,startUpHandler,RISING);  
		#else
				 detachInterrupt(0);
				 attachInterrupt(0,startUpHandler,RISING); //trigger ISR function on start button press.
		#endif

		TTUI::pTTUI->startBttnHold = true;
		TTUI::pTTUI->holdBttnStart = currentTime/1000;
		
	
	
	
}
void BrightnessTest(CCShield& brd,unsigned long int time)
{
  FlickerBrightness f(brd);
  unsigned long int i;
  
#if 0
  for (i=0;i<CCShield_NUMOUTS;i++)
    {
      f.brightness[cvt(i)]=i*3;
    }
#endif
  
  brd.setBrightness(255);  // By default highest brightness unless the dial is moved
  
  state = 1;  // Set the trigger variable, THEN attach the interrupt
  attachInterrupt(wakePin-2,sleepInterrupt,RISING);
  unsigned long int cnt=0;
        
  for (uint8_t j=0;j<CCShield_NUMOUTS;j++)
        {
        f.brightness[j] = 0;
        }

  for (i=0;(i<time) && state;i++)
  {
    //attachInterrupt(wakePin-2,sleepInterrupt,RISING);
    if ((i&63)==0)
      {
      for (uint8_t j=0;j<CCShield_NUMOUTS;j++)
        {
        if ((j&3)==0) f.brightness[j]=(cnt&255);
        //else f.brightness[j] = 0;
        }
        cnt+=1;
        if ((cnt&255)==0) f.shift();
      }

    f.loop();
  }
  
}
void rc_init() {
  pinMode(RC_CH1_INPUT, INPUT);
  pinMode(RC_CH2_INPUT, INPUT);
  pinMode(RC_CH3_INPUT, INPUT);
  pinMode(RC_CH4_INPUT, INPUT);
  pinMode(RC_CH5_INPUT, INPUT);
  pinMode(RC_CH6_INPUT, INPUT);

  attachInterrupt(RC_CH1_INPUT, calc_ch_1, CHANGE);
  attachInterrupt(RC_CH2_INPUT, calc_ch_2, CHANGE);
  attachInterrupt(RC_CH3_INPUT, calc_ch_3, CHANGE);
  attachInterrupt(RC_CH4_INPUT, calc_ch_4, CHANGE);
  attachInterrupt(RC_CH5_INPUT, calc_ch_5, CHANGE);
  attachInterrupt(RC_CH6_INPUT, calc_ch_6, CHANGE);
}
Exemple #4
0
/******************************************************************************
 * Register pin as having pushbutton attached.
 *     pin     number of pin with button attached
 *     settle  settle time for the button
 * Returns the pushbutton ID (which is just the pin).
 ******************************************************************************/
unsigned int btn_register(unsigned int pin, unsigned int settle)
{
    BUTTON *new_button = (BUTTON *) malloc(sizeof(BUTTON));

    // fill in button information
    new_button->pin = pin;
    new_button->settle = settle;
    new_button->state = digitalRead(pin);
    new_button->raw_state = !new_button->state;
    new_button->last_time = millis();
    new_button->next = buttons;
    buttons = new_button;

    // make pin an input pin
    pinMode(pin, INPUT);

    // if state of pin changes call ISR routine
    attachInterrupt(pin, btn_isr, CHANGE);

    return pin;
}
/*
  * Updates function
  *
  */
extern "C" void HH10D_Update_wrapper(const real_T *Humidity ,
                                     real_T *xD,
                                     const int8_T  *interruptNum,  const int_T  p_width0,
                                     const real_T *sampleTime, const int_T  p_width1)
{
    /* %%%-SFUNWIZ_wrapper_Update_Changes_BEGIN --- EDIT HERE TO _END */
    if (xD[0] != 1) {
#ifndef MATLAB_MEX_FILE
#ifndef WIRE_LIB
        Wire.begin();
#define WIRE_LIB 1
#endif
        int hh10dAddress = 81;
        hh10dSensitivity   =  float(i2cReadIntRegister(hh10dAddress, 10)); //Read sensitivity from HH10D EEPROM
        hh10dOffset =  float(i2cReadIntRegister(hh10dAddress, 12)); //Same for offset
        attachInterrupt(interruptNum[0],hh10dInterruptHandler,RISING);
#endif
        xD[0] = 1;
    }
    /* %%%-SFUNWIZ_wrapper_Update_Changes_END --- EDIT HERE TO _BEGIN */
}
Exemple #6
0
void WiShield::init()
{
	zg_init();

#ifdef USE_DIG0_INTR
	attachInterrupt(0, zg_isr, LOW);
#endif

#ifdef USE_DIG8_INTR
	// set digital pin 8 on Arduino
	// as ZG interrupt pin
	PCICR |= (1<<PCIE0);
	PCMSK0 |= (1<<PCINT0);
#endif

	while(zg_get_conn_state() != 1) {
		zg_drv_process();
	}

	stack_init();
}
void time_init()
{
/*
	delay(1000);

	TIMSK2 &= ~((1<<OCIE2A)|(1<<OCIE2B)|(1<<TOIE2));
	ASSR |= (1<<AS2);
	TCNT2 = 0x00;
	TCCR2B = (1<<CS22)|(1<<CS20);
	while(ASSR & 0x07);
	TIFR2 = 0;
	TIMSK2 |= (1<<TOIE2);
*/

	/*shao pinPullup(RTC_INT_P, PULLUP_ENABLE);
	EICRA |= _BV(ISC00);
	EIMSK |= _BV(INT0);*/
    pinMode(RTC_INT_P, INPUT_PULLUP);
    attachInterrupt(2, INT2_Interrupt, CHANGE);

	time_wake();
}
AS5134::AS5134(int cs, int clk, int dio, int quad_a, int quad_b, int minPeriod)
{
  digitalWrite(cs, LOW);
  digitalWrite(clk, LOW);
  pinMode(cs,  OUTPUT);
  pinMode(clk, OUTPUT);
  pinMode(dio, INPUT);
  *portConfigRegister(dio) |= PORT_PCR_PE; //pull enable
  *portConfigRegister(dio) &= ~PORT_PCR_PS; //pull down
  pinMode(quad_a, INPUT);
  pinMode(quad_b, INPUT);
  this->cs = cs;
  this->clk = clk;
  this->dio = dio;
  this->quad_a = quad_a;
  this->quad_b = quad_b;
  this->minPeriod = minPeriod;
  attachInterrupt(quad_a, AS5134::CalculateSpeed, RISING);
  Timer1.initialize(minPeriod);
  Timer1.start();
  Timer1.attachInterrupt(AS5134::ResetSpeed);
}
/**
* @static 
* interrupt handler - called whenever the interrupt pin state changes
*/
void PulseInZero::pinChange(){
	
	static unsigned long pulseStart = 0;
	
	state = !state;
	
	if(state){ 
		// interrupt pin has changed, a pulse has started
		pulseStart = micros(); // store the current microseconds
		attachInterrupt(0, pinChange, FALLING); // now wait for the falling edge
	} else {  	
		
		// pulse complete, detach the interrupt...
		detachInterrupt(0);
		
		// pin state changed again - pulse ended
		unsigned long duration = micros() - pulseStart; // get the pulse length
		
		active = false;
		onComplete(duration);
	}
}
Exemple #10
0
void RepRapPCBTest::setup() {
	diButtonPin.initialize(buttonPin, true);
	btn.initialize(&diButtonPin, false);
	diLedPin.initialize(ledPin, 0);
	led.initialize(&diLedPin, states, size(states), true);

	reader.initialize(115200, size(readerBuffer), readerBuffer);
	diRotorPinA.initialize(rotorPinA, true);
	diRotorPinB.initialize(rotorPinB, true);
	rotor.initialize(&diRotorPinA, &diRotorPinB);
	attachInterrupt(0, UpdateRotor, CHANGE);

	pcb.initialize();
	repRapMode = Idle;
	repRapMode = Idle;
	selectedAxisName = pgmAxisNameX;
	selectedAxis = &pcb.axisX;

    Serial.pgm_println(PSTR("Initialized"));
    Serial.pgm_println(PSTR("Press the button to stop"));
    printHelp();
}
Exemple #11
0
void tsl230_init()
{
  // attach interrupt to pin2,
  // send output pin of TSL230R to arduino 2
  // call handler on each rising pulse  
  attachInterrupt(0, add_pulse, RISING);  

  // setup TSL230R pins

  pinMode(TSL_FREQ_PIN, INPUT);
  pinMode(TSL_S0, OUTPUT);
  pinMode(TSL_S1, OUTPUT);
  pinMode(TSL_S2, OUTPUT);
  pinMode(TSL_S3, OUTPUT);  

  // 1x sensitivity,
  // divide-by-1 scaling
  digitalWrite(TSL_S0, HIGH);
  digitalWrite(TSL_S1, LOW);
  digitalWrite(TSL_S2, LOW);
  digitalWrite(TSL_S3, LOW);
}
void init() {

	Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
	Serial.systemDebugOutput(true); // Debug output to serial
	//commandHandler.registerSystemCommands();

	// Enable rboot in makefile!
	int slot = rboot_get_current_rom();
	Serial.printf("\r\nCurrently running rom %d.\r\n", slot);


	if (!digitalRead(SWITCH_PIN)) {

			// Reboot in operation-mode
			rboot_set_current_rom(0);
			Serial.println("Restarting...");
			System.restart();
		}

	pinMode(GREEN_LED_PIN, OUTPUT);
	pinMode(RED_LED_PIN, OUTPUT);
	pinMode(SWITCH_PIN, INPUT);

	WifiStation.enable(true);
	WifiStation.enableDHCP(true);

	WifiAccessPoint.enable(false);

	configMode = false;
	smartConfigActive = false;

	debounceTimer.initializeMs(1200, debounceReset);
	configTimer.initializeMs(1000, switchDelay);
	attachInterrupt(SWITCH_PIN, switchPressed, RISING);

	setConfigMode(true);

}
void AP_InertialSensor_MPU6000::hardware_init()
{
    // MPU6000 chip select setup
    pinMode(_cs_pin, OUTPUT);
    digitalWrite(_cs_pin, HIGH);
    delay(1);

    // Chip reset
    register_write(MPUREG_PWR_MGMT_1, BIT_H_RESET);
    delay(100);
    // Wake up device and select GyroZ clock (better performance)
    register_write(MPUREG_PWR_MGMT_1, MPU_CLK_SEL_PLLGYROZ);
    delay(1);
    // Disable I2C bus (recommended on datasheet)
    register_write(MPUREG_USER_CTRL, BIT_I2C_IF_DIS);
    delay(1);
    // SAMPLE RATE
    register_write(MPUREG_SMPLRT_DIV,0x04);     // Sample rate = 200Hz    Fsample= 1Khz/(4+1) = 200Hz
    delay(1);
    // FS & DLPF   FS=2000º/s, DLPF = 98Hz (low pass filter)
    register_write(MPUREG_CONFIG, BITS_DLPF_CFG_98HZ);
    delay(1);
    register_write(MPUREG_GYRO_CONFIG,BITS_FS_2000DPS);  // Gyro scale 2000º/s
    delay(1);
    register_write(MPUREG_ACCEL_CONFIG,0x08);           // Accel scele 4g (4096LSB/g)
    delay(1);

    // INT CFG => Interrupt on Data Ready
    register_write(MPUREG_INT_ENABLE,BIT_RAW_RDY_EN);         // INT: Raw data ready
    delay(1);
    register_write(MPUREG_INT_PIN_CFG,BIT_INT_ANYRD_2CLEAR);  // INT: Clear on any read
    delay(1);
    // Oscillator set
    // register_write(MPUREG_PWR_MGMT_1,MPU_CLK_SEL_PLLGYROZ);
    delay(1);

    attachInterrupt(6,data_interrupt,RISING);
}
Exemple #14
0
// ============================ Initialization ================================
void setup() {
  //motor setup
  pinMode(MTRLFWD,OUTPUT);
  pinMode(MTRRFWD,OUTPUT);
  pinMode(MTRLREV,OUTPUT);
  pinMode(MTRRREV,OUTPUT);
  pinMode(MTRLEN,OUTPUT);
  pinMode(MTRREN,OUTPUT);
  digitalWrite(MTRLEN, LOW);
  digitalWrite(MTRREN, LOW);

  //Color Sensor Setup
  pinMode(REDLED,OUTPUT);
  pinMode(BLUELED,OUTPUT);
  digitalWrite(REDLED, HIGH);
  digitalWrite(BLUELED,LOW);
  colorState = REDSTATE;
  lastColorMeasurement = DARK;
  currentColorMeasurement = DARK;
  //Interrupt Setup for color
  //Timer1.initialize(10000); //10 milliseconds
  //Timer1.attachInterrupt(colorMeasure);

  // set initial state to hunting
  state = HALT;
  previousState = HALT;

  // Collision setup
  debounceTimer = 0;
  collisionTimer = 0;
  collisionTimeout = 0;
  collide = false;
  attachInterrupt(2,collision, RISING); // pin 21s


  
  //Serial.begin(9600);
}
Exemple #15
0
static void setup_mrf(void)
{
  // Serial.println("setup mrf node " xstr(ARDUINO_NODE_ID));

  //SPI.setClockDivider(B00000001); // spi speed

  mrf.reset();
  mrf.init();

  mrf.set_pan(0xabba);
  mrf.address16_write(THIS_NODE);

  //mrf.set_promiscuous(true);

  //mrf.write_short(MRF_RXMCR, 0x02); // error mode

  attachInterrupt(0, mrf_interrupt, CHANGE);
  interrupts();

  delay(1000);

  Serial.println("done setup mrf");
}
Exemple #16
0
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
#ifdef __Raspberry_Pi
	return 1;
#else
	// Let serial prints finish (debug, log etc)
	bool pinTriggeredWakeup = true;
	Serial.flush();
	RF24::powerDown();
	attachInterrupt(interrupt, wakeUp, mode);
	if (ms>0) {
		pinIntTrigger = 0;
		sleep(ms);
		if (0 == pinIntTrigger) {
			pinTriggeredWakeup = false;
		}
	} else {
		Serial.flush();
		LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
	}
	detachInterrupt(interrupt);
	return pinTriggeredWakeup;
#endif
}
Exemple #17
0
/***********************************************************
* 
* startUpHandler ISR
* 
***********************************************************/
void startUpHandler(void)
{

	  unsigned long currentTime = millis();

		#ifdef TT_SHIELD
				 PCintPort::detachInterrupt(START_BUTTON);
			     PCintPort::attachInterrupt(START_BUTTON,startDownHandler,FALLING);  
		#else
				 detachInterrupt(0);
				 attachInterrupt(0,startDownHandler,FALLING); //trigger ISR function on start button press.
		#endif
		
		if(TTUI::pTTUI->startBttnHold == true) //press&hold reset wasn't done, so count it as a normal press
		{
			TTUI::pTTUI->initStart(currentTime);
		}
			
		TTUI::pTTUI->startBttnHold = false;
	
	
	
}
Exemple #18
0
void send_qx1(unsigned char byte)
{
	DDRD = 0xff;					// Set PORT D as output

	PORTC &= 0xf0;
	PORTC |= BUS_SELECT_DATA;			// Select the bus

	PORTD = byte;					// Write the byte

	PORTC &= 0xf0;
	PORTC |= BUS_SELECT_ADDRESS;			// Latch the bus

	DDRD = 0x00;					// Set PORT D as input

	digitalWrite(INT0, HIGH); 			// Activate internal pullup resistor
	attachInterrupt(0, read_qx1, FALLING);		// Declare interrupt routine
	digitalWrite(FDC_DRQ, LOW);			// Fire DRQ Interrupt
	__asm__("nop\n\t""nop\n\t");			// Waits ~ 125 nsec
	if(qx1bus == 0xff)
		mb8877.reg[STATUS] |= FDC_ST_LOSTDATA;	// QX1 did not load DATA in time
	else
		mb8877.reg[STATUS] &= ~FDC_ST_LOSTDATA;	// QX1 got DATA in time
}
Exemple #19
0
void PS2Keyboard::begin(int dataPin) {
  // Prepare the global variables
  ps2Keyboard_DataPin = dataPin;
  ps2Keyboard_CurrentBuffer = 0;
  ps2Keyboard_CharBuffer = 0;
  ps2Keyboard_BufferPos = 0;
  ps2Keyboard_BreakActive = false;

  // initialize the pins
  pinMode(PS2_INT_PIN, INPUT);
  digitalWrite(PS2_INT_PIN, HIGH);
  pinMode(dataPin, INPUT);
  digitalWrite(dataPin, HIGH);
  

  attachInterrupt(1, ps2interrupt, FALLING);
#if 0
  // Global Enable INT1 interrupt
  EIMSK |= ( 1 << INT1);
  // Falling edge triggers interrupt
  EICRA |= (0 << ISC10) | (1 << ISC11);
#endif
}
Exemple #20
0
void setup() {
    // initialize serial
    // Serial.flush(); // flush the serial buffer on setup.
    Serial.begin(115200); // open serial port, sets data rate to 9600bps
    Serial.println("Power on test");
    inputString.reserve(200);
    valve_open = false;
    
    // relay for solenoid cut off valve
    pinMode(RELAY_A, OUTPUT);
    
    // flowmeter shit
    pinMode(flowmeterPin, INPUT);
    digitalWrite(flowmeterPin, HIGH); // Need to set these HIGH so they won't just tick away
    
    // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
    // Configured to trigger on a RISING state change (transition from HIGH
    // state to LOW state)
    attachInterrupt(0, count, FALLING);
    
    setupTime();
    
}
void initialize_robot(void)
{
	uint8_t count = 10;
	uint8_t devStatus;

	//Wire.begin();
	Wire.begin();

	/*pinMode(IRQ_PORT, INPUT);
	digitalWrite(IRQ_PORT, HIGH);
	attachInterrupt(DMP_IRQ, imu_isr, RISING); */

	imu_init();
	dmpReady = true;

	pinMode(SOL_LED, OUTPUT);

	pinMode(IRQ_PORT, INPUT);
	digitalWrite(IRQ_PORT, HIGH);
	attachInterrupt(DMP_IRQ, imu_isr, CHANGE);
	Serial.print("IRQ on pin: D");
	Serial.println(IRQ_PORT);
}
Exemple #22
0
void Server::init(pageServingFunction function) {

	// WiShield init
	zg_init();

#ifdef USE_DIG0_INTR
	attachInterrupt(0, zg_isr, LOW);
#endif

#ifdef USE_DIG8_INTR
	// set digital pin 8 on Arduino
	// as ZG interrupt pin
	PCICR |= (1<<PCIE0);
	PCMSK0 |= (1<<PCINT0);
#endif

	while(zg_get_conn_state() != 1) {
		zg_drv_process();
	}

	// Start the stack
	stack_init();

	// Store the callback function for serving pages
	// and start listening for connections on port 80 if
	// the function is non-null
	callbackFunc = function;
	if (callbackFunc) {
		// Listen for server requests on port 80
		uip_listen(HTONS(80));
	}

#ifdef DEBUG
	verbose = true;
	Serial.println("WiServer init called");
#endif // DEBUG
}
/*static*/
void OC::DigitalInputs::Init() {

  static const struct {
    uint8_t pin;
    void (*isr_fn)();
  } pins[DIGITAL_INPUT_LAST] =  {
    {TR1, tr1_ISR},
    {TR2, tr2_ISR},
    {TR3, tr3_ISR},
    {TR4, tr4_ISR},
  };

  for (auto pin : pins) {
    pinMode(pin.pin, OC_GPIO_TRx_PINMODE);
    attachInterrupt(pin.pin, pin.isr_fn, FALLING);
  }

  clocked_mask_ = 0;
  std::fill(clocked_, clocked_ + DIGITAL_INPUT_LAST, 0);

  // Assume the priority of pin change interrupts is lower or equal to the
  // thread where ::Scan function is called. Otherwise a safer mechanism is
  // required to avoid conflicts (LDREX/STREX or store ARM_DWT_CYCCNT in the
  // array to check for changes.
  //
  // A really nice approach would be to use the FTM timer mechanism and avoid
  // the ISR altogether, but this only works for one of the pins. Using more
  // explicit interrupt grouping might also improve the handling a bit (if
  // implemented on the DX)
  //
  // It's still not guaranteed that 4 simultaneous triggers will be handled
  // exactly simultaneously though, but that's micro-timing dependent even if
  // the pins have higher prio.

  //  NVIC_SET_PRIORITY(IRQ_PORTB, 0); // TR1 = 0 = PTB16
  // Defaults is 0, or set OC_GPIO_ISR_PRIO for all ports
}
bool hal_aci_tl_event_get(hal_aci_data_t *p_aci_data)
{
  bool was_full;

  if (!a_pins_local_ptr->interface_is_interrupt && !aci_queue_is_full(&aci_rx_q))
  {
    m_aci_event_check();
  }

  was_full = aci_queue_is_full(&aci_rx_q);

  if (aci_queue_dequeue(&aci_rx_q, p_aci_data))
  {
    if (aci_debug_print)
    {
      Serial.print(" E");
      m_aci_data_print(p_aci_data);
    }

    if (was_full && a_pins_local_ptr->interface_is_interrupt)
	  {
      /* Enable RDY line interrupt again */
      attachInterrupt(a_pins_local_ptr->interrupt_number, m_aci_isr, LOW);
    }

    /* Attempt to pull REQN LOW since we've made room for new messages */
    if (!aci_queue_is_full(&aci_rx_q) && !aci_queue_is_empty(&aci_tx_q))
    {
      m_aci_reqn_enable();
    }

    return true;
  }

  return false;
}
Exemple #25
0
void setup()
{

   pinMode(ledPin, OUTPUT);
   attachInterrupt(1, blink, FALLING); // pin 3
   pinMode (encoder0PinB,INPUT);
   pinMode(stepper_enable, OUTPUT);
   digitalWrite(stepper_enable, LOW);
// set the speed of the motor to 30 RPMs
   stepper.setSpeed(80);

   lcd.begin(20, 4);
   lcd.clear();
   lcd.print("Stepper, Vers. 1.0");
   lcd.setCursor(0, 2);
   lcd.print("Position:");
   sprintf(buffer,"%3d",val);
   lcd.setCursor(10, 2);
   lcd.print(buffer);
   
   
 //  Serial.begin (9600);
 
}
Exemple #26
0
int Tune::play(char* trackName)
{
	if (isPlaying()) return 1;
	
	// Exit if track not found
	if (!track.open(trackName, O_READ))
	{
		sd.errorHalt("Track not found !");
		return 3;
	}
	
	playState = playback;
	
	// Reset decode time & bitrate from previous playback
	writeSCI(SCI_DECODE_TIME, 0);
	delay(100);
	
	skipTag(); // Skip ID3v2 tag if there's one
	
	feed(); // Feed VS1011e
	attachInterrupt(0, feed, RISING); // Let the interrupt handle the rest of the process
	
	return 0;
}
Exemple #27
0
void setup()
{    
    Serial.begin(230400);
    delay(3000);  //3 seconds delay for enabling to see the start up comments on the serial board

#ifdef SERIAL_TRACE    
    Serial.println("Hello!");
#endif

    // Show we are in setup
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, HIGH);

    // Port setup
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, HIGH);

    // Servo & Lidar
    g_servo.setup();
    g_lidar.begin();
    // TODO: Does this increase speed of LIDAR readings
    // g_lidar.beginContinuous();

    // Motor setup
    for(unsigned int i=0; i<countof(g_amotors); ++i) {
        g_amotors[i].setup();
        attachInterrupt(g_amotors[i].ENCODER_IRQ, c_afnInterrupts[i], CHANGE);
    }

    // Initialise the IMU (connection is optional)
    g_bBNO = g_bno.begin();
    delay(1000);
    if(g_bBNO) g_bno.setExtCrystalUse(true);

    OnDisconnection();
}
// Based on user forum details at: http://forum.rfduino.com/index.php?topic=155.msg4003#msg4003
static void timer(unsigned int ms)                                 // directly pass the value you want the cycle to be in mS
{
  NRF_TIMER2->TASKS_STOP = 1;                                      // Stop timer
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;                        // sets the timer to TIME mode (doesn't make sense but OK!)
  NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;               // with BLE only Timer 1 and Timer 2 and that too only in 16bit mode
  NRF_TIMER2->PRESCALER = 9;                                       // Prescaler 9 produces 31250 Hz timer frequency => t = 1/f =>  32 uS
                                                                   // The figure 31250 Hz is generated by the formula (16M) / (2^n) 
                                                                   // where n is the prescaler value 
                                                                   // hence (16M)/(2^9)=31250 
  NRF_TIMER2->TASKS_CLEAR = 1;                                     // Clear timer
  
  //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  //        Conversion to make cycle calculation easy
  //        Since the cycle is 32 uS hence to generate cycles in mS we need 1000 uS 
  //        1000/32 = 31.25  Hence we need a multiplication factor of 31.25 to the required cycle time to achive it
  //        e.g to get a delay of 10 mS      we would do
  //        NRF_TIMER2->CC[0] = (10*31)+(10/4);
  //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  NRF_TIMER2->CC[0] = (ms * 31) + (ms / 4);                                                                                  //CC[0] register holds interval count value i.e your desired cycle
  NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;                                     // Enable COMAPRE0 Interrupt
  NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);                             // Count then Complete mode enabled
  attachInterrupt(TIMER2_IRQn, TIMER2_Interrupt);                                                                            // also used in variant.cpp in the RFduino2.2 folder to configure the RTC1 
}
void RandomFader(CCShield& brd,unsigned long int time)
{
  FlickerBrightness f(brd);
  ChangeBrightness display(f,DeltaDone);
  unsigned long int i;
  longevity = analogRead(rightDial)+1;
  
  brd.setBrightness(255);  // By default highest brightness unless the dial is moved
  
  state = 1;  // Set the trigger variable, THEN attach the interrupt
  attachInterrupt(wakePin-2,sleepInterrupt,RISING);

  for (i=0;(i<time) && state;i++)
  {
    //attachInterrupt(wakePin-2,sleepInterrupt,RISING);
    if ((i&15)==0)  // Sample less often to use less cpu (this loop goes so fast, it doesn't matter anyway
      {
 
      if ((i&63)==0)  // Adjust brightness.  Do it in an isolated block to save a byte
        {
        lightRate = analogRead(leftDial);
        lightRate = lightRate*2;
        if (lightRate < 5) lightRate=5;

        longevity = (analogRead(rightDial)*2)+1;
        }
      }
      
    if ((i%lightRate)==0)
      {
      byte led = gerhardRand(CCShield_NUMOUTS);
      display.set(led,gerhardRand(CCShield_MAX_BRIGHTNESS-CCShield_MIN_INTENSITY)+CCShield_MIN_INTENSITY-1,(gerhardRand(longevity*10))+(longevity*5));
      }
    display.loop();
   }
}
Exemple #30
0
void RepRapMain::setup() {
	reader.initialize(115200, size(readerBuffer), readerBuffer);

	extruderTemperatureSensor = new TemperatureSensor_TC1047(extruderTemperatureSensorPin);
	extruderTemperatureControl.initialize(extruderTemperatureSensor, extenderOutput.createPinHandler(extruderHeaterPin));

	bedTemperatureSensor = new TemperatureSensor_TC1047(bedTemperatureSensorPin);
	bedTemperatureControl.initialize(bedTemperatureSensor, extenderOutput.createPinHandler(bedHeaterPin));

	reprap.initialize(&reader, &extruderTemperatureControl, &bedTemperatureControl);

	diButtonPin.initialize(buttonPin, true);
	btn.initialize(&diButtonPin, false);
	diRotorPinA.initialize(rotorPinA, true);
	diRotorPinB.initialize(rotorPinB, true);
	rotor.initialize(&diRotorPinA, &diRotorPinB);
	attachInterrupt(0, UpdateRotor, CHANGE);

	diLedPin.initialize(ledPin);
	led.initialize(&diLedPin, states, size(states), true);
	led.setState(btn.isAutoRepeatEnabled());

	Serial_println("ok");
}