void main(void) { WDTCTL = WDTPW +WDTHOLD; // Stop WDT initializeUART(); // Setup UART for RS-232 P6DIR &= ~0x05; // Configure P6.3 and P6.7 as input pins P6SEL |= 0x05; // Configure P6.3 and P6.7 as analog pins //Set up timer to send ADC info to PC overy 100 ms TACCR0 = 3277; //3277 / 32768 Hz = 0.1s TACTL = TASSEL_1 + MC_1; //ACLK, up mode TACCTL0 = CCIE; //enabled interupt //Set up ADC 12 ADC12CTL0 = ADC12ON + SHT0_6 + MSC; // configure ADC converter ADC12CTL1 = SHP + CONSEQ_1; // Use sample timer, single sequence ADC12MCTL0 = INCH_0; // ADC chan 0 is A3 pin - Stick X-axis ADC12MCTL1 = INCH_5 + EOS; // ADC chan 1 is A7 pin - Stick Y-axis //EOS - End of Sequence for Conversions ADC12IE |= 0x02; // Enable ADC12IFG.8 for (int i = 0; i < 0x3600; i++); // Delay for reference start-up ADC12CTL0 |= ENC; // Enable conversions _EINT(); while (1) { ADC12CTL0 |= ADC12SC; // Start conversions __bis_SR_register(LPM0_bits + GIE); // enter LPM0 } }
uint8_t MG2639_Cell::begin(unsigned long baud) { unsigned long setBaud = 0; uint8_t tries = 0; initializePins(); // Set up power and UART pin direction initializeUART(baud); // Initialize UART to requested baud // Try turning echo off first (send ATE0 command). If it succeeds, // we're already at the target baud and the module's on. if (setEcho(0) <= 0) { // If setEcho fails, we can't communicate with the shield. The baud // may be incorrect, or the shield may be off. First time in, we'll // assume the module is on. // tries should be at least > 2 - one time assuming module is on, // another time assuming module is off while (setBaud <= 0 && tries < 4) { setBaud = autoBaud(); // Try to find the baud rate if (setBaud <= 0) { // If autoBaud fails to find anything, the module must be off. powerPulse(); // Send a power pulse (hold PWRKEY for 3s) delay(MODULE_WARM_UP_TIME); // Delay ~3s for module to warm up } tries++; // Increment tries and go again } // If we still can't find the baud rate, or communicate with the shield // we give up. Return a fail. if (setBaud <= 0) return 0; // If autoBaud succeeds, the module is on, we just need to // change the baud rate. if (setBaud != baud) { int baudRsp; #if (ARDUINO >= 10601) // Software serial after 1.6.1 works much better, no need for brute force baudRsp = changeBaud(setBaud, baud); #else baudRsp = bruteForceBaudChange(setBaud, baud, 100); #endif // Look for any error _except_ ERROR_UNKOWN_RESPONSE -- a change from // 115200 will result in that error, even though the change baud // worked. (SoftwareSerial can't read reliably at that high rate.) if ((baudRsp == 0) || (baudRsp == ERROR_FAIL_RESPONSE) || (baudRsp == ERROR_TIMEOUT)) { return 0; } } delay(COMMAND_RESPONSE_TIME); } return 1; }
int main() { // initialize the board (void) Board_initGeneral(120 * 1000 * 1000); // initialize i2c initializeI2C(); // setup i2c task, who does the work (void) setup_I2C_Task(); // initialize uart initializeUART(); // setup uart task, printing the output (void) setup_UART_Task(); // setup the events which are used in combination with the queues (void) setup_Events(); // initialize interrupts initializeInterrupts(); // setup the interrupts - both for the ALTITUDE CLICK module and the USR_SW setup_Interrupts(); System_printf("Start BIOS\n"); System_flush(); /* Start BIOS */ BIOS_start(); }
int MG2639_Cell::changeBaud(unsigned long from, unsigned long to) { int iRetVal; char changeBaudCmd[14]; memset(changeBaudCmd, 0, 14); // Print something like "AT+IPR=9600" to changeBaudCmd string sprintf(changeBaudCmd, "%s=%lu", SET_BAUD_RATE, to); initializeUART(from); // Set UART baud to [from] baud sendATCommand((const char *)changeBaudCmd); // Send the baud change command // SoftwareSerial included with Arduino 1.6.1+ is drastically improved. // We can reliably send strings at 115200 #if (ARDUINO >= 10601) iRetVal = readWaitForResponse(RESPONSE_OK, COMMAND_RESPONSE_TIME); initializeUART(to); // Set SS UART to [to] baud rate return iRetVal; #else // Earlier versions of SS can't reliably read, but we should be able // to tell the difference between "ERROR" and "OK" due to the size // of the received string. unsigned long timeIn = millis(); unsigned long timeout = 10; // Set timeout to 10ms int received = 0; clearBuffer(); // Clear rxBuffer while (timeIn + timeout > millis()) // Check for a timeout { if (dataAvailable()) // If data available on UART { received += readByteToBuffer(); // Read it into rxBuffer, inc received } } initializeUART(to); // Set SS UART to [to] baud rate return received; // Return number of characters received #endif }
void initialize() { DDRA = 0x00; // PORT A est en mode entrée DDRB = 0xff; // PORT B est en mode sortie DDRC = 0xff; // PORT C est en mode sortie DDRD = 0xff; // PORT D est en mode sortie initializeUART(); initializeFilterClock(); vibrationLedOff(); //because the starting state is assumed unknown amplifierLedOn(); amplifierLedOff(); updateAmplifierStatus(); }
unsigned long MG2639_Cell::autoBaud() { int i; int echoResponse; for (i=0; i<BAUD_COUNT; i++) { initializeUART(baudRates[i]); // Set UART to baud rate printChar('\r'); // Print a '\r' to send any possible garbage command delay(10); // Wait for a possible "ERROR" response // Try turning echo off to see if the module responds with "OK" echoResponse = setEcho(0); // If setEcho is successful, we found the baud rate! Break this loop if (echoResponse > 0) break; } // If we found the baud rate, return the matching value if (echoResponse > 0) return baudRates[i]; else return 0; // Otherwise we failed to find it, return 0 }
main() { // Disable JTAG (on RA0 and RA1 ) mJTAGPortEnable( DEBUG_JTAGPORT_OFF ); // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); initializeUART(); initializeADC(); initializeLCD(); initializeRPG(); /* Initialize SD card */ setup_SDSPI(); SD_setStart(); /* Fill tempBuffer[] with int 0 to 63 * Write it to the current block. * Empty tempBuffer[] to all 0. * Read from the current block to make sure that it returns the right value. */ fillTempBuffer(); testSDReadWrite(tempBuffer); curr_read_block = curr_block; ConfigTimer1(); // Enable Timer1 for second counts configureInterrupts(); // T2CON = 0x8030; // TMR1 on, prescale 1:256 PB mPORTASetPinsDigitalOut( LED_MASK ); // LEDs = output mPORTDSetPinsDigitalIn( PB_MASK_D ); // PBs on D = input curr_state = READY; // enable interrupts INTEnableInterrupts(); int i = 0; while( 1 ) { if (getPrintToUARTFlag() == 1){ LCDMenuControl(); //mPORTAToggleBits( LED_MASK ); convertAndPrintIntegerToString("i => ", i++); convertAndPrintIntegerToString("timeElapse => ", timeElapsed); convertAndPrintIntegerToString("timeElapsedLEDSample => ", timeElapsedLEDSample); convertAndPrintIntegerToString("timeElapsedLEDTurnedOff => ", timeElapsedLEDTurnedOff); convertAndPrintIntegerToString("sampleLEDNow => ", sampleLEDNow); convertAndPrintIntegerToString(" ADC Value => ", getChannel5Value()); printShadowDetect(); printLightLevel(); drawLightDetectedBar(); controlPowerRelay(); switch(curr_state) { case READY : WriteString("State => READY "); break; case SLEEP : WriteString("State => SLEEP "); break; case HIBERNATE : WriteString("State => HIBERNATE"); break; case BUSY : WriteString("State => BUSY "); break; } WriteString("\r"); setPrintToUARTFlag(0); } if (NEW_BYTE_RECEIVED == 1){ curr_state = READY; NEW_BYTE_RECEIVED = 0; //mPORTAToggleBits( LED_MASK ); char tempArray[] = "g"; tempArray[0] = characterByteReceived; WriteString(tempArray); if(curr_state = HIBERNATE) { addByteToBuffer(characterByteReceived); } else { PutCharacter(characterByteReceived); } } if(bufferIndex == 512) { SDWriteBlock(currBlock); currBlock++; bufferIndex = 0; } if((curr_state == READY) && (timeElapsed >= SLEEP_TIMEOUT) && (timeElapsed < HIBERNATE_TIMEOUT)) { curr_state = SLEEP; } else if((curr_state == SLEEP) && (timeElapsed >= HIBERNATE_TIMEOUT)) { curr_state = HIBERNATE; timeElapsed = 0; } if (transmitDataFromSDCard == 1) { transmitDataFromSDCard = 0; forwardDataToPrinter(); } } // main (while) loop return 0; } // main
// Main Application int main(int argc, char** argv) { // Setup Main System SYSTEMConfigPerformance(80000000L); DDPCONbits.JTAGEN = 0; // Initialize System Pins initializeAllPins(); // Initialize the Real Time Clock initializeRTCC(); // Initialize Communication Systems initializeUART(); // Configure for multi-vectored mode & Enable Interrupts INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableInterrupts(); // Configure XBee xbee_baud.size = 1; strcpy(xbee_baud.data, "6"); xbee_channel.size = 2; strcpy(xbee_channel.data, "15"); xbee_network.size = 4; strcpy(xbee_network.data, "3421"); configureXBee(xbee_baud, xbee_channel, xbee_network); // Loop Infinitely while(1) { // Update Terminal Every Second, using the RTCC //while(!RtccGetSync()); // Update Termina Every 300mS Delayms(200); /* //Copy new GPS string to Globals if (gpsTempBuf.ready == 1) { // Copy GPS TempBuf to Sentence UINT8 gpsGGAPosition = 0; UINT8 ggaPosition = 0; int i = 0; for (i = 0; i < gpsTempBuf.size; i++) { gpsTempBuf.data[i] = gpsTempBuf.data[i]; // Parse specific GPS Data if (gpsTempBuf.data[i] == ',') { gpsGGAPosition++; ggaPosition = 0; } char temp[1] = { gpsTempBuf.data[i] }; if (gpsTempBuf.data[i] != ',') { switch(gpsGGAPosition) { case 1: gpsBaseCurrent.time.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.time.size++; ggaPosition++; break; case 2: gpsBaseCurrent.Latitude.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.Latitude.size++; ggaPosition++; break; case 3: gpsBaseCurrent.Longitude.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.Longitude.size++; ggaPosition++; break; case 4: gpsBaseCurrent.Fix = atoi(temp); ggaPosition++; break; case 5: gpsBaseCurrent.NumSatellites = atoi(temp); ggaPosition++; break; case 6: gpsBaseCurrent.HorizontalDilution.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.HorizontalDilution.size++; ggaPosition++; break; case 7: gpsBaseCurrent.Altitude.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.Altitude.size++; ggaPosition++; break; case 8: gpsBaseCurrent.HeightOfGeoid.data[ggaPosition] = gpsTempBuf.data[i]; gpsBaseCurrent.HeightOfGeoid.size++; ggaPosition++; break; default: break; } } } // Update System Time DFloat currentTime; currentTime.timeBytes.RSV = 0x0000; currentTime.timeBytes.HU = gpsBaseCurrent.time.data[0]; currentTime.timeBytes.HL = gpsBaseCurrent.time.data[1]; currentTime.timeBytes.MU = gpsBaseCurrent.time.data[2]; currentTime.timeBytes.ML = gpsBaseCurrent.time.data[3]; currentTime.timeBytes.SU = gpsBaseCurrent.time.data[4]; currentTime.timeBytes.SL = gpsBaseCurrent.time.data[5]; RtccSetTimeDate(currentTime.UValue, 0); } */ /* // Perform Magnetometer Calculations float tempMX = (float) compassCurrent.Signed.X; tempMX *= magGain[1] / 1000; float tempMY = (float) compassCurrent.Signed.Y; tempMY *= magGain[1] / 1000; float tempMZ = (float) compassCurrent.Signed.Z; tempMZ *= magGain[1] / 1000; // Create Unit Vectors float totalM = sqrtf(powf(tempMX,2) + powf(tempMY,2) + powf(tempMZ,2)); tempMX /= totalM; tempMY /= totalM; tempMZ /= totalM; */ // Convert to Degrees //tempMX = acosf(tempMX) * degrees_per_radian; //tempMY = acosf(tempMY) * degrees_per_radian; //tempMZ = acosf(tempMZ) * degrees_per_radian; // Match Current Angle with Calculated //sprintf(buf, "%04d X(%+07.2f, %5.3f) Y(%+07.2f, %5.3f) Z(%+07.2f, %5.3f) %02dmS %02dC \r\n", outputs, angleCurrent.X.Value, tempMX, angleCurrent.Y.Value, tempMY, angleCurrent.Z.Value, tempMZ, angleCurrent.T, gyroCurrent.TU); sprintf(buf, "%04d X(%+07.2f, %4d)\tY(%+07.2f, %4d)\tZ(%+07.2f, %4d)\tA(%3d, %4d)\tPWM(%3d, %3d, %3d, %3d) \r\n", outputs, angleCurrent.X.Value, errorReading[0], angleCurrent.Y.Value, errorReading[1], angleCurrent.Z.Value, errorReading[2], altitudeReading[0], errorReading[3], pwmReading[0], pwmReading[1], pwmReading[2], pwmReading[3]); outputs++; // Working, Shows Gyroscope Calculated Angles! //sprintf(buf, "Gyro: %+08.3f, %+08.3f, %+08.3f\r\n", tempX, tempY, tempZ); // Get Current Time //tempTime.l=RtccGetTime(); // Format String //sprintf(buf, "Gyro: %07f, %07f, %07f | Acl: %07.3f, %07.3f, %07.3f | Ang: %03.3f, %03.3f, %03.3f | UT: %02d mS | BST: %02x:%02x:%02x | FCT: %02x:%02x:%02x\r\n", tempX, tempY, tempZ, accelCurrent.X, accelCurrent.Y, accelCurrent.Z, angleCurrent.X.Value, angleCurrent.Y.Value, angleCurrent.Z.Value, angleCurrent.T, tempTime.hour, tempTime.min, tempTime.sec, timeFCBCurrent.hour, timeFCBCurrent.min, timeFCBCurrent.sec); // Place on Bluetooth putsBluetooth(buf, strlen(buf)); } return (EXIT_SUCCESS); }