void stopMotors() { SetDCPWM1(0); MOTOR1_1 = 0; SetDCPWM2(0); MOTOR2_1 = 0; }
/****************************************************************************** * 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 setSpeedMotor1(short speed) { static BYTE prev_dir; BYTE dir; dir = (speed > 0); // check if motor has changed direction if (dir != prev_dir) { // stop motor SetDCPWM1(0); // wait a short time delay_ms(TRANSIENT_DELAY); // update direction prev_dir = dir; } if (dir == FORWARD) { SetDCPWM1(speed); MOTOR1_1 = 0; } else { // speed is negative SetDCPWM1(1023+speed); MOTOR1_1 = 1; } }
////////////////////////////////////////////////////////// // 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); }
int main(int argc, char** argv) { unsigned int dutycycle=0; bool crescente=true; TRISD=0; // PORTD inteiro em Output TRISCbits.RC2=0; // Output do PWM1/P1A PORTD=0x00; // zera os leds dao PortD. LED_BRCO=0; LED_VERD=0; LED_VERM=0; LED_AMAR=1; // Inicialmente para fins de debug, acende o Led Amarelo configTimers(); // Inicializa os Timers 0, 1 e 2 (PWM) while (1) { LED_AMAR=1; LED_VERM=0; // Pisca-Pisca __delay_ms(10); if (crescente) dutycycle=dutycycle+4; // incrementa a intensidade else dutycycle=dutycycle-4; // descrementa a intensidade LED_AMAR=0; LED_VERM=1; // Pisca-Pisca __delay_ms(10); SetDCPWM1(dutycycle); // Atualiza o novo dutyCycle if (dutycycle<=4) crescente=true; // Led Azul Acendendo else if (dutycycle >= 1019) crescente=false; // Led Azul Apagando } return (EXIT_SUCCESS); // nao necessario, porem default do main C. }
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; }
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 }
/***************************************************************** * Function: stopMotor * Input Variables: void * Output return: none * Overview: Stops the motor ******************************************************************/ void stopMotor(){ SetDCPWM1(MOTORSTOP); }
// 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); }
/***************************************************************** * Function: startMotor * Input Variables: void * Output return: none * Overview: Moves the motor ******************************************************************/ void startMotor(){ SetDCPWM1(MOTORSPEED); }
enum XPL_CMD_MSG_TYPE_RSP xpl_handle_message_part(void) { switch (xpl_msg_state) { case WAITING_CMND: // If it is a command header -> set buffer state to CMD_RECEIVED if (strcmpram2pgm("xpl-cmnd", xpl_rx_buffer_shadow)==0) { xpl_msg_state = CMND_RECEIVED; } break; case CMND_RECEIVED: // check if we have the target in the buffer if (strcmpram2pgm("target=*", xpl_rx_buffer_shadow) == 0){ // Yes, message is wildcard and hence destined to us xpl_msg_state = WAITING_CMND_TYPE; } else if (strncmpram2pgm("target=hollie-utilmon.", xpl_rx_buffer_shadow, XPL_TARGET_VENDOR_DEVICEID_INSTANCE_ID_OFFSET)==0){ if (strcmp(xpl_instance_id, xpl_rx_buffer_shadow + XPL_TARGET_VENDOR_DEVICEID_INSTANCE_ID_OFFSET) == 0){ // bingo message if for us xpl_msg_state = WAITING_CMND_TYPE; } else { // Too bad, message is not for us. Wait for the next one xpl_msg_state = WAITING_CMND; } } break; case WAITING_CMND_TYPE: if (strcmpram2pgm("config.list", xpl_rx_buffer_shadow) == 0) { // No need to wait for the command here, this is a simple device, we only support one command xpl_msg_state = WAITING_CMND; return CONFIGURATION_CAPABILITIES_MSG_TYPE; } else if (strcmpram2pgm("config.response", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONFIG_RESPONSE; } else if (strcmpram2pgm("sensor.request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_SENSOR_REQUEST; } else if (strcmpram2pgm("hbeat.request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_HBEAT_REQUEST; } else if (strcmpram2pgm("config.current", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONFIG_CURRENT; } else if (strcmpram2pgm("control.basic", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONTROL_BASIC; } else if (strcmpram2pgm("}", xpl_rx_buffer_shadow) == 0) { // just wait for command } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONFIG_CURRENT: if (strcmpram2pgm("command=request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return CONFIG_STATUS_MSG_TYPE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_HBEAT_REQUEST: if (strcmpram2pgm("command=request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; // We need a random backoff here to ensure that we don't flood the // network with all nodes responding at the same time. // We use the time_ticks for that. That variable is never bigger than // 300, so we divide by ten and feed that value into the delay function Delay10KTCYx(time_ticks/10); return HEARTBEAT_MSG_TYPE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_SENSOR_REQUEST: if (strcmpram2pgm("command=current", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_SENSOR_REQUEST_DEVICE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_SENSOR_REQUEST_DEVICE: if (strcmpram2pgm("device=gas", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return GAS_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("device=water", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return WATER_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("device=elec", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return ELEC_DEVICE_CURRENT_MSG_TYPE; } else if (strncmpram2pgm("device=output", xpl_rx_buffer_shadow,13) == 0) { xpl_output_id = xpl_convert_2_ushort(xpl_rx_buffer_shadow+13); xpl_msg_state = WAITING_CMND; return OUTPUT_DEVICE_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_BASIC: if (strcmpram2pgm("device=pwmout", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONTROL_VALUE; } else if (strcmpram2pgm("mode=flood", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return FLOOD_NETWORK_MSG_TYPE; } else if (strncmpram2pgm("device=output", xpl_rx_buffer_shadow,13) == 0) { xpl_output_id = xpl_convert_2_ushort(xpl_rx_buffer_shadow+13); xpl_msg_state = WAITING_CMND_CONTROL_OUPUT; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_VALUE: if (strcmpram2pgm("type=variable", xpl_rx_buffer_shadow) == 0) { // do nothing } else if (strncmpram2pgm("value=", xpl_rx_buffer_shadow, 6) == 0) { // Extract the value to set the PWM to xpl_pwm_value = xpl_convert_2_ushort(xpl_rx_buffer_shadow+6); if (xpl_pwm_value == 255) { SetDCPWM1(0x3FF); } else { SetDCPWM1(((short)xpl_pwm_value)<<2); } xpl_msg_state = WAITING_CMND; return PWM_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_OUPUT: if (strcmpram2pgm("type=output", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONTROL_OUPUT_CURRENT; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_OUPUT_CURRENT: if (strcmpram2pgm("current=enable", xpl_rx_buffer_shadow) == 0 || strcmpram2pgm("current=high", xpl_rx_buffer_shadow) == 0 || strcmpram2pgm("current=on", xpl_rx_buffer_shadow) == 0) { output_state_enable(xpl_output_id); return OUTPUT_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("current=disable", xpl_rx_buffer_shadow) == 0 || strcmpram2pgm("current=low", xpl_rx_buffer_shadow) == 0 || strcmpram2pgm("current=off", xpl_rx_buffer_shadow) == 0) { output_state_disable(xpl_output_id); return OUTPUT_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("current=pulse", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONTROL_OUPUT_CURRENT_PULSE; } else if (strcmpram2pgm("current=toggle", xpl_rx_buffer_shadow) == 0) { output_state_toggle(xpl_output_id); return OUTPUT_DEVICE_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_OUPUT_CURRENT_PULSE: if (strncmpram2pgm("data1=", xpl_rx_buffer_shadow,6) == 0) { output_state_pulse(xpl_output_id,xpl_convert_2_ushort(xpl_rx_buffer_shadow+6)); return OUTPUT_DEVICE_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONFIG_RESPONSE: // what we write here depends on the node type, this is not yet generic code :( // maybe we need to implement here a function from the xpl_impl.c file // For now we just parse the instance_id and put it in EEPROM if (strncmpram2pgm("newconf=", xpl_rx_buffer_shadow, 8) == 0) { // Copy the new instance id to the correct variable strcpy(xpl_instance_id,xpl_rx_buffer_shadow + 8); } else if (strncmpram2pgm("newoutputs=", xpl_rx_buffer_shadow, 11) == 0) { output_count = xpl_convert_2_ushort(xpl_rx_buffer_shadow+11); } else if (strncmpram2pgm("newinputs=", xpl_rx_buffer_shadow, 10) == 0) { input_count = xpl_convert_2_ushort(xpl_rx_buffer_shadow+10); } else if (xpl_rx_buffer_shadow[0] == '}'){ xpl_msg_state = WAITING_CMND; xpl_trig_register |= WRITE_EEPROM; } break; } return -1; }
PROCESS_STATE TrainMotorHome( int speed, int AcDeceleration ) { static int Current_Forward_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_Forward_Speed ) { // putrsUSART( "Forward: Train at requested speed...\r\n" ); ReturnStatus = Command_Complete; } else if ( speed > Current_Forward_Speed ) { Current_Forward_Speed = Current_Forward_Speed + Steps; // putrsUSART( "Forward: +400\r\n" ); if ( Current_Forward_Speed > 1023) { // putrsUSART( "Forward: More than 1023 setting to 'speed'...\r\n" ); Current_Forward_Speed = speed; } SetDCPWM1( Current_Forward_Speed ); } else if ( speed < Current_Forward_Speed ) { Current_Forward_Speed = Current_Forward_Speed - Steps; // putrsUSART( "Forward: +400\r\n" ); if ( Current_Forward_Speed < speed ) { // putrsUSART( "Forward: less than 'speed' so setting to 'speed'...\r\n" ); Current_Forward_Speed = speed; } SetDCPWM1( Current_Forward_Speed ); } Set_Timer(TRAIN_MOVEMENT, 0, 1, 0 ); } return ReturnStatus; }
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 V_lent(){ SetDCPWM1(750); }
void V_rapide(){ SetDCPWM1(0x03FF); }
void V_stop(){ SetDCPWM1(0x0000); }
enum XPL_CMD_MSG_TYPE_RSP xpl_handle_message_part(void) { char lpcount; char strlength; char input_value[8]; //printf("\nmp@st%d@flc%d@fd%d@fwp%d@fwr%d@%s",xpl_msg_state,xpl_flow,xpl_rx_fifo_data_count,xpl_rx_fifo_write_pointer,xpl_rx_fifo_read_pointer,xpl_rx_buffer_shadow); switch (xpl_msg_state) { case WAITING_CMND: // If it is a command header -> set buffer state to CMD_RECEIVED if (strcmpram2pgm("xpl-cmnd", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = CMND_RECEIVED; } break; case CMND_RECEIVED: // check if we have the target in the buffer if (strcmpram2pgm("target=*", xpl_rx_buffer_shadow) == 0) { // Yes, message is wildcard and hence destined to us xpl_msg_state = WAITING_CMND_TYPE; } else if (strncmpram2pgm("target=hollie-utilmon.", xpl_rx_buffer_shadow, XPL_TARGET_VENDOR_DEVICEID_INSTANCE_ID_OFFSET) == 0) { if (strcmp(xpl_instance_id, xpl_rx_buffer_shadow + XPL_TARGET_VENDOR_DEVICEID_INSTANCE_ID_OFFSET) == 0) { // bingo message if for us xpl_msg_state = WAITING_CMND_TYPE; } else { // Too bad, message is not for us. Wait for the next one xpl_msg_state = WAITING_CMND; } } break; case WAITING_CMND_TYPE: if (strcmpram2pgm("config.list", xpl_rx_buffer_shadow) == 0) { // No need to wait for the command here, this is a simple device, we only support one command xpl_msg_state = WAITING_CMND; return CONFIGURATION_CAPABILITIES_MSG_TYPE; } else if (strcmpram2pgm("config.response", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONFIG_RESPONSE; } else if (strcmpram2pgm("sensor.request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_SENSOR_REQUEST; } else if (strcmpram2pgm("hbeat.request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_HBEAT_REQUEST; } else if (strcmpram2pgm("config.current", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONFIG_CURRENT; } else if (strcmpram2pgm("control.basic", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_CONTROL_BASIC; } else if (strcmpram2pgm("}", xpl_rx_buffer_shadow) == 0) { // just wait for command } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONFIG_CURRENT: if (strcmpram2pgm("command=request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return CONFIG_STATUS_MSG_TYPE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_HBEAT_REQUEST: if (strcmpram2pgm("command=request", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; // We need a random backoff here to ensure that we don't flood the // network with all nodes responding at the same time. // We use the time_ticks for that. That variable is never bigger than // 300, so we divide by ten and feed that value into the delay function Delay10KTCYx(time_ticks / 10); return HEARTBEAT_MSG_TYPE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_SENSOR_REQUEST: if (strcmpram2pgm("command=current", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND_SENSOR_REQUEST_DEVICE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_SENSOR_REQUEST_DEVICE: if (strcmpram2pgm("device=gas", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return GAS_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("device=water", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return WATER_DEVICE_CURRENT_MSG_TYPE; } else if (strcmpram2pgm("device=elec", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return ELEC_DEVICE_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_BASIC: if (strcmpram2pgm("device=pwmout", xpl_rx_buffer_shadow) == 0) { #ifdef PWM_ENABLED xpl_msg_state = WAITING_CMND_CONTROL_VALUE; #else xpl_msg_state = WAITING_CMND; #endif } else if (strcmpram2pgm("mode=flood", xpl_rx_buffer_shadow) == 0) { xpl_msg_state = WAITING_CMND; return FLOOD_NETWORK_MSG_TYPE; } else if (xpl_rx_buffer_shadow[0] == '{') { //do nothing } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONTROL_VALUE: if (strcmpram2pgm("type=variable", xpl_rx_buffer_shadow) == 0) { // do nothing } else if (strncmpram2pgm("current=", xpl_rx_buffer_shadow, 8) == 0) { // Extract the value to set the PWM to strcpy(input_value, xpl_rx_buffer_shadow + 8); strlength = strlen(input_value); xpl_pwm_value = 0; lpcount = 0; while (lpcount < strlength) { xpl_pwm_value *= 10; xpl_pwm_value += (input_value[lpcount] - 0x30); lpcount++; } if (xpl_pwm_value == 255) { SetDCPWM1(0x3FF); } else { SetDCPWM1(((short) xpl_pwm_value) << 2); } xpl_msg_state = WAITING_CMND; return PWM_CURRENT_MSG_TYPE; } else { xpl_msg_state = WAITING_CMND; } break; case WAITING_CMND_CONFIG_RESPONSE: // what we write here depends on the node type, this is not yet generic code :( // maybe we need to implement here a function from the xpl_impl.c file // For now we just parse the instance_id and put it in EEPROM if (strncmpram2pgm("newconf=", xpl_rx_buffer_shadow, 8) == 0) { // Make sure we're not receiving data right now, as interrupts will be disabled during EEPROM write later in this function if (xpl_flow == FLOW_ON) { xpl_flow = FLOW_OFF; putc(XOFF, _H_USART); } // We are about to change our ID here, so send an end message to notify the network xpl_send_config_end(); // Copy the new instance id to the correct variable strcpy(xpl_instance_id, xpl_rx_buffer_shadow + 8); // Put the new instance id name in EEPROM so that it retains value after a power cycle strlength = strlen(xpl_instance_id); // We put this in a local variable because else it it re-calculated every loop cycle for (lpcount = 0; lpcount < strlength; lpcount++) { eeprom_write(lpcount + XPL_EEPROM_INSTANCE_ID_OFFSET, xpl_instance_id[lpcount]); } // End with a '\0' in EEPROM eeprom_write(lpcount + XPL_EEPROM_INSTANCE_ID_OFFSET, 0x00); // We don't reset here any more, there are more settings to come! // Reset the xpl function to apply the new name // Buffer content gets lost here, but we don't mind as we need to start again xpl_init_instance_id(); xpl_msg_state = WAITING_CMND; xpl_flow = FLOW_ON; putc(XON, _H_USART); return HEARTBEAT_MSG_TYPE; // Don't return yet, we need to get the PWM_enable setting from the command } break; } return -1; }