Ejemplo n.º 1
0
Archivo: main.c Proyecto: sndae/b3r1
/*****************************************************************************
 *  showADC - Displays ADC reading on the LCD display.
 *****************************************************************************/
void showADC(void)
{
    InitADC();

    while (!(getkey() == 1))
    {
        gyroRaw = GetADC(gyro_sensor);
        accelRaw = GetADC(accel_sensor);
        TimerWait(100);
        show12bits(gyroRaw, accelRaw);
    }
    LCD_ShowColons(0);
}
UINT16 GetChanVal(BYTE pin){
	UINT16 val;
	BYTE mode = GetChannelMode(pin);
	switch (mode){
	case HIGH_IMPEDANCE:
		val=1;
		break;
	case IS_DI:
	case IS_DO:
	case IS_COUNTER_INPUT_HOME:
	case IS_COUNTER_OUTPUT_HOME:
		val = GetDIO(pin);
		break;
	case IS_SERVO:
		val = GetServoPos(pin);
		break;
	case IS_PWM:
		val = GetPWM(pin);
		break;
	case IS_DC_MOTOR_VEL:
	case IS_DC_MOTOR_DIR:
		val = GetDCMotor(pin);
		break;
	case IS_ANALOG_IN:
		val=GetADC(pin);
		break;
	default:
		val=1;
		break;
	}
	return val;
}
Ejemplo n.º 3
0
boolean checkAnalog(){
	int i=0;
	for(i=8;i<16;i++){
		if ((GetChannelMode(i) == IS_ANALOG_IN) ){
			setDataTableCurrentValue(i, GetADC(i));
		}
	}
	return true; 
}
Ejemplo n.º 4
0
void initPinState(uint8_t i){
	uint8_t mode =GetChannelMode(i);
	if ((mode == IS_ANALOG_IN) ){
		setDataTableCurrentValue(i,GetADC(i));
	}else if(((mode == IS_DI) )  || ((mode == IS_COUNTER_INPUT_HOME)||(mode == IS_COUNTER_OUTPUT_HOME))){
		setDataTableCurrentValue(i,GetDIO(i)?1:0);
	}else{
		//setDataTableCurrentValue(i,0);
	}
}
Ejemplo n.º 5
0
uint8_t GetGear()
{
	const uint8_t NEUTRAL = 50;
	const uint8_t UNKNOWN = 254; //=4.9V
	const uint8_t TOLERANCE = 10;

	static uint8_t gear; //stores gear number when position is unknown (pos between gears)

	/** \par Voltage divider
	 * R1 = 180R\n
	 * R2 = 100R\n
	 * Vout =Vin*R2/(R1+R2)\n
	 * For Vin=10V, ADC reads 3.6V. We treat everything >2V as gearbox neutral position.\n
	 * 2V = 100
	 */

	//Port can be used in digital input mode, but reading analog allows more fine tune.
	if( GetADC(NEUTRAL_PIN) >= NEUTRAL )
	{
		gear=0;
		return gear; //neutral position
	}

	GetADC( GEAR_PIN ); //(the first reading after changing ADC channel may be corrupted
	int adc = GetADC( GEAR_PIN ); //works better (more stable)

	//Gearbox gives 5V if it's in unknown position (when changing gears)
	if( adc >= g_Config.UnknownLevel )
		return gear;

	static uint8_t g;

	for(g=0; g<g_Config.MaxGearNumber-1;g++)
	{
		if( adc-TOLERANCE >= g_Config.GearLevel[5-g] )
		{
			gear = 5-g+1;
			return gear;
		}
	}

	return 1;
}
Ejemplo n.º 6
0
// Returns an average of 10 measurements from the ADC on the specified channel
unsigned int AverageADC(unsigned char channel)	
{
	unsigned int sum = 0;
	int i;
	for(i=0; i<10; ++i)
	{
		sum += GetADC(channel);
	}
	return sum/10;
}
Ejemplo n.º 7
0
//RECEIVER CODE
unsigned char rx_byte (void)
{
	unsigned char j;
	int volt_zero;
	
	while(GetADC(0)<=min);
	wait_bit_time();
	wait_bit_time();

	//Skip the start bit
	val=0;
	//wait_one_and_half_bit_time();
	for(j=0; j<8; j++)
	{
		volt_zero = GetADC(0);
		val|=(volt_zero>min)?(0x01<<j):0x00;
		wait_bit_time();
		
	}
	//printf("Last Command = %u \n",val);
	return val;
}
Ejemplo n.º 8
0
int main(void)
{
	uint8_t count, second=0;
	uint32_t val;
	InitUART0 ();
	InitRTC();	
	UART0_dbg_msg (
	"********************************************************************************\n\r"
	" Internal DAC test of LPC1788\n\r"
	"\t - UART Comunication: 9600 bps \n\r"
	" Write to debug console current voltage on AD[2]-AD[3]\n\r"
	"********************************************************************************\n\r");
	if (!InitADC (2))
		{
			UART0_dbg_msg ("InitADC exception, channel must be 0..7\n\r");
			while (1);
		}
	InitDAC (0x03FF);
	while (1) 
	{
		//input DAC value
		do
		{
		UART0_dbg_msg ("Input DAC value in range 0..1023, as a sample 0983\n\r");
		while (!UART0_get_dec (&val,4)) 
			UART0_dbg_msg ("DAC value is 10-bit number\n\r");
		if (val>1024) {
										UART0_dbg_msg ("DAC value isn't in range 0..1023\n\r");
										UART0_clear_rx_buffer();
									}
		} while (val>1024);
		count=0;
		//Set DAC value
		SetDAC(val);
		//Convert DAC value through ADC 5 times
		while(count<5)
		{	
		if (second != LPC_RTC->SEC)
			{
			second=LPC_RTC->SEC;
			ADC_dbg(GetADC());
			count++;
			}
		}
	}
}
BOOL GetChannelValue(BowlerPacket * Packet){
	BOOL ret=FALSE;
	BYTE pin = Packet->use.data[0];
	BYTE mode = GetChannelMode(pin);
	//int i;
	UINT16 val=GetChanVal(pin);
	if(IsAsync(pin)){
		//AsynAck();
	}
	Packet->use.head.Method=BOWLER_POST;
	if ((mode == IS_DC_MOTOR_VEL)||(mode == IS_DC_MOTOR_DIR)||(mode == IS_DO)|| (mode == IS_PWM)|| (mode == IS_SERVO) || (mode == IS_DI) ||(mode == IS_COUNTER_OUTPUT_HOME)||(mode == IS_COUNTER_INPUT_HOME)){
		set8bit(Packet, val,1);
		Packet->use.head.DataLegnth=6;
		ret = TRUE;
	}else if ((mode == IS_ANALOG_IN)){
		val=GetADC(pin);
		set16bit(Packet,val,1);
		Packet->use.head.DataLegnth=7;
		ret = TRUE;
	}else if ( (mode == IS_UART_TX) || (mode == IS_UART_RX)){
		//Number of bytes in the stream to be sent
		Packet->use.head.DataLegnth=5;
		UINT16 numBytes=Get_UART_Byte_CountPassThrough();
		if(numBytes>0){
			UARTGetArrayPassThrough(Packet->use.data+1,numBytes);
			//Offset using pointer, rather then shuffeling
//			for (i=0;i<numBytes;i++){
//				Packet->use.data[(numBytes)-i]=Packet->use.data[(numBytes-1)-i];
//			}
			Packet->use.data[0]=17;
			Packet->use.head.DataLegnth+=numBytes;
		}
		return TRUE;
	}else{
		return FALSE;
	}
	return ret;
}
Ejemplo n.º 10
0
void task2(void * pdata)
{
    static bool  sb_channel_light =false;
    u8 buffer[40] = {0};
    while(1)
    {
        //printf("task2 ");
        Uart_PrintStr("task2 ");

        __enable_interrupt();

        if(g_flag__rx_frame_complete)//串口收到消息,字符串
        {
            g_flag__rx_frame_complete=false;
            //pBuf=UART2_GetData(&len);
            Uart_GetStr( (char *)g_uart_rx_content);
            if(strstr((char const *)g_uart_rx_content,"light") )
            {
                //printf("you send the message is :light \r\n");
                Uart_PrintStr("you send the message is :light \r\n");
                sb_channel_light = true;
            }
            else if(strstr((char const *)g_uart_rx_content,"bat"))
            {
                Uart_PrintStr("you send the message is :bat \r\n");
                sb_channel_light = false;
            }
            else if(strstr((char const *)g_uart_rx_content,"halt"))
            {
                Uart_PrintStr("you send the message is :halt,now halt the system! \r\n");
                asm("halt\n");
                // halt(); /* Program halted */
            }
        }

        static u8 voltageStr[20] = {0};
        static u16 temp_vol = 0;//8bit will overflow !! >255
        if(sb_channel_light)
        {
            temp_vol = (u16)GetADC(ADC_CHANNEL_LIGHT);

        }
        else
        {
            temp_vol = (u16)GetADC(ADC_CHANNEL_BATTERY);
        }
        voltageStr[0]= temp_vol/100 +'0';
        voltageStr[1] = '.';
        voltageStr[2]= temp_vol%100/10  +'0';
        voltageStr[3]= temp_vol%10  +'0';
        voltageStr[4]= '\0';
        if(sb_channel_light)
        {
            sprintf(buffer,"light: %s V\r\n",voltageStr);
            Uart_PrintStr(buffer);
            //printf("light: %s V\r\n",voltageStr);
        }
        else
        {
            //printf("battery: %s V\r\n",voltageStr);
            sprintf(buffer,"battery: %s V\r\n",voltageStr);
            Uart_PrintStr(buffer);
        }

        g_flag_awu =true;

#if 0
        if(g_flag_awu)//已退出睡眠
        {
            g_flag_awu = false;
            LEDOn(GREEN);
            Delay(0x3ffff);
            LEDOn(LED_OFF);
            asm("halt\n");
        }
#endif
        //      current_uart_handler(g_uart_rx_content, len);
        //      break;

        OSTimeDlyHMSM(0,0,0,TIME_DELAY-800 );
        //LEDOn(LED_OFF);
#if 0
        __disable_interrupt();
        //LED2_On();
        //LEDOn(LED_OFF);
        __enable_interrupt();
        OSTimeDlyHMSM(0,0,0,TIME_DELAY );
        __disable_interrupt();
        //LED2_Off();
        //LEDOn(LED_OFF);
        __enable_interrupt();
        OSTimeDlyHMSM(0,0,0,TIME_DELAY);
#endif
    }
}
Ejemplo n.º 11
0
float voltage (unsigned char channel)
{
	return ( (GetADC(channel)*VCC)/1023.0 ); // VCC=4.84V (measured)
}
Ejemplo n.º 12
0
touchscreen_data	GetTS_Fast(void)
{
	
#define XDELTA_MAX	100
#define	YDELTA_MAX	100
	touchscreen_data samples[TS_NUM_SAMPLES*2];
	/*static*/ /*touchscreen_data ts_result_p, ts_result_n, ts_result;*/
	touchscreen_data ts_result;
	int i;
	int yvalue_min = 1023;
	int yvalue_max = 0;
	int xvalue_min = 1023;
	int xvalue_max = 0;
	int xdelta, ydelta;
	int ts_max_value = 2<<(TS_RESOLUTION_BITS-1);
	
	float x_sample, y_sample;	// For coordinates approximation (Gao)
	
	for ( i=0; i<TS_NUM_SAMPLES; i++)
	{
		FIO0DIR_bit->P0_21 = 0;
		PINMODE1_bit->P0_21 = 0x02;
		PINMODE1_bit->P0_23 = 0x02;
		polarizeXx();
		InitADC(TS_Y_CHANNEL);

		samples[i*2].xvalue = GetADC(TS_Y_CHANNEL);
		
		FIO0DIR_bit->P0_22 = 0;
		PINMODE1_bit->P0_22 = 0x02;
		PINMODE1_bit->P0_24 = 0x02;
		polarizeYy();
		InitADC(TS_X_CHANNEL);
	
		samples[i*2].yvalue = GetADC(TS_X_CHANNEL);
		
		samples[i*2].pvalue = 0;

		
		FIO0DIR_bit->P0_21 = 0;
		PINMODE1_bit->P0_21 = 0x02;
		PINMODE1_bit->P0_23 = 0x02;
		polarizexX();
		InitADC(TS_Y_CHANNEL);
	
		samples[(i*2)+1].xvalue = GetADC(TS_Y_CHANNEL);
		
		FIO0DIR_bit->P0_22 = 0;
		PINMODE1_bit->P0_22 = 0x02;
		PINMODE1_bit->P0_24 = 0x02;
		polarizeyY();
		InitADC(TS_X_CHANNEL);ts_settling_delay();
		samples[(i*2)+1].yvalue = GetADC(TS_X_CHANNEL);
		
		samples[(i*2)+1].pvalue = 0;
		
	}
	
	ts_result.xvalue = 0;
	ts_result.yvalue = 0;
	ts_result.pvalue = 0;
		
	for (i = 0; i<(TS_NUM_SAMPLES	); i++)
	{
		int tempyval = ts_max_value - samples[i*2+1].yvalue;
		int tempxval = ts_max_value - samples[i*2+1].xvalue;
		
		ts_result.xvalue += samples[i*2].xvalue;
		ts_result.yvalue += samples[i*2].yvalue;
		
		ts_result.xvalue += ((ts_max_value)-samples[(i*2)+1].xvalue);
		ts_result.yvalue += ((ts_max_value)-samples[(i*2)+1].yvalue);
		
		
		if (yvalue_min > samples[i*2].yvalue) yvalue_min = samples[i*2].yvalue;
		if (yvalue_max < samples[i*2].yvalue) yvalue_max = samples[i*2].yvalue;
		
		if (yvalue_min > tempyval) yvalue_min = tempyval;
		if (yvalue_min < tempyval) yvalue_max = tempyval;
		
		if (xvalue_min > samples[i*2].xvalue) xvalue_min = samples[i*2].xvalue;
		if (xvalue_max < samples[i*2].xvalue) xvalue_max = samples[i*2].xvalue;
		
		if (xvalue_min > tempxval) xvalue_min = tempxval;
		if (xvalue_min < tempxval) xvalue_max = tempxval;
	}
	
	
	ts_result.xvalue = ts_result.xvalue / (TS_NUM_SAMPLES*2);
	ts_result.yvalue = ts_result.yvalue / (TS_NUM_SAMPLES*2);
	
	xdelta = xvalue_max - xvalue_min;
	ydelta = yvalue_max - yvalue_min;
	
	if (	(ts_result.xvalue < 910) && \
		(ts_result.yvalue < 910) && \
		(xdelta < XDELTA_MAX) && \
		(ydelta < YDELTA_MAX) )
	{
		ts_result.pvalue = 1024;
	}
	else
	{
		ts_result.pvalue = 0;
	}
	
	ts_result.yvalue = ts_max_value - ts_result.yvalue;
	
	/* Coordinate Approximation */
	x_sample = ts_result.xvalue;
	y_sample = ts_result.yvalue;
	if(ts_result.pvalue > 1000)
	{
		ts_result.xvalue = (INT16U)((x_sample - 100) * TS_X_RATIO);
		ts_result.yvalue = (INT16U)((y_sample - 165) * TS_Y_RATIO);
	}
	else
	{
		ts_result.xvalue = 0;
		ts_result.yvalue = 0;
	}
	
	return ts_result;
	
}
Ejemplo n.º 13
0
Archivo: main.c Proyecto: sndae/b3r1
/*****************************************************************************
 *	Balance -
 *****************************************************************************/
