// Set up the UART for the GSM chip void gsmSetup(void) { // Configure Port Direction TRISGbits.TRISG6 = 0; // Turn RG6 into output for GSM TX TRISGbits.TRISG7 = 1; // Turn RG7 into input for GSM RX // Configure PPS pins for GSM iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP26); // Assign U1RX to pin RP26 iPPSOutput(OUT_PIN_PPS_RP21,OUT_FN_PPS_U1TX); // Assign U1TX to pin RP21 // Close UART in case it's already open CloseUART1(); // Enable UART Interface ConfigIntUART1(UART_RX_INT_DIS | UART_RX_INT_PR7 | UART_TX_INT_DIS | UART_TX_INT_PR7); // Receive interrupt disabled // Transmit interrupt disabled OpenUART1(UART_EN, UART_TX_ENABLE, 25); // Module enable // BRG generates 4 clocks per bit period // Transmit enable // 9600 baud rate (@ 4 MHz FCY) }
void UARTinit() { CloseUART1(); U1MODEbits.STSEL = 0; // 1 Stop bit U1MODEbits.PDSEL = 0; // No Parity, 8 data bits U1MODEbits.BRGH = 0; // Low-Speed mode - ovde je bila nula! U1BRG = 31; // BAUD Rate Setting for 57600 U1MODEbits.UARTEN = 1; // Enable UART U1STAbits.UTXEN = 1; IFS0bits.U1RXIF = 0; }
/** * Start the servo hardware */ void initServos(){ SERVO_HW_INIT(); if(getPrintLevel() == NO_PRINT){ CloseUART1(); } int i; for(i=0;i<dataTableSize;i++){ pinOff(i); setServo(i,128,0); } // runLinearInterpolationServo(start,stop); // runSort(); // for(i=0;i<numPidMotor;i++){ // println_I("Servo Positions: ");p_sl_I(getServoPosition(i)); // } // for(i=0;i<numPidMotor;i++){ // println_I("Sorted Servo Positions index: ");p_sl_I(sort[i]); print_I(" value: ");p_sl_I(position[sort[i]]); // } setTimerLowTime(); }
int main(void) { // Bring all Outputs Low LATB = 0x0000; LATD = 0x0000; LATF = 0x0000; LATG = 0x0000; // Disable analog I/O on PORT B and G ANSB = 0x0000; ANSG = 0x0000; // Setup the GPS and USB UART usbSetup(); gpsSetup(); gsmSetup(); sdSetup(); // Clear and enable GSM and USB interrupts U1RX_Clear_Intr_Status_Bit; U3RX_Clear_Intr_Status_Bit; EnableIntU1RX; EnableIntU3RX; // Clear and enable GPS interrupt // U2RX_Clear_Intr_Status_Bit; // EnableIntU2RX; // Set up to echo GPS data // sendCommand(PMTK_SET_BAUD_9600); // DELAY_MS(100); // sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); // sendCommand(PMTK_SET_NMEA_OUTPUT_OFF); // Enable for log dump // DELAY_MS(100); // sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // Disable for log dump // DELAY_MS(100); // Start GPS data logger // startLOCUS(); strToUSB("Starting SM5100B Communication...\n"); // Loop Forever while(1) { blink(LED_GRN_P42); DELAY_MS(1000); // Print out logger info // displayLOCUSInfo(); } // Disable Interrupts and close UART DisableIntU1RX; DisableIntU2RX; DisableIntU3RX; CloseUART1(); CloseUART2(); CloseUART3(); }
void initUART1( unsigned long baudrate ) { /* Holds the value of baud register */ unsigned int baudvalue; /* Holds the value of uart config reg */ unsigned int U1MODEvalue; /* Holds the information regarding uart * Tx and Rx interrupt modes */ unsigned int U1STAvalue; /* Turn off UART1module */ CloseUART1(); /** Clear UART1 Receive Interrupt Flag * Note: If flag is not cleared, the program will continue to * think an interrupt has occured and remain within the * the user defined interrupt routine. */ IFS0bits.U1RXIF = 0; IFS0bits.U1TXIF = 0; /** Configure UART1 Receive and Transmit Interrupts */ ConfigIntUART1( UART_RX_INT_EN & // Enable UART1 Receive Interrupts UART_RX_INT_PR3 & // Set UART1 Receive interrupts to priority 3 UART_TX_INT_DIS & // Disable UART1 Transmit interrupts UART_TX_INT_PR3 ); // Set UART1 Transmit interrupts to priority 3 /* Configure UART1 module to transmit 8 bit data with one stopbit. * * Load a value into Baud Rate Generator. Example is for 38400. * See section 19.3.1 of datasheet. * baudvalue = (Fcy/(16*BaudRate))-1 * baudvalue = (40M/(16*38400))-1 * baudvalue = 65 */ /* Slow speed settings */ if(baudrate <= 57600UL) { baudvalue = ( unsigned int )( ( FCY / ( 16 * baudrate ) ) - 1 ); U1MODEvalue = UART_EN & // Enable UART1 UART_IDLE_CON & // Work in idle mode UART_IrDA_DISABLE & // Disable IrDA encoder and decoder UART_MODE_FLOW & // UART1 pin in flow control mode UART_UEN_00 & // U1TX and U1RX used, U1TCS, U1RTS, and BCLK controlled by latch for flow control UART_DIS_WAKE & // Disable wakeup on start UART_DIS_LOOPBACK & // Disable loopback mode UART_DIS_ABAUD & // Baudrate me asurment disabled UART_UXRX_IDLE_ONE & // U1Rx Idle state is 1 (not 0) UART_BRGH_SIXTEEN & // Baud Rate Generator generates 16 clocks per bit second (16x baudrate) UART_NO_PAR_8BIT & // Do not use a parity bit UART_1STOPBIT; // Use 1 stop bit } /* High speed settings */ else { baudvalue = ( unsigned int )( ( FCY / ( 4 * baudrate ) ) - 1 ); U1MODEvalue = UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_FLOW & UART_UEN_00 & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_UXRX_IDLE_ONE & UART_BRGH_FOUR & // Baud Rate Generator generates 4 clocks per bit second (4x baudrate) high speed UART_NO_PAR_8BIT & UART_1STOPBIT; } /* Status register settings */ U1STAvalue = UART_INT_TX & // Interrupt on transfer of every character to Trap Service Routine UART_TX_ENABLE & // Enable UART1 transmit UART_IrDA_POL_INV_ZERO & // IrDA encoded, idle state is 0 (not 1) UART_SYNC_BREAK_DISABLED & // Sync break transmission disabled UART_INT_RX_CHAR & // Trigger an interrupt on every character received UART_ADR_DETECT_DIS & // Disable address detection UART_TX_BUF_FUL & // Transmit buffer is full UART_RX_OVERRUN_CLEAR; // Rx buffer over run bit clear /* Open UART1 connection * * This function is defined in uart.h the builtin library fuctions * provided with the compiler. See uart.h or the C30 library users * guide for more infromation */ OpenUART1( U1MODEvalue, U1STAvalue, baudvalue ); __delay_ms(10); /* Prints a welcome banner alerting user serial communication port * has been opened. */ //putsUART1("UART1 started!\r\n"); }