PIC18FxxK22 code examples

Re: PIC18FxxK22 code examples

Postby vloki » Tue Jul 01, 2014 9:14 am

vloki wrote:- blink LED using timer and CCP interrupt
vloki
Verified identity
 
Posts: 166
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:15 am

vloki wrote:- encoder interrupt to rotate LEDs

Code: Select all
void EncoderLEDs(void)
{
    unsigned char dummy = 0b00000100;    // LEDs RB2..RB5

//--------------------------------------------------------------------- __init()
    LED_1_TRI = OUTPUT_PIN;
    LED_2_TRI = OUTPUT_PIN;
    LED_3_TRI = OUTPUT_PIN;
    LED_4_TRI = OUTPUT_PIN;
   
    ENC_INT_TRI = INPUT_PIN;
    ENC_DIR_TRI = INPUT_PIN;

    mENC_IR_CLR();
    mENC_IR_EN();

    flags.all = 0;

    INTCONbits.GIE = 1;
//----------------------------------------------------------------------- main()
    while(1){
        if(flags.encUp){
            dummy = dummy << 1;
            if(dummy > 0b00100000) dummy = 0b00000100;
            LATB = ~dummy;
            flags.encUp = 0;
        }
        if(flags.encDown){
            dummy = dummy >> 1;
            if(dummy < 0b00000100 ) dummy = 0b00100000;
            LATB = ~dummy;
            flags.encDown = 0;
        }
    }
}

bla bla ...

<edit>two different versions for C18/XC8 interrupt code

The interrupt handling for C18 and XC8 compiler is a bit different.

First the C18 version, which needs to define interrupt vectors and #pragma:
Code: Select all
/** I N C L U D E S ***********************************************************/
#include "uCQuick/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 high_isr(void);
//void low_isr(void);

/** I N T E R R U P T  V E C T O R S ************* C18 compiler only **********/

#pragma interrupt high_isr
#pragma code high_vector=0x08

#ifdef USE_IR_PRIORITIES
   void interrupt_at_high_vector(void)
   {
     _asm goto high_isr _endasm
   }
   #pragma interruptlow low_isr
   #pragma code low_vector=0x18
   void interrupt_at_low_vector(void)
   {
     _asm goto low_isr _endasm
   }
   #pragma code
#endif

/** D E C L A R A T I O N S ***************************************************/
//##############################################################################
// Function:        void high_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
void high_isr(void)
{
    if (ENC_IR){
#ifdef ENCODER_PANASONIC
        mENC_INT_TOG_EDGE();
#endif
        if(ENC_DIR == ENC_DIR_UP)
            flags.encUp = 1;
        else
            flags.encDown = 1;
        mENC_IR_CLR();
        return;
    }

    while(1){;}                         // (detect unexpected IR sources)
}

//##############################################################################
// Function:        void low_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
#ifdef USE_IR_PRIORITIES
    void low_isr(void)
    {
        while(1){;}                         // (detect unexpected IR sources)
    }
#endif


XC8 version with interrupt / interrupt low_priority keywords
Code: Select all
//##############################################################################
// Function:        void high_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
void interrupt high_isr(void)
{
    if (ENC_IR){
#ifdef ENCODER_PANASONIC
        mENC_INT_TOG_EDGE();
#endif
        if(ENC_DIR == ENC_DIR_UP)
            flags.encUp = 1;
        else
            flags.encDown = 1;
        mENC_IR_CLR();
        return;
    }

    while(1){;}                         // (detect unexpected IR sources)
}

//##############################################################################
// Function:        void low_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
#ifdef USE_IR_PRIORITIES
    void interrupt low_priority low_isr(void)
    {
        while(1){;}                         // (detect unexpected IR sources)
    }
#endif
Last edited by vloki on Fri Dec 05, 2014 8:26 am, edited 3 times in total.
vloki
Verified identity
 
Posts: 166
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:15 am

vloki wrote:- encoder interrupt modifies variable that is displayed at LCD

interrupt code is like above ;-)
Code: Select all
void EncoderLCD(void)
{
    short value = 0;

//--------------------------------------------------------------------- __init()
    LCD_Init();
    LCD_ConstTextOut(0,0,"uC-Quick");
    LCD_ConstTextOut(1,0," HS-Ulm ");

    ENC_INT_TRI = INPUT_PIN;    // encoder setup
    ENC_DIR_TRI = INPUT_PIN;

    mENC_IR_CLR();
    mENC_IR_EN();

    flags.all = 0;

    INTCONbits.GIE = 1;
//----------------------------------------------------------------------- main()
    while(1){
        if(flags.encUp){
            value++;
            LCD_ConstTextOut(1,0,"        ");   // delete old value
            LCD_ValueOut(1,1,value);
            flags.encUp = 0;
        }
        if(flags.encDown){
            value--;
            LCD_ConstTextOut(1,0,"        ");   // delete old value
            LCD_ValueOut(1,1,value);
            flags.encDown = 0;
        }
    }
}


