PIC18FxxK22 code examples

PIC18FxxK22 code examples

Postby vloki » Tue Jul 01, 2014 8:51 am

Last edited by vloki on Fri Jul 29, 2016 8:14 am, edited 16 times 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 » Tue Jul 01, 2014 8:55 am

vloki wrote:- the used hardware (schematic)
Attachments
uCquick_Board_2013_002.pdf
(43.35 KiB) Downloaded 2559 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 » Tue Jul 01, 2014 8:58 am

vloki wrote:- the pin definition header

Based on the used board we created a header with meaningful names for use in the demo projects
Code: Select all
//##############################################################################
//      filename:               uCQ_2013.h
//
//      meaningful pin-names (signals) and macro definitions
//      for demo projects
//
//##############################################################################
//
//      Author:                 V.SchK
//      Company:                HS-Ulm
//
//      Revision:               1.0
//      Date:                   September 2013
//      Assembled using         C18 3.40+
//
//      todo   - add comments ;-)
//              -
//
//##############################################################################/*

#ifndef UCQ_2013_H
#define   UCQ_2013_H

#include <p18cxxx.h>


#define ENCODER_PANASONIC   //

//----------------------------------------------------------internal clock freq.
#define IRCF_16MHZ  0b111       //PIC18FxxK22 devices
#define IRCF_8MHZ   0b110
#define IRCF_4MHZ   0b101
#define IRCF_2MHZ   0b100
#define IRCF_1MHZ   0b011
#define IRCF_500KHZ 0b010
#define IRCF_250KHZ 0b001
#define IRCF_31KHZ  0b000

// ---------------------------------------------------------------pin directions
#define INPUT_PIN           1
#define OUTPUT_PIN          0
#define INPUT_REG           0xFF
#define OUTPUT_REG          0x00

// -----------------------------------------------------------------push buttons
#define BTN_L               PORTBbits.RB2               // left button
#define BTN_L_TRI           TRISBbits.TRISB2
#define BTN_R               PORTBbits.RB4               // right button
#define BTN_R_TRI           TRISBbits.TRISB4

#define BTN_ACTIVATED       0
#define mGET_BTN_L()        (BTN_L == BTN_ACTIVATED)
#define mGET_BTN_R()        (BTN_R == BTN_ACTIVATED)
//#define mGET_BTN_ENC()      mGET_ENC_BTN()

#define WAIT_BTN_CAP()      Nop();Nop();Nop();Nop();   // (FOSC == 4MHz)

// -------------------------------------------------------------------------LEDs
#define LED_1               LATBbits.LATB2
#define LED_1_TRI           TRISBbits.TRISB2
#define LED_2               LATBbits.LATB3
#define LED_2_TRI           TRISBbits.TRISB3
#define LED_3               LATBbits.LATB4
#define LED_3_TRI           TRISBbits.TRISB4
#define LED_4               LATBbits.LATB5
#define LED_4_TRI           TRISBbits.TRISB5

#define LED_ON              0
#define LED_OFF             1
#define mSET_LED_1_ON()     LED_1 = LED_ON
#define mSET_LED_1_OFF()    LED_1 = LED_OFF
#define mTOG_LED_1()        LED_1 = !LED_1
#define mSET_LED_2_ON()     LED_2 = LED_ON
#define mSET_LED_2_OFF()    LED_2 = LED_OFF
#define mTOG_LED_2()        LED_2 = !LED_2
#define mSET_LED_3_ON()     LED_3 = LED_ON
#define mSET_LED_3_OFF()    LED_3 = LED_OFF
#define mTOG_LED_3()        LED_3 = !LED_3
#define mSET_LED_4_ON()     LED_4 = LED_ON
#define mSET_LED_4_OFF()    LED_4 = LED_OFF
#define mTOG_LED_4()        LED_4 = !LED_4

