Re: PIC18FxxK22 code examples
Posted: Tue Jul 01, 2014 9:14 am
vloki wrote:- blink LED using timer and CCP interrupt
An unofficial support site for PIC users. Note: This site is not approved or endorsed by Microchip Inc.
http://picforum.ric323.com/
vloki wrote:- blink LED using timer and CCP interrupt
vloki wrote:- encoder interrupt to rotate LEDs
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;
}
}
}
/** 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
//##############################################################################
// 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
vloki wrote:- encoder interrupt modifies variable that is displayed at LCD
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;
}
}
}
//##############################################################################
// 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 wrote:- the LCD module source code
vloki wrote:- basic ADC display result with 4 LEDs
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 wrote:- ADC using special event trigger - displayed at LCD
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;
}
}
}
//##############################################################################
// 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
vloki wrote:- Analog output using DAC module ...[/url]
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;
}
}
vloki wrote:- Generating primitive analog signal output
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;
}
}
vloki wrote:- Generate sound output via PWM
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;
}
}
}
vloki wrote:- Sending serial data USART
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 ???
}
}
}
#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)