PIC18F46K42 xpress EVAL Board - CCP - Capture Mode program - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: December 07, 2018, 07:39:07 am
content: Hi,I have a PIC18F46K42 xpress evaluation board. Now I am developing CCP capture mode program using MPLAB Xpress PIC18F46K42 evaluation board on MPLAB CLOUD IDE and MPLAB X IDE also. I given input as 1khz square wave from digital oscilloscope.Code:/#include <stdio.h>#include <stdlib.h>#include <PIC18F46K42.h>void main(){int signal_period;TRISCbits.TRISC2 = 1;OSCCON1 = 0x60;PIE4bits.CCP1IE = 1;PIR4bits.CCP1IF = 0;CCP1CON = 0x85;CCP1CAP = 0x00;CCPR1L = 0x00;CCPR1H = 0x00;CCPTMRS0 = 0x01;PIR4bits.TMR1IF = 0;T1CON = 0x02;T1CONbits.ON = 0;T1CONbits.ON = 1;while(1){while(!(PIR4bits.CCP1IF));PIR4bits.CCP1IF=0; ////////////////////////here i am put break pointsignal_period = (CCPR1H<<8) | (CCPR1L & 0xFF);while(!(PIR4bits.CCP1IF));PIR4bits.CCP1IF = 0;signal_period = (CCPR1H<<8) | (CCPR1L & 0xFF) - signal_period;T1CONbits.ON = 0;}}I put the break point on that mentioned line(PIR4bits.CCP1IF=0; ), that the debugger does not came that line. But i wrote the before condition line aswhile(PIR4bits.CCP1IF = 1);,the debugger came that line and also if came that linesignal periodvalue does not change in all places and given only the value is 0x0000.Again one more doubt, i have connected the mplab xpress board to usb port of my system. Then wrote a program on mplab cloud ide, after compilation download the hex file and copied the hex file to my mplab xpress board. Then i fed input to corresponding pin. After put break point on respective line of my code, then i started the debug. Is it correct?Please let me know, what is going wrong.....

author: Buriedcode
author_rank: Super Contributor
timestamp: December 07, 2018, 09:25:08 am
content: In order to use interrupts you should enable the GIEH and GIEL bits in the INTCON0 register, these are the global interrupt enable bits for both high and low priority.Page 133 of the datasheet explains the sequence, and page 136 explains the register.