// ---------------------------------------------------------------digital inputs
#define TTL_IN          PORTAbits.RA0
#define TTL_IN_TRI      TRISAbits.TRISA0
#define ST_IN           PORTCbits.RC3
#define ST_IN_TRI       TRISCbits.TRISC3

// ---------------------------------------------------------------rotary encoder
#define ENC_BTN         PORTBbits.RB1
#define ENC_BTN_TRI     TRISBbits.TRISB1
#define mGET_ENC_BTN()  (ENC_BTN == BTN_ACTIVATED)

#define ENC_INT         PORTBbits.INT0
#define ENC_INT_TRI     TRISBbits.TRISB0
#define ENC_DIR         PORTAbits.RA2
#define ENC_DIR_TRI     TRISAbits.TRISA2
//#define ENC_DIR         PORTEbits.RE3
//#define ENC_DIR_TRI     TRISEbits.TRISE3

#define ENC_IR          INTCONbits.INT0IE && INTCONbits.INT0IF
#define mENC_IR_DIS()   INTCONbits.INT0IE = 0
#define mENC_IR_CLR()   INTCONbits.INT0IF = 0
                                //    ___     ___     ___
#ifdef ENCODER_PANASONIC        // __|   |___|   |___|   |__ (toggles)
    #define ENC_DIR_UP          INTCON2bits.INTEDG0
    #define ENC_DIR_DOWN        !INTCON2bits.INTEDG0
    #define mENC_IR_EN()        INTCON2bits.INTEDG0 = !ENC_INT; \
                                INTCONbits.INT0IE = 1
    #define mENC_INT_TOG_EDGE() INTCON2bits.INTEDG0 = !INTCON2bits.INTEDG0
#else
    #define ENC_DIR_UP      0   // __|___|___|___|___|___|__ (pulses)
    #define ENC_DIR_DOWN    1
    #define mENC_IR_EN()    INTCONbits.INT0IE = 1
#endif

// ----------------------------------------------------------analog input (poti)
#define POTI_TRI        LATAbits.LATA0
#define ADC_POTI        ADC_CH0
#define ADC_IR          PIE1bits.ADIE && PIR1bits.ADIF
#define mADC_IR_EN()    PIE1bits.ADIE = 1
#define mADC_IR_DIS()   PIE1bits.ADIE = 0
#define mADC_IR_CLR()   PIR1bits.ADIF = 0

// ----------------------------------------------------------------------speaker
#define SPEAKER         LATCbits.LATC2
#define SPEAKER_TRI     TRISCbits.TRISC2
#define mTOG_SPEAKER()  SPEAKER = !SPEAKER

// ------------------------------------------------------------------------RS232
#define TX_PIN          LATCbits.LATC6
#define RX_PIN          PORTCbits.RC7
#define TX_TRI          TRISCbits.TRISC6
#define RX_TRI          TRISCbits.TRISC7
#define TX_FREE         PIR1bits.TX1IF

// ----------------------------------------------------------time meaurement CCP
#define CAPTR_IR        PIE1bits.CCP1IE && PIR1bits.CCP1IF
#define mCAPTR_IR_EN()  PIE1bits.CCP1IE = 1
#define mCAPTR_IR_DIS() PIE1bits.CCP1IE = 0
#define mCAPTR_IR_CLR() PIR1bits.CCP1IF = 0

// --------------------------------------------------------------------clock CCP
#define SEC_IR          PIE2bits.CCP2IE && PIR2bits.CCP2IF
#define mSEC_IR_EN()    PIE2bits.CCP2IE = 1
#define mSEC_IR_DIS()   PIE2bits.CCP2IE = 0
#define mSEC_IR_CLR()   PIR2bits.CCP2IF = 0

// ------------------------------------------------------------debug help signal
#define dbgSIGNAL       SPEAKER
#define dbgSIGNAL_TRI   SPEAKER_TRI
#define dbgPULSE()      mTOG_SPEAKER();mTOG_SPEAKER()

#endif   /* UCQ_2013_H */
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 » Tue Jul 01, 2014 9:06 am

vloki wrote:- the configuration bits