Code: Select all
//##############################################################################
//    filename:        lcd_config.h
//##############################################################################
//    configuration file for LCD library (pins, voltage, ...)
//##############################################################################
//
//      Author:               V.SchK
//      Company:           HS-Ulm
//
//      Revision:                 1.0
//      Date:               June. 2012
//      Assembled using       C18 3.40+
//
//
//##############################################################################

#include <delays.h>

#ifndef _LCD_CONFIG_H
#define _LCD_CONFIG_H

#define LCD_TIMEOUT 100

#define LCD_DELAY_5MS() Delay10KTCYx(1) // 1tcy = 1us (FOSC 4.000.000)
#define LCD_DELAY_1US() Nop()//;Nop();Nop();Nop();
#warning "be sure to define LCD-Delays properly !!!"

#warning "LCD-pinning for uC-Quick board !!!"

#define   LCD_E      LATCbits.LATC1
#define   LCD_E_DIR        TRISCbits.TRISC1
#define   LCD_RW      LATCbits.LATC0
#define   LCD_RW_DIR   TRISCbits.TRISC0
#define   LCD_RS      LATAbits.LATA5
#define   LCD_RS_DIR   TRISAbits.TRISA5

#define   LCD_D4_IN        PORTBbits.RB2
#define   LCD_D5_IN           PORTBbits.RB3
#define   LCD_D6_IN           PORTBbits.RB4
#define   LCD_D7_IN           PORTBbits.RB5
#define   LCD_D4_OUT   LATBbits.LATB2
#define   LCD_D5_OUT   LATBbits.LATB3
#define   LCD_D6_OUT   LATBbits.LATB4
#define   LCD_D7_OUT   LATBbits.LATB5
#define   LCD_D4_DIR   TRISBbits.TRISB2
#define   LCD_D5_DIR   TRISBbits.TRISB3
#define   LCD_D6_DIR   TRISBbits.TRISB4
#define   LCD_D7_DIR   TRISBbits.TRISB5

#endif //_LCD_CONFIG_H
vloki
Verified identity
 
Posts: 166
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 Jul 03, 2014 8:44 am

vloki wrote:- the LCD module source code
Attachments
LCD_lib_busy.c
(7.63 KiB) Downloaded 197 times
lcd_config_template.h
(5.7 KiB) Downloaded 173 times
LCD_lib_busy.h
(6.99 KiB) Downloaded 159 times
Last edited by vloki on Thu Jul 03, 2014 8:46 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 166
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 Jul 03, 2014 8:45 am

vloki wrote:- basic ADC display result with 4 LEDs

Code: Select all
void AnalogLEDs(void)
{
//--------------------------------------------------------------------- __init()
    LED_1_TRI = OUTPUT_PIN;
    LED_2_TRI = OUTPUT_PIN;
    LED_3_TRI = OUTPUT_PIN;
    LED_4_TRI = OUTPUT_PIN;

    POTI_TRI = INPUT_PIN;
    ANSELAbits.ANSA0 = 1;
    OpenADC(ADC_FOSC_8 & ADC_LEFT_JUST & ADC_12_TAD,
            ADC_POTI & ADC_INT_OFF,
            ADC_TRIG_CCP5 & ADC_REF_VDD_VDD & ADC_REF_VDD_VSS);
//----------------------------------------------------------------------- main()
    while(1){
        ADCON0bits.GO = 1;
        while(ADCON0bits.NOT_DONE){;}
        LATB = ~(ADRESH >> 2);
    }
}
vloki
Verified identity
 
Posts: 166
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 Jul 03, 2014 8:52 am

vloki wrote:- ADC using special event trigger - displayed at LCD

Code: Select all
near union DemoFlags flags;
near unsigned short adc_value;

