inline void writeZero(void) { SERIAL_OUTPUT_PORT |= SERIAL_OUTPUT_PIN; _no_operation(); SERIAL_OUTPUT_PORT &= ~SERIAL_OUTPUT_PIN; }
void delay_millisec(unsigned int number_of_milliseconds) { TA0CCTL0 |= CM_0 + CCIE; //Enable compare mode + Interrupts on Timer TA0CTL |= TASSEL_1; //Use ACLK as source for timer - 32KHz unsigned int i = 0; unsigned int delayCycles = 32; // for 1 milli sec delay Number of cycles in the timer TA0CCR0 = delayCycles; TA0CTL |= MC_1; //Use UP mode timer for (i = 0; i<number_of_milliseconds; i++) { TA0R = 0; // Timer/counter register while (TA0R < delayCycles) { _bis_SR_register(LPM0_bits + GIE); //Enter Low Power Mode 3 with interrupts _no_operation(); } } }
/************************************************************************************************ * Execute_Mgr_Cmd() * * * * *****************************************************************************************************/ void Execute_Mgr_Cmd() { uint8 ret; ret=receive_msg_from_Mgr(&gsm); if(!ret){ _no_operation(); return; } else{ turnLedOn(SYSLED2); // __delay_cycles(22000000); //Execute Commands. feature_Interpreter(&gsm); turnLedOff(SYSLED2); return; } }
void FLASH_WRITE_PAGE ( ADDRESS Ad, /*! Page to be used */ PAGE *p /*! Pointer to the data buffer containing data to be written */ ) { unsigned int cycle; BYTE * pageTmp; pageTmp = (BYTE*)p; BYTE Status_Read; //! Page address (Ad*512) Ad= Ad<<9; //! Write Enable P3OUT &= ~BIT7; //!FLASH_CS low while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = SPI_WREN_CMD; while((UCB0STAT & UCBUSY) == UCBUSY); P3OUT |= BIT7; //!FLASH_CS HIGH _no_operation(); //! Read status and untill WREN OK P3OUT &= ~BIT7; //!FLASH_CS low while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = SPI_RDSR_CMD; //! Send Read Status Register CMD do { SPITXDATA =0x00; //! Send Dummy Status_Read = SPIRXDATA; }while((Status_Read & 0x02) == 0); while((UCB0STAT & UCBUSY) == UCBUSY); P3OUT |= BIT7; //!FLASH_CS HIGH _no_operation(); //! Send the read command P3OUT &= ~BIT7; //!FLASH_CS low while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = SPI_PP_4B_CMD;//!0x12 - Write command to UCB0TXBUF (SPI Buffer) //! Send the 4-byte address while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = (BYTE)((Ad >> 24) & 0x000000FF); while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = (BYTE)((Ad >> 16) & 0x000000FF); while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = (BYTE)((Ad >> 8) & 0x000000FF); while((UCB0STAT & UCBUSY) == UCBUSY); SPITXDATA = (BYTE) (Ad & 0x000000FF); for(cycle = 0; cycle < PAGESIZE ; cycle++) { SPITXDATA = *(pageTmp+cycle); } //! Deselect the device when finished while((UCB0STAT & UCBUSY) == UCBUSY); P3OUT |= BIT7; //!FLASH_CS HIGH }
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); } }
// 62.5ns per instruction. // // 1.25us ideal per "bit" = 20 instructions per "bit" // 1.25us (+/- 300ns => 950ns - 1.55ns) // 800us high, 450us low = ZERO => 13 cycles high, 7 cycles low // 450us high, 800us low = ONE => 7 cycles high, 13 cycles low // // NOTES: BIT0 runs slightly slower than the Bits: BIT7 - BIT1 because of the // pointer increment and the while loop test. // // TIMING MEASUREMENTS: // tH ONE: 725ns (nominal 800ns +/- 150ns) OK!!! (-75ns off) // tL ONE: 715ns (nominal 450ns +/- 150ns) OK... (+265ns off) // period ONE: 1440ns (nominal 1250ns +/- 600ns) OK!!! (+190ns off) // // tH ZERO: 390ns (nominal 400ns +/- 150ns) OK!!! (-10ns off) // tL ZERO: 790ns (nominal 850ns +/- 150ns) OK!!! (-60ns off) // period ZERO: 1180ns (nominal 1250ns +/- 600ns) OK!!! (-70ns off) // // tH LAST ONE: 725ns (nominal 800ns +/- 150ns) OK!!! (-75ns off) // tL LAST ONE: 985ns (nominal 450ns +/- 150ns) Uhh (+535ns off) // period LAST ONE: 1550ns (nominal 1250ns +/- 600ns) ok!!! (+460ns off) // // tH LAST ZERO: 460ns (nominal 400ns +/- 150ns) OK!!! (+60ns off) // tL LAST ZERO: 1100ns (nominal 850ns +/- 150ns) OK (+250ns off) // period LAST ZERO: 1560ns (nominal 1250ns +/- 600ns) OK!!! (+310ns off) // // // PSUEDO: // 1. Turn off Interrupts! Time critical. // 2. 100us pause to reset the data cycle. // 3. get end address of pixels[] (pixels + numberOfBytes) // 4. Get pointer for each byte to be written (bit by bit). // 5. Write each byte (bit by bit) for "numberOfBytes". // 6. increment pointer. // 7. All Data written. Turn on interrups back on. //---------------------------------------------------------------------------- void show(struct WS2812B_Strip *strip) { if (strip->inShow == true) return; // 1. Turn off Interrupts! Time critical. // DISABLE global interrupts. "Bit Clear Status Register" __bic_SR_register(GIE); strip->inShow = true; // 2. Turn output low for 50us. (800 cycles @16MHz ~= 50us). SERIAL_OUTPUT_PORT &= ~SERIAL_OUTPUT_PIN; __delay_cycles(800); // 3. get end address of pixels[] (pixels + numberOfBytes) // 4. Get pointer for each byte to be written (bit by bit). uint16_t bytesLeft = strip->numberOfBytes; uint8_t *ptr = strip->pixels; // Pointer to the current byte we are writing. // 5. Write each byte (bit by bit) for "numberOfBytes". while(bytesLeft != 0) // 2 cycles. { if (*ptr & BIT7) writeOne(); else writeZero(); if (*ptr & BIT6) writeOne(); else writeZero(); if (*ptr & BIT5) writeOne(); else writeZero(); if (*ptr & BIT4) writeOne(); else writeZero(); if (*ptr & BIT3) writeOne(); else writeZero(); if (*ptr & BIT2) writeOne(); else writeZero(); if (*ptr & BIT1) writeOne(); else writeZero(); if (*ptr & BIT0) { // Write last One SERIAL_OUTPUT_PORT |= SERIAL_OUTPUT_PIN; bytesLeft--; ptr++; _no_operation(); _no_operation(); _no_operation(); _no_operation(); SERIAL_OUTPUT_PORT &= ~SERIAL_OUTPUT_PIN; } else { // Write last Zero SERIAL_OUTPUT_PORT |= SERIAL_OUTPUT_PIN; bytesLeft--; ptr++; SERIAL_OUTPUT_PORT &= ~SERIAL_OUTPUT_PIN; } } // 7. All Data written. Turn on interrups back on. __bis_SR_register(GIE); // Enable global interrupts. "Bit Set Status Register" strip->inShow = false; }