Exemple #1
0
void boardInit(void)
{
    SIM_SCGC5 |= 0x00000400; //enable Port B clock
    SIM_SCGC5 |= 0x00000200; //enable Port A clock
    SIM_SCGC5 |= 0x00001000; //enable Port D clock

    PORTB_PCR19 |= (uint32_t)0x00000100; //Configure portB19 as GPIO (GREEN)
    GPIOB_PDDR |= (uint32_t)0x00080000; //Configure portB19 as output

    PORTA_PCR12 |= (uint32_t)0x000A0102; //Configure portA12 as GPIO with falling edge interrupt and pullup enabled.
    GPIOA_PDDR &= ~(uint32_t)(1<<12); //Configure portA12 as input.

    PORTB_PCR18 |= (uint32_t)0x00000100; //Configure portB18 as GPIO (RED)
    GPIOB_PSOR |= (uint32_t)0x00040000;
    GPIOB_PDDR |= (uint32_t)0x00040000; //Configure portB18 as output

    PORTD_PCR1 |= (uint32_t)0x00000100; //Configure portD1 as GPIO (BLUE)
    FGPIOD_PSOR |= (uint32_t)0x00000002;
    FGPIOD_PDDR |= (uint32_t)0x00000002; //Configure portD1 as output

    //SIM_SCGC6 |= (uint8_t) 0x00800000; // Enable PIT clock

    SIM_SOPT2 |= 0x01000000; // Set TPM to use the MCGFLLCLK as a clock source
    // MCGFLLCLK is either 24MHz or 23 986 176Hz
    SIM_SCGC6 |= 0x01000000; // Enable TPM0 clock

    uart0Config();
    uart0Enable();

    llwuConfigure();
    vllsEnable();
    vllsConfigure(1);

    PMC_REGSC = 0x08;

    systickConfigure();

    interruptSetPriority(30,0); //Port A ISR
    interruptEnable(30);

    interruptSetPriority(7,0); // Configure LLWU interrupt as highest priority.
    interruptEnable(7); //Enable LLWU interrupt.

    systickEnable();

    rtcInit();
    rtcStart();

    interruptSetPriority(21,0); // Configure RTC Seconds interrupt as highest priority.
    interruptEnable(21); //Enable RTC Seconds interrupt.

    i2cInit();
    interruptSetPriority(9,3); // Configure I2C interrupt as Lowest priority.
    interruptEnable(9); //Enable I2C interrupt.

    interruptSetPriority(22,3); // Configure PIT interrupt as Lowest priority.
    interruptEnable(9); //Enable PIT interrupt.
}
/*
 * @brief Application Program Loop
 */
void application()
{
	// Stop Watchdog Timer
	stopWatchdogTimer();

	// Configure DCO Frequency 1 MHz
	configureDCOFrequency1MHz();

	// Configure PushButtons Inputs
	pinDigitalInput(RTCSET);
	pinDigitalInput(RTCINC);
	pinDigitalInput(RTCDEC);

	// Configure PullUps
	pinDigitalEnabledPullUp(RTCSET);
	pinDigitalEnabledPullUp(RTCINC);
	pinDigitalEnabledPullUp(RTCDEC);

	// Configure High Low Interrupt
	pinDigitalSelectHighLowTransitionInterrupt(RTCSET);
	pinDigitalSelectHighLowTransitionInterrupt(RTCINC);
	pinDigitalSelectHighLowTransitionInterrupt(RTCDEC);

	// Configure Pin Interrupts
	pinDigitalEnableInterrupt(RTCSET);
	pinDigitalEnableInterrupt(RTCINC);
	pinDigitalEnableInterrupt(RTCDEC);

	// Init PAP Motor
	papMotorInit();

	// RTC Init
	rtcInit();

	// Start RTC
	rtcStart();

	// Initialize LCD Module
	lcdInit();

	// Write Message Constant
	lcdWriteMessage(1,1,mensaje);

	// Write Date and Time LCD Module
	lcdDataTimeFormat(2, 1, rtcGetHour(), rtcGetMinute(), rtcGetSecond());
	lcdDataDateFormat(2, 11, rtcGetDay(), rtcGetMonth(), rtcGetYear());

	// Enable Interrupts
	enableInterrupts();

	// Entry Low Power Mode 0
	entryLowPowerMode0();
}
Exemple #3
0
/**********************************************************
 * Sends a frame multiple times to the COG driver. A frame
 * is sent multiple times to improve contrast and 
 * reduce ghosting. The number of times the frame
 * is resent is dependent on the type of display
 * and current temperature. 
 * 
 * @param frameBuffer
 *   Pointer to the COG/EPD frame buffer
 **********************************************************/