author: arul
author_rank: Regular Contributor
timestamp: December 08, 2018, 05:56:57 pm
content: Hi,Now I am enabled global interrupt then using PIC18F46K42 Configuration Bit Settings, before i am configured oscillator using respective register. And enabled the corresponding Peripheral Pin Select for CCP1 module. Then changed the timer1 clock source (because timer1 clock source have more than one option, am little confused here. I selected the HFINTOSC(1Mhz) as my system clock source. In timer1 have CCP1, HFINTOSC, FOSC etc clock souce. So which is suit for timer1 clock source? ).Code:// PIC18F46K42 Configuration Bit Settings// 'C' source line config statements// CONFIG1L#pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)#pragma config RSTOSC = HFINTOSC_1MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 4 MHz and CDIV = 4:1)// CONFIG1H#pragma config CLKOUTEN = OFF // Clock out Enable bit (CLKOUT function is disabled)#pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be cleared and set only once)#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)// CONFIG2L#pragma config MCLRE = EXTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR )#pragma config PWRTS = PWRT_OFF // Power-up timer selection bits (PWRT is disabled)#pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)#pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)#pragma config LPBOREN = OFF // Low Power BOR Enable bit (ULPBOR disabled)#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)// CONFIG2H#pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.45V)#pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)#pragma config DEBUG = ON // Debugger Enable bit (Background debugger enabled)#pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)// CONFIG3L#pragma config WDTCPS = WDTCPS_31// WDT Period selection bits (Divider ratio 1:65536; software control of WDTPS)#pragma config WDTE = OFF // WDT operating mode (WDT Disabled; SWDTEN is ignored)// CONFIG3H#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)#pragma config WDTCCS = SC // WDT input clock selector (Software Control)// CONFIG4L#pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)#pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)#pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)#pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)// CONFIG4H#pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)#pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)#pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)#pragma config LVP = ON // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)// CONFIG5L#pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)// CONFIG5H// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.#include <xc.h>#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <stdbool.h>#include <string.h>void main(){int signal_period;CCP1PPS = 0x12;TRISCbits.TRISC2 = 1;//////////oscillator configuration////////// OSCCON1 = 0x62; ///////////////why i disable here because used PIC18F46K42 Configuration Bit Settings to enable the oscillator setting// OSCFRQ = 0x02;/////////global interrupt config//////////INTCON0bits.GIEH = 1;INTCON0bits.GIEL = 1;INTCON0bits.IPEN = 1;/////////////ccp1 interrupt config/////////PIR4bits.CCP1IF = 0;PIE4bits.CCP1IE = 1;////////////ccp1 reg config////////////////CCP1CON = 0x85;CCP1CAP = 0x00;CCPR1L = 0x00;CCPR1H = 0x00;CCPTMRS0 = 0x01;//01-timer1 ; 10-timer3; 11-timer5; 76b-ccp4; 54b-ccp3; 32b-ccp2; 10b-ccp1///////////timer1config////////////////////PIR4bits.TMR1IF = 0;T1CON = 0x02;T1CLK = 0x03; //0D-CLC1; 03-HFINTOSC; 02-Fosc;TMR1L = 0x00;TMR1H = 0x00;////////timer1ON/OFFconfig//////////////////T1CONbits.ON = 0;T1CONbits.ON = 1;while(1){/////////CAPTURE1/////////////////////////while(!(PIR4bits.CCP1IF));PIR4bits.CCP1IF = 0;signal_period = (CCPR1H<<8) | (CCPR1L & 0xFF);while(!(PIR4bits.CCP1IF)); //(PIR4bits.CCP1IF == 0);PIR4bits.CCP1IF = 0;signal_period = (CCPR1H<<8) | (CCPR1L & 0xFF)- signal_period;TMR1L = 0x00; //timer1TMR1H = 0x00;PORTAbits.RA6 = 0;}}Now the corresponding output like as CCP1IF is not set, that is mean not detect the edges.Here what is going wrong?

