Пример #1
0
int PwmOut_Stop( int index )
{
  if ( index < 0 || index > 3 )
    return CONTROLLER_ERROR_ILLEGAL_INDEX;
  
  if ( PwmOut_users[ index ] <= 0 )
    return CONTROLLER_ERROR_NOT_LOCKED;

  if ( --PwmOut_users[ index ] == 0 )
  {
    // Look up the IO's that are involved
    int ioA;
    int ioB;
    PwmOut_GetIos( index, &ioA, &ioB );
  
    // Turn them off
    Io_SetValue( ioA, false );
    Io_SetValue( ioB, false );

    Io_Stop( ioA );
    Io_Stop( ioB );

    Pwm_Stop( index );
  }

  return CONTROLLER_OK;
}
Пример #2
0
static void StateMeasuring(void)
{
	UINT16 poxOutputSignalRaw, SpO2BaselineSignalRaw;
	
	/* read SPO2 signals: these are the steps:
		1. turn on IR LED
		2. wait 
		3. read baseline and read IR signal
		4. turn off IR LED (both LEDs off)
		5. wait 
		6. turn on Red LED
		7. wait 
		8. read Red signal
		9. turn off Red LED (both LEDs off)
		10. wait
	*/
	
	UINT16 elapsedTimeCopy = ElapsedTimeIn_us;

	switch (SpO2ActualSubStateSubState)
	{
		case 0:		
			SPO2_SET_SWITCH_CONTROL(SPO2_CONTROL_IR);	//set switch to select IR signals
			Pwm_Start(PwmIRValue);	//start pwm with IR value
			START_TIMER_us();	//start timer to wait 250us
			
		
			SpO2ActualSubStateSubState++;
			break;
			
		case 1:
			if (elapsedTimeCopy >= IR_ON_TIME_us)
			{			
				ElapsedTimeIn_us = 0;

				{
					poxOutputSignalRaw =   ADC1_Read12b(SPO2_ADC_CHANNEL_OUTPUT_SIGNAL);	//read IR baseline and IR signal
					SpO2BaselineSignalRaw = ADC0_Read12b(SPO2_ADC_CHANNEL_BASELINE_SIGNAL);
                                        
                                        #if (defined SPO2_FIR)
                                        poxOutputSignalRaw = u32SPO2_Filter(poxOutputSignalRaw,&gu16IR_Samples[0]);
                                        //SpO2BaselineSignalRaw = ADC0_Read12b(SPO2_ADC_CHANNEL_OUTPUT_SIGNAL); //Read IR Baseline
                                        SpO2BaselineSignalRaw = u32SPO2_BaselineFilter(SpO2BaselineSignalRaw, &gu16IR_Base[0]);//Remove AC
                                        #endif
									
					MovingAverage_PushNewValue16b(&SpO2IRBaselineSignal, SpO2BaselineSignalRaw);	//store IR baseline and IR signal
					MovingAverage_PushNewValue16b(&SpO2IRLedSignal, poxOutputSignalRaw);
				}				
				
				
				Pwm_Stop(0);		//turn off LEDs									
				SpO2ActualSubStateSubState++;
			}
			break;
	
		case 2:
			if (elapsedTimeCopy >= IR_OFF_TIME_us)
			{
				ElapsedTimeIn_us = 0;
//PTFD_PTFD2 = 1;

				SPO2_SET_SWITCH_CONTROL(SPO2_CONTROL_RED);	//set switch to select Red signal								
				Pwm_Start(PwmRedValue);	//start pwm for Red LED
				SpO2ActualSubStateSubState++;					
			}
			break;
			
		case 3:
			if (elapsedTimeCopy >= RED_ON_TIME_us)
			{				
				ElapsedTimeIn_us = 0;
//PTFD_PTFD2 = 0;				
				//if (IsTimeToUpdateGraphData)
				{					
					poxOutputSignalRaw = ADC1_Read12b(SPO2_ADC_CHANNEL_OUTPUT_SIGNAL);	//Read RED signal
					SpO2BaselineSignalRaw = ADC0_Read12b(SPO2_ADC_CHANNEL_BASELINE_SIGNAL);	//Read RED baseline				
                                        
                                        #if (defined SPO2_FIR)
                                        poxOutputSignalRaw = u32SPO2_Filter(poxOutputSignalRaw,&gu16R_Samples[0]);
                                        //SpO2BaselineSignalRaw = ADC0_Read12b(SPO2_ADC_CHANNEL_OUTPUT_SIGNAL);		//Read RED baseline
                                        SpO2BaselineSignalRaw = u32SPO2_BaselineFilter(SpO2BaselineSignalRaw, &gu16R_Base[0]);//Remove AC
                                        #endif
                                        
					MovingAverage_PushNewValue16b(&SpO2RedBaselineSignal, SpO2BaselineSignalRaw);	//store Red baseline and red signal
					MovingAverage_PushNewValue16b(&SpO2RedLedSignal, poxOutputSignalRaw);
				}

				Pwm_Stop(0);		//turn off LEDs
		
				SpO2ActualSubStateSubState++;
			}
			break;
			
		case 4:
			if (elapsedTimeCopy >= RED_OFF_TIME_us)
			{			
				ElapsedTimeIn_us = 0;
				SpO2ActualSubStateSubState = 0;				
				SpO2SamplesBetweenPulses++;
							
				if (IsTimeToUpdateGraphData && SpO2IsDiagnosticMode)
				{					
					IsTimeToUpdateGraphData = FALSE;										
					SaveGraphData();
					SwTimer_StartTimer(TimerSendGraphData, SPO2_UPDATE_GRAPH_PERIOD);
				}

				#define RECALIBRATE_TIME	512
									
				if (TimerToRecalibrate == 0)
				{
					static unsigned char toogle = 0;
					
					#define MAX_CALIBRATION_ATTEMPTS	128
					static UINT16 calibrationAttempts = 0;
														
					//check if both baselines are within the limits					
					if 	((SpO2IRBaselineSignal.Result > BASELINE_SETPOINT_UPPER_LIMIT_MIN)  || (SpO2IRBaselineSignal.Result < BASELINE_SETPOINT_LOWER_LIMIT_MIN) ||
						((SpO2RedBaselineSignal.Result > BASELINE_SETPOINT_UPPER_LIMIT_MIN) || (SpO2RedBaselineSignal.Result < BASELINE_SETPOINT_LOWER_LIMIT_MIN)))
					{

						if (toogle == SPO2_CONTROL_RED)
						{
							if ((SpO2RedBaselineSignal.Result > BASELINE_SETPOINT_UPPER_LIMIT_MID) || (SpO2RedBaselineSignal.Result < BASELINE_SETPOINT_LOWER_LIMIT_MID))
							{
								CalibrateRedLedIntensity();						
								TimerToRecalibrate = 16;
								
								//if (calibrationAttempts < 0xFF)
								{
									calibrationAttempts++;
								}
							}
							toogle = SPO2_CONTROL_IR;							
						}
						else if (toogle == SPO2_CONTROL_IR)
						{							
							if ((SpO2IRBaselineSignal.Result > BASELINE_SETPOINT_UPPER_LIMIT_MID)  || (SpO2IRBaselineSignal.Result < BASELINE_SETPOINT_LOWER_LIMIT_MID))
							{
								CalibrateIRLedIntensity();
								TimerToRecalibrate = 16;

								//if (calibrationAttempts < 0xFF)
								{
									calibrationAttempts++;
								}							
							}
							toogle = SPO2_CONTROL_RED;
						}
					}
					else
					{					
						TimerToRecalibrate = RECALIBRATE_TIME;
						calibrationAttempts = 0;
					}
									
				}
				else
				{
					TimerToRecalibrate--;					
				}
				
				
				//if (IsSignalStable)
				{
					FindMaxAndMin();
				}
				
				if ((SpO2ValidValue >= 10) && (!SpO2IsDiagnosticMode))
				{
					//generate pox ok event
					SpO2ActualEvent = SPO2_EVENT_MEASUREMENT_COMPLETE_OK;
					SpO2_AbortMeasurement();
				}
			}
			break;
	}
}