// Initialise the device static void __quadrature_init(SENSOR* sensor){ QUADRATURE* encoder = (QUADRATURE*)sensor; pin_make_input(encoder->channelA, FALSE); pin_make_input(encoder->channelB, FALSE); pin_change_attach(encoder->channelA, &__encChA, encoder); pin_change_attach(encoder->channelB, &__encChB, encoder); }
//------------- Private methods - dont call directly ----- static void __spiSWInit(SPI_ABSTRACT_BUS* _spi, boolean master){ SPI_SW* spi = (SPI_SW*)_spi; if(master){ pin_make_output(spi->MOSI, TRUE); // make MOSI an output and set high pin_make_input(spi->MISO,TRUE); // make MISO an input with a pullup }else{ pin_make_input(spi->SCLK,FALSE); // make clock an input with no pullup pin_make_input(spi->MOSI,FALSE); // make MOSI an input with no pullup pin_make_output(spi->MISO,TRUE); // make MISO an output } __spiSWSetClock(_spi,_spi->clock); __spiSWSetDataOrder(_spi,_spi->order); __spiSWSetMode(_spi,_spi->mode); }
// Now create any global variables such as motors, servos, sensors etc // This routine is called once only and allows you to do set up the hardware // Dont use any 'clock' functions here - use 'delay' functions instead void appInitHardware(void){ a2dSetPrescaler(ADC_PRESCALE_DIV128); cyrf6936_Initialise_hard(); // Initialise SPI bus as master. (RF modules connected to hardware SPI) spiBusInit(&bus, TRUE); spiDeviceSelect(&cyrf_0, TRUE); spiDeviceSelect(&cyrf_0, FALSE); spiDeviceSelect(&cyrf_1, TRUE); spiDeviceSelect(&cyrf_1, FALSE); // set I/O pins for RF module(s). pin_make_input(D4, FALSE); // set PACTL pin to input. (module on connector J1) pin_make_input(D5, FALSE); // set PACTLn pin to input. (module on connector J1) pin_make_input(D6, FALSE); // set PACTL pin to input. (module on connector J2) pin_make_input(D7, FALSE); // set PACTLn pin to input. (module on connector J2) #ifdef RF_MODULE_ARTAFLEX // pin_make_output(D4, FALSE); // set RXPA pin to output. (module on connector J1) pin_make_output(D5, FALSE); // set TXPA pin to output. (module on connector J1) pin_make_output(D6, FALSE); // set RXPA pin to output. (module on connector J2) pin_make_output(D7, FALSE); // set TXPA pin to output. (module on connector J2) #endif pin_make_input(E7, TRUE); // set UNIGEN RF module IRQ pin to input. (module on connector J1) pin_make_input(E6, TRUE); // set UNIGEN RF module IRQ pin to input. (module on connector J2) pin_make_output(G3, FALSE); // set UNIGEN RF module RST pin to output. (both modules) //pin_low(G3); // don't reset yet. // set I/O pins for status LEDs pin_make_output(C0, TRUE); // set LED pin for output pin_make_output(C1, TRUE); // set LED pin for output pin_make_output(C2, FALSE); // set LED pin for output //pin_high(C0); // LED off //pin_high(C1); // LED off //pin_low(C2); // LED on // Set UART1 to 19200 baud uartInit(UART1, 38400); // Tell rprintf to output to UART1 rprintfInit(&uart1SendByte); // Initialise the servo controller using Hardware PWM servoPWMInit(&bank1); // Initialise WatchDog Timer wdt_enable( WDTO_500MS ); }
// Read all the values and store into the device static void __srf04_read(SENSOR* sensor){ TICK_COUNT duration; Devantech_SRF04* device = (Devantech_SRF04*)sensor; // initialise the pins pin_make_output(device->out,FALSE); // Set low pin_make_input(device->in,FALSE); // 10us high trigger pulse pin_pulseOut(device->out,10,TRUE); // Measure the inbound pulse duration = pin_pulseIn(device->in,TRUE); device->distance.cm = fraction32(duration, srf04_frac); }
// Call back - for when the speed has been set static void setSpeed(__ACTUATOR *actuator, DRIVE_SPEED speed){ MOTOR* motor = (MOTOR*)actuator; const TimerCompare* channel = compareFromIOPin(motor->pwm); const Timer* timer = compareGetTimer(channel); uint16_t top = timerGetTOP(timer); // New compare threshold uint16_t delay=0; if( speed > 0 ){ delay = interpolateU(speed, 0, DRIVE_SPEED_MAX, 0 , top); // Set direction1 high, direction2 low pin_make_output(motor->direction1,TRUE); pin_make_output(motor->direction2,FALSE); }else if(speed < 0){ delay = interpolateU(speed, 0, DRIVE_SPEED_MIN, 0 , top); // Set direction1 low, direction2 high low pin_make_output(motor->direction1,FALSE); pin_make_output(motor->direction2,TRUE); }else{ // brake if(motor->direction2){ // There are two direction pins - so set both to same value pin_make_output(motor->direction1,FALSE); pin_make_output(motor->direction2,FALSE); }else{ // Only has one direction pin // Set direction1 to an input with no pullup ie disconnect pin_make_input(motor->direction1,FALSE); } } // Change the duty cycle compareSetThreshold(channel,delay); }
// Make all databus pins into inputs with pullups static void databus_input(const HD44780* device){ for(uint8_t i=0; i<8; i++){ pin_make_input(device->data[i],TRUE); } }
// Make the data line high static void sda_high(const I2C_SOFTWARE_BUS* i2c){ pin_make_input(i2c->sda,FALSE); // Assumes external resistor (works) }
// Make the clock high static void scl_high(const I2C_SOFTWARE_BUS* i2c){ do{ pin_make_input(i2c->scl,FALSE); // Assumes external resistor (works) }while(pin_is_low(i2c->scl)); }