Ejemplo n.º 1
0
int main(void)
{
    /* Configure the oscillator both devices */
    
    ConfigureOscillator();
    
    /* Initialize IO ports and peripherals for both devices */
   
    InitGPIO(); 
    InitUART();
    InitI2c();
    InitI2cCompass();
  
    /* Program for the bracelet */
    
#ifdef PROTECTED
    
    /* Initialize IO ports and peripherals */

      InitTimerUS();
 
    /* The values of the magnetic field will be save in x and y */
    s16 x = 0;
    s16 y = 0;   
    
    while(1)
    {
       I2cReadData(&x, &y);
       ComputeAngle(&angle, x, y);
       PutData16(angle);
       __delay_ms(500);
    }
    
#endif
    
    /* Program for the bodyguard*/
    
#ifdef BODY_GUARD
    

    /* Initialize IO ports and peripherals */
    InitADC();
    InitPWM();
    InitLcd();
    InitTimerServo();
    
    /* TODO <INSERT USER APPLICATION CODE HERE> */
    
    u16 ADC_values[NMB_SENSORS];
    u16 average[NMB_SENSORS];
    u8 i;
    u8 j;
    
    /* The values of the magnetic field will be save in x and y */
    s16 x = 0;
    s16 y = 0;
    
    u16 angle2=0;
    
    char T[5];
    
    
    memset(ADC_values,0x00,sizeof(ADC_values));
    memset(average, 0x00,sizeof(average));

    /*
    for(i=0; i<NMB_SENSORS; i++) 
    {
        ADC_values[i]=0;
    }*/
    LcdClear();
    
#if MAGNETIC_SENSOR    
    while(1)
    {
       I2cReadData(&x, &y);
       angle2=((-atan2(x,y)*180)/3.14)+180;
        /* Computes the angle using the arctan2 which provides an angle
        * between -180° and 180°, then converts the result that is in radian
        * into degree (*180/pi) and in the end add 180° so the angle is between
        * 0° and 360° */
       //LcdPutFloat(angle2,0);
       LcdPutFloat(angle2,0);
       LcdGoto(1,2);
       LcdPutFloat(angle,0);
       __delay_ms(500);
       LcdClear();
        
    }
#endif

#ifdef BODY_GUARD_MODE
   while(1)
    { 
        
        for(j=0; j<NMB_MEASURES; j++)
        {
            /* SENSORS SAMPLING */
            for(i=0; i<NMB_SENSORS; i++)
            {
                StartADC(ADC_values);
            }
            ObjectDetection(ADC_values, average);
        }
        LcdClear();
        
        LcdPutFloat(CCP2RB, 0);
       
        //__delay_ms(1000);
        
        LcdClear();
        
        /* Set Flags */       
        ObjectReaction(average);        
        DistanceFlag(average[US]);
        
        /* react */
        
        AutoBodyGuard();

    }
#endif
               
#ifdef AUTO_FLEE
    while(1)
    { 
        
        for(j=0; j<NMB_MEASURES; j++)
        {
            /* SENSORS SAMPLING */
            for(i=0; i<NMB_SENSORS; i++)
            {
                StartADC(ADC_values);
            }
            ObjectDetection(ADC_values, average);
        }
        LcdClear();
        
        /* Set Flags */       
        ObjectReaction(average);        
        //DistanceFlag(average[US]);
        AutoFLee();

    }
    
#endif
#endif
    return 0;
}
Ejemplo n.º 2
0
int main(void)

