void main() { //Iniitalize pulsewidth FanPWM_WritePulseWidth(255); //Start Fan PWM FanPWM_Start(); //Enable FanPWM interrupt FanPWM_EnableInt(); //Enable global interrupts M8C_EnableGInt; //Start tach timer TachTimer_Start(); //Enable Tach Timer interrupts TachTimer_EnableInt(); //Start LCD LCD_Start(); //Enable GPIO interrupts INT_MSK0 |= 0b00100000; //Initialize display LCD_Init(); LCD_Position(0,0); LCD_PrCString("Pulse Width: "); LCD_Position(0,13); LCD_PrHexByte(FanPWM_bReadPulseWidth()); while(1)//control loop { if(bDataAvailable == 1)//if data is available { //Clear it bDataAvailable = 0; //Calculate fan speed and write RPM and num cycles to lCD wSpeedRPM = ( ( (60/4) * 3000000 * cNumCycles )+ ((wFirstValue - wLastValue)/2) )/(wFirstValue - wLastValue); //Write to LCD LCD_Init(); LCD_Position(0,0); LCD_PrCString("RPM: ");//Begin writing at 0,5 LCD_Position(0,5); LCD_PrHexInt(wSpeedRPM); LCD_Position(1,0); LCD_PrCString("#Cycles: ");//Begin writing at 1,9 LCD_Position(1,9); LCD_PrHexByte(cNumCycles); } }//end control loop }
void main(void) { int result; float voltage; int status; M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts // Insert your main routine code here. //Start PGA in high power mode PGA_Start(PGA_HIGHPOWER); //Start ADCINC in high power mode ADCINC_Start(ADCINC_HIGHPOWER); //Start LCD LCD_Start(); //Run the ADC continuously ADCINC_GetSamples(0); SleepTimer_Start(); SleepTimer_SetInterval(SleepTimer_1_HZ); SleepTimer_EnableInt(); while (1) { SleepTimer_SyncWait(1, SleepTimer_WAIT_RELOAD); // Wait for data to be ready while (ADCINC_fIsDataAvailable() == 0); // Get Data and clear flag result=ADCINC_iClearFlagGetData(); voltage = result * SCALE_FACTOR; LCD_Position(0, 0); LCD_PrCString(" "); LCD_Position(0, 0); LCD_PrHexInt(result); LCD_Position(1, 0); LCD_PrCString(" "); LCD_Position(1, 0); LCD_PrString(ftoa(voltage, &status)); } }
void main(void) { M8C_EnableGInt; LCD_Start(); // Start LCD LCD_Position(0,0); LCD_PrCString("PSoC I2C Slave"); EzI2Cs_SetRamBuffer(1, 1, (char *)&Wert); // Start I²C Buffer, Size of 1 Byte, Allowing to Read/Write 1 Byte I2C_Init(); while(1) { LCD_Position(1,0); LCD_PrCString("Wert:"); LCD_Position(2,0); LCD_PrHexInt(Wert); } }
float EvaluateUltrasoonSensor(void)//(Timer3) { if (FlagUltrasoon & DATA_AVAILABLE_ULTRASOON)// do if databit is set { WORD pulseWidthUltrasoon = PulseWidthUltrasoon; #if (DEBUG_LCD) LCD_Position(1,12); LCD_PrHexInt(pulseWidthUltrasoon); #endif FlagUltrasoon &= ~DATA_AVAILABLE_ULTRASOON; // als meting gebeurt is trigger sensor opnieuw en we disablen timer1 TriggerUltrasoon(); return pulseWidthUltrasoon; } return MIN_SAFE_DISTANCE + 1; }
// normalization function for Elevator(Timer2) float EvaluateElevator(DWORD value) { // Check if pulsewidth data is available if(FlagsElevator & DATA_AVAILABLE_ELEVATOR) { #if (DEBUG_LCD) LCD_Position(1,0); LCD_PrHexInt(value); #endif // stick in center if (Within(value, CENTER_ELEVATOR, MARGIN_ELEVATOR)) { #if (DEBUG_LCD) LCD_Position(1,5); LCD_PrCString("C"); #endif return 0; } else if (value > CENTER_ELEVATOR) // stick up { #if (DEBUG_LCD) LCD_Position(1,5); LCD_PrCString("U"); #endif return ((float)value - CENTER_ELEVATOR) / (float)(MAX_ELEVATOR - CENTER_ELEVATOR); } else if (value < CENTER_ELEVATOR) // stick down { #if (DEBUG_LCD) LCD_Position(1,5); LCD_PrCString("D"); #endif return -(CENTER_ELEVATOR - (float)value) / (float)(CENTER_ELEVATOR - MIN_ELEVATOR); } // action finished, clear flag to avoid doing it again FlagsElevator &= ~DATA_AVAILABLE_ELEVATOR; } return 0; }
// normalization function for Aileron (Timer1) float EvaluateAileron(DWORD value) { // Check if pulsewidth data is available if(FlagsAileron & DATA_AVAILABLE_AILERON) { #if (DEBUG_LCD) LCD_Position(0,0); LCD_PrHexInt(value); #endif // stick in center if (Within(value, CENTER_AILERON, MARGIN_AILERON)) { #if (DEBUG_LCD) LCD_Position(0,5); LCD_PrCString("C"); #endif return 0; } else if (value < CENTER_AILERON) // stick left { #if (DEBUG_LCD) LCD_Position(0,5); LCD_PrCString("L"); #endif return ((float)value - CENTER_AILERON) / (float)(MAX_AILERON - CENTER_AILERON); } else if (value > CENTER_AILERON) // stick right { #if (DEBUG_LCD) LCD_Position(0,5); LCD_PrCString("R"); #endif return -(CENTER_AILERON - (float)value) / (float)(CENTER_AILERON - MIN_AILERON); } // action finished, clear flag to avoid doing it again FlagsAileron &= ~DATA_AVAILABLE_AILERON; } return 0; }
void switchDown() { //Gather current count currentCount = Timer_wReadTimerSaveCV(); //3 seconds clocked at .0001 sec per cycles is 30000 timer ticks if(currentCount >= 30000) { //Reset timer Timer_WriteCompareValue(0); } else { //Check for previous values if(dataAvailable != 0) { //Data has been collected - collect second value time2 = currentCount; //Data is no longer available since the second value has been collected dataAvailable = 0; //Let user know LCD_Position(1,0); LCD_PrCString("2"); //Reset the timer for next presses Timer_WriteCompareValue(0); } else { //Data has not been collected - collect first value time1 = currentCount; //Let user know LCD_Position(1,0); LCD_PrCString("1"); } } //Write the difference in times to the LCD LCD_Position(0,0); LCD_PrHexInt(time1 - time2); //Down count so 1-2 }
void main(void) { // Enable Global Interrupt M8C_EnableGInt; // Clear the flags FlagsElevator = 0; FlagsAileron = 0;// new for motorcontroll2 FlagUltrasoon = 0; // Start timers and enable interrupt Timer1_Start(); Timer1_EnableInt(); Timer2_Start();// new for motorcontroll2 Timer2_EnableInt();// new for motorcontroll2 Timer3_Start(); Timer3_EnableInt(); TriggerUltrasoon(); // Init motors PWM1_Start(); PWM2_Start(); #if (DEBUG_LCD) LCD_Start(); #endif while (TRUE) { float aileronNormalized, elevatorNormalized; float distance; float speed, direction; float motorLeft, motorRight; BOOL forward; aileronNormalized = EvaluateAileron(PulseWidthAileron); direction = fabs(aileronNormalized); elevatorNormalized = EvaluateElevator(PulseWidthElevator); speed = fabs(elevatorNormalized); forward = (elevatorNormalized >= 0); distance = EvaluateUltrasoonSensor(); if (distance < MIN_SAFE_DISTANCE) { if (forward) speed = 0; } motorLeft = speed; // default is straight forward motorRight = speed; if (aileronNormalized < 0) // turning left { motorRight = speed; motorLeft = speed * (1 - direction); } else if (aileronNormalized > 0) // turning right { motorLeft = speed; motorRight = speed * (1 - direction); } if (forward) { // ccw PRT1DR |= 0x08; // AIN1 PRT1DR &= ~0x02; // AIN2 PRT1DR |= 0x20; // BIN1 PRT1DR &= ~0x80; // BIN2 } else { //cw PRT1DR &= ~0x08; // AIN1 PRT1DR |= 0x02; // AIN2 PRT1DR &= ~0x20; // BIN1 PRT1DR |= 0x80; // BIN2 } // Denormalize to Engine motorLeft *= (MAX_POWER - MIN_POWER); motorLeft += MIN_POWER; motorRight *= (MAX_POWER - MIN_POWER); motorRight += MIN_POWER; PWM1_WritePulseWidth(motorLeft); PWM2_WritePulseWidth(motorRight); #if (DEBUG_LCD) LCD_Position(0,7); LCD_PrHexInt(motorLeft); LCD_Position(1,7); LCD_PrHexInt(motorRight); LCD_Position(0,12); LCD_PrCString(forward ? "F" : "B"); #endif } }