Code: Select all
//##############################################################################
//       filename:           uC_Config.c
//
//        configuraation bits for demo projects
//
//##############################################################################
//
//      Author:               V.SchK
//      Company:           HS-Ulm
//
//   Revision:           1.0
//    Date:               July. 2012
//        Assembled using         C18 3.40+
//
//      todo   - add comments ;-)
//                -
//
//##############################################################################
#include <p18cxxx.h>

// PIC18F25K22 Configuration Bit Settings
// CONFIG1H
#pragma config FOSC = INTIO7    // Oscillator (LP,XT,HSHP,HSMP,RC,RCIO6,ECHP,
                                //  ECHPIO6,INTIO67,INTIO7,ECMPIO6,ECLP,ECLPIO6)
#pragma config PLLCFG = OFF     // 4X PLL Enable
#pragma config PRICLKEN = ON    // Primary clock enable
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable
#pragma config IESO = OFF       // Internal/External Oscillator Switchover

// CONFIG2L
#ifdef __DEBUG
    #pragma config PWRTEN = OFF // Power-up Timer Enable
#else
    #pragma config PWRTEN = ON
#endif
#pragma config BOREN = OFF      // Brown-out Reset Enable  (OFF,ON,NOSLP,SBODIS)
#pragma config BORV = 190       // Brown Out Reset Volt.(285,250,220,190)[V/100]

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable (OFF,NOSLP,SWON,ON)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC1  // ECCP2 B output mux (PORTB3, PORTC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable
#pragma config CCP3MX = PORTB5  // CCP3 MUX (PORTB5, PORTC6)
#pragma config HFOFST = ON      // HFINTOSC Fast Start-up
#pragma config T3CMX = PORTC0   // Timer3 Clock input mux (PORTB5,PORTC0)
#pragma config P2BMX = PORTB5   // ECCP2 B output mux (PORTB5,PORTC0)
#ifdef __DEBUG
    #pragma config MCLRE = EXTMCLR  // MCLR Pin Enable (MCLR / RE3)
#else
    #pragma config MCLRE = INTMCLR
#endif

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable
#pragma config LVP = OFF        // Single-Supply ICSP Enable
#pragma config XINST = OFF      // Extended Instruction Set Enable

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection Block 0
#pragma config CP1 = OFF
//#pragma config CP2 = OFF
//#pragma config CP3 = OFF

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection
#pragma config CPD = OFF        // Data EEPROM Code Protection

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection Block 0
#pragma config WRT1 = OFF
//#pragma config WRT2 = OFF
//#pragma config WRT3 = OFF

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection
#pragma config WRTB = OFF       // Boot Block Write Protection
#pragma config WRTD = OFF       // Data EEPROM Write Protection

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0
#pragma config EBTR1 = OFF
//#pragma config EBTR2 = OFF
//#pragma config EBTR3 = OFF

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection
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 » Tue Jul 01, 2014 9:11 am

vloki wrote:- main and basic initialization
Code: Select all
//##############################################################################
//       filename:           uC-Quick_demo_main.c
//
//        main file for demo projects
//
//##############################################################################
//
//      Author:               V.SchK
//      Company:           HS-Ulm
//
//   Revision:           1.0
//    Date:               September. 2012
//        Assembled using         C18 3.40+
//
//      todo   - add comments ;-)
//                -
//
//##############################################################################

#include "uCQ_2013.h"
#include "demo_functions.h"


//--- P R I V A T E   P R O T O T Y P E S --------------------------------------
//void __init();