{
	uint16_t CalcCRC16(uint8_t data_array[], int data_lenght);
	 
	 int i;
	 /////////////////////OSCYLATOR 32MHZ///////////////////////////////////////////
	init_osc32();
	 ////KONFIGURACJA ADC////////////////////////////////////////////////////////////////
	 konfiguracja_adc();
	 /////////UART////////////////////////////////////////////////////////////////////////
	 PORTC_OUTSET = PIN7_bm; //Let's make PC7 as TX
	 PORTC_DIRSET = PIN7_bm; //TX pin as output
	  
	 PORTC_OUTCLR = PIN6_bm;
	 PORTC_DIRCLR = PIN6_bm; //PC6 as RX
	 setUpSerial();
	///////////////////////////////////////////////////////////////////////////////
	PORTF_DIR=PIN0_bm;
	//PORTF_OUTSET=PIN0_bm;  //zapalenie diody 
	PORTC.DIRSET	=	PIN0_bm;		// pin C0  jako wyjœcie
	PORTC.DIRSET	=	PIN1_bm;		// pin C0  jako wyjœcie
	////////////////////////Flip//////////////////////////////////////////////// 
	PORTE.DIRCLR	=	PIN5_bm;				// pin E5 jako wejœcie
	PORTE.PIN5CTRL	=	PORT_OPC_PULLUP_gc;		// podci¹gniêcie do zasilania
	////////////////////////////Klawisze gora do³ prawo lewo//////////////////////////////////////////
	PORTF.DIRCLR	=	PIN1_bm|PIN2_bm|PIN5_bm|PIN6_bm;
	PORTF.PIN1CTRL	=	PORT_OPC_PULLUP_gc|PORT_ISC_FALLING_gc;	
	PORTF.PIN2CTRL	=	PORT_OPC_PULLUP_gc;	
	PORTF.PIN5CTRL	=	PORT_OPC_PULLUP_gc;	
	PORTF.PIN6CTRL	=	PORT_OPC_PULLUP_gc;
	///////////////////////////////////Przerwanie INT0/////////////////////////////////////////////////////
	init_int0();
	//////////////////////////TIMER0//////////////////////////////////////////////////////
	timer();
	//////////////////////W£¥CZENIE PRZERWAÑ//////////////////////////////////////////
	sei();								// globalne w³¹czenie przerwañ
	////////////////////////////////////////////////////////////////////////////////////////
	LcdInit();									// inicjalizacja sterownika LCD
	LcdGoto(0,0);
	Lcd("*Inzynierka_DiagBoX*");					// wyœwietlenie napisu
	LcdGoto(20,0);
	Lcd("Przedstawia:");
	LcdGoto(25,1);
	Lcd("Marek Wudarczyk");
   _delay_ms(1000);
    LcdClear();
	
           while (1){   
			   LcdClear();
			   LcdGoto(0,0);
	           LcdDec(ADC);
			   LcdGoto(5,0);
			   Lcd("CNT = ");
			   LcdDec(TCC0.CNT);
			   LcdGoto(0,1);
			   LcdDec(TCC0.PER);
			   LcdGoto(20,1);
			   Lcd("czas:");
			   LcdDec(c);
			   
			  i=usart_getc();
				if(i==2){
					while(1){
							uint8_t i,j=0;			
							i=usart_getc();
							uint16_t ADC = ADC_GetResults();
							uint8_t buff[] = {mlody_bajt(ADC),stary_bajt(ADC)};
							buff[2] = mlody_bajt(CalcCRC16(buff,2));
							buff[3] = stary_bajt(CalcCRC16(buff,2));
								while(j<4){
									sendChar(buff[j]);
									j++;
								}
								
								 LcdClear();
								Lcd("CRC:");
								LcdDec(buff[2]);
							    _delay_ms(10);
								 if(i == 2){
									break;
			 }
					}
						i=0;
				}
			if(i==1){
					PORTF_OUTTGL=PIN0_bm;  
					i=0;
			}
			if(!(PORTE.IN & PIN5_bm)) /* je¿eli przycisk FLIP jest wciœniêty*/ {
				while(1){  
						uint32_t licz;
						licz++;
						if((licz>15100)&&(PORTE.IN & PIN5_bm)){
							licz=0;
						
							c+=10;
							break;
						}
			
				}	 
			 }

		_delay_ms(100);

       
	}
			}