Example #1
0
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;
	}
}
Example #2
0
void Pwm_init() 
{   RCONbits.IPEN = 1; // Enable priority levels on interrupts 
    IPR1bits.RCIP = 1;
    INTCONbits.GIEH = 1;
	OpenTimer2(0x06);  // chon gia tri 16 prescale cho timer2
	OpenPWM2(0xff);	   // set gia tri thanh ghi PR2 la 0xff
}
Example #3
0
void main()
{
    unsigned char periode = 0x4D;                         // PWM Period = [(PR2) + 1] * 4 * TOSC *(TMR2 Prescale Value)
    unsigned int Duty_cycle_1 = 50;                         // 1/4000 = [PR2 + 1] * 4*[1 / 20000000] * 16
    unsigned int Duty_cycle_2 = 170;
    CCP2_DIR = OUT;
    CCP1_DIR = OUT;
    OpenPWM1( periode );
    OpenPWM2( periode );
    while(1)                                            
    {
     PwmCycle(Duty_cycle_1, Duty_cycle_2);       // period and Duty cycle required for the PWM
    }
}
Example #4
0
void initMotor() {
  // Set the motor outputs as digital output
  MOTOR1_0_DIR = 0;
  MOTOR1_1_DIR = 0;
  MOTOR2_0_DIR = 0;
  MOTOR2_1_DIR = 0;

  // Maximal PWM freq: 5000Hz (using L293DNE motor driver)
  // PWM period = (0xFF + 1)*4*Tosc*T2Prescaler
  OpenPWM1(0xFF);   // configuring PWM module 1
  OpenPWM2(0xFF);   // configuring PWM module 2

  // Configuring timer 2 which provides timing for PWM
  // TIMER_INT_OFF: disable timer interrupt
  // T2_PS_1_4: Timer2 prescaling set to 4
  // T2_POST_1_1: Timer2 postscaling set to 1
  OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1);

  setSpeedMotor1(0);
  setSpeedMotor2(0);
}
Example #5
0
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;
	
}
Example #6
0
// 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);
    }
}