This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1294NCPDT sinewave zero cross detection using analog comparator

Other Parts Discussed in Thread: TM4C1294NCPDT, LM339, SYSBIOS

Hi

I intend to use the internal analog comparator to detect sinewave ( positive half wave 3V3 pp ) zero crossing to drive triacs safely.

Is there an application note for this purpose, already?

Thanks

Sergio

  • Hello Sergio,

    There is no such application note. Note that IO's don't tolerate a -ve voltage. So the signal must be shifted so that the min is 0V.

    Regards
    Amit
  • Hi Amit

    Yes, I know that IO's don't tolerate negative voltages ( this is why I said positive half-wave 3V3 pp ).

    Anyway, thank you for your kind support.

    Sergio
  • Hi Sergio,

    My apologies. But what are you setting the threshold point as? The Analog Comp will be a hard detect w/o hysteresis. ADC's Digital Comparator on the other hand will be a good candidate as it allows for hysteresis and thereby introduce a guard band which could prove more useful and noise tolerant.

    Regards
    Amit
  • Hi Amit

    Sorry due delay to answer to you.

    I am using the analog comparator with a sinewave decoupled from AC line and subtratec from a 3V3 DC voltage and as a result, the signal injected in C- input it is a 3V3 DC modulated with the same sinewave. So, controlling the internal REF we can generate a square-wave syncronized with different sinusoidal phases.

    Follows the code used to initialize the ANALOG COMPARATOR 2:

    void Init_CompC2(void)

    {

       GPIOPinConfigure(GPIO_PD2_C2O); // configure PD2 as C2O

       GPIOPinTypeComparator(GPIO_PORTP_BASE, GPIO_PIN_1); // define PP1 as C2-

       ComparatorIntRegister(COMP_BASE, 2 , INT_COMP2Handler);// this line is issuing compiler error ( see in the end of this post )

    // COMP0 must be enabled first

    //

    SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);

    //

    // Configure the internal voltage reference.

    //

    ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);

    //

    // Configure comparator 2.

    //

    ComparatorConfigure(COMP_BASE, 2, (COMP_TRIG_NONE | COMP_INT_RISE | COMP_ASRCP_REF | COMP_OUTPUT_NORMAL));

    //

    //

       ComparatorIntEnable(COMP_BASE,2);

       IntEnable(INT_COMP2);

    }

    void INT_COMP2Handler(void)

    {

    contador_comp2++;

    ComparatorIntClear(COMP_BASE,2);

    }

    /*

    The problem we are facing now is that the compiler send following error:

    <Linking>
    "../EK_TM4C1294XL.cmd", line 38: error #10099-D: program will not fit into available memory.  run placement with alignment fails for section ".vtable" size 0x26c , overlaps with ".vecs", size 0x360 (page 0)

    error #10010: errors encountered during linking; "TIO_BRASCONTROL_v20.out" not built

     

    If I comment out the above line in BLUE, the compiler error dissapears.


    Could you advice why is this error ocurring, please?

    Sergio

     

  • That's a link error, not a compile error. You need to check your map file and verify your link and locate setup. It looks like you are trying to place two parts of the program in the same location (or you just may be running out of memory.

    Robert
  • Hi Robert

    Yes, it is a link error, sorry. As the code creation (compilation + linking) is done in the same process, I used to say "compilation error", but, yes, you are right.

    But, it look likes that the function ComparatorIntRegister() could be trying to register the COMP2 INT VECTOR in a wrong place. Reading the source code of this function, there appears "INT_COMP0_TM4C123 + ui32Comp" and my processor is TM4C1294NCPDT. I am studying the code to fully understand its behaviour.

    If someone have a code example on how to use analog comparator , this are welcome. The example in the Tivaware Peripheral Driver Library manual is too much simple and it is not complete. I need to intercept COMP2 interrupt and need to use C2O ( COMP2 output ).

    Thanks,

    Sergio
  • Hello Sergio,

    While functionally the code is correct as the interrupt vector is the same for TM4C123 and TM4C129, it could become an issue if the Interrupt Vector map changes. What are the arguments for the ComparatorIntRegister you are using?

    Regards
    Amit
  • Hi Amit

    It is in BLUE, in previous post:

    .

    .

    .

    ComparatorIntRegister(COMP_BASE, 2 , INT_COMP2Handler);

    .

    .

    void INT_COMP2Handler(void)
    {
        contador_comp2++;

        ComparatorIntClear(COMP_BASE,2);

    }

  • Hi Amit

    In time, the C2O ( output ) it is not working too.

    I can read the internal output value:

    contador_comp2 = ComparatorValueGet(COMP_BASE, 2);
    System_printf("COMP2 = %d\n\r", contador_comp2 );

    As the input frequency is 60Hz, sometimes I print 1, sometimes I print 0, so, apparently, COMP2 is working.

    So, the issues I have is C2O not working and linking problem in interrupt.

    Thanks.

    Sergio
  • Hello Sergio,

    First thing first. Please send over your CCS project (Zipped). I think the startup file is not correct.

    Regards
    Amit
  • Amit Ashara said:
    First thing first.

    Ahem - really - 10 posts in - yet dare this reporter, "bomb-throw" by suggesting that such, "Voltage crossing thru 0V is far easier - and better - and faster handled via long standard, simple analog hardware!"    We note that poster has "subtracted" - likely via an op-amp - so "additional, external HW is (already) in play!

    Our tech firm has (for decades) achieved such "zero crossing detection" via simple XX339 (analog comparator) - which enables wide, flexible & precise "comparator switch-point" hysteresis settings/adjustments.   Run from a -5V charge pump (7660) there's "no need" for "subtraction" - nor to "struggle to master" (and further burden Amit) that which is clearly & greatly detailed w/in readily available analog tech documentation.

    Yes the (unthinking) trend is toward, "MCU as kitchen sink."   Yet that sink often proves, "Far from stainless" - and faster, FAR easier, inexpensive (external) methods deserve fair (i.e. some) consideration...   [should the volume potential approach "serious" (a possible justification) - it's doubtful that such a question would "land" here...]

  • Hi Amit

    Good morning.

    Is it possible to send specific configuration files only? I can't send entire source-code, due NDA ( Non-Disclosure Agreement ) with my client. I have some proprietary code inside it.

    Thank you for your understanding.

    Sergio
  • Master Yoda ( cb1_mobile ).

    Your words are so complex as are your thoughts.

    In a world so troubled nowadays, simple words are better for a fully understanding. Simple that.

    Are you suggesting to use EXTERNAL analog comparator to detect zero-crossing?

    Sergio

  • Sergio Kamakura said:
    Are you suggesting to use EXTERNAL analog comparator to detect zero-crossing? 

    Yes indeed - that method is so much faster, easier & more flexible to implement.

    An external analog comparator (available in tiny sot-23) easily accepts your negative-going input signal - yet supplies a proper (0V - 3V3) output signal to your MCU's GPIO.   Many, many examples exist detailing the implementation.   (you'll have to limit the comparator's output swing to 0V - simple divider/diode combination - achieve that)

    Note that it has recently been suggested that you employ (both) the MCU's ADC (and) a digital comparator - which further delays, complicates, and lessens the "robustness" of so complex a method!   (the more items involved - the greater the chance for problems...)

    There's yet another key/critical point in favor of this, "Use an external, analog comparator" suggestion.    My firm - only and always - does this!    Why?  

    Because to use the MCU's built-in comparator practically, "Locks you in" to that device!     Thus - when a newer/better MCU arrives - what then?   (you'll have a whole new "learning curve" to enjoy - and likely "wind up" right back here @ the forum - seeking help!)    Our method (external analog comparator) is simply, "Gerber (pcb file) Cut/Pasted" from board to board - freeing us entirely to focus upon the new design - not the mastery of an arcane, overly complex MCU sub-system - which is better achieved via low cost, known good/proven, external hardware...

    If you'd be good enough to "list" those words which you believe "unclear" I'll gladly offer definition.   By the way - do you find the MCU manual completely clear?   (often several "reads" of a tech document prove normal/customary)   You suggest "simple words" - yet who is to define such?

  • Hello Sergio,

    Or you can make a small project which shows the issue (derived from the main project) and send it over.

    Regards
    Amit
  • Hi cb1_mobile

    With all due respect cb1, if I have an analog comparator inside TM4C1294ncpdt that we have already paid for it, why not use it? And it is already integrated with interrupt structure of the processor, is that right?

    We have AC zero-crossing detector using LM339 already fully operational in other projects, but, we want to try to use all resources available inside the TM4C1294ncpdt. Is no sin, isn't?

    Regarding the manual, until now, I consider the TI's manuals, the best in the world. Since the 8080's era, I have read tons of manuals, and TI's is far, the best.

    As I always say: not just talk, you have to make himself understood.

    Sergio

  • By all means - invite (unneeded) complexity to your table - while delaying your (or client's) product release - and reducing system robustness.
    When the time comes to "upgrade" your MCU (this vendor or others) you'll get to enjoy this design "headache" all over - surely that makes great sense...
  • udpEcho_TivaTM4C1294NCPDT.zipHi Amit


    Follows zip file with entire project UDP_Echo modified with routines that gives link problem, as you requested.

    Thanks,

    Sergio

  • Hi cb1_mobile

    At contrary, using internal analog comparator reduce the complexity of our design, without loosing any robustness. I am sure that the engineers from TI, spent a lot of time and effort to integrate an analog comparator inside a digital kernel for a reason: use it because it is good.

    Amit wonder that the problem is in the startup file. Hope he is right.

    Thanks.

    Sergio
  • Amit

    And what about the C2O pin ( PD2 ) that is not responding accordingly?

    I am using the following command in EK_TM4C1294XL_initGPIO():

    GPIOPinConfigure(GPIO_PD2_C2O);

    Hope that this pin doesn't need special configuration such PD7 ( NMI ).

    Do you have recommendations about PD2 configuration as C2O?

    Thanks,

    Sergio

  • Sergio Kamakura said:
    I am sure that the engineers from TI, spent a lot of time and effort to integrate an analog comparator inside a digital kernel for a reason: use it because it is good.

    I would suggest fitness for purpose is a far better criteria.

    Reduction in parts != reduction in complexity, besides adding a level shift to remove a comparator hardly seems an advance.

    And Amit's suggestion that there is no hysteresis seems a step backwards.  I rather hope that's an exaggeration on his part.

    One thing to consider is that such circuits in a micro are generally less robust to external signals than their discrete counterparts.

    Robert

  • Hi Robert

    Thank you for your considerations. That's great.

    But, what is in focus in this post is a linker problem. I could repeat the same problem in udp_Echo example. Amit wonder that is a startup file problem.

    I have a problem in PD2 pin as C2O output also. I already posted explanation about.

    I have a lot of reasons why I chose to use internal analog comparator. After fix those 2 issues, I will be glad to discuss those reasons.

    Thanks,

    Sergio

  • Hello Sergio,

    Your project is based on TI RTOS. You do not need to have a cmd file as the same will be managed by the RTOS.

    Regards
    Amit
  • Hi Amit

    This example ( udp_Echo ) was imported from TIRTOS examples by CCS. I didn't create any cmd file.

    Which cmd file are you referring?

    Sergio
  • Hello Sergio,

    OK. Now I know what the issue is. You are trying to put an interrupt handler in the same section of memory where TI-RTOS puts the interrupt handler table as well.

    Since the TI RTOS (I have a newer version which is different) needs to know the call back function (which in essence is the Interrupt Handler), I would need to find out which function assignment it is so that Int_COMPC2 can be directly assigned.

    Regards
    Amit
  • Hi Amit

    Wonderful.

    Sergio
  • Hello Sergio,

    Double click the cfg file in TI-RTOS The rest as shown in the image and create a new Hwi handler.

    I would strongly urge you to refer to the documentation on TI-RTOS/SYSBIOS or log a forum post on TI-RTOS if further clarifications are required.

    Regards

    Amit

  • Hi Amit

    Yes, it worked!

    I used Interrupt number as 43, accordingly with page 115 from TM4C1294 manual.

    I need to fine tune something, because the comp2 interrupt are stopping after some time, but, I can walk again, thank you.

    Sorry to disturb you with a matter from other forum ( TIRTOS ).

    And, what about the C2O output that is not showing 60Hz pulses as expected? In my previous post I explained how I configured this pin. Is that correct for a TIRTOS application? I am afraid that the functions I am using it is not adequate for TIRTOS, as happened with previous issue you solved. Perhaps it is better to address this issue to TIRTOS forum?

    Thanks Amit

    Sergio
  • Hello Sergio,

    Inspite of the fact that there is a TI RTOS you can check the configuration of the GPIO in the register window to see if the value programmed into GPIODEN, GPIOAFSEL, GPIOPCTL are correct as per the configuration required for a peripheral pin function.

    Regards
    Amit