void updateStage(uint8_t *frameBuffer)
{
  /* Calculate the stage time in ms */
  uint32_t endTime = getTempAdjustedStageTime(epdConfig.stageTime);
  
  /* Resend the frame for the entire stage time */
  rtcStart();
  do 
  {
    sendFrame(frameBuffer);
  }
  while ( rtcGetMs() < endTime );
  rtcStop();
}
/**
 * @brief Interrupt Service Routine Port 1
 */
void isrPort1()
{
	// Process RTCSET Pin
	if(pinDigitalIsPendingInterrupt(RTCSET))
	{
		if(pinDigitalRead(RTCSET) == 0)
		{
			delayMs(10);

			if(pinDigitalRead(RTCSET) == 0)
			{
				digitToModify++;

				// LCD Blink On
				lcdCursorBlinkOn();

				// Write On Configure RTC Indicator
				lcdWriteSetPosition(1,17,'*');

				// Stop RTC
				rtcStop();

				switch(digitToModify)
				{
					case 1:
						lcdSetCursor(2,1);
						break;
					case 2:
						lcdSetCursor(2,4);
						break;
					case 3:
						lcdSetCursor(2,7);
						break;
					case 4:
						lcdSetCursor(2,11);
						break;
					case 5:
						lcdSetCursor(2,14);
						break;
					case 6:
						lcdSetCursor(2,17);
						break;
					default:
						digitToModify = 0;

						// LCD Blink Off
						lcdCursorBlinkOff();

						// Write Off Configure RTC Indicator
						lcdWriteSetPosition(1,17,' ');

						// Start RTC
						rtcStart();

						break;
				}
			}
		}
		// Clear RTCSET Interrupt
		pinDigitalClearPendingInterrupt(RTCSET);
	}

	// Process RTCINC Pin
	if(pinDigitalIsPendingInterrupt(RTCINC))
	{
		if(pinDigitalRead(RTCINC) == 0)
		{
			delayMs(10);

			if(pinDigitalRead(RTCINC) == 0)
			{
				switch(digitToModify)
				{
				case 1:
					rtcSetHour(rtcGetHour() + 1);
					lcdDataDecFormat(rtcGetHour(),2);
					lcdSetCursor(2,1);
					break;
				case 2:
					rtcSetMinute(rtcGetMinute() + 1);
					lcdDataDecFormat(rtcGetMinute(),2);
					lcdSetCursor(2,4);
					break;
				case 3:
					rtcSetSecond(rtcGetSecond() + 1);
					lcdDataDecFormat(rtcGetSecond(),2);
					lcdSetCursor(2,7);
					break;
				case 4:
					rtcSetDay(rtcGetDay() + 1);
					lcdDataDecFormat(rtcGetDay(),2);
					lcdSetCursor(2,11);
					break;
				case 5:
					rtcSetMonth(rtcGetMonth() + 1);
					lcdDataDecFormat(rtcGetMonth(),2);
					lcdSetCursor(2,14);
					break;
				case 6:
					rtcSetYear(rtcGetYear() + 1);
					lcdDataDecFormat(rtcGetYear(),4);
					lcdSetCursor(2,17);
					break;
				default:
					directionPap = 0;
					break;
				}
			}
		}
		// Clear RTCINC Interrupt
		pinDigitalClearPendingInterrupt(RTCINC);
	}

	// Process RTCDEC Pin
	if(pinDigitalIsPendingInterrupt(RTCDEC))
	{
		if(pinDigitalRead(RTCDEC) == 0)
		{
			delayMs(10);

			if(pinDigitalRead(RTCDEC) == 0)
			{
				switch(digitToModify)
				{
				case 1:
					rtcSetHour(rtcGetHour() - 1);
					lcdDataDecFormat(rtcGetHour(),2);
					lcdSetCursor(2,1);
					break;
				case 2:
					rtcSetMinute(rtcGetMinute() - 1);
					lcdDataDecFormat(rtcGetMinute(),2);
					lcdSetCursor(2,4);
					break;
				case 3:
					rtcSetSecond(rtcGetSecond() - 1);
					lcdDataDecFormat(rtcGetSecond(),2);
					lcdSetCursor(2,7);
					break;
				case 4:
					rtcSetDay(rtcGetDay() - 1);
					lcdDataDecFormat(rtcGetDay(),2);
					lcdSetCursor(2,11);
					break;
				case 5:
					rtcSetMonth(rtcGetMonth() - 1);
					lcdDataDecFormat(rtcGetMonth(),2);
					lcdSetCursor(2,14);
					break;
				case 6:
					rtcSetYear(rtcGetYear() - 1);
					lcdDataDecFormat(rtcGetYear(),4);
					lcdSetCursor(2,17);
					break;
				default:
					directionPap = 1;
					break;
				}
			}
		}
		// Clear RTCDEC Interrupt
		pinDigitalClearPendingInterrupt(RTCDEC);
	}
}
Exemple #5
0
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    // Setup and pin configurations
    clkSetup();
    directionSetup();
    timerA0Setup();
    boardSetup();
    rtcSetup();
	initUART();
	pinGSM();
