Problem with PIC16lf1824

Discussion in 'Embedded Systems and Microcontrollers' started by morpherious, Sep 27, 2016.

  1. morpherious

    Thread Starter New Member

    Sep 26, 2016
    4
    0
    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 (Text):
    1.  
    2. // PIC16F1824 Configuration Bit Settings
    3.  
    4. // 'C' source line config statements
    5.  
    6. // CONFIG1
    7. #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
    8. #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
    9. #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
    10. #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
    11. #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
    12. #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
    13. #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
    14. #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
    15. #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
    16. #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
    17.  
    18. // CONFIG2
    19. #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
    20. #pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
    21. #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
    22. #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    23. #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
    24.  
    25. // #pragma config statements should precede project file includes.
    26. // Use project enums instead of #define for ON and OFF.
    27.  
    28. #include <xc.h>
    29. #include <stdio.h>
    30. #include <stdlib.h>
    31. #include "pic16lf1824.h"
    32.  
    33. void main(int argc, char** argv) {
    34.  
    35.  TRISA = 0b00001011;//Set RA0/ICSPDAT,RA1/ICSPCLK and RA3/MCLR as input
    36.  TRISC = 0b00000011;//Set RC0/C2IN+ and RC1/C12IN1- as input
    37.  PORTA = 0x0;//Clear Port
    38.  PORTC = 0x0;//Clear Port
    39.  
    40.  OPTION_REG = 0b00000000;//Set Prescaler to 1/2
    41.  OSCCON = 0b01101000;//Set INTSOC to 4MHZ
    42.  ANSELC = 0b00000011;//Set RC0/C2IN+ and RC1/C12IN1- as analog input
    43.  INTCON = 0b11000000;//Enable Global and PIE Interrupt
    44.  CM2CON0 = 0b10000000;//C1ON =1
    45.  CM2CON1 = 0b00000001;//C2VP connects to C2IN+, C2VN connects to C12IN1-
    46.  PIE2 = 0b01000000;//Comparator C2 Interrupt Enable
    47.  
    48.  while(1){}
    49. }
    50. void interrupt ISR()
    51. {
    52.  if(C2IF)
    53.   {
    54.      C2IF = 0;
    55.   }
    56. }
    57.  

    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?
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    358
    Flip a pin in the isr so you know it for sure if the isr is firing or not.
     
  3. morpherious

    Thread Starter New Member

    Sep 26, 2016
    4
    0
    Thank you for your reply. The ISR is working, with what you suggested. I even implemented a Timer0 timeout and that fires the ISR
     
  4. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,889
    375
    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.
     
  5. jayanthd

    Member

    Jul 4, 2015
    264
    27
    LVP should be OFF.

    Code (C):
    1.  
    2. #define _XTAL_FREQ 4000000
    3.  
    at the top of the code before includes.
     
  6. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,889
    375
    Unless the TS is using low voltage programming.
     
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    358
    fairly simple code, per the datasheet:

    Code (Text):
    1.  
    2. //comparator 2 isr
    3. void interrupt isr(void) {
    4.     if (C2IF) {
    5.         C2IF = 0;                            //reset the flag
    6.         IO_FLP(OUT_PORT, OUT);                //flip the pin
    7.     }
    8. }
    9.  
    10. int main(void) {
    11.    
    12.     mcu_init();                               //initialize the mcu
    13.     IO_CLR(OUT_PORT, OUT); IO_OUT(OUT_DDR, OUT);    //out as output
    14.     cmp2_init();                            //reset comparator 2
    15.     ei();                                    //enable interrupt
    16.     while (1) {
    17.     }
    18. }
    19.  
    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
     
  8. morpherious

    Thread Starter New Member

    Sep 26, 2016
    4
    0

    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
     
  9. morpherious

    Thread Starter New Member

    Sep 26, 2016
    4
    0
    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.
     
  10. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,889
    375
    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.
     
  11. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    358
    you should read the datasheet, hard.
     
  12. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    That is a great advice! :)
     
Loading...