Page 3 of 3

Re: PIC18FxxK22 code examples

PostPosted: Sun Jul 06, 2014 11:09 am
by vloki
vloki wrote:- Receiving serial data USART

Code: Select all
void RS232_RX(void)
{
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_4MHZ;
    LED_1_TRI = LED_2_TRI = LED_3_TRI = LED_4_TRI = OUTPUT_PIN;
    TX_TRI = RX_TRI = INPUT_PIN;
    SPBRG1 = 16;                    // 57600 for 4MHz
    SPBRGH1 = 0;
    TXSTA1bits.BRGH = 1;
    BAUDCON1bits.BRG16 = 1;
    RCSTA1bits.SPEN = 1;
    RCSTA1bits.CREN1 = 1;
//----------------------------------------------------------------------- main()
    while(1){
        unsigned char command;
        if(PIR1bits.RC1IF){
            command = RCREG1;
            switch(command){
                case '0': mSET_LED_1_OFF(); mSET_LED_2_OFF();
                          mSET_LED_3_OFF(); mSET_LED_4_OFF(); break;
                case '1': mSET_LED_1_ON(); break;
                case '2': mSET_LED_2_ON(); break;
                case '3': mSET_LED_3_ON(); break;
                case '4': mSET_LED_4_ON(); break;
                default: break;
            }
        }
    }
}

Re: PIC18FxxK22 code examples

PostPosted: Sun Jul 06, 2014 11:10 am
by vloki
vloki wrote:- Bidirectional USART communication

Code: Select all
void MiniRS232(void)
{
    unsigned char command;
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_4MHZ;

    LED_1_TRI = OUTPUT_PIN;
    LED_2_TRI = OUTPUT_PIN;
    LED_3_TRI = OUTPUT_PIN;
    LED_4_TRI = OUTPUT_PIN;
    ENC_BTN_TRI = INPUT_PIN;

#define FOSC        4000000 // in a global header
#define BAUDRATE    19200   // ""
//#define SPBRG_VAL (((FOSC/BAUDRATE)+32)/64)-1   // BRG16=0, BGH=0 !
//#define SPBRG_VAL (((FOSC/BAUDRATE)+8)/16)-1    // BRG16=0, BGH=1 !
#define SPBRG_VAL (((FOSC/BAUDRATE)+2)/4)-1     // BRG16=1, BGH=1 !

    baud1USART(BAUD_IDLE_RX_PIN_STATE_HIGH &
                 BAUD_IDLE_TX_PIN_STATE_HIGH &
                 BAUD_16_BIT_RATE &
                 BAUD_WAKEUP_OFF &
                 BAUD_AUTO_OFF);

    Open1USART( USART_TX_INT_OFF & USART_RX_INT_OFF &
                USART_ASYNCH_MODE & USART_EIGHT_BIT &
                USART_CONT_RX &
                USART_BRGH_HIGH & USART_ADDEN_OFF,
                SPBRG_VAL );
//----------------------------------------------------------------------- main()
    while(1){
        if(DataRdy1USART()){
            command = RCREG1;
            switch(command){
                case '0': mSET_LED_1_OFF(); mSET_LED_2_OFF();
                          mSET_LED_3_OFF(); mSET_LED_4_OFF(); break;
                case '1': mSET_LED_1_OFF(); break;
                case '2': mSET_LED_2_OFF(); break;
                case '3': mSET_LED_3_OFF(); break;
                case '4': mSET_LED_4_OFF(); break;
                case '5': mSET_LED_1_ON(); break;
                case '6': mSET_LED_2_ON(); break;
                case '7': mSET_LED_3_ON(); break;
                case '8': mSET_LED_4_ON(); break;
                case '9': mSET_LED_1_ON(); mSET_LED_2_ON();
                          mSET_LED_3_ON(); mSET_LED_4_ON(); break;
                case '?':
                    BTN_L_TRI = BTN_R_TRI = INPUT_PIN;
                    WAIT_BTN_CAP();
                    command = mGET_BTN_L();
                    command += 2 * mGET_ENC_BTN();
                    command += 4 * mGET_BTN_R();
                    if(command < 10)command += '0';
                    else command += 'A' - 10;
                    TXREG1 = command;
                    LED_1_TRI = LED_2_TRI = LED_3_TRI = LED_4_TRI = OUTPUT_PIN;
                    break;
                default: break;
            }//switch(command){
        }//if(DataRdy1USART()){
    }//while(1){
}

Re: PIC18FxxK22 code examples

PostPosted: Sun Oct 05, 2014 12:13 pm
by Miljkoyu
Hi, this is the best post about PIC18FxxK22 for beginners. Thank you for that.
Do you translate your "Tutorial in German language" in English? It would be very useful.

Re: PIC18FxxK22 code examples

PostPosted: Thu Oct 16, 2014 10:53 am
by vloki
Miljkoyu wrote:Hi, this is the best post about PIC18FxxK22 for beginners. Thank you for that.
Do you translate your "Tutorial in German language" in English? It would be very useful.

Sorry, no progress here.
No one seemed to be interested ;-)

Re: PIC18FxxK22 code examples

PostPosted: Thu Nov 13, 2014 11:30 am
by vloki
A complete MPLABX project with configurations for C18 / XC8 compiler

Re: PIC18FxxK22 code examples

PostPosted: Fri Nov 21, 2014 8:58 am
by vloki
Placeholder for USART error handling

Re: PIC18FxxK22 code examples

PostPosted: Fri Nov 21, 2014 8:58 am
by vloki
Placeholder for USART receiver interrupt mode

Re: PIC18FxxK22 code examples

PostPosted: Fri Nov 21, 2014 9:00 am
by vloki
Placeholder for receiver with protocol and additional buffer

Re: PIC18FxxK22 code examples

PostPosted: Fri Nov 21, 2014 9:02 am
by vloki
Placeholder for transmit with protocol and buffer

Re: PIC18FxxK22 code examples

PostPosted: Tue Jan 27, 2015 8:18 pm
by jpnbino
Thank you a lot for the code. Very useful for me.
;)