//Soros aszinkron kommunikáci és átviteli sebesség beállítása
void init_SerialKommunication(unsigned int BaudRate)
{
TXSTAbits.BRGH = 1; // high speed
BAUDCONbits.BRG16 = 1; //
switch (BaudRate)
{
case 300: SPBRGH = 0x9C; //300bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x3F; //Error[%]=0%
break;
case 1200: SPBRGH = 0x27; //1200bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x0F; //Error[%]=0%
break;
case 2400: SPBRGH = 0x13; //2400bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x87; //Error[%]=0%
break;
case 4800: SPBRGH = 0x09; //4800bps 20MHz Osc, FOSC=48MHz
SPBRG = 0xC3; //Error[%]=0%
break;
case 9600: SPBRGH = 0x04; //9600bps 20MHz Osc, FOSC=48MHz
SPBRG = 0xE1; //Error[%]=0%
break;
case 19200: SPBRGH = 0x02; //19200bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x27; //Error[%]=0%
break;
case 38400: SPBRGH = 0x01; //38400bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x39; //Error[%]=-0,5%
break;
case 57600: SPBRGH = 0x00; //57600bps 20MHz Osc, FOSC=48MHz
SPBRG = 0xD0; //Error[%]=0,16%
break;
case 115200: SPBRGH = 0x00; //115200bps 20MHz Osc, FOSC=48MHz
SPBRG = 0x67; //Error[%]=0,16%
break;
default: ;
}
TXSTAbits.SYNC = 0; // asynchronous mode
RCSTAbits.SPEN = 1; // enable serial port - configures RX/DT and TX/CK pins as serial port pins
RCSTAbits.RX9 = 0; // 8-bit receiver
TXSTAbits.TX9 = 0; // 8-bit transmission
RCSTAbits.CREN = 1; // Enables receiver
PIE1bits.RCIE = 1; // Enables the EUSART receive interrupt
TXSTAbits.TXEN = 0; // reset transmitter
TXSTAbits.TXEN = 1; // transmit enabledTXSTAbits.SYNC=0; // asynchronous mode
RCSTAbits.SPEN = 1; // enable serial port - configures RX/DT and TX/CK pins as serial port pins
TXSTAbits.TX9 = 0; // 8-bit transmission
TXSTAbits.TXEN = 0; // reset transmitter
TXSTAbits.TXEN = 1; // transmit enabled
}