void AnalogLCD(void)
{
//--------------------------------------------------------------------- __init()
    SPEAKER_TRI = OUTPUT_PIN;

    ANSELAbits.ANSA0 = 1;
    LATAbits.LATA0 = INPUT_PIN;
    OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD,
            ADC_POTI & ADC_INT_ON,
            ADC_TRIG_CCP5 & ADC_REF_VDD_VDD & ADC_REF_VDD_VSS);

    OpenTimer5(TIMER_INT_OFF & T5_16BIT_RW & T5_SOURCE_FOSC_4 &
               T5_PS_1_2 & T5_OSC1EN_OFF & T5_SYNC_EXT_OFF,
               TIMER_GATE_OFF);

    CCPTMRS1bits.C5TSEL  = 2;   // timer <-> ccp module (CCP5 / TMR5)
    CCPR5 = 50000;              // Fosc/4 / prescaler / Fadc = 1MHz /2 /10Hz
    CCP5CONbits.CCP5M = 0b1011; // Compare Mode with Special Event Trigger

    flags.all = 0;

    LCD_Init();
    LCD_ConstTextOut(0,0,"uC-Quick");
    LCD_ConstTextOut(1,0," HS-Ulm ");

    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
//----------------------------------------------------------------------- main()
    while(1){
        if(flags.newADC){
            LCD_ConstTextOut(1,0,"        ");   // delete old value
            LCD_ValueOut(1,1,ADRES);
            flags.newADC = 0;
        }
    }
}


<edit> c18 / xc8 usable interrupt code ...

Code: Select all
//##############################################################################
//    filename:        Quick_intr.c
//##############################################################################
//    interrupt functions for uCQuick demo project
//##############################################################################
//
//      Author:               V.SchK
//      Company:           HS-Ulm
//
//      Revision:                   2.0 (XC8 compatibility)
//      Date:                      November 2014
//      Assembled using      MPLAB X  1.4 / C18 3.40+ / XC8 1.32+
//
//       todo   - add comments ;-)
//                -
//
//##############################################################################

/** I N C L U D E S ***********************************************************/
#include "uCQuick/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 high_isr(void);
//void low_isr(void);

/** I N T E R R U P T  V E C T O R S ************* C18 compiler only **********/

#ifndef __XC8        // not XC8 compiler -->> C18 compiler

    #pragma interrupt high_isr
    #define interrupt                           // remove keyword
    #pragma code high_vector=0x08

    #ifdef USE_IR_PRIORITIES
        void interrupt_at_high_vector(void)
        {
          _asm goto high_isr _endasm
        }
        #define low_priority                    // remove keyword
        #pragma interruptlow low_isr
        #pragma code low_vector=0x18
        void interrupt_at_low_vector(void)
        {
          _asm goto low_isr _endasm
        }
        #pragma code
    #endif
#endif

/** D E C L A R A T I O N S ***************************************************/
//##############################################################################
// Function:        void high_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
void interrupt high_isr(void)
{
     if (ADC_IR){
        if (adc_value != ADRES){
            adc_value = ADRES;
            flags.newADC = 1;
        }
        mADC_IR_CLR();
        return;
    }
    while(1){;}                         // (detect unexpected IR sources)
}

//##############################################################################
// Function:        void low_isr(void)
// PreCondition:    None
// Input:
// Output:
// Side Effects:
// Overview:
//##############################################################################
#ifdef USE_IR_PRIORITIES
    void interrupt low_priority low_isr(void)
    {
        while(1){;}                         // (detect unexpected IR sources)
    }
#endif
Last edited by vloki on Fri Dec 05, 2014 8:31 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 166
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:07 am

vloki wrote:- Analog output using DAC module ...[/url]
Code: Select all
void Analog_Out(void)
{
    unsigned char dac_value = 0;

//--------------------------------------------------------------------- __init()
    BTN_L_TRI = INPUT_PIN;
    BTN_R_TRI = INPUT_PIN;
    ENC_BTN_TRI = INPUT_PIN;

    VREFCON0bits.FVRS = 3;      // 4.096 V
    VREFCON0bits.FVREN = 1;
    VREFCON1bits.DACPSS = 2;    // FVR
    VREFCON1bits.DACNSS = 0;    // VSS
    VREFCON1bits.DACOE = 1;
    VREFCON1bits.DACEN = 1;
    TRISAbits.TRISA2 = INPUT_PIN;

//----------------------------------------------------------------------- main()
    while(1){
        dac_value = 0;
        if(mGET_BTN_L()) dac_value +=  4;
        if(mGET_BTN_R()) dac_value +=  8;
        if(mGET_ENC_BTN()) dac_value += 16;

        VREFCON2 = dac_value;
    }
}
Last edited by vloki on Sun Jul 06, 2014 11:33 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 166
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:08 am

vloki wrote:- Generating primitive analog signal output

