int16_t main(void) { //initialize all system clocks init_clock(); //initialize serial communications init_uart(); //initialize pin driving library (to be able to use the &D[x] defs) init_pin(); //initialize the UI library init_ui(); //initialize the timer module init_timer(); //initialize the OC module (used by the servo driving code) init_oc(); //Set servo control pins as output pin_digitalOut(PAN_PIN); pin_digitalOut(TILT_PIN); pin_digitalOut(SONIC_OUT_PIN); pin_digitalIn(SONIC_IN_PIN); //Set LED off led_off(LED); //Configure blinking rate for LED when connected timer_setPeriod(LED_TIM, 0.2); timer_start(LED_TIM); //Configure timer for reciever timeout timer_setPeriod(DIST_TIM, 0.05); //configure PWM on sonic output pin oc_pwm(PWM_OC, SONIC_OUT_PIN, NULL, SONIC_FREQ, 0x0000); //According to HobbyKing documentation: range .8 through 2.2 msec //Set servo control pins as OC outputs on their respective timers oc_servo(SERVO1_OC, PAN_PIN, SERVO1_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, pan_set_val); oc_servo(SERVO2_OC, TILT_PIN, SERVO2_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, tilt_set_val); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests led_on(LED); //There's no point in driving the servos when there's no one connected yet. } while (1) { ServiceUSB(); // service any pending USB requests //blink the LED if (timer_flag(LED_TIM)) { timer_lower(LED_TIM); led_toggle(LED); } //Update the servo control values. pin_write(PAN_PIN, pan_set_val); pin_write(TILT_PIN, tilt_set_val); } }
//INITIALIZE MOTOR void initMotor(void) { pin_write(ENA, 1); //set enable pin to ON pin_write(SLEW, 1); //set slew rate to HIGH pin_write(INV, 0); //set invert OFF pin_write(D1, 0); //disable IN1 is OFF pin_write(D2, 1); //disable IN2 is ON }
static ssize_t uart_soft_write_cb(void *arg_p, const void *txbuf_p, size_t size) { int i, j; uint8_t data; struct uart_soft_driver_t *self_p; const uint8_t *tx_p = txbuf_p; self_p = container_of(arg_p, struct uart_soft_driver_t, chout); for (i = 0; i < size; i++) { sys_lock(); pin_write(&self_p->tx_pin, 0); /* Put 8 bits on the transmission wire. */ data = tx_p[i]; for (j = 0; j < 8; j++) { time_busy_wait_us(self_p->sample_time); pin_write(&self_p->tx_pin, data & 1); data >>= 1; } time_busy_wait_us(self_p->sample_time); pin_write(&self_p->tx_pin, 1); time_busy_wait_us(self_p->sample_time); sys_unlock(); } return (size); }
int test_exti(struct harness_t *harness_p) { int i; struct exti_driver_t exti; struct pin_driver_t pin; pin_init(&pin, &pin_d4_dev, PIN_OUTPUT); pin_write(&pin, 1); BTASSERT(exti_init(&exti, &exti_d3_dev, EXTI_TRIGGER_FALLING_EDGE, isr, NULL) == 0); BTASSERT(exti_start(&exti) == 0); for (i = 0; i < 10; i++) { pin_write(&pin, 0); time_busy_wait_us(10000); pin_write(&pin, 1); time_busy_wait_us(10000); } std_printf(FSTR("flag = %d\r\n"), (int)flag); BTASSERT(flag == 10); return (0); }
void pwm_set_direction(unsigned char direction) { // Direction is a bit [0 or 1] specifying the direction // the motor should be commanded to turn. 1 is "forwards", // and 0 is "reverse". Assumes fast decay mode operation. if (pwm_direction != direction) { // The direction to be set is different than the motor's current // direction. A change should be made. uint16_t prev_duty; pwm_direction = direction; // Update pwm_direction if (direction == 1) { // If 1, PWM_I1 should PWM, PWM_I2 should be 0. // printf("Setting motor direction FORWARD...\r\n"); prev_duty = pin_read(PWM_I2); pin_write(PWM_I2, (uint16_t)(0)); pin_write(PWM_I1, prev_duty); } else if (direction == 0) { // If 0, PWM_I1 should 0, PWM_I2 should be 1. // printf("Setting motor direction REVERSE...\r\n"); prev_duty = pin_read(PWM_I1); pin_write(PWM_I1, (uint16_t)(0)); pin_write(PWM_I2, prev_duty); } else { printf("ERR: Invalid PWM direction %d received.\r\n", direction); } } }
int16_t main(void) { InitUSB(); // initialize the USB registers and serial interface engine initChip(); initMotor(); led_on(&led1); timer_setPeriod(LED_TIMER, 0.5); //start internal clock with defined period timer_start(LED_TIMER); pin_write(IN1, 1); pin_write(IN2, 0); pin_write(D2, 65536*2/5); while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } while (1) { ServiceUSB(); // service any pending USB requests //LED1 TOGGLE TO CONFIRM RUNNING CODE if (timer_flag(LED_TIMER)) { timer_lower(LED_TIMER); led_toggle(&led1); pin_write(D2, VAL1); } } }
void pwm_set_raw_duty(uint16_t raw_duty) { if (pwm_direction == 1) { pin_write(PWM_I1, raw_duty); } else { pin_write(PWM_I2, raw_duty); } }
// // Exported functions // void gps_setup() { int gps_success = 0; strcpy(gps_time, "000000"); strcpy(gps_aprs_lat, "0000.00N"); strcpy(gps_aprs_lon, "00000.00E"); #ifdef GPS_USING_UBLOX // Setup for the uBLOX MAX-6/7/8 // Check to see if we need to power up the GPS #ifdef GPS_POWER_PIN pinMode(GPS_POWER_PIN, OUTPUT); pin_write(GPS_POWER_PIN, LOW); #endif #ifdef GPS_POWER_SLEEP_TIME // Add a bit of a delay here to allow the GPS a chance to wake up // (even if we aren't powering it via a digital pin) delay(GPS_POWER_SLEEP_TIME); #endif #ifdef GPS_LED_PIN // LED for GPS Status pinMode(GPS_LED_PIN, OUTPUT); pin_write(GPS_LED_PIN, LOW); #endif // Set MAX-6 to flight mode int setup_attempts_remaining = 120; while (!gps_success && (setup_attempts_remaining--) >= 0) { sendUBX(setNav, sizeof(setNav)/sizeof(uint8_t)); gps_success = getUBX_ACK(setNav); if (!gps_success) { // Should find a better way to indicate a problem? #ifdef GPS_LED_PIN pin_write(GPS_LED_PIN, HIGH); #endif delay(500); } } #ifdef GPS_LED_PIN pin_write(GPS_LED_PIN, LOW); #endif // Finally turn off any NMEA sentences we don't need (in this case it's // everything except GGA and RMC) sendUBX(setGLL, sizeof(setGLL)/sizeof(uint8_t)); // Disable GGL sendUBX(setGSA, sizeof(setGSA)/sizeof(uint8_t)); // Disable GSA sendUBX(setGSV, sizeof(setGSV)/sizeof(uint8_t)); // Disable GSV sendUBX(setVTG, sizeof(setVTG)/sizeof(uint8_t)); // Disable VTG #endif }
int8_t port_pin_write(uint8_t portx, uint8_t pin, uint8_t state) { #if defined (__AVR_ATmega169__) || defined (__AVR_ATmega169P__) if (portx == portb) // it is PORTB { pin_write(PORTB,pin,state); } else if // it is PORTD { pin_write(PORTD,pin,state); } else // out of range { return(-1); // return ERROR
int8_t digital_write(uint8_t pin, uint8_t value) { #if defined (__AVR_ATmega169__) || defined (__AVR_ATmega169P__) if ( pin < 8) // it is PORTB { pin_write(PORTB,pin,value); } else if // it is PORTD { pin -= 8; // converts it to numbering for PORTD pin_write(PORTD,pin,value); } else // out of range { return(-1); // return ERROR
// Interrupt Service Routine for TIMER 3. This is used to switch between the // buzzing and quiet periods when ON_CYCLES or OFF_CYCLES are reached. extern "C" void __ISR (_TIMER_3_VECTOR, ipl5) T3_IntHandler (void) { alarm--; if (alarm == 0) { buzzing = !buzzing; if (is_buzzer_on && buzzing) { switch(BUZZER_PIN) { case 9: OpenOC4(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE, DUTY_CYCLE, 0); break; case 10: OpenOC5(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE, DUTY_CYCLE, 0); break; } alarm = ON_CYCLES; } else { switch(BUZZER_PIN) { case 9: CloseOC4(); break; case 10: CloseOC5(); break; } alarm = OFF_CYCLES; pin_write(BUZZER_PIN, LOW); } } // Clear interrupt flag // This will break other interrupts and millis() (read+clear+write race condition?) // IFS0bits.T3IF = 0; // DON'T!! // Instead: mT3ClearIntFlag(); }
void RadioHx1::setup() { // Configure pins pinMode(PTT_PIN, OUTPUT); pin_write(PTT_PIN, LOW); pinMode(AUDIO_PIN, OUTPUT); }
void RadioRSUV3::setup() { // Configure pins pinMode(PTT_PIN, OUTPUT); pin_write(PTT_PIN, HIGH); pinMode(AUDIO_PIN, OUTPUT); }
void quad_debug(_QUAD *self, _PIN *lut3, _PIN *lut2, _PIN *lut1, _PIN *lut0 ) { /* Given a quadrature encoder object and four configured digital output pins, writes the value used to index the LUT across the four pins as follows-- LUT Index Bit | Source [LSB --> MSB] | **************************************** 0 (LSB) | most recent value of B 1 | most recent value of A 2 | previously read value of B 3 (MSB) | previously read value of A */ pin_write(lut0, self -> b_curr); // b0 pin_write(lut1, self -> a_curr); // b1 pin_write(lut2, self -> b_prev); // b2 pin_write(lut3, self -> a_prev); // b3 }
uint16_t PIDcalc(uint16_t set_point){ actual_position = pin_read(potentiometer); uint16_t error; uint16_t duty; uint16_t threshold = 500; // printf("actual_position %u\n\r", actual_position); error = abs((set_point - actual_position)); // printf("error %u\n\r", error); if (error > threshold){ duty = (Kp * error); // printf("duty %u\n\r", duty); pin_write(pwmpin, duty); PIDcalc(set_point); } else{ pin_write(pwmpin, 0); } }
// Private : Hardware scan void Keypad_MAX::scanKeys() { // Re-intialize the pins every time for sharing with other hardware. for (byte r=0; r<sizeKpd.rows; r++) { pin_mode(rowPins[r],INPUT_PULLUP); } // bitMap stores ALL the keys that are being pressed. for (byte c=0; c<sizeKpd.columns; c++) { pin_mode(columnPins[c],OUTPUT); pin_write(columnPins[c], LOW); // Begin column pulse output. for (byte r=0; r<sizeKpd.rows; r++) { bitWrite(bitMap[r], c, !pin_read(rowPins[r])); // keypress is active low so invert to high. } // Set pin to high impedance input. Effectively ends column pulse. pin_write(columnPins[c],HIGH); pin_mode(columnPins[c],INPUT); } }
int16_t main(void) { init_clock(); init_uart(); init_ui(); init_timer(); init_oc(); led_on(&led2); timer_setPeriod(&timer2, 0.5); timer_start(&timer2); val1 = 0; val2 = 0; interval = 0.02; min_width = 5.5E-4; max_width = 2.3E-3; pos = 0; //16 bit int with binary point in front of the MSB oc_servo(&oc1,&D[0],&timer1, interval,min_width, max_width, pos); oc_servo(&oc2,&D[2],&timer3, interval,min_width, max_width, pos); printf("Good morning\n"); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } while (1) { ServiceUSB(); //write the values to the servos (move the servos to the requested position) pin_write(&D[0],val1); pin_write(&D[2],val2); if (timer_flag(&timer2)) { //show a heartbeat and a status message timer_lower(&timer2); led_toggle(&led1); printf("val1 = %u, val2 = %u\n", val1, val2); } } }
int sensors_lm60(int powerPin, int readPin) { uint16_t adc = 0; pin_write(powerPin, HIGH); // Turn the LM60 on delayMicroseconds(5); // Allow time to settle adc = analogRead(readPin); // Real read pin_write(powerPin, LOW); // Turn the LM60 off int mV = 3300L * adc / 1024L; // Millivolts switch(TEMP_UNIT) { case 1: // C // Vo(mV) = (6.25*T) + 424 -> T = (Vo - 424) * 100 / 625 return (4L * (mV - 424) / 25) + CALIBRATION_VAL; case 2: // K // C + 273 = K return (4L * (mV - 424) / 25) + 273 + CALIBRATION_VAL; case 3: // F // (9/5)C + 32 = F return (36L * (mV - 424) / 125) + 32 + CALIBRATION_VAL; } }
void init_motor(void){ //outputs pin_digitalOut(IN1); //D2-bar pin_write(IN1,1); pin_digitalOut(IN2); //D2-bar pin_write(IN2,0); pin_digitalOut(D1); //D1 pin_write(D1,0); //no tri-stating! pin_digitalOut(ENA); //ENA pin_write(ENA,1); //Enable the system pin_digitalOut(&D[7]); //SLEW pin_write(&D[7],0); //low slew rate pin_digitalOut(INV); //INV pin_write(INV,0); //don't invert the inputs! //inputs pin_analogIn(CURRENT_PIN); //direction sensor pin_analogIn(VEMF_PIN); //Vemf sensor pin_analogIn(FB_PIN); //0.24% of active high side current pin_digitalIn(REV_PIN); //tach input }
void power_save() { /* Enter power saving mode. SLEEP_MODE_IDLE is the least saving * mode, but it's the only one that will keep the UART running. * In addition, we need timer0 to keep track of time, timer 1 * to drive the buzzer and timer2 to keep pwm output at its rest * voltage. */ set_sleep_mode(SLEEP_MODE_IDLE); sleep_enable(); power_adc_disable(); power_spi_disable(); power_twi_disable(); pin_write(LED_PIN, LOW); sleep_mode(); // Go to sleep pin_write(LED_PIN, HIGH); sleep_disable(); // Resume after wake up power_all_enable(); }
// Exported functions void buzzer_setup() { pinMode(BUZZER_PIN, OUTPUT); pin_write(BUZZER_PIN, LOW); buzzing = false; is_buzzer_on = false; alarm = 1; // There are two timers capable of PWM, 2 and 3. We are using 2 for the modem, // so use 3 for the buzzer. OpenTimer3(T3_ON | T3_PS_1_8, PWM_PERIOD); ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_5); }
void modem_hal_start() { ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_6); // The above could also be accomplished by ... ? /* IFS0bits.T2IF = 0; // Clear the T2 interrupt flag, so that the ISR doesn't go off immediatelly IEC0bits.T2IE = 1; // Enable T2 interrupt */ // Turn PTT led on pin_write(43, HIGH); }
void modem_hal_stop() { // Return to rest duty cycle SetDCOC1PWM(REST_DUTY); //OCxRS = REST_DUTY; // Disable playback interrupt mT2IntEnable(0); //IEC0bits.T2IE = 0; // Turn PTT led off pin_write(43, LOW); }
int main() { struct nrf24l01_driver_t nrf24l01; struct pin_driver_t pin[3]; uint8_t state[32]; sys_start(); nrf24l01_init(&nrf24l01, &spi_device[0], &pin_d10_dev, &pin_d6_dev, &exti_device[1], SERVER_ADDRESS); nrf24l01_start(&nrf24l01); /* Initialize led pins. */ pin_init(&pin[0], &pin_d7_dev, PIN_OUTPUT); pin_init(&pin[1], &pin_d8_dev, PIN_OUTPUT); pin_init(&pin[2], &pin_d9_dev, PIN_OUTPUT); pin_write(&pin[0], 0); pin_write(&pin[1], 0); pin_write(&pin[2], 0); while (1) { /* Read state from client. */ nrf24l01_read(&nrf24l01, state, sizeof(state)); std_printf(FSTR("state = 0x%x\r\n"), (int)state[0]); /* Upadte LED. */ pin_write(&pin[0], (state[0] >> 0) & 0x1); pin_write(&pin[1], (state[0] >> 1) & 0x1); pin_write(&pin[2], (state[0] >> 2) & 0x1); } return (0); }
int sensors_lm60(int powerPin, int readPin) { pin_write(powerPin, HIGH); // Turn the LM60 on analogReference(INTERNAL); // Ref=1.1V. Okay up to 108 degC (424 + 6.25*108 = 1100mV) analogRead(readPin); // Disregard the 1st conversion after changing ref (p.256) delay(10); // This is needed when switching references int adc = analogRead(readPin); // Real read pin_write(powerPin, LOW); // Turn the LM60 off int mV = 1100L * adc / 1024L; // Millivolts switch(TEMP_UNIT) { case 1: // C // Vo(mV) = (6.25*T) + 424 -> T = (Vo - 424) * 100 / 625 return (4L * (mV - 424) / 25) + CALIBRATION_VAL; case 2: // K // C + 273 = K return (4L * (mV - 424) / 25) + 273 + CALIBRATION_VAL; case 3: // F // (9/5)C + 32 = F return (36L * (mV - 424) / 125) + 32 + CALIBRATION_VAL; } }
/* void ping() { pin_write(pingPin, HALF_INT); timer_after(&timer4, 500e-6, 1, send); } */ void motorControl(dist) { while (stepCount != dist){ if (motorOn == 1){ stepCount += step; motorOn = 0; pin_write(stepPin, 0); } else if (motorOn == 0){ motorOn = 1; if (stepCount > dist){ step = -1; dir = 0; } else if (stepCount < dist){ step = 1; dir = 1; } pin_write(dirPin, dir); pin_write(stepPin, 1); } timer_start(&timer1); led_toggle(&led1); while (1) { count = timer_read(&timer1); led_toggle(&led2); if (count > period){ break; } } timer_stop(&timer1); timer_lower(&timer1); } }
int test_falling(struct harness_t *harness_p) { int i; struct pcint_driver_t pcint; pin_write(&pin, 1); count = 0; BTASSERT(pcint_init(&pcint, &pcint_a9_dev, PCINT_TRIGGER_FALLING_EDGE, isr, NULL) == 0); BTASSERT(pcint_start(&pcint) == 0); /* 10 falling, 9 rising edges. */ for (i = 0; i < 10; i++) { pin_write(&pin, 1); time_busy_wait_us(10000); pin_write(&pin, 0); time_busy_wait_us(10000); } BTASSERT(pcint_stop(&pcint) == 0); /* The interrupt is disabled, no increment. */ pin_write(&pin, 1); time_busy_wait_us(10000); pin_write(&pin, 0); time_busy_wait_us(10000); std_printf(FSTR("count: %d\r\n"), count); BTASSERT(count == 10); return (0); }
/** * def write(self, value) */ static mp_obj_t class_pin_write(mp_obj_t self_in, mp_obj_t value_in) { struct class_pin_t *self_p; int value; value = mp_obj_get_int(value_in); if ((value != 0) && (value != 1)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "bad pin value %d", value)); } self_p = MP_OBJ_TO_PTR(self_in); pin_write(&self_p->drv, value); return (mp_const_none); }
void showBlank(void){ volatile uint8_t segments = 0b00000000; volatile uint8_t segmentsZero = 0b00000000; int a; for(a = 0; a < 3; a++){ int z; for (z = 0 ; z < 8 ; z++){ volatile uint8_t transferSegment = segments & (1 << (7 - z)); pin_clear(segmentClock); pin_write(segmentData, transferSegment); pin_set(segmentClock); } } pin_clear(segmentLatch); pin_set(segmentLatch); }
void postNumber(int number, uint8_t decimal){ volatile uint8_t segments = 0b00000000; volatile uint8_t segmentsZero = 0b00000000; #define a 1<<0 #define b 1<<6 #define c 1<<5 #define d 1<<4 #define e 1<<3 #define f 1<<1 #define g 1<<2 #define dp 1<<7 switch (number){ case 1: segments = b | c | segmentsZero; break; case 2: segments = a | b | d | e | g | segmentsZero; break; case 3: segments = a | b | c | d | g | segmentsZero; break; case 4: segments = f | g | b | c | segmentsZero; break; case 5: segments = a | f | g | c | d | segmentsZero; break; case 6: segments = a | f | g | e | c | d | segmentsZero; break; case 7: segments = a | b | c | segmentsZero; break; case 8: segments = a | b | c | d | e | f | g | segmentsZero; break; case 9: segments = a | b | c | d | f | g | segmentsZero; break; case 0: segments = a | b | c | d | e | f | segmentsZero; break; case ' ': segments = 0 | segmentsZero; break; case 'c': segments = g | e | d | segmentsZero; break; case '-': segments = g | segmentsZero; break; } if (decimal == 1){ segments |= dp; } int y; for (y = 0 ; y < 8 ; y++){ volatile uint8_t transferSegment = segments & (1 << (7 - y)); pin_clear(segmentClock); pin_write(segmentData, transferSegment); pin_set(segmentClock); } }