//##############################################################################
// Function:        void __main(void)
//         called from the c18 startup code
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:        Es kann jeweils nur eine Funktion in main ausgeführt werden
//                  da die Demofunktionen praktisch komplett eigenständige Pro-
//                  gramme mit eigener while(1){...} Schleife sind
//##############################################################################
void main()
{
#ifdef __XC8        // XC8 compiler IDE MAKRO
    __init();       //   C18 calls this function in startup code
#endif

//    while (1) {...}

        TestLEDs();             // LEDs in Debug-Mode (single step) ON/OFF
//        ButtonLEDs();           // ENC_BTN switch on LED_1/2/3/4
//        ButtonsChangeLEDs();    // Buttons toggle the LEDs
//        TTL_ST_toLED();         // determining of logic levels
//        Blink_Counter();        // timing based on counter loop
//        Blink_Timer();          // timing using a timer module
//        Blink_Interrupt(); TODO     // timing with timer and CCP modules (IR)
//        EncoderLEDs();          // Encoder "rotates" LEDs
//        EncoderLCD();           // Encoder -> LCD
//        AnalogLEDs();           // ADC (poti) display at LEDs
//        AnalogLCD();            // ADC (poti) -> LCD
//        Analog_Out();           // Analog signal out (buttons)
//        Signal_Out();           // Analog signal out (array)
//        Sound_PWM();            // sound out at speaker using PWM module
//        RS232_TX();             // Send serial data
//        RS232_RX();             // Receive serial data
//        MiniRS232();            // Serial communication (both Directions)
//        USrangeFinder(); TODO       //
//        TimeMeasure();          // measure time betw. two pos. edges [us]
//        FreqMess(); TODO        // measure frequency (1s)
//        ClockMenu();            // LCD Clock with menu for setting

//        BoardTest();            // function test (all) components

// Add On Boards ///////////////////////////////////////////////////////////////

//        Test_MPU6000();
//        Test_GPS();
//        TestRadar();
}

//##############################################################################
// Function:        void __init(void)
//                      called from the c18 startup code
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
void __init()
{
    OSCCONbits.IRCF = IRCF_4MHZ;
//    OSCCONbits.IRCF = IRCF_16MHZ;
//    OSCTUNEbits.PLLEN = 1;    // ->64MHz

    ANSELA = 0x00;  // all pins digital IO
    ANSELB = 0x00;
    ANSELC = 0x00;
}
Last edited by vloki on Tue Jul 01, 2014 9:19 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 » Tue Jul 01, 2014 9:11 am

vloki wrote:- blinking LEDs using single step in debug mode

Code: Select all
void TestLEDs(void)
{
//--------------------------------------------------------------------- __init()
    LATBbits.LATB2 = 1;     // LED1 off
    LATBbits.LATB3 = 1;     // LED2 off
    LATBbits.LATB4 = 1;     // LED3 off
    LATBbits.LATB5 = 1;     // LED4 off

    TRISBbits.TRISB2 = 0;   // pin for LED1 -> output
    TRISBbits.TRISB3 = 0;   // pin for LED2 -> output
    TRISBbits.TRISB4 = 0;   // pin for LED3 -> output
    TRISBbits.TRISB5 = 0;   // pin for LED4 -> output

//    LATB  |= 0b00111100;    // binary
//    TRISB &= 0b11000011;    // hexadezimal
//    LATB  |= 0x3C;
//    TRISB &= 0xC3;

//    LED_1_TRI = OUTPUT_PIN;
//    LED_2_TRI = OUTPUT_PIN;
//    LED_3_TRI = OUTPUT_PIN;
//    LED_4_TRI = OUTPUT_PIN;
//----------------------------------------------------------------------- main()
    while(1){
        LATBbits.LATB2 = 0; // LED1 on          <- place break-point here
        LATBbits.LATB3 = 0; // LED2 on          then single step
        LATBbits.LATB4 = 0; // LED3 on
        LATBbits.LATB5 = 0; // LED4 on

        LATB = 0b11111111;  // all off binary
        LATB = 0b00000000;  // all on
        LATB = 0xFF;        //   hex
        LATB = 0x00;
        LATB |= 0b00111100; //   mask
        LATB &= 0b11000011;

        LATB |= 0b00000100; // LED1 off
        LATB |= 0b00001000; // LED2 off
        LATB |= 0b00010000; // LED3 off
        LATB |= 0b00100000; // LED4 off

        LATB &= 0b11111011; // LED1 on

        mSET_LED_1_OFF();
        mSET_LED_1_ON();
        mSET_LED_1_OFF();
    }
}
Last edited by vloki on Tue Jul 01, 2014 9:20 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 » Tue Jul 01, 2014 9:12 am