void balance(void)
{
    unsigned long TimerMsWork;

    long int g_bias = 0;
    double x_offset = 532;		//offset value 2.56V * 1024 / 4.93V = 4254
    double q_m = 0.0;
    double int_angle = 0.0;
    double x = 0.0;
    double tilt = 0.0;

    int pwm;

    InitADC();
    init_pwm();

    // initialize the UART (serial port)
    uartInit();

    // set the baud rate of the UART for our debug/reporting output
    uartSetBaudRate(115200);

    // initialize rprintf system
    rprintfInit(uartSendByte);

    // initialize vt100 library
    vt100Init();

    // clear the terminal screen
    vt100ClearScreen();

    TimerMsWork = TimerMsCur();

    DDRB |= (1 << PB0);	// Make B0 an output for LED


    /* as a 1st step, a reference measurement of the angular rate sensor is
     * done. This value is used as offset compensation */

    for (int i=1 ; i<=200; i++) // determine initial value for bias of gyro
    {
        g_bias = g_bias + GetADC(gyro_sensor);
    }

    g_bias = g_bias / 200;


    while (!(getkey() == 1))
    {
        /* insure loop runs at specified Hz */
        while (!TimerCheck(TimerMsWork, (dt_PARAM * 1000) -1))
            ;
        TimerMsWork = TimerMsCur();

        // toggle pin B0 for oscilloscope timings.
        PORTB = PINB ^ (1 << PB0);

        // get rate gyro reading and convert to deg/sec
//		q_m = (GetADC(gyro_sensor) - g_bias) / -3.072;	// -3.07bits/deg/sec (neg. because forward is CCW)
        q_m = (GetADC(gyro_sensor) - g_bias) * -0.3255;	// each bit = 0.3255 /deg/sec (neg. because forward is CCW)
        state_update(q_m);


        // get Accelerometer reading and convert to units of gravity.
//		x = (GetADC(accel_sensor) - x_offset) / 204.9;	// (205 bits/G)
        x = (GetADC(accel_sensor) - x_offset) * 0.00488;	// each bit = 0.00488/G

        // x is measured in multiples of earth gravitation g
        // therefore x = sin (tilt) or tilt = arcsin(x)
        // for small angles in rad (not deg): arcsin(x)=x
        // Calculation of deg from rad: 1 deg = 180/pi = 57.29577951
        tilt = 57.29577951 * (x);
        kalman_update(tilt);

        int_angle += angle * dt_PARAM;

        rprintf("  x:");
        rprintfFloat(8, x);
        rprintf("  angle:");
        rprintfFloat(8, angle);
        rprintf("  rate:");
        rprintfFloat(8, rate);

        // Balance.  The most important line in the entire program.
        //	balance_torque = Kp * (current_angle - neutral) + Kd * current_rate;
        //	rprintf("bal_torq: ");
        //	rprintfFloat(8, balance_torque);
        //	rprintfCRLF();

        //steer_knob = 0;

        // change from current angle to something proportional to speed
        // should this be the abs val of the cur speed or just curr speed?
        //double steer_cmd = (1.0 / (1.0 + Ksteer2 * fabs(current_angle))) * (Ksteer * steer_knob);
        //double steer_cmd = 0.0;

        // Get current rate of turn
        //double current_turn = left_speed - right_speed; //<-- is this correct
        //double turn_accel = current_turn - prev_turn;
        //prev_turn = current_turn;

        // Closed-loop turn rate PID
        //double steer_cmd = KpTurn * (current_turn - steer_desired)
        //					+ KdTurn * turn_accel;
        //					//+ KiTurn * turn_integrated;

        // Possibly optional
        //turn_integrated += current_turn - steer_cmd;

        //	Differential steering
        //left_motor_torque	= balance_torque + steer_cmd; //+ cur_speed + steer_cmd;
        //right_motor_torque	= balance_torque - steer_cmd; //+ cur_speed - steer_cmd;


        // Limit extents of torque demand
        //left_motor_torque = flim(left_motor_torque, -MAX_TORQUE, MAX_TORQUE);
//		if (left_motor_torque < -MAX_TORQUE) left_motor_torque = -MAX_TORQUE;
//		if (left_motor_torque > MAX_TORQUE)  left_motor_torque =  MAX_TORQUE;

        //right_motor_torque = flim(right_motor_torque, -MAX_TORQUE, MAX_TORQUE);
//		if (right_motor_torque < -MAX_TORQUE) right_motor_torque = -MAX_TORQUE;
//		if (right_motor_torque > MAX_TORQUE)  right_motor_torque =  MAX_TORQUE;

        pwm = (int) ((angle -3.5) * Kp) + (rate * Kd); // + (int_angle * Ki);

        rprintf("  pwm:%d\r\n", pwm);

        // Set PWM values for both motors
        SetLeftMotorPWM(pwm);
        SetRightMotorPWM(pwm);
    }
    SetLeftMotorPWM(0);
    SetRightMotorPWM(0);
}
Ejemplo n.º 14
0
/**
 * \brief Returns amount of light in phototransistor.
 *
 * \return 8 bit ADC result.
*/
uint8_t GetLight()
{
	return GetADC( LIGHT_PIN );
}
Ejemplo n.º 15
0
int main(void)
{
	int i;
	
	
	RCC_Configuration();	                           //?????????????	        
	
	NVIC_Configuration();    
	LED_Config();
	TIM7_Configuration(10) ;
 
  RCC_Config();



 ADC_initial();
 UART1_Init();
 UART1_Config(9600);
 UART1_Cmd(ENABLE);
 UART1_Write("stm start",9);
 LCD_GLASS_Configure_GPIO();
 LCD_GLASS_Init();
      
	

 while (1)
 {
	  int wdt=0;
		int adc_wdt=0;
	  double adc=0;
	 //// solution 2 working String
	   i=0;
	 
     memcpy(buff2,buff, strlen(buff)); // ? buff ??? buff2
     memset(buff, 0, strlen(buff)); // ?? buff ???????  
						
	   while(1)
		 {
			  if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET)
				{
						char c = USART_ReceiveData(USART1); 
						i=i+1;				
						if(c == '\r')
							break;
						else 
						if (c == '\n')
							break;
						else
							 sprintf (buff, "%s%c", buff,c);
						
				}else
				{	
				    wdt++;
					  adc_wdt++;
					  if(adc_wdt%100==0)
						{
							 adc=(adc*99.0+GetADC())/100.0;	
						}
					  if(adc_wdt>10000)
						{
							 adc_wdt=0;
							 LCDPrint(" %0.1f ",adc);
						
						}
						if(wdt==50)
						{
								 wdt=0;
								
								 for(i=0;i<4;i++)
								 {
									 if(flag[i]==0)
									 {
											LED[i]++;
											if(LED[i]>300)
											{
													flag[i]=1;
											}
									 }else
									 {
											 LED[i]--;
											if(LED[i]==0)
											{
													flag[i]=0;
											} 
									 }
									}
								
							}
					}
				}
			/*	strcat(buff,"\n");
				UART1_Write(buff, strlen(buff));
				Lcd_print(buff);
*/				//	UART1_Write(")",1);
				USART_ClearFlag(USART1, USART_FLAG_RXNE);
			
			}
}
Ejemplo n.º 16
0
int8_t GetLineError( void )
{
    uint16_t    ledMask = 0;
    int8_t      posn = 0;
    uint8_t     i;

    for ( i = 0; i < 8; i++ )
    {
        gADC[ i ] = GetADC( i );
    }

    // We have 5 sensors, which we'll assume even values, and the between
    // the sensors will be assigned odd values.
    //
    //    -4    -2    0    2    4
    // -5    -3    -1    1    3    5

    // Normalize the raw sensor values such that the low calibration value
    // gets assigned 0x00 and the high calibration value gets assigned 0xFF
    // and the other values are scaled proportionally.

    for ( i = 0; i < 5; i++ )
    {
        uint8_t     lineMap = gLineMap[ i ];
        uint16_t    lineADC = gADC[ lineMap ];
        uint16_t    calWhite = gMemParam.cal_white[ lineMap ];
        uint16_t    calBlack = gMemParam.cal_black[ lineMap ];

        if ( lineADC < calWhite )
        {
            lineADC = calWhite;
        }
        else if ( lineADC > calBlack )
        {
            lineADC = calBlack;
        }

        lineADC -= calWhite;
        lineADC *= 256;
        lineADC /= ( calBlack - calWhite );

        if ( lineADC > 0xFF )
        {
            lineADC = 0xFF;
        }
        gLineADC[ i ] = lineADC;
    }

    gLowMask = 0;
    gHighMask = 0;

    for ( i = 0; i < 5; i++ )
    {
        if ( gLineADC[ i ] > gMemParam.thresh_lo )
        {
            gLowMask |= ( 1 << i );
        }
        if ( gLineADC[ i ] > gMemParam.thresh_hi )
        {
            gHighMask |= ( 1 << i );
            ledMask   |= gLedMask[ i ];
        }
    }

    if (( gHighMask == 0 ) || (( gHighMask & ( gHighMask - 1 )) == 0 ))
    {
        // Exactly 0 or 1 bits are set in gHighMask. Check to see if two
        // bits are on in the low mask

        switch ( gLowMask )
        {
        case 0x03:
        {
            posn = -3;
            ledMask = LED_MASK_0 | LED_MASK_1;
            break;
        }

        case 0x06:
        {
            posn = -1;
            ledMask = LED_MASK_1 | LED_MASK_2;
            break;
        }

        case 0x0C:
        {
            posn = 1;
            ledMask = LED_MASK_2 | LED_MASK_3;
            break;
        }

        case 0x18:
        {
            posn = 3;
            ledMask = LED_MASK_3 | LED_MASK_4;
            break;
        }
        }

        if ( gHighMask == 0 )
        {
            // Check for the case that the line is just outside the outside
            // sensors

            if ( gLowMask == 0x01 )
            {
                posn = -4;
                ledMask = LED_MASK_0;
            }
            else if ( gLowMask == 0x10 )
            {
                posn = 4;
                ledMask = LED_MASK_4;
            }
        }

        if ( posn == 0 )
        {
            switch ( gHighMask )
            {
            case 0x01:
                posn = -4;
                break;
            case 0x02:
                posn = -2;
                break;
            case 0x04:
                posn =  0;
                break;
            case 0x08:
                posn =  2;
                break;
            case 0x10:
                posn =  4;
                break;
            }
        }
    }

    EXP_TransferWord( ~ledMask, EXP_OUT_LED_MASK );

    return posn;

} // GetLineError
Ejemplo n.º 17
0
/***********************************************************
电池充电

**************************************************************/
int charge(void)
{

	FRESULT result;
	FATFS fs;
	FIL file;
	DIR DirInf;  
	uint32_t bw;


	char time_buf[TIME_MAX];
	char write_buf[WR_MAX];
	char tmp_buf[8];
	int i = 0;
	int min_count = 0;
	int adc_value = 0;
	int adc_tmp = 0;
	int flag = 0;
	int times = 0;





	double charge_time = 0;
	double vol;

/* 挂载文件系统,打开文件,定位文件指针 */
	result = f_mount(0, &fs);		
	if (result != FR_OK){
		printf("FileSystem Mounted Failed (%d)\r\n", result);
		goto ERROR;
	}

	result = f_opendir(&DirInf, "/");
	if (result != FR_OK){ 
		printf("Open Root Directory Error (%d)\r\n", result);
		goto ERROR;
	}
	
	result = f_open(&file, conf->file_name, FA_OPEN_ALWAYS | FA_READ | FA_WRITE);	
	if(result !=FR_OK){
		printf("open error, errornu:%d\n\r",result);
	 	goto ERROR;
	}

	if(f_lseek(&file,file.fsize) != FR_OK){
		printf("f_lseek error, errornu:%d\n\r",result);
		goto ERROR;
	}


	beeper_on();					   /* 蜂鸣器提示 */
	delay_ms(200);
	beeper_off();

	load_con_off();					/*负载通道关闭 */
	charge_con_on(); 				/*打开CHAR_CON充电通道 */

	bsp_LedOn(2);

	memset(time_buf,0,TIME_MAX);	  		/*清空数组,防止乱码*/
	memset(write_buf,0,WR_MAX);

	StartTimer(0,  100);  	
	adc_value = GetADC();
							
	do{
	       if(jump)
			goto JMP;	       	
		if(CheckTimer(0)) {
		
			vol = ADC_SAMPLING();	
			charge_time ++;
			min_count ++;
			
			StartTimer(0, 1000);


			adc_tmp = GetADC();


		  if((abs(adc_value - adc_tmp) > 3) || (times != 0)){
		     		
				if(abs(adc_value - adc_tmp) <= 3){
					times = 0;
				}
				else{
						times++;
					if(times >= 10){
						times = 0;
						flag = 0;
						adc_value = adc_tmp;	
					}
				}
		  }
		  else if((abs(adc_value - adc_tmp) <= 3) && (times == 0)){
			 
				flag ++;
			}
		
					
			if(min_count >= 60){	      		/* 每隔一分钟采样一次 */

				min_count = 0;
				record_time(time_buf);
				strcat(time_buf,",");						
	
				strcat(write_buf, time_buf);
				memset(time_buf,0,TIME_MAX);		
	
								
				sprintf(tmp_buf, "%.2f", vol);
				strcat(write_buf, tmp_buf);								      
				strcat(write_buf,",充电\n");
	
				if(i > 9){		     /* 10次采样将数据记录到flash中 */
					result = f_write(&file,write_buf,sizeof(write_buf),&bw);			
					if(result != FR_OK){
						printf("file write faild, errornu:%d\n\r",result);
						goto ERROR;
					}
					
					memset(write_buf,0,WR_MAX);
					i = 0;
				}
				i++;	
	
			}

		}
		   			
	}while(charge_timeout(charge_time/3600, vol, flag));   /* 充电时间 */

JMP:
	jump = 0;
							
	if(i != 0){			/* 最后将不足10次的数据记录到flash中 */

		result = f_write(&file,write_buf,(15 + TIME_MAX)*i, &bw);			
		if(result != FR_OK){
			printf("file write faild, errornu:%d\n\r",result);
			goto ERROR;
			
		}
		i = 0;		
	}
	memset(write_buf,0,WR_MAX);

	charge_con_off();	    /* 关闭CHAR_CON充电通道 */
	bsp_LedOff(2);

       /* 关闭文件,卸载文件系统 */
	f_close(&file);		      
	f_mount(0, NULL);
	return 0;
							
ERROR:			    	/* 错误处理 */
	bsp_LedOff(2);
	f_close(&file);
	f_mount(0, NULL);
	return 1;


}