PIC18FxxK22 code examples

Re: PIC18FxxK22 code examples

Postby vloki » Sun Jul 06, 2014 11:09 am

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;
            }
        }
    }
}
Last edited by vloki on Sun Jul 06, 2014 11:13 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Sun Jul 06, 2014 11:10 am

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){
}
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby Miljkoyu » Sun Oct 05, 2014 12:13 pm

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.
Miljkoyu
 
Posts: 1
Joined: Sun Oct 05, 2014 11:02 am

Re: PIC18FxxK22 code examples

Postby vloki » Thu Oct 16, 2014 10:53 am

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 ;-)
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Thu Nov 13, 2014 11:30 am

A complete MPLABX project with configurations for C18 / XC8 compiler
Attachments
uCQ_HS-ULM_Base_20141113.zip
(21.8 KiB) Downloaded 1762 times
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Fri Nov 21, 2014 8:58 am

Placeholder for USART error handling
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Fri Nov 21, 2014 8:58 am

Placeholder for USART receiver interrupt mode
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Fri Nov 21, 2014 9:00 am

Placeholder for receiver with protocol and additional buffer
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby vloki » Fri Nov 21, 2014 9:02 am

Placeholder for transmit with protocol and buffer
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18FxxK22 code examples

Postby jpnbino » Tue Jan 27, 2015 8:18 pm

Thank you a lot for the code. Very useful for me.
;)
jpnbino
 
Posts: 1
Joined: Mon Dec 01, 2014 4:29 am

Previous

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 7 guests

cron