Пример #1
0
int main(void)
{

	/*Set the clocking to directly run from the crystal at 8MHz*/
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);

    /* Set the clock for the GPIO Ports */
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

	/*Enable ADC Peripheral*/
	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

	/* Set the type of the GPIO Pins required */
	GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); //initialiing the on-board LEDs as outputs
	GPIOPinTypeGPIOOutput(digital2base, digital2pin);
	GPIOPinTypeGPIOOutput(digital3base, digital3pin);
	GPIOPinTypeGPIOOutput(digital4base, digital4pin);
	GPIOPinTypeGPIOOutput(digital5base, digital5pin);
	GPIOPinTypeGPIOOutput(digital6base, digital6pin);
	GPIOPinTypeGPIOOutput(digital7base, digital7pin);
	GPIOPinTypeGPIOOutput(digital8base, digital8pin);
	GPIOPinTypeGPIOOutput(digital9base, digital9pin);
	GPIOPinTypeGPIOOutput(digital10base, digital10pin);
	GPIOPinTypeGPIOOutput(digital11base, digital11pin);
	GPIOPinTypeGPIOOutput(digital12base, digital12pin);
	
	/* Set the GPIO Pins for the LCD as output */
	
	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4| GPIO_PIN_5| GPIO_PIN_6| GPIO_PIN_7);

	/*Initialize digital control pins*/
	GPIOPinWrite(relaycontrolbase, relaycontrolpin, 0); //relay switch off inside main() function
	GPIOPinWrite(speakerbase, speakerpin, 0); //speaker off inside main() function

	/* UART config */
	InitConsole();
	
	/* Enable is set low */
	GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0);

    /* Set up the period for the SysTick timer.  The SysTick timer period will
    be equal to 1ms.*/
    SysTickPeriodSet(SysCtlClockGet()/1000);

    /* Enable SysTick. */
    SysTickEnable();

    ConfigureLCD();
	
	/*WELCOME SCREEN */

    UARTprintf("WELCOME\n");
    delay(2000);

	/*AUTO CALIBRATION OF ACCELEROMETER TO PRESENT STATE ON RESET */

	/*start of auto calibration */

	/*Configure ADC Peripheral*/
	/*Configure ADC Sequence*/
	ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
	/*Enable ADC sequence*/
	ADCSequenceDisable(ADC0_BASE, 0);
	SysCtlDelay(SysCtlClockGet()/10);
	ADCSequenceEnable(ADC0_BASE, 0);


   /*Clear ADC Interrupt*/
	ADCIntClear(ADC0_BASE, 0);

	IntMasterEnable();

	ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH1);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);

    ADCProcessorTrigger(ADC0_BASE, 0);
    while(!ADCIntStatus(ADC0_BASE, 0, false))
    {
    }
    ADCIntClear(ADC0_BASE, 0);

    unsigned int i= 0;
    unsigned long sumx = 0;
    unsigned long sumz = 0;
    unsigned int xref = 0;
    unsigned int zref = 0;

    SysCtlDelay(SysCtlClockGet());

    for(i=0; i<= 100; i++)
    {

    SysCtlDelay(SysCtlClockGet()/50);
    ADCSequenceDataGet(ADC0_BASE, 0, temp);
    sumx = sumx + temp[1];
    sumz = sumz + temp[0];
    SysCtlDelay(SysCtlClockGet()/50);

    }

    xref = sumx/101;
    zref = sumz/101;

    UARTprintf("Xref = \n");
	UARTprintf(xref);
	delay(3000);
	
	UARTprintf("Yref = \n");
	UARTprintf(yref);
	delay(3000);
	
   /*end of auto calibration */

	/*Configure ADC Peripheral*/
	
	/*Configure ADC Sequence FOR READING JUST THE IR SENSOR*/
	ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_ALWAYS, 0); // 0 for taking 8 samples as 6 channels of ADC are involved

	/*Enable ADC sequence*/
	ADCSequenceDisable(ADC0_BASE, 0);
	SysCtlDelay(SysCtlClockGet()/10);
	ADCSequenceEnable(ADC0_BASE, 0);

    /*Clear ADC Interrupt*/
	ADCIntClear(ADC0_BASE, 0);

	IntMasterEnable();

	/* sensors are monitored on an infinite loop */

	while(1)
	{

		/*Reinitiate blinkcounter*/
		blinkcounter = 0;

		/*Reading just IR sensor values in a loop */

		for(i=0; i<= 5; i++)
	    {

	    	/*Configure ADC Sequence*/
	    	ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);

	    	/*Enable ADC sequence*/
	    	ADCSequenceDisable(ADC0_BASE, 0);
	   	    ADCSequenceEnable(ADC0_BASE, 0);

	   	    /*Clear ADC Interrupt*/
	        ADCIntClear(ADC0_BASE, 0);

	        IntMasterEnable();

	        ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH4 | ADC_CTL_IE | ADC_CTL_END);

            ADCProcessorTrigger(ADC0_BASE, 0);

            while(!ADCIntStatus(ADC0_BASE, 0, false))
	  	    {
	   	    }

            ADCIntClear(ADC0_BASE, 0);

            ADCSequenceDataGet(ADC0_BASE, 0, temp);

	   	    if(temp[0] < 1000)
	   	    {
	   	    	 blinkcounter++;
	   	    }
	   	    
	   	    UARTprintf("IR value %04d\n", temp[0]);

	   	    SysCtlDelay(SysCtlClockGet()/5);

	    }

    /*Configure ADC Sample Collection from 4 Channels READ ALL OTHER SENSORS*/

    ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); //button

	ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1); //z axis

	ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH3); // x axis

	ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH5 | ADC_CTL_IE | ADC_CTL_END); //alcohol

	//  For ADC Processor Triggering
    ADCProcessorTrigger(ADC0_BASE, 0);

    while(!ADCIntStatus(ADC0_BASE, 0, false))
    {
    }
    ADCIntClear(ADC0_BASE, 0);

    /*assign sensor values to sensor variables */
    alcoholsense = temp[3];
    xval = temp[2];
    zval = temp[1];
    butval = temp[0];

    UARTprintf("Xval =  %04d\n",xval);
	SysCtlDelay(SysCtlClockGet()/3);

	UARTprintf("Yval = %04d\n", yval);
	SysCtlDelay(SysCtlClockGet()/3);

	UARTprintf("AlcoholVal = %04d\n",alcoholsense);
	SysCtlDelay(SysCtlClockGet()/3);

	UARTprintf("ButtonVal %04d\n", butval);
	SysCtlDelay(SysCtlClockGet()/3);

    /*calculate instataneous deviaton from reference values for head tilting */
	
	deltax = abs(xref - xval);
    deltaz = abs(zref - zval);

    UARTprintf("Z DELTA = %04d\n", deltaz);
    SysCtlDelay(SysCtlClockGet()/3);
    
	
	UARTprintf("X DELTA = %04d\n", deltax);
    SysCtlDelay(SysCtlClockGet()/3);

