/****************************************************************************** * Function: void UserInit(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This routine should take care of all of the demo code * initialization that is required. * * Note: * *****************************************************************************/ void UserInit(void) { // Real time clock start RtccInitClock(); RtccWrOn(); { rtccTimeDate initd; initd.f.year=0x10; initd.f.mon=0x01; initd.f.mday=0x01; initd.f.wday=5; // 2010.01.01 is friday initd.f.hour=0; initd.f.min=0; initd.f.sec=0; RtccWriteTimeDate(&initd,FALSE); RtccWriteAlrmTimeDate(&initd); } mRtccOn(); mRtccAlrmEnable(); RtccSetAlarmRpt(RTCC_RPT_MIN,TRUE); // Mtouch init mTouchInit(); mTouchCalibrate(); cmdstr[0]=0; screen=0; screenvalid=0; position=0; buttonstate.Val=0; buttonpressed.Val=0; devicereset=0; clockss=0; resetcounter=0; alarmcnt=0; TRISBbits.TRISB1=0; PPSUnLock(); iPPSOutput(OUT_PIN_PPS_RP4,OUT_FN_PPS_CCP1P1A); //Configre RP24 as C1OUT pin PPSLock(); //----Configure pwm ---- period = 0xFF; OpenPWM1( period); //Configure PWM module and initialize PWM period //-----set duty cycle---- duty_cycle = 256; SetDCPWM1(duty_cycle); //set the duty cycle //----set pwm output---- outputconfig = HALF_OUT ; outputmode = PWM_MODE_1; SetOutputPWM1( outputconfig, outputmode); //output PWM in respective modes ADCON0bits.CHS=4; /* Make sure A/D interrupt is not set */ PIR1bits.ADIF = 0; /* Begin A/D conversion */ }//end UserInit
void main(){ char period = 0xAA; Initialize_ADC(); OpenPWM1(period); //PWM on pin 17 SetDCPWM1(Get_ADC()); //ADC on pin 2 }
// Initialisation du Moteur void M_prop_init(void){ TRIS_M_SENS_1 = 0; // Sortie TRIS_M_SENS_2 = 0; // Sortie TRIS_M_ENABLE = 0; // Sortie // Ouverture du PWM OpenPWM1(255); SetDCPWM1(0); // Stop Stop(); }
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 } }
////////////////////////////////////////////////////////// // xpl_init // Initialisation of the xPL library. Tries to restore the // INSTANCE_ID from EEPROM void xpl_init(void){ // Enable the PWM hardware if required // !!!!!!! Warning enabling means that resistor must be dismanteled from print, if not it will be fried !!!!!!!!!!!!!!! #ifdef PWM_ENABLED #warning "PWM output enabled on PORTC.2" // Enable PORTC.2 output direction TRISC &= 0xFB; // Enable the PWM timer OpenTimer2(TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_1); // And enable the PWM OpenPWM1(249); SetDCPWM1(1000); #endif // Init the helper libraries do that we know if need to // library specific code if (!oo_init()){ xpl_node_configuration |= ONE_WIRE_PRESENT; oo_read_temperatures(); } xpl_init_instance_id(); xpl_count_gas = 0; xpl_count_water = 0; xpl_count_elec = 0; // Only apply this function after we have read the EEPROM, as we enable serial reception // in this function and when we do that we need to know our ID. xpl_init_state(); xpl_rx_fifo_write_pointer = 0; xpl_rx_fifo_read_pointer = 0; xpl_rx_fifo_data_count = 0; //xpl_rate_limiter = time_ticks; xpl_hbeat_sent = 0; xpl_flow = FLOW_ON; putc(XON, _H_USART); }
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); }
void configTimers (void) { //TIMER0 OpenTimer0 ( TIMER_INT_ON & T0_8BIT & T0_EDGE_FALL & T0_PS_1_256 & T0_SOURCE_INT ); // Clock Interno, contador de 8 bits e Prescaler 1/256 WriteTimer0 ( 0x3D ); // Gera um intervalo de 0,1 segundo a 4 mhz //TMR0ON=1; // Liga o Timer0 //extern volatile __bit TMR0ON @ (((unsigned) &T0CON)*8) + 7; //#define TMR0ON_bit BANKMASK(T0CON), 7 TMR0IF=0; // Zera o Overflow do Timer0 TMR0IE=1; // Habilita a Interrupcao para o Overflow do Timer0 ////////////////////////////////////////////////////////////////////// // TIMER1 OpenTimer1(TIMER_INT_ON & T1_SOURCE_EXT & T1_SYNC_EXT_OFF & T1_PS_1_1 & T1_OSC1EN_ON & T1_16BIT_RW ); //WriteTimer1( 0xBE5 ); // equivalemnte a 1/2 segundo em 4,00 mhz WriteTimer1( 0x8000 ); // equivalente a 1 segundo em 32,768 khz //TMR1ON = 1; // Liga o Timer1 -> Igual ao TIMER_INT_ON TMR1IF = 0; // Zera o Overflow para o Timer1 TMR1IE=1; // Habilita Interrupcao para o Overflow do Timer1 ////////////////////////////////////////////////////////////////////// // PWM TIMER2 OpenPWM1(0x7C); // Inicializa o PWM com intervalo de 2khz e PS_1/16 // num clock de 4 mhz SetDCPWM1(0x000F); // Configura o Duty Cycle Inicial OpenTimer2( TIMER_INT_OFF & T2_PS_1_16 ); // Para o PWM funcionar corretamente, a Interrupcao // TIMER_INT_OFF (ou TMR2IE) deve ser desabilitada // obs: como o PWM funciona como um "temporizador", nao precisa executar // interrupcao ou acao; a propria porta de saida PWM1 ja executa //TMR2=0; // Clear Timer2 -> Nao necessario para este exemplo //T2CKPS1=1; // Pre-Scaler 16 (ja setado no T2_PS1_16), redundante SetOutputPWM1( SINGLE_OUT , PWM_MODE_1 ); // Configura o CCP1CON // apenas o PWM1: P1A modulated; P1B, P1C, P1D assigned as port pins // no modo PxA,PxC active high, PxB,PxD active high */ //CCP1IE=1; // Habilita Interrupcao no modulo CCP1, Nao Necessario //TMR2IF=0; // Limpando o flag de overflow do Timer2, Nao Necessario TMR2ON=1; // Ligando o Timer2 //TMR2IE=0; // -> TIMER_INT_OFF , redundante // Pisca Verde/Vermelho 2 vezes para sinalizar inicio do PWM __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0; __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0; __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0; __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0; PEIE=1; // Habilita As Interrupcoes dos Perifericos GIE=1; // Habilita as Interrupcoes Globais }
// 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); } }
/***************************************************************** * Function: setupMotor * Input Variables: none * Output return: none * Overview: Sets up the motor control using PWM ******************************************************************/ void setupMotor(){ OpenPWM1(MOTORCLOCKTICKS); SetDCPWM1(MOTORSTOP); }
void init(void) { unsigned char msgdata[ 8 ]; vscp18f_init( TRUE ); // Initialize the uP // PortA // RA0 - SENS - Input // RA1 - - Output // RA2 - - Output // RA3 - - Output // RA4 - - Output // RA5 - - Output // RA6 - OSC TRISA = 0b00000001; PORTA = 0b00000000; // PortB // RB0 - RFID_RX- Input // RB1 - - Output // RB2 - CAN TX - Output // RB3 - CAN RX - input // RB4 - - input // RB5 - - input // RB6 - - input // RB7 - - input TRISB = 0b11111001; PORTB = 0b00000000; // RC0 - - Output // RC1 - Button - Input // RC2 - PWM - Output // RC3 - - Output // RC4 - - Output // RC5 - - Output // RC6 - - Output // RC7 - - Output TRISC = 0b00000010; PORTC = 0b00000000; // TIMERS OpenTimer0(TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1 ); OpenTimer1(TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF ); OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1 ); // ADC OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD , ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VREFMINUS /*ADC_VREFPLUS_VDD*/ & ADC_REF_VREFPLUS_VSS /*ADC_VREFMINUS_VSS*/ , 0x0E); // PWM 125 kHz PWM_Calibr(&PWM_f,&PWM_adc); OpenPWM1(PWM_f);// 79 SetDCPWM1((PWM_f*2)-6); // 156 PWM_enable = ON; RCONbits.IPEN = 1; // enable HI and LOW priorities // INT priority IPR1bits.TMR1IP = 0; // Timer1 LOW priority OpenRB0INT( PORTB_CHANGE_INT_ON & RISING_EDGE_INT & PORTB_PULLUPS_OFF ); // Global INT ebable INTCONbits.GIEH = 1; INTCONbits.GIEL = 1; return; }
void isr_low( void ) { unsigned char temp; unsigned short tval0, tval1; char *p; // Clock if ( PIR1bits.TMR1IF ) { // If a Timer1 Interrupt, Then... WriteTimer1( 0xD8F0 ); // 1 mS vscp_timer++; /************** Handle Init button **************/ if ( PORTCbits.RC1 ) { vscp_initbtncnt = 0; } else { // Active vscp_initbtncnt++; } /************** VSCP LED *****************/ vscp_statuscnt++; if ( ( VSCP_LED_BLINK1 == vscp_initledfunc ) && ( vscp_statuscnt > 100 ) ) { if ( PORTAbits.RA2 ) { PORTAbits.RA2 = 0; } else { PORTAbits.RA2 = 1; } vscp_statuscnt = 0; } else if ( VSCP_LED_ON == vscp_initledfunc ) { PORTAbits.RA2 = 1; vscp_statuscnt = 0; } else if ( VSCP_LED_OFF == vscp_initledfunc ) { PORTAbits.RA2 = 0; vscp_statuscnt = 0; } /***************** Key *********************/ if( KeyTimer ) { KeyTimer--; } else { KeyOff(); } /***************** Red LED *********************/ if( RedLedTimer ) { RedLedTimer--; } else { RedLedOff(); } /***************** Green LED *******************/ if ( GreenLedTimer ) { GreenLedTimer--; } else { GreenLedOff(); } /***************** Beeper **********************/ if ( BeeperTimer ) { BeeperTimer--; } else { PORTCbits.RC0 = 0; if ( PWM_enable == OFF ) { OpenPWM1( PWM_f ); SetDCPWM1( ( PWM_f * 2 ) - 6 ); PWM_enable = ON; } } /**************** EmData Timer ****************/ if ( EmDataTimer ){ EmDataTimer--; } PIR1bits.TMR1IF = 0; // Clear Timer1 Interrupt Flag } return; }