static void init(STEPPER_MOTOR* stepper){ POLOLU_A4983* motor = (POLOLU_A4983*)stepper; // Make pins into outputs with initial values pin_make_output(motor->direction, FALSE);// clockwise pin_make_output(motor->enable, TRUE); // disconnected pin_make_output(motor->pulse, FALSE); pin_make_output(motor->reset, FALSE); // start reset the device pin_make_output(motor->reset, TRUE); // end reset the device - will now respond to pulses }
void speechInit(const IOPin* pin){ pin_make_output(pin,FALSE); // Activate non-inverted PWM on the hardware pin TCCR1A |= (BV( COM1B0) << 1); }
// 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 ); }
//------------- 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); }
static void setConnected(__ACTUATOR *actuator, boolean connected){ MOTOR* motor = (MOTOR*)actuator; const TimerCompare* channel = compareFromIOPin(motor->pwm); // Turn on/off the pin to start/stop sending PWM // reverts to default output logic level of low compareSetOutputMode(channel, (connected) ? CHANNEL_MODE_NON_INVERTING : CHANNEL_MODE_DISCONNECT); pin_make_output(motor->direction1,FALSE); }
// Set up PWM on the given pin boolean pwmInitDeciHertz(const IOPin* pin, uint32_t deciHertz, PERCENTAGE duty, uint32_t* actualDeciHertz){ boolean rtn = FALSE; const TimerCompare* channel = compareFromIOPin(pin); if(channel==null){ setError(PWM_PIN_NOT_AVAILABLE); }else{ // The pin is valid // The pin is valid and available TIMER_MODE mode; uint16_t icr; uint16_t prescaler; uint32_t actual; const Timer* timer = compareGetTimer(channel); // Find the best PWM setting boolean valid = timerCalcPwm(timer, deciHertz, 100, &mode, &icr, &prescaler, &actual); if(!valid){ // There is no PWM setting that is valid setError( (timerIsInUse(timer)) ? PWM_TIMER_IN_USE : TIMER_HAS_NO_PWM ); }else{ // Lets set up the PWM timerSetMode(timer,mode); if(modeIsICR(mode)){ // Set the ICR PORT icrPort = pgm_read_word(&timer->pgm_icr); _SFR_MEM16(icrPort)=icr; } // Mark the channel as in use // compareAttach(channel,&nullTimerCompareCallback,0,null); // Turn the pin into an output, low pin_make_output(pin, FALSE); // Turn on the PWM pin output compareSetOutputMode(channel, CHANNEL_MODE_NON_INVERTING); // Turn on the timer timerSetPrescaler(timer,prescaler); // Set the initial duty cycle pwmSetDutyCycle(pin,duty); // Set the return value if(actualDeciHertz){ *actualDeciHertz = actual; } rtn = TRUE; } } return rtn; }
static void init(SEGLED* led){ if(!led->initialised){ for(uint8_t i=0; i<8;i++){ const IOPin* pin = led->segment[i]; pin_make_output(pin,FALSE); } led->initialised=TRUE; } }
// Pass the list of motors, the list should be in PROGMEM space to save Flash RAM // The specified Timer must implement timer compare interrupts and, if so, it will // use the timer compare channel A (if there is more than one) void motorL293Init(MOTOR_DRIVER* driver, uint32_t freq){ // Make sure each servo is set as an output for(int i= driver->num_motors-1;i>=0;i--){ MOTOR* motor = (MOTOR*)pgm_read_word(&driver->motors[i]); // Connect motor to driver motor->actuator.sclass = &c_l293; // Make sure the motor pins are set as output pins pin_make_output(motor->pwm, FALSE); pin_make_output(motor->direction1, FALSE); pin_make_output(motor->direction2, FALSE); // Start off braking act_setSpeed(motor,DRIVE_SPEED_BRAKE); // Indicate the motor is connected act_setConnected(motor,TRUE); } }
static boolean initPWM(const IOPin* pin, uint32_t deciHertz){ const TimerCompare* channel = compareFromIOPin(pin); if(channel==null){ setError(PWM_PIN_NOT_AVAILABLE); return FALSE; } if(compareIsInUse(channel)){ setError(PWM_PIN_IN_USE); return FALSE; } TIMER_MODE mode; uint16_t icr; uint16_t prescaler; const Timer* timer = compareGetTimer(channel); // Find the best PWM setting for 10kHz, with 128 steps boolean valid = timerCalcPwm(timer, deciHertz, 128, &mode, &icr, &prescaler); if(!valid){ // There is no PWM setting that is valid setError( (timerIsInUse(timer)) ? PWM_TIMER_IN_USE : TIMER_HAS_NO_PWM ); }else{ // Lets set up the PWM if(!timerIsInUse(timer)){ timerSetMode(timer,mode); if(modeIsICR(mode)){ // Set the ICR PORT icrPort = pgm_read_word(&timer->pgm_icr); _SFR_MEM16(icrPort)=icr; } } // Make it an output pin and set high for brake pin_make_output(pin,TRUE); // Use inverting PWM compareSetOutputMode(channel,CHANNEL_MODE_INVERTING); // Mark the channels as in use compareAttach(channel,&nullTimerCompareCallback,0,null); // Do this last as it then turns on the timer timerSetPrescaler(timer,prescaler); } return valid; }
// 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); if(motor->direction2==null){ // one wire so delay = top - delay delay = top - delay; } // Set direction1 high, direction2 low (if there is one) 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 (if there is one) 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); delay = top; // full speed brake }else{ // Only has one direction pin // Set direction1 low pin_make_output(motor->direction1,FALSE); // PWM delay = 0 so pwm = low // ie both low = brake } } // Change the duty cycle compareSetThreshold(channel,delay); }
// 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 outputs static void databus_output(const HD44780* device){ for(uint8_t i=0; i<8; i++){ pin_make_output(device->data[i],TRUE); } }
// Make the data line low static void sda_low(const I2C_SOFTWARE_BUS* i2c){ pin_make_output(i2c->sda,FALSE); }
static void __spiSWSetMode(SPI_ABSTRACT_BUS* _spi,SPI_MODE mode){ SPI_SW* spi = (SPI_SW*)_spi; // Set the clock to its initial state pin_make_output(spi->SCLK, (mode == SPI_MODE_2 || mode == SPI_MODE_3)); }