Code: Select all
void Signal_Out(void){

    unsigned char signal_idx = 0;
    unsigned char signal_data[32] = {6, 7, 8, 7, 6, 6, 6, 4,22,31,
                                     0, 3, 6, 6, 8, 9,11,12,11, 8,
                                     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};

//--------------------------------------------------------------------- __init()
    VREFCON0bits.FVRS = 3;      // 4.096 V
    VREFCON0bits.FVREN = 1;
    VREFCON1bits.DACPSS = 2;    // FVR
    VREFCON1bits.DACNSS = 0;    // VSS
    VREFCON1bits.DACOE = 1;
    VREFCON1bits.DACEN = 1;
    TRISAbits.TRISA2 = INPUT_PIN;

//----------------------------------------------------------------------- main()
    while(1){
        VREFCON2 = signal_data[signal_idx];
        if(++signal_idx >= 32) signal_idx = 0;
    }
}
Last edited by vloki on Sun Jul 06, 2014 11:32 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 166
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:08 am

vloki wrote:- Generate sound output via PWM
Code: Select all
void Sound_PWM(void){   //PWM   ###TODO -> Lautstaerke !
    unsigned short frequency = 400;

//--------------------------------------------------------------------- __init()
    LCD_Init();
    LCD_ConstTextOut(0,0,"uC-Quick");
    LCD_ConstTextOut(1,0," Sound ");

    ENC_INT_TRI = INPUT_PIN;    // encoder setup
    ENC_DIR_TRI = INPUT_PIN;

    mENC_IR_CLR();
    mENC_IR_EN();

    CCP1CONbits.CCP1M = 0b1100;                             // PWM mode
    CCP1CONbits.P1M = 0;                                    // single
    CCPTMRS0bits.C1TSEL = 0;                                // CCP1-TMR2
    OpenTimer2(TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_1);
//    OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
    PR2 = 62500 / frequency;                                // 1MHz:16
    CCPR1L = PR2 >> 1;
    SPEAKER_TRI = OUTPUT_PIN;

    flags.all = 0;

    INTCONbits.GIE = 1;
//----------------------------------------------------------------------- main()
    while(1){
        if(flags.encUp){
            if(frequency < 800) {frequency = frequency + 10;}
              else {frequency = frequency + 100;}
            if(frequency > 15600){frequency = 15600;}
            PR2 = (62500 / frequency) -1;             // 1MHz : 16
            CCPR1L = PR2 >> 1;
            LCD_ConstTextOut(1,0,"      ");      // delete old value
            LCD_ValueOut(1,1,frequency);
            flags.encUp = 0;
        }
        if(flags.encDown){
            if(frequency < 800) {frequency = frequency - 10;}
              else {frequency = frequency - 100;}
            if(frequency < 250) {frequency = 250;}
            PR2 = (62500 / frequency)-1;              // 1MHz : 16
            CCPR1L = PR2 >> 1;
            LCD_ConstTextOut(1,0,"      ");      // delete old value
            LCD_ValueOut(1,1,frequency);
            flags.encDown = 0;
        }
    }
}
Last edited by vloki on Sun Jul 06, 2014 11:35 am, edited 1 time in total.
vloki
Verified identity
 
Posts: 166
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:09 am

vloki wrote:- Sending serial data USART


This code bla bla ...
Code: Select all
void RS232_TX()
{
    unsigned char character = '0';
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_4MHZ;
    TX_TRI = RX_TRI = INPUT_PIN;
    SPBRG1 = 16;                    // 57600 at 4MHz
    SPBRGH1 = 0;
    TXSTA1bits.BRGH = 1;
    BAUDCON1bits.BRG16 = 1;
    RCSTA1bits.SPEN = 1;
    TXSTAbits.TXEN = 1;
    ENC_BTN_TRI = INPUT_PIN;
//----------------------------------------------------------------------- main()
    while(1){
        if(mGET_ENC_BTN()){           // if button is pressed
            if(++character > 'z')
                character = '0';
            if(TX_FREE)             // if() <-> while() ???
                TXREG1 = character;
            while(mGET_ENC_BTN()){;}  // ? why we need this line ???
        }
    }
}

<edit>in pin (project) definition header
Code: Select all
#define IRCF_4MHZ   0b101
...
// ------------------------------------------------------------------------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
...
#define BTN_ACTIVATED       0
...
#define ENC_BTN         PORTBbits.RB1
#define ENC_BTN_TRI     TRISBbits.TRISB1
#define mGET_ENC_BTN()  (ENC_BTN == BTN_ACTIVATED)
Last edited by vloki on Fri Aug 29, 2014 12:55 pm, edited 3 times in total.
vloki
Verified identity
 
Posts: 166
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

PreviousNext

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

cron