void SetupInterrupts(void) { //ConfigIntTimer1(T1_INT_PRIOR_4 & T1_INT_OFF); //ConfigIntTimer2(T2_INT_PRIOR_4 & T2_INT_OFF); ConfigIntUART1(UART_TX_INT_EN & UART_TX_INT_PR4 & UART_RX_INT_EN & UART_RX_INT_PR4); //UART TX/RX }
// 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 initUART1(int pbClk) { #define config1 UART_EN | UART_IDLE_CON | UART_RX_TX | UART_DIS_WAKE | UART_DIS_LOOPBACK | UART_DIS_ABAUD | UART_NO_PAR_8BIT | UART_1STOPBIT | UART_IRDA_DIS | UART_DIS_BCLK_CTS_RTS| UART_NORMAL_RX | UART_BRGH_SIXTEEN #define config2 UART_TX_PIN_LOW | UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR | UART_ADR_DETECT_DIS | UART_RX_OVERRUN_CLEAR OpenUART1(config1, config2, pbClk / 16 / DESIRED_BAUDRATE - 1); // calculate actual BAUD generate value. ConfigIntUART1(UART_INT_PR2 | UART_RX_INT_EN); INTEnableSystemMultiVectoredInt(); }
void UART1_Config(void){ OpenUART1(( UART_EN & //Enable UART Module UART_IDLE_CON & //Work while idle UART_IrDA_DISABLE & //Disable Infared decoder UART_MODE_SIMPLEX & //UART Simplex mode (RTS pin) UART_UEN_00 & //TX/RX pins configured all other latched UART_DIS_WAKE & //Disable wake on Start bit UART_DIS_LOOPBACK & //Disable loopback UART_DIS_ABAUD & //Disable Auto Baud UART_UXRX_IDLE_ONE & //Receive idle is 1 UART_BRGH_SIXTEEN & //16 clocks per bit period UART_NO_PAR_8BIT & //No parity bit UART_1STOPBIT //One stop bit ), ( UART_INT_TX_LAST_CH & //Interrupt when last character shifted out UART_IrDA_POL_INV_ZERO & //IrDA encoded, UxTX Idel state is '0' UART_SYNC_BREAK_DISABLED & //Sync break transmission disabled or completed UART_TX_ENABLE & //Transmit enable UART_TX_BUF_NOT_FUL & //Transmit buffer is not full UART_INT_RX_CHAR & //Interrupt on every char received UART_ADR_DETECT_DIS & //address detect disable UART_RX_OVERRUN_CLEAR //Rx buffer Over run status bit clear ), ( __BAUDUART1_ //Baud rate )); ConfigIntUART1( UART_RX_INT_DIS & //Receive interrupt enabled UART_RX_INT_PR1 & //Priority RX interrupt 1 UART_TX_INT_DIS & //transmit interrupt disabled UART_TX_INT_PR2 //Priority TX interrupt 1 ); #ifdef InvertRxTxBits U1STAbits.UTXINV = 0; U1MODEbits.URXINV = 0; #endif //Set the tristate and map pins for Uart TRIS_U1TX; //TRIS_U1RX; MAP_U1TX; //MAP_U1RX; }
void LS1_UART_Init (void) { CPU_INT08U config; CPU_INT32U config1; CPU_INT32U config2; config1 = UART_EN // UART module enabled | UART_IDLE_CON // UART works in IDLE mode | UART_RX_TX // Communication is done through the normal pins | UART_DIS_WAKE // Disable Wake-up on START bit detect during SLEEP | UART_DIS_LOOPBACK // Disable loop back | UART_DIS_ABAUD // Input to capture module from ICx pin | UART_NO_PAR_8BIT // 8 bits no parity | UART_1STOPBIT // 1 stop bit | UART_IRDA_DIS // IrDA disabled | UART_MODE_FLOWCTRL // UART pins in flow control mode | UART_DIS_BCLK_CTS_RTS // Disable BCLK, CTS, and RTS pins | UART_NORMAL_RX // UxRX idle stat is '1' | UART_BRGH_SIXTEEN; // 16x baud clock config2 = UART_TX_PIN_LOW // IrDA encoded UxTx idle stat is '0' | UART_RX_ENABLE // Enable UxRx pin | UART_TX_ENABLE // Enable UxTx pin // | UART_INT_TX // Interrupt on trasnfer of each character to TSR | UART_INT_RX_CHAR // Interrupt on every char received | UART_ADR_DETECT_DIS // Disable 9-bit address detect | UART_RX_OVERRUN_CLEAR; // Rx buffer overrun status bit clear OpenUART1(config1, config2, BSP_CLK_FREQ / (16 * UART1_BAUDRATE) - 1); // Configure the settings config = UART_INT_PR3 | UART_INT_SUB_PR0 | UART_RX_INT_EN; ConfigIntUART1(config); }
BOOL xMBPortSerialInit( BYTE ucPORT, UINT32 ulBaudRate, BYTE ucDataBits, eMBParity eParity ) { WORD_VAL wClk; MBRXIF = 0; MBRXIE = 0; MBTXIF = 0; MBTXIE = 0; //Включим передачу на 485 пока временно для отладки //pRE = 0; TRISBbits.TRISB5 = 1; TRISBbits.TRISB15 = 0; //TRISAbits.TRISA7 = 0; // MBBAUDCON = kMBBAUDCON_SETUP; // (((SYS_FREQ/4L+2L*kBAUDRATE)/kBAUDRATE)/16L)-1) //wClk.Val = 207;//(SYS_FREQ/4L+2L*kBAUDRATE)/kBAUDRATE/4L - 1L; // MBBRG = wClk.byte.LB; // MBBRGH = wClk.byte.HB; // MBRCSTA = kMBRCSTA_SETUP; // MBTXSTA = kMBTXSTA_SETUP; _U1RXR = 56; //RP5 = RX _RP55R = 1; //RP15 = TX OpenUART1(UART_EN & UART_IDLE_STOP & UART_IrDA_DISABLE & UART_DIS_WAKE & UART_UEN_00 & UART_DIS_ABAUD & UART_BRGH_FOUR & UART_UXRX_IDLE_ONE & UART_NO_PAR_8BIT & UART_2STOPBITS & UART_DIS_LOOPBACK, UART_INT_TX_BUF_EMPTY & UART_IrDA_POL_INV_ZERO & UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_ADR_DETECT_DIS, BRG) ; ConfigIntUART1(UART_RX_INT_EN & UART_TX_INT_EN & UART_RX_INT_PR1 & UART_TX_INT_PR1); //U1STAbits.UTXINV = 1; //_UARTEN = 0; //_URXINV = 0; _UTXINV = 0; //_UARTEN = 1; // MBTXREG = 0x55; // MBTXREG = 0x55; //pbClk=SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); /*if (ucPORT == 1){ OpenUART1(UART_EN, // Module is ON UART_RX_ENABLE | UART_TX_ENABLE, // Enable TX & RX pbClk/16/ulBaudRate-1); // ConfigIntUART1(UART_INT_PR2 | UART_RX_INT_EN); } else if (ucPORT == 2){ OpenUART2(UART_EN | UART_NO_PAR_8BIT | UART_2STOPBITS, // Module is ON UART_RX_ENABLE | UART_TX_ENABLE, // Enable TX & RX pbClk/16/ulBaudRate-1); // ConfigIntUART2(UART_INT_PR2 | UART_RX_INT_EN); }*/ // INTCONbits.GIE = 1; //INTEnableSystemMultiVectoredInt(); return TRUE; }
UINT16 SetupUART(void) { PPSOutput(OUT_FN_PPS_U1TX, OUT_PIN_PPS_RP37); PPSInput(PPS_U1RX, PPS_RP20); OpenUART1(UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_SIMPLEX & UART_UEN_00 & UART_EN_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_UXRX_IDLE_ONE & UART_BRGH_SIXTEEN & UART_NO_PAR_8BIT & UART_1STOPBIT , UART_INT_TX_BUF_EMPTY & UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_ADR_DETECT_DIS , 23 //BAUD = 9600 ); U1STAbits.UTXINV = 0; //make TX active low //U1STAbits.URXISEL = 0x00; //force interrupt on every 8 bits recieved ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR4 & UART_TX_INT_DIS ); /*This is a copy of UART setup working for TX OpenUART1(UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_SIMPLEX & UART_UEN_00 & UART_EN_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_UXRX_IDLE_ZERO & UART_BRGH_SIXTEEN & UART_NO_PAR_8BIT & UART_1STOPBIT , UART_INT_TX_BUF_EMPTY & UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_INT_RX_BUF_FUL & UART_ADR_DETECT_DIS , 23 //BAUD = 9600 ); U1STAbits.UTXINV = 0; */ return 0; //return success }
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"); }