Problem with PIC16lf1824

Thread Starter

morpherious

Joined Sep 26, 2016
4
So the following is just code to try to turn on comparator interrupt. . For the micro controller PIC16(L)F1824

http://ww1.microchip.com/downloads/en/DeviceDoc/41419C.pdf

Code:
// PIC16F1824 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)

// #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 "pic16lf1824.h"

void main(int argc, char** argv) {

 TRISA = 0b00001011;//Set RA0/ICSPDAT,RA1/ICSPCLK and RA3/MCLR as input
 TRISC = 0b00000011;//Set RC0/C2IN+ and RC1/C12IN1- as input
 PORTA = 0x0;//Clear Port 
 PORTC = 0x0;//Clear Port 
 
 OPTION_REG = 0b00000000;//Set Prescaler to 1/2
 OSCCON = 0b01101000;//Set INTSOC to 4MHZ
 ANSELC = 0b00000011;//Set RC0/C2IN+ and RC1/C12IN1- as analog input
 INTCON = 0b11000000;//Enable Global and PIE Interrupt
 CM2CON0 = 0b10000000;//C1ON =1
 CM2CON1 = 0b00000001;//C2VP connects to C2IN+, C2VN connects to C12IN1-
 PIE2 = 0b01000000;//Comparator C2 Interrupt Enable

 while(1){}
}
void interrupt ISR()
{
 if(C2IF)
  {
     C2IF = 0;
  }
}

For the following code, the comparator 2 interrupt never triggers, as checked using breakpoints.

The attachment below shows the two inputs into the comparator. With the rectified ac going in on RC0/C2IN+ and the reference(which is close to but not zero) going in on RC1/C12IN1-. The comparator should fire while the rectified signal is great than the reference signal. Also by setting C2IF=1, the interrupt does occur. Yet the comparator doesn't do one itself. Where am I going wrong?
 

Attachments

AlbertHall

Joined Jun 4, 2014
12,625
The comparator should fire while the rectified signal is great than the reference signal.
Do the little arrowheads labelled 1 and 2 on the left of the picture indicate the zero volts level for each channel? If so it looks like the rectified waveform never gets below your reference.
 

dannyf

Joined Sep 13, 2015
2,197
fairly simple code, per the datasheet:

Code:
//comparator 2 isr
void interrupt isr(void) {
    if (C2IF) {
        C2IF = 0;                            //reset the flag
        IO_FLP(OUT_PORT, OUT);                //flip the pin
    }
}

int main(void) {
   
    mcu_init();                               //initialize the mcu
    IO_CLR(OUT_PORT, OUT); IO_OUT(OUT_DDR, OUT);    //out as output
    cmp2_init();                            //reset comparator 2
    ei();                                    //enable interrupt
    while (1) {
    }
}
Comparator 2 is triggered on both falling and rising edges. The input signal is compared to a 2.5v reference.

16f1936 is used because its comparator module is identical to yours.
16f1936_cmp2.PNG
 

Thread Starter

morpherious

Joined Sep 26, 2016
4
fairly simple code, per the datasheet:

Code:
//comparator 2 isr
void interrupt isr(void) {
    if (C2IF) {
        C2IF = 0;                            //reset the flag
        IO_FLP(OUT_PORT, OUT);                //flip the pin
    }
}

int main(void) {
  
    mcu_init();                               //initialize the mcu
    IO_CLR(OUT_PORT, OUT); IO_OUT(OUT_DDR, OUT);    //out as output
    cmp2_init();                            //reset comparator 2
    ei();                                    //enable interrupt
    while (1) {
    }
}
Comparator 2 is triggered on both falling and rising edges. The input signal is compared to a 2.5v reference.

16f1936 is used because its comparator module is identical to yours.
View attachment 112644

Thank you for your reply. Can you please tell me what compiler is this. I am a beginner and have only used XC8 uptill now. Thanks
 

Thread Starter

morpherious

Joined Sep 26, 2016
4
Do the little arrowheads labelled 1 and 2 on the left of the picture indicate the zero volts level for each channel? If so it looks like the rectified waveform never gets below your reference.
Thank you for your reply. Those little arrows do indicate the zero level. Nonetheless, the comparator should still fire because on the current settings if V+ is greater than V- then the comparator should be on. That never happens.
 

AlbertHall

Joined Jun 4, 2014
12,625
CM2CON1 = 0b00000001;//C2VP connects to C2IN+, C2VN connects to C12IN1-
You need to set the interrupt to respond to positive or negative transitions of the comparator output, C2INTP or C2INTN, CM2CON1 7:6.
You can also set both of these to respond to transitions in both directions.
 
Top