kekecjan
Newbie level 4
Hello guys !
I began learning programming PIC uC in MPLABX 5.4 , in few days i learned a lot but now i would really need some help about CCP compare mode, i am using PIC18F2221 external crystal oscillator 8MHz. I have done a lot of research on internet about CCP and timers but still i am struggling with CCP1IF to become 1, i know it's simple because i used timer0 to blink LED exactly one ms. Sooo what i want to do is this comparing register CCPR1 and Timer1 register TMR1. the result has to be CCP1IF=1 (i am not using interrupts) but unfortunately this CCP1IF is alway 0 , but when i make simple if statement like this: if(TMR1 == CCPR1) {LATC4=~LATCH4;} then LED on PORTC4 will blink but CCP1IF should not be 0 if values of registers are the same. I hope anyone has a clue what could be wrong.
BTW : #include "UART.h" is made by me and #include "XC8.h" cuse includes some configurations and _XTAL_FREQ.
I began learning programming PIC uC in MPLABX 5.4 , in few days i learned a lot but now i would really need some help about CCP compare mode, i am using PIC18F2221 external crystal oscillator 8MHz. I have done a lot of research on internet about CCP and timers but still i am struggling with CCP1IF to become 1, i know it's simple because i used timer0 to blink LED exactly one ms. Sooo what i want to do is this comparing register CCPR1 and Timer1 register TMR1. the result has to be CCP1IF=1 (i am not using interrupts) but unfortunately this CCP1IF is alway 0 , but when i make simple if statement like this: if(TMR1 == CCPR1) {LATC4=~LATCH4;} then LED on PORTC4 will blink but CCP1IF should not be 0 if values of registers are the same. I hope anyone has a clue what could be wrong.
BTW : #include "UART.h" is made by me and #include "XC8.h" cuse includes some configurations and _XTAL_FREQ.
C:
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "XC8.h"
#include "UART.h"
//#include <MojConfig.h> //include _xtal_freqs
// PIC18F2221 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = XT // Oscillator (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 2 // Brown-out Reset Voltage bits ()
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = RC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ANA // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = BB256 // Boot Block Size Select bits ( 256 Word)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bitProtect Boot (Boot block not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
volatile char count=0;
/* delata oba :void __interrupt(high_priority) myIsr(void) in void interrupt low_priority TMR_ISR(void) */
/*void interrupt TMR_ISR(void){
UART_Print("ISR \n");
if(CCP1IE && CCP1IF){ // preverim ce je verflow Interrupt Enable bit in Overflow Interrupt Flag bit
PIR1bits.CCP1IF=0;
UART_Print("CCPISR \n");//NUJNO ga postavim na 0 (must be cleared in software)
//count++;
LATCbits.LATC4 = ~LATCbits.LATC4;count=0;
//if(count==125){LATCbits.LATC4 = ~LATCbits.LATC4;count=0;}
}
} */
void Timer3_Init(void){
T3CONbits.RD16=1;
T3CONbits.T3CCP2=0;
T3CONbits.T3CCP1=1;
T3CONbits.T3CKPS1=0;
T3CONbits.T3CKPS0=1;
T3CONbits.TMR3ON=0;
T3CONbits.TMR3CS=0;
TMR3=0;
}
void Timer1_Init(void){
T1CONbits.RD16=1;
T1CONbits.T1RUN=0;
T1CONbits.T1CKPS1=1;
T1CONbits.T1CKPS0=1;
T1CONbits.T1OSCEN=1;
T1CONbits.TMR1ON=1;
TMR1=0;
}
//const char message[] = "PIC18F2221 TIMER0 \n " ;
char text[16];
void main(void)
{ TRISC4=0; //PORTC4 output
TRISC2=0;
CCP1CON= 0x00001010; //Compare mode: generate software interrupt on compare match (CCPxIF bit is set,CCP pin reflects I/O state)
//CCPR2H=(unsigned char) 1000>>8;
// CCPR2L=(unsigned char) 1000;
CCPR1=1000;
UART_Init(9600);
Timer1_Init();
Timer3_Init();
while(1)
{
if(TMR1==CCPR1){LATC4=~LATC4;sprintf(text,"TMR %u CCP %u %d %d \n", TMR1, CCPR1 , CCP1IF, TMR1IF);UART_Print(text);TMR1IF=0;}
}
}