author: arul
author_rank: Regular Contributor
timestamp: December 12, 2018, 04:50:41 am
content: Hi,I am generating PWM signal at CCP1(pwm_mode) module. I got PWM output at the corresponding output pin. After that I fed PWM output as a input to the CCP4(capture_mode) module. Still the respective edge is not detected in capture mode.This is modeCode:// CONFIG1L#pragma config FEXTOSC = OFF // External Oscillator Selection->Oscillator not enabled#pragma config RSTOSC = HFINTOSC_64MHZ // Reset Oscillator Selection->HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1// CONFIG1H#pragma config CLKOUTEN = ON // Clock out Enable bit->CLKOUT function is enabled#pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit->PRLOCK bit can be cleared and set only once#pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled// CONFIG2L#pragma config MCLRE = EXTMCLR // MCLR Enable bit->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR#pragma config PWRTS = PWRT_OFF // Power-up timer selection bits->PWRT is disabled#pragma config MVECEN = OFF // Multi-vector enable bit->Interrupt contoller does not use vector table to prioritze interrupts#pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit->IVTLOCK bit can be cleared and set only once#pragma config LPBOREN = OFF // Low Power BOR Enable bit->ULPBOR disabled#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored// CONFIG2H#pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits->Brown-out Reset Voltage (VBOR) set to 2.45V#pragma config ZCD = OFF // ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit->PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset#pragma config DEBUG = OFF // Debugger Enable bit->Background debugger disabled#pragma config XINST = OFF // Extended Instruction Set Enable bit->Extended Instruction Set and Indexed Addressing Mode disabled// CONFIG3L#pragma config WDTCPS = WDTCPS_31 // WDT Period selection bits->Divider ratio 1:65536; software control of WDTPS#pragma config WDTE = OFF // WDT operating mode->WDT Disabled; SWDTEN is ignored// CONFIG3H#pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required#pragma config WDTCCS = SC // WDT input clock selector->Software Control// CONFIG4L#pragma config BBSIZE = BBSIZE_512 // Boot Block Size selection bits->Boot Block size is 512 words#pragma config BBEN = OFF // Boot Block enable bit->Boot block disabled#pragma config SAFEN = OFF // Storage Area Flash enable bit->SAF disabled#pragma config WRTAPP = OFF // Application Block write protection bit->Application Block not write protected// CONFIG4H#pragma config WRTB = OFF // Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected#pragma config WRTC = OFF // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected#pragma config WRTD = OFF // Data EEPROM Write Protection bit->Data EEPROM not write-protected#pragma config WRTSAF = OFF // SAF Write protection bit->SAF not Write Protected#pragma config LVP = ON // Low Voltage Programming Enable bit->Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored// CONFIG5L#pragma config CP = OFF // PFM and Data EEPROM Code Protection bit->PFM and Data EEPROM code protection disabled#include <xc.h>#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <stdbool.h>#include <string.h>#include <PIC18F46K42.h>void main(void){int dutyValue;int signal_period;//////oscillator configuration/////////OSCCON1 = 0x60;OSCCON3 = 0x00;OSCEN = 0x00;OSCFRQ = 0x02;OSCTUNE = 0x00;///////ccp1_pwm1 pin config//////////////////TRISCbits.TRISC2 = 0;RC2PPS = 0x09;///////////ccp4 pin configuration/////////TRISBbits.TRISB0 = 1;CCP4PPS = 0x18;////////ccp1_pwm1 reg config//////////////CCP1CON = 0x8C;CCPR1H = 0x01; //0x00; //0x01;CCPR1L = 0xFF; //0x80; //0xFF;CCPTMRS0bits.C1TSEL = 0x1;////////////ccp4 reg config////////////////CCP4CON = 0x85;CCP4CAP = 0x00;CCPR4L = 0x00;CCPR4H = 0x00;CCPTMRS0 = 0x04;//01-timer1 ; 10-timer3; 11-timer5; 76b-ccp4; 54b-ccp3; 32b-ccp2; 10b-ccp1/////////////ccp4 interrupt config/////////PIR10bits.CCP4IF = 0;PIE10bits.CCP4IE = 1;///////timer2 ccp1_pwm1 config///////////////T2CLKCON = 0x01;T2HLT = 0x00;T2RST = 0x00;T2PR = 0xFF;T2TMR = 0x00;PIR4bits.TMR2IF = 0;PIE4bits.TMR2IE = 1;T2CON = 0x80;///////////timer1 ccp2_capture config////////////////////T1CON = 0x02;T1CLK = 0x01; //0D-CLC1; 03-HFINTOSC; 02-Fosc;TMR1L = 0x00;TMR1H = 0x00;PIR4bits.TMR1IF = 0;PIE4bits.TMR1IE = 0;/////////global interrupt enable//////////INTCON0bits.GIEH = 1;INTCON0bits.GIEL = 1;INTCON0bits.IPEN = 1;T1CONbits.ON = 0;T1CONbits.ON = 1;while(1){///////////ccp1_pwm1///////////////////////dutyValue = 0x1FF; // 255; // &= 0x03FF;if(CCP1CONbits.FMT){dutyValue <<= 6;CCPR1H = dutyValue >> 8;CCPR1L = dutyValue;}else{CCPR1H = dutyValue >> 8;CCPR1L = dutyValue;}///////////ccp4_capture4/////////////////while(!(PIR10bits.CCP4IF)); //(PIR10bits.CCP4IF == 0);PIR10bits.CCP4IF = 0;signal_period = (CCPR4H<<8) | (CCPR4L & 0xFF);while(!(PIR10bits.CCP4IF)); //(PIR10bits.CCP4IF == 0);PIR10bits.CCP4IF = 0;signal_period = (CCPR4H<<8) | (CCPR4L & 0xFF)- signal_period;TMR1L = 0x00; //timer1TMR1H = 0x00;}}Please let me know, what is going wrong.......