void init_game(_TIMER *level_timer, _TIMER *decay_timer, _PIN *coin_op, Display *score_display, Display *high_display) { game.hit_flag = 0; game.coin_flag = 0; game.score = 0; game.high = 0; pin_digitalIn(coin_op); game.coin_op = coin_op; game.level_timer = level_timer; game.level_ticks = 0; game.level_limit = MAX_LEVEL; game.level = 0; game.lose_flag = 0; game.lose_ticks = 0; timer_setPeriod(game.level_timer, 0.2); timer_start(game.level_timer); game.decay_timer = decay_timer; game.decay_ticks = 0; game.decay_limit = MAX_DECAY; game.life = MAX_LIFE; timer_setPeriod(game.decay_timer, 0.002); timer_start(game.decay_timer); game.score_display = score_display; game.high_display = high_display; write_display(game.high_display, game.high, 0); write_display(game.score_display, game.score, 0); game.state = over_game; game.last_state = (STATE_HANDLER_T)NULL; }
int16_t main(void) { int led2_on; int led3_on; led2_on = 0; led3_on = 1; init_clock(); init_ui(); init_timer(); led_on(&led1); timer_setPeriod(&timer2, 0.1); timer_start(&timer2); while (1) { if (timer_flag(&timer2)) { timer_lower(&timer2); led_toggle(&led1); } if(!sw_read(&sw2)){ timer_setPeriod(&timer2, 0.5); timer_start(&timer2); led2_on = 1; led3_on = 0; } else if(!sw_read(&sw3)){ timer_setPeriod(&timer2, 0.1); timer_start(&timer2); led2_on = 0; led3_on = 1; } led_write(&led2, led2_on); led_write(&led3, led3_on); } }
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(); imu_init() //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. x_gout = gyro_read(OUT_X_L); } }
void setup(void) { // Initialize PIC24 modules. init_clock(); init_ui(); init_timer(); init_pin(); init_oc(); init_spi(); init_uart(); // Configure single SPI comms. system pin_digitalOut(SPI_CS); pin_set(SPI_CS); spi_open(spi_inst, SPI_MISO, SPI_MOSI, SPI_SCK, spi_freq, spi_mode); // Configure & start timers used. timer_setPeriod(&timer1, 1); timer_setPeriod(&timer2, 1); // Timer for LED operation/status blink timer_setPeriod(&timer3, LOOP_TIME); // Timer for main control loop timer_start(&timer1); timer_start(&timer2); timer_start(&timer3); // Configure dual PWM signals for bidirectional motor control oc_pwm(&oc1, PWM_I1, NULL, pwm_freq, pwm_duty); oc_pwm(&oc2, PWM_I2, NULL, pwm_freq, pwm_duty); InitUSB(); // initialize the USB registers and // serial interface engine while (USB_USWSTAT != CONFIG_STATE) { // while periph. is not configured, ServiceUSB(); // service USB requests } }
int16_t main(void) { init(); REV = 0; REQUESTED_DIRECTION = 0; SENSED_DIRECTION = 0; DUTY = 0; led_on(&led2); timer_setPeriod(HB_TIMER, 0.5); timer_start(HB_TIMER); printf("Good morning!\n"); oc_pwm(&oc3,D2n,PWM_TIMER,FREQ,DUTY); while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } while (1) { ServiceUSB(); SetMotorVelocity(DUTY,REQUESTED_DIRECTION); get_direction(); get_feedback(); if (timer_flag(HB_TIMER)) { timer_lower(HB_TIMER); led_toggle(&led1); } } }
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 setup(void) { // Initialize PIC24 modules. init_clock(); init_ui(); init_timer(); init_pin(); init_oc(); init_spi(); init_uart(); // Configure single SPI comms. system pin_digitalOut(SPI_CS); pin_set(SPI_CS); spi_open(spi_inst, SPI_MISO, SPI_MOSI, SPI_SCK, spi_freq, spi_mode); // Configure & start timers used. timer_setPeriod(&timer1, 1); timer_setPeriod(&timer2, 1); // Timer for LED operation/status blink timer_setPeriod(&timer3, LOOP_TIME); // Timer for main control loop timer_start(&timer1); timer_start(&timer2); timer_start(&timer3); // Configure motor current conversion coefficient CURRENT_CONV_COEF = MAX_ADC_OUTPUT * MOTOR_VOLTAGE_RESISTOR; cur_control.Kp = KP; cur_control.Kd = KD; cur_control.Ki = KI; cur_control.dt = LOOP_TIME; cur_control.integ_min = -100; cur_control.integ_max = 100; cur_control.integ_state = 0; read_motor_current(&motor); cur_control.prev_position = convert_motor_torque(motor.current); // Configure dual PWM signals for bidirectional motor control oc_pwm(&oc1, PWM_I1, NULL, pwm_freq, pwm_duty); oc_pwm(&oc2, PWM_I2, NULL, pwm_freq, pwm_duty); pin_analogIn(MOTOR_VOLTAGE); pin_digitalOut(DEBUGD0); pin_digitalOut(DEBUGD1); InitUSB(); // initialize the USB registers and // serial interface engine while (USB_USWSTAT != CONFIG_STATE) { // while periph. is not configured, ServiceUSB(); // service USB requests } }
void setup() { timer_setPeriod(&timer1, 1); // Timer for LED operation/status blink // timer_setPeriod(&timer2, 0.01); // Timer for UART servicing // timer_setPeriod(&timer3, 0.01); timer_start(&timer1); // timer_start(&timer2); // timer_start(&timer3); }
int16_t main(void) { init_clock(); init_ui(); init_pin(); init_spi(); init_timer(); init_oc(); init_md(); led_off(&led2); led_off(&led3); ENC_MISO = &D[1]; ENC_MOSI = &D[0]; ENC_SCK = &D[2]; ENC_NCS = &D[3]; read_angle.w=0x3FFF; Pscale.w=1; Iscale.w=0; direction.w=1; speed.w=0; angle_now.i=180; angle_prev.i=180; angle.w=10; uint8_t loop = 0; pin_digitalOut(ENC_NCS); pin_set(ENC_NCS); spi_open(&spi1, ENC_MISO, ENC_MOSI, ENC_SCK, 2e8,1); timer_setPeriod(&timer1, 0.05); timer_start(&timer1); 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(); if (timer_flag(&timer1)) { timer_lower(&timer1); angle_prev=angle_now; angle_prev_con=angle; // service any pending USB requests angle_now = enc_readReg(read_angle); angle_now = mask_angle(angle_now); angle=convert_Angle(angle_prev,angle_now,&loop); spring_simple(angle); } } }
void wait_period(float period){ timer_setPeriod(&timer2, period); timer_start(&timer2); while(1){ if (timer_flag(&timer2)) { timer_lower(&timer2); break; } } }
int16_t main(void) { init_clock(); init_ui(); init_timer(); led_on(&led1); led_on(&led3); timer_setPeriod(&timer2, 0.5); timer_start(&timer2); while (1) { if (timer_flag(&timer2)) { timer_lower(&timer2); led_toggle(&led1); led_toggle(&led2); <<<<<<< HEAD }
int16_t main(void) { init_clock(); init_ui(); init_timer(); led_on(&led1); timer_setPeriod(&timer2, 1.0); timer_start(&timer2); while (1) { if (timer_flag(&timer2)) { timer_lower(&timer2); led_toggle(&led1); led_toggle(&led2); } led_write(&led3, !sw_read(&sw3)); } }
int16_t main(void) { //initialize modules init_clock(); init_timer(); init_uart(); timer_setPeriod(&timer1,1e-2);//100 Hz timer_lower(&timer1); timer_start(&timer1); printf("Begin"); while (1) { if (timer_flag(&timer1)){ printf("GO"); timer_lower(&timer1); } } }
void receive(_TIMER *self) { timer_setPeriod(&timer5, 50e-3); timer_start(&timer5); led_toggle(&led1); overflowing = 0; while(pin_read(&D[12]) == 0){ if (timer_flag(&timer5) != 0){ led_toggle(&led3); overflowFlag += 1; overflowing = 1; break; } } if (overflowing == 0) { overflowFlag = 0; } readTime = timer_read(&timer5); led_toggle(&led2); timer_stop(&timer5); }
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); } } }
int16_t main(void) { init_clock(); init_ui(); init_timer(); init_pin(); init_oc(); init_md(); led_on(&led2); led_on(&led3); timer_setPeriod(&timer1, 0.5); timer_start(&timer1); while (1) { if (timer_flag(&timer1)) { timer_lower(&timer1); if (speed > 24000) { up=0; } if (speed < 12000) { up=1; } if (up) { speed=speed+300; } if (!up) { speed=speed-300; } // direction = !direction; md_speed(&mdp, speed); md_direction(&mdp, direction); } } }
int16_t main(void) { init_pin(); init_clock(); init_uart(); init_ui(); init_timer(); init_oc(); //setup the signal input pin pin_digitalIn(&D[4]); val1 = 0; val2 = 0; pos = 0; //16 bit int with binary point in front of the MSB led_on(&led2); timer_setPeriod(&timer2, PULSE_FREQUENCY); //how often we send a pulse timer_start(&timer2); timer_setPeriod(&timer3, 0.5); //heartbeat timer_start(&timer3); oc_servo(&oc1,&D[0],&timer4, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos); oc_servo(&oc2,&D[2],&timer5, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos); oc_pwm(&oc3,&D[3],NULL,FREQ,ZERO_DUTY); 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(); pin_write(&D[0],val1); pin_write(&D[2],val2); //adapted from Patrick and Charlie's approach if (!send_pulse && timer_read(&timer2) < PULSE_WIDTH){ send_pulse = 1; pin_write(&D[3],HALF_DUTY); get_distance = 1; } else if (send_pulse && timer_read(&timer2) >= PULSE_WIDTH) { send_pulse = 0; pin_write(&D[3],ZERO_DUTY); } if (timer_read(&timer2) >= ECHO_TIME) { if (pin_read(&D[4]) && get_distance) { printf("%d\n", timer_read(&timer2)); get_distance = 0; } } if (timer_flag(&timer3)) { //show a heartbeat and a status message timer_lower(&timer3); led_toggle(&led1); } } }
void VendorRequests(void) { WORD temp; uint16_t i = 1; switch (USB_setup.bRequest) { case HELLO: printf("Hello World!\n"); BD[EP0IN].bytecount = 0; // set EP0 IN byte count to 0 BD[EP0IN].status = 0xC8; // send packet as DATA1, set UOWN bit break; case SET_VALS: pan_set_val = USB_setup.wValue.w; tilt_set_val = USB_setup.wIndex.w; BD[EP0IN].bytecount = 0; // set EP0 IN byte count to 0 BD[EP0IN].status = 0xC8; // send packet as DATA1, set UOWN bit break; case GET_VALS: temp.w = x_gout; BD[EP0IN].address[0] = temp.b[0]; BD[EP0IN].address[1] = temp.b[1]; temp.w = tilt_set_val; BD[EP0IN].address[2] = temp.b[0]; BD[EP0IN].address[3] = temp.b[1]; BD[EP0IN].bytecount = 4; // set EP0 IN byte count to 4 BD[EP0IN].status = 0xC8; // send packet as DATA1, set UOWN bit break; case PRINT_VALS: printf("pan_set_val = %u, tilt_set_val = %u\n", pan_set_val, tilt_set_val); BD[EP0IN].bytecount = 0; // set EP0 IN byte count to 0 BD[EP0IN].status = 0xC8; // send packet as DATA1, set UOWN bit break; case GET_DIST: //Configure timer for transducer burst timer_setPeriod(PWM_TIM, 0.0005); timer_start(PWM_TIM); //start the timer for the PWM signal timer_start(DIST_TIM); //start the timer for the distance measurement pin_write(SONIC_OUT_PIN, 0x8000); //send the sonic burst by setting it to 50% duty cycle while (1) { if (timer_flag(PWM_TIM)) { //If the timer runs out for PWM pin_write(SONIC_OUT_PIN, 0x0000); //Stopping the sonic burst transmission by setting it to 0% duty cycle timer_stop(PWM_TIM); //stop PWM timer break; } } timer_setPeriod(PWM_TIM, 0.001); //reset the PWM timer period to 1 milliseconds timer_start(PWM_TIM); while(1) { if (timer_flag(PWM_TIM)) { //If the timer runs out for PWM if(pin_read(SONIC_IN_PIN) == 1){ //If the pin gets back a value, the echo is received temp.w = timer_read(DIST_TIM); //Get the value of the timer for Distance and store in temp.w to be sent to laptop break; } else if(timer_flag(DIST_TIM)) { //timeout period will be set based on experimentation temp.w = 0xFFFF; break; } } } timer_stop(DIST_TIM); timer_stop(PWM_TIM); /*while(pin_read(SONIC_IN_PIN) == 0 && i != 0) { i++; } */ BD[EP0IN].address[0] = temp.b[0]; BD[EP0IN].address[1] = temp.b[1]; temp.w = pan_set_val; BD[EP0IN].address[2] = temp.b[0]; BD[EP0IN].address[3] = temp.b[1]; temp.w = tilt_set_val; BD[EP0IN].address[4] = temp.b[0]; BD[EP0IN].address[5] = temp.b[1]; BD[EP0IN].bytecount = 6; // set EP0 IN byte count to 6 BD[EP0IN].status = 0xC8; // send packet as DATA1, set UOWN bit break; default: USB_error_flags |= 0x01; // set Request Error Flag } }