void main(void) { char period = 249; //----------------------open TMR2 before Open PWM2-------------------------------------------- OpenTimer2(TIMER_INT_OFF&T2_PS_1_16&T2_POST_1_1); //-------------------------Configure pwm ----------------------------------------------------- OpenPWM2(period); //Configure PWM module and initialize PWM period //---------------------set duty cycle--------------------------------------------------------- SetDCPWM2(duty_cycle[k]); //set the duty cycle //-----------------------use TMR0 to change position of servo---------------------------------- OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4 ); while(1) { while(!INTCONbits.TMR0IF);//waiting for the flag is set INTCONbits.TMR0IF = 0;//clear flag for next times overflow SetDCPWM2( duty_cycle[k]);//new duty cycle k=k+1; if(k==13) k=-1; } }
void main() { TRISB=0x00; TRISA=0x00; TRISDbits.TRISD7=0; // chan dieu khien coi bao a=0; index=0x03; Init_UART(); Init_Tran_UART(); Init_Rec_UART(); Osc(); Pwm_init(); VITRI=VITRI_3; //putrsUSART ((const far rom char*)"\r\nNguy hiem muc 3\r\n"); while(1) { LATAbits.LATA0= (index );// lay 4 bit chua gia tri cua index truyen sang 7447, tranh chan ngat int0 LATBbits.LATB1= (index >>1); LATBbits.LATB2= (index >>2); LATBbits.LATB3= (index >>3); LATDbits.LATD7= a; SetDCPWM2(VITRI); } while(1); }
void stopMotors() { SetDCPWM1(0); MOTOR1_1 = 0; SetDCPWM2(0); MOTOR2_1 = 0; }
void setSpeedMotor2(short speed) { static BYTE prev_dir; BYTE dir; dir = (speed > 0); // check if motor has changed direction if (dir != prev_dir) { // stop motor SetDCPWM2(0); // wait a short time delay_ms(TRANSIENT_DELAY); // update direction prev_dir = dir; } if (dir == FORWARD) { SetDCPWM2(speed); MOTOR2_1 = 0; } else { // speed is negative SetDCPWM2(1023+speed); MOTOR2_1 = 1; } }
void main() { OSCCONbits.IRCF = 0b011;//1 MHz TRISB = 0x00; TRISA = 0x00; TRISDbits.TRISD7 = 0; // chan dieu khien coi bao alarm = 0; Led7 = 0x03; Init_UART(); // Init_Tran_UART(); // Init_Rec_UART(); //initialize UART module // OpenUSART(USART_TX_INT_OFF & // USART_RX_INT_ON & // USART_ASYNCH_MODE & // USART_EIGHT_BIT & // USART_BRGH_HIGH & // BAUD_16_BIT_RATE, // 12); INTCONbits.PEIE = 1;//Peripheral interrupt enable INTCONbits.GIE = 1; // Golbal interrupt enable Pwm_init(); VITRI = VITRI_3; while(1) { LATAbits.LATA0 = Led7;// lay 4 bit chua gia tri cua Led7 truyen sang 7447, tranh chan ngat int0 LATBbits.LATB1 = (Led7 >>1); LATBbits.LATB2 = (Led7 >>2); LATBbits.LATB3 = (Led7 >>3); LATDbits.LATD7 = alarm; SetDCPWM2(VITRI); if(EnableProcess) { Processing(); EnableProcess = 0; } } //while(1); }
void Init(){ // Activation des interruptions INTCONbits.GIEH = 1; // Activation interruptions hautes INTCONbits.GIEL = 1; // Activation interruptions basses RCONbits.IPEN=1; // Activation des niveau d'interruptions // Initialisation du Timer 0 pour la base de temps OpenTimer0( TIMER_INT_ON & // interruption ON T0_16BIT & // Timer 0 en 16 bits T0_SOURCE_INT & // Source interne (Quartz + PLL) T0_PS_1_32); // 128 cycle, 1 incrémentation WriteTimer0(0xffff - 2074); timer_emi = 0; timer_led = 0; // Initialisation de l'UART TXSTAbits.TX9 = 0; // Mode 8 bits TXSTAbits.TXEN = 1; // Activation du module UART de transmission TXSTAbits.BRGH = 0; // Gestion de la base de temps BAUDCONbits.BRG16 = 1; // Gestion de la base de temps BAUDCONbits.TXCKP = 1; // Inversion de la sortie série SPBRGH = 3; // Gestion de la base de temps => 0x0361 = 829 SPBRG = 61; // Gestion de la base de temps RCSTAbits.SPEN=1; TRISCbits.TRISC6 = 1; TRISCbits.TRISC7 = 1; // Configuration de la MLI OpenTimer2( TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1 ); TRISBbits.TRISB3=0; OpenPWM2(82); SetDCPWM2((unsigned int)164); // On allume la LED TRIS_LED = 0; LED = 1; }
void main(void) { float pmax; uint16_t pmax_ton; // Configure the oscillator for the device ConfigureOscillator(); // Initialize I/O and Peripherals for application InitApp(); // User application SetDCPWM2(33); /* T0CON = 0; T0CONbits.TMR0ON = 1; INTCONbits.TMR0IE = 1; INTCONbits.GIE = 1; */ while (1) { } }
// Process USB commands void processUsbCommands(void) { // Check if we are in the configured state; otherwise just return if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) { // We are not configured return; } // Check if data was received from the host. if (!HIDRxHandleBusy(USBOutHandle)) { // Command mode switch (ReceivedDataBuffer[0]) { case 0x01: // System Commands switch (ReceivedDataBuffer[1]) { case 0x01: // System Commands // Copy any waiting debug text to the send data buffer ToSendDataBuffer[0] = 0xFF; // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; case 0x02: // Feeder Commands switch (ReceivedDataBuffer[1]) { case 0x02: // Feeder Status if (atmegaFeederRunning){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x03: // Go to feeder StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(ReceivedDataBuffer[2]); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x04: // Reset Feeder Z StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(80); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x05: // Full feeder reset StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(70); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x06: // Reset ATMEGA IC atmegaResetPin = 0; Delay1KTCYx(10); atmegaResetPin = 1; break; default: // Unknown command received break; } break; case 0x03: // Vacuum and Vibration Commands switch (ReceivedDataBuffer[1]) { case 0x01: // Vacuum 1 set if (ReceivedDataBuffer[2] == 0x01){ setVac1on; } else{ setVac1off; } break; case 0x02: // Vacuum 2 set if (ReceivedDataBuffer[2] == 0x01){ setVac2on; } else{ setVac2off; } break; case 0x03: // Vibration Motor set if (ReceivedDataBuffer[2] == 0x01){ setVibrationon; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x01); // sends a control byte to the device IdleI2C(); StopI2C(); } else{ setVibrationoff; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x02); // sends a control byte to the device IdleI2C(); StopI2C(); } break; case 0x04: // Vacuum 1 status if (vac1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x05: // Vacuum 2 status if (vac2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // Vibration Motor status if (vibrationrunning == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x07: // Vibration Motor status vibrationmotor_duty_cycle = ReceivedDataBuffer[2]; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x03); // sends a control byte to the device IdleI2C(); WriteI2C(vibrationmotor_duty_cycle); // sends a control byte to the device IdleI2C(); StopI2C(); break; default: // Unknown command received break; } break; case 0x04: // LED Commands switch (ReceivedDataBuffer[1]) { case 0x01: // LED Base Camera on/off if (ReceivedDataBuffer[2] == 0x01){ OpenPWM1(0xFF); led1_duty_cycle = led1_duty_cycle * 4; SetDCPWM1(led1_duty_cycle); // outBaseLED = 1; led1running = 1; }else{ ClosePWM1(); // outBaseLED = 0; led1running = 0; } break; case 0x02: // LED Base Camera PWM set led1_duty_cycle = ReceivedDataBuffer[2]; Write_b_eep(baseLED_EEPROM_address, led1_duty_cycle); led1_duty_cycle = led1_duty_cycle * 4; SetDCPWM1(led1_duty_cycle); break; case 0x03: // LED Head Camera on/off if (ReceivedDataBuffer[2] == 0x01){ OpenPWM2(0xFF); led2_duty_cycle = led2_duty_cycle * 4; SetDCPWM2(led2_duty_cycle); // outBaseLED = 1; led2running = 1; }else{ ClosePWM2(); // outBaseLED = 0; led2running = 0; } break; case 0x04: // LED Head Camera PWM set led2_duty_cycle = ReceivedDataBuffer[2]; Write_b_eep(headLED_EEPROM_address, led2_duty_cycle); led2_duty_cycle = led2_duty_cycle * 4; SetDCPWM2(led2_duty_cycle); break; case 0x05: // LED Base Status if (led1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // LED Head Status if (led2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; default: // Unknown command received break; } // Re-arm the OUT endpoint for the next packet USBOutHandle = HIDRxPacket(HID_EP, (BYTE*) & ReceivedDataBuffer, 64); } }
PROCESS_STATE TrainMotorAway( int speed, int AcDeceleration ) { static int Current_Backward_Speed = 0; int Steps = 0; PROCESS_STATE ReturnStatus = In_Progress; switch ( AcDeceleration ) { case 1: Steps = 100; break; case 2: Steps = 200; break; case 3: Steps = 300; break; case 4: Steps = 400; break; case 5: Steps = 500; break; } if ( Check_Timer( TRAIN_MOVEMENT ) == MATURED ) // Matured { if ( speed == Current_Backward_Speed ) { // putrsUSART( "Forward: Train at requested speed...\r\n" ); ReturnStatus = Command_Complete; } else if ( speed > Current_Backward_Speed ) { Current_Backward_Speed = Current_Backward_Speed + Steps; // putrsUSART( "Forward: +400\r\n" ); if ( Current_Backward_Speed > 1023) { // putrsUSART( "Forward: More than 1023 setting to 'speed'...\r\n" ); Current_Backward_Speed = speed; } SetDCPWM2( Current_Backward_Speed ); } else if ( speed < Current_Backward_Speed ) { Current_Backward_Speed = Current_Backward_Speed - Steps; // putrsUSART( "Forward: +400\r\n" ); if ( Current_Backward_Speed < speed ) { // putrsUSART( "Forward: less than 'speed' so setting to 'speed'...\r\n" ); Current_Backward_Speed = speed; } SetDCPWM2( Current_Backward_Speed ); } Set_Timer(TRAIN_MOVEMENT, 0, 1, 0 ); } return ReturnStatus; }