//	clkDebug();
	readDip();
	// test
	V4Stop();
	V5Stop();

	// Sensor variables
	int sensorValue = 0;
	int sensorData[LengthOfSensordata] = {0};
	char dataEnable = 0; 				// != 0 if the vector is filled ones
	int dataPosition = 0;
	int overflowCount = 0;
	char alarm = '0';

	// Parameters (From and to the flash)
	int lowerThresholds = readFlashLowTolerance();
	int upperThresholds = readFlashHighTolerance();
	int normalLvl = readFlashSensorOffset();			// Default higth over the water lvl

	// GSM decision variable
	char execution = '0';
	char disableAlarmFlag = '0';	// Disable = 1

	// RTC variable time offset1 = 1 min
	unsigned int rtcOffsetH = 0xFC6C;
	unsigned int rtcOffsetL = 0x78FF;

	__enable_interrupt();

	while(1)
	{
		V5Start();

		_no_operation();
		char c = '0';
		if(loop2Mode == '1' || startMode == '1')
		{	// Power on the GSM regulator
			V4Start(); 	// Enable power to GSMJa
			if(P8IN &= BIT4)
			{
				c = checkAT();
				if(c =='0') pwrOnOff();
			}
			else
			{
				pwrOnOff();
				Delay();
				c = '0';
			}
		}
		sensorValue = mainFunctionSensor(sensorData, LengthOfSensordata, &dataPosition, &dataEnable, &overflowCount);

		if (loop2Mode == '1' || startMode == '1')
		{	// wait for connection and check if SMS
			unsigned int count = 0;

			while(!(P8IN &= BIT4) || count < 40000)
			{
				count++;
				__delay_cycles(100);
			}
			if(c == '0') c = checkAT();

			if(c == '0')
			{
				V4Stop();
				Delay();
				Delay();
				V5Start();
				V4Start();
				if(P8IN &= BIT4)
				{
					c = checkAT();
					if(c =='0') pwrOnOff();
				}
				else
				{
					pwrOnOff();
					Delay();
				}
			}
			initGSM();
			execution = readSMS();

			if (execution == '0')
			{	// Nothing
				_no_operation();// test
			}
			else if (execution == 'S')
			{	// Status report
				responseStatus("STATUS i ", (normalLvl-sensorValue));
				deleteSMS();
			}
			else if (execution == 'N')
			{	// Confirm Nr change
				responseNrChange("Nummerlista uppdaterad i ");
				deleteSMS();
			}
			else if (execution == 'L')
			{	// Confirm changed normal level
				normalLvl = readFlashSensorOffset();
				responseLvlChange("Offset i ", normalLvl);
				deleteSMS();
			}
			else if (execution == 'T')
			{	// Confirm changed thresholds
				lowerThresholds = readFlashLowTolerance();
				upperThresholds = readFlashHighTolerance();
				responseThChange("Toleranser i ", lowerThresholds, upperThresholds);
				deleteSMS();
			}
			else if (execution == 'E')
			{	// Enable SMS
				sendSMS("Modulen har blivit aktiverad i ");
				disableAlarmFlag = '0';
				deleteSMS();
			}
			else if (execution == 'D')
			{	// Disable SMS
				sendSMS("Modulen har blivit inaktiverad i");
				disableAlarmFlag = '1';
				deleteSMS();
			}
			else if (execution == 'A')
			{	// Disable SMS with when alarm
				sendSMS("Larmet stoppat");
				deleteSMS();
				disableAlarmFlag = '1';		// Reseted when the lvl goes back to normal.
			}
			else
			{	/* Nothing */  }
		}

		// if the GSM mode disable turn off the power
		if (loop2Mode != '1' && startMode != '1')
		{
			V5Stop();
			V4Stop();
		}

		if (dataEnable != 0 && overflowCount == 0)
		{	// Process the sensor value
			alarm = evaluateData(sensorValue, normalLvl, upperThresholds, lowerThresholds, &rtcOffsetH, &rtcOffsetL, &timerAlarmFlag);
		}
		else if (overflowCount > 10)
		{	// Alarm overflow (Problem om man minskar RTC och något ligger ivägen!!!!)
			alarm = 'O';
		}
		else
		{}
		if (alarm != '0')
		{
			if (loop2Mode != '1' && startMode != '1' && disableAlarmFlag != '1' && timerAlarmFlag == '1')
				startGSMmodule();
			if(loop2Mode == '1' && startMode == '1' && disableAlarmFlag != '1' && timerAlarmFlag == '1')
			{
				c = checkAT();
				if(c == '0')
					startGSMmodule();
			}
			if (alarm == '+')
			{	// Alarm for high water lvl
				if (disableAlarmFlag != '1' && timerAlarmFlag == '1')
				{
					sendAlarm("Hog vattenniva i ", (normalLvl-sensorValue));
					timerAlarmFlag = '0';
				}
			}
			else if (alarm == '-')
			{	// Alarm for low water lvl
				if (disableAlarmFlag != '1' && timerAlarmFlag == '1')
				{
					sendAlarm("Lag vattenniva i ", (normalLvl-sensorValue));
					timerAlarmFlag = '0';
				}
			}
			else if (alarm == 'O' && timerAlarmFlag == '1')
			{	// Alarm for overflow
				sendSMS("Sensor kan vara ur funktion i ");
				timerAlarmFlag = '0';
			}
			else {}

			if (loop2Mode != '1' && startMode != '1' && timerAlarmFlag == '1')
			{
				V5Stop();
				V4Stop();
			}
		}
		else if (alarm == '0' && timerAlarmFlag == '1')
		{	// return to normal mode
			disableAlarmFlag = '0';
			// RTC for Repeat alarm
			// Send sms
		}
		rtcStart(rtcOffsetH, rtcOffsetL);
	}
}