//Check first for alcohol detection. Completely shutdown the system. Alarm will constantly ring.

	if(alcoholsense > 1000)
	{
		UARTprintf("Alcohol\n");
	    UARTprintf("Present\n");
		delay(1000);
		UARTprintf("Alarm\n");
	    UARTprintf("Triggered\n");
		SysCtlDelay(SysCtlClockGet()/2);
		alcoholdetected();
	}

//	Check for eyes being closed or blinking at a very fast rate or partially closed for long.

    if(blinkcounter >= 4)
    {

    	UARTprintf("Fatigued State");
		delay(2000);
		UARTprintf("Alarm Triggered");
		delay(1000);
    	SysCtlDelay(SysCtlClockGet()/2);
    	alarmState();

    }

    /*Check for accelerometer alert state*/

    /*Reinitiate accelerometer Counter */
    counter = 0;

    /*If one of the values in head position differs from auto calibrated values, re-sensing is done */

    if((deltax >= 70) || (deltaz >= 70))
    {
       	for(count = 0; count <= 10; count++)
       	{
                /*Configure ADC Sequence just for the accelerometer */ 
         		
		        ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
          	    /*Enable ADC sequence*/
           	    ADCSequenceDisable(ADC0_BASE, 0);
           	    SysCtlDelay(SysCtlClockGet()/10);
           	    ADCSequenceEnable(ADC0_BASE, 0);


           	   /*Clear ADC Interrupt*/
           	   ADCIntClear(ADC0_BASE, 0);
           	   IntMasterEnable();

           	   ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH1);

           	   ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);

           	   ADCProcessorTrigger(ADC0_BASE, 0);
           	   while(!ADCIntStatus(ADC0_BASE, 0, false))
           	   {
               }
               ADCIntClear(ADC0_BASE, 0);

               ADCSequenceDataGet(ADC0_BASE, 0, temp);

               deltax = abs(xref - temp[1]);
               deltaz = abs(zref - temp[0]);

               if((deltax >= 70) || (deltaz >= 70))
               {
            	       counter++;
            	       SysCtlDelay(SysCtlClockGet()/5);
           	   }
          	}
    	}

        if (counter >= 7)
        {

            
            UARTprintf("Head Tilted");
			delay(2000);
			UARTprintf("ALARM Triggered");
			SysCtlDelay(SysCtlClockGet()/2);
            alarmState();

        }

        else
        {

        	UARTprintf("NO ALARM DETECTED");
			delay(1000);
            SysCtlDelay(SysCtlClockGet()/5);

        }

    }

    return 0;

}
Пример #2
0
int main(void) {
	int i;
	char buff1[3];
	char buff2[3];
	initDisplay();
	initSomGenerator();
	initTimer0();
	initRC5();
	resetTimer0();
	initRTC();
	initRIT();
	setPriorities();

	int lastAlarmToggle = 0;

	char alarmBit;

	//////////////////////////////////
	char* test = generateSom();
	char som[10];
	strcpy(som, test);
	free(test);

	//////////////////////////////////

//	char arr[3];
//	readRAM(arr, 0x08);
//	delay(500);
//	alarmBit = arr[2];
//	clear();



	int arr2[3];
 	//int arr[3] = {0x00, 0x24, 0x12};
	//setTime(arr);

	readTime(arr2, 0x00);
 	if(arr2[2] < 10){
 		sprintf(buff1, "0%d", arr2[2]);
 	} else {
 		sprintf(buff1, "%d", arr2[2]);
 	}

 	if(arr2[1] < 10){
 		sprintf(buff2, "0%d", arr2[1]);
 	} else {
 		sprintf(buff2, "%d", arr2[1]);
 	}

	//Set the last known values in RIT.c
	lastValues[0] = arr2[0];
	lastValues[1] = arr2[1];
	lastValues[2] = arr2[2];

	sprintf(output, "%s:%s", buff1, buff2);

//    printf("%d\n", arr2[0]);
//    printf("%d\n", arr2[1]);
//    printf("%d\n", arr2[2]);
	//Main loop
	while (1) {


		while (!isTimeForAlarm(alarmBit) && (getCommand(commandCount - 1) != 15 && getCommand(commandCount - 1) != 11)) {
			//This is the main loop, all we do here is print the current time to the display since the current time is updated through the RIT and the EINT takes care of the remote control
			//Prints the current time
			printToDisplay(output);
		}

		if (getCommand(commandCount - 1) == 15) {
			//Goes into the set alarm state
			addAlarmState();
		}

		if (getCommand(commandCount - 1) == 11 && commandCount != lastAlarmToggle) { // '11' needs to be replaced with an other number, because it's an temporary button
			//Toggle alar state
			alarmBit = !alarmBit;
			toggleAlarmState(alarmBit);
			lastAlarmToggle = commandCount;
			//Give the user some feedback(Tell them wether they set or unset the alarm)
			for(i=0; i<200; i++){
				asm("nop");
				if(alarmBit == 1){
					printToDisplay("  +  ");
				} else {
					printToDisplay("  -  ");
				}

			}
		}

		if (isTimeForAlarm(alarmBit)) {
			//Goes into the alarm state
			alarmState();
			resetAlarmTime();

		}
		printToDisplay(output);
	}
	return 0;
}