vloki wrote:- reading a button and light up some LEDs

Code: Select all
void ButtonLEDs(void)
{
//--------------------------------------------------------------------- __init()
    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;

//----------------------------------------------------------------------- main()
    while(1){
        if(mGET_ENC_BTN()){
            mSET_LED_1_ON();
            mSET_LED_2_ON();
            mSET_LED_3_ON();
            mSET_LED_4_ON();
        }
        else{
            mSET_LED_1_OFF();
            mSET_LED_2_OFF();
            mSET_LED_3_OFF();
            mSET_LED_4_OFF();
        }
    }
}
Last edited by vloki on Tue Jul 01, 2014 9:21 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 » Tue Jul 01, 2014 9:13 am

vloki wrote:- logic levels for TTL and schmitt-trigger inputs

Code: Select all
void TTL_ST_toLED(void)
{
//--------------------------------------------------------------------- __init()
    TTL_IN_TRI = INPUT_PIN;
    ST_IN_TRI = INPUT_PIN;      // jumper JP9 (poti->RC3) !!!
    LED_1_TRI = OUTPUT_PIN;
    LED_2_TRI = OUTPUT_PIN;
    mSET_LED_3_OFF();
    LED_3_TRI = OUTPUT_PIN;
    mSET_LED_4_OFF();
    LED_4_TRI = OUTPUT_PIN;

//----------------------------------------------------------------------- main()
    while(1){               // VDD = 5V
        LED_1 = !TTL_IN;    // ON/OFF ~1.3V
        LED_2 = !ST_IN;     // ON ~2.9V    OFF ~1.4V
    }
}
Last edited by vloki on Tue Jul 01, 2014 9:24 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 » Tue Jul 01, 2014 9:13 am

vloki wrote:- blink LED using a counter loop

Code: Select all
void Blink_Counter(void)
{
    unsigned short time;
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_31KHZ;              // 31.25kHz clock
    ENC_BTN_TRI = INPUT_PIN;
    LED_2_TRI = OUTPUT_PIN; // LED button
    LED_3_TRI = OUTPUT_PIN; // LED blink
    mSET_LED_3_ON();

//----------------------------------------------------------------------- main()
    while (1) {
        if(mGET_ENC_BTN())  mSET_LED_2_ON();
        else                mSET_LED_2_OFF();

//        for (time = 0; time < 3906; time++) {;} // wait 1/2 sec. ???
        for (time = 0; time < 300; time++) {;}  // wait 1/2 sec. (better ;-)
          mTOG_LED_3();                         //   then toggle LED
    }
}
Last edited by vloki on Tue Jul 01, 2014 9:24 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 » Tue Jul 01, 2014 9:14 am

vloki wrote:- blink LED using a timer flag.

Code: Select all
void Blink_Timer(void)
{
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_4MHZ;
    ENC_BTN_TRI = INPUT_PIN;
    LED_2_TRI = OUTPUT_PIN;     // LED button
    LED_3_TRI = OUTPUT_PIN;     // LED blink
    mSET_LED_3_ON();
                                // TIMER_0 setup
    T0CON = 0x82;               // TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0

//----------------------------------------------------------------------- main()
    while (1) {
        if(mGET_ENC_BTN())  mSET_LED_2_ON();
        else                mSET_LED_2_OFF();

        if(INTCONbits.TMR0IF){      // if timer 0 overflow
            mTOG_LED_3();           //   then toggle LED
            INTCONbits.TMR0IF = 0;  //   and clear flag
        }
    }
}
Last edited by vloki on Tue Jul 01, 2014 9:25 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

Next

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 6 guests

cron