Author Topic: Pic24 Comparator (and interupt and timer) MikroC  (Read 2320 times)

0 Members and 1 Guest are viewing this topic.

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Pic24 Comparator (and interupt and timer) MikroC
« on: August 24, 2015, 07:06:37 pm »
Hi all,

i'm working on a smaal DIY meter but needed a more powerfull micro as i'm still adding things to it.
when i try to use the Comparator of the PIC24FJ256GB106 it seems the COUT1 (output of the comparator) stay's @ 0

here is my testcode:

http://pastebin.com/embed_js.php?i=0FExEvTJ

Also i'm not able to use following : CM1CON.CEN = 1;


extra information:
Chip: PIC24FJ256GB106
Editor/compiler: MikroC DSPic
Meerweten, Want meten is Weten
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Pic24 Comparator (and interupt and timer) MikroC
« Reply #1 on: August 24, 2015, 10:56:55 pm »
The CEN bit is called CMxCON.CON. I suspect it was changed to avoid a conflict in the bitfield structure.

In Mikro C there are two different ways to access file register bits - as individual bits, or through bitfield structures. You have used bitfield names which are in the structure CM1CONBITS, so your code should look like this:-

  CM1CONBITS.CEN = 1;
  ...
  CM1CONBITS.EVPOL = 0b10;
  ...
  CM1CONBITS.CCH0 = 0b00;

Or you could set and clear individual bits like this:-

  CM1CON.CON = 1;
  ...
  CM1CON.EVPOL1 =1;
  CM1CON.EVPOL0 = 0;
  ...
  CM1CON.CCH1 = 0;
  CM1CON.CCH0 = 0;

This may be more efficient (smaller/faster code) for small bitfields.

Also in your interrupt routine CMIF and IFS1.CMIF_bit  should be IFS1BITS.CMIF or CMIF_bit.
 

 
 

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Re: Pic24 Comparator (and interupt and timer) MikroC
« Reply #2 on: August 25, 2015, 07:55:17 pm »
Thanks now atleast i can use the Comparator :D
but it seems my interupt isn't working properly :(

here's my updated code:
http://pastebin.com/fpT2swGL
Meerweten, Want meten is Weten
 

Offline Chris C

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: Pic24 Comparator (and interupt and timer) MikroC
« Reply #3 on: August 26, 2015, 04:43:59 pm »
CM1CONBITS.CEVT is getting cleared partway through the comparator setup, then never cleared again.  Once set by a comparator event, no further events/interrupts will be generated until you clear it.  I would recommend clearing it in two places:

1) After the comparator is fully set up, rather than partway through the process.  After "IEC1BITS.CMIE = 1" in Init() would be a good spot.
2) After "IFS1BITS.CMIF = 0;" in ComparorInterrupt().

If it still doesn't work, put a check in the main loop to see if either CM1CONBITS.CEVT or IFS1BITS.CMIF goes high.  If both do, then the interrupt is being generated, and your ISR not receiving it.  If neither do, there may be an issue with the comparator setup.



 

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Re: Pic24 Comparator (and interupt and timer) MikroC
« Reply #4 on: August 26, 2015, 05:09:11 pm »
Nice that fixed the job :D
hope i get it going now :D
Meerweten, Want meten is Weten
 

Offline Chris C

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: Pic24 Comparator (and interupt and timer) MikroC
« Reply #5 on: August 26, 2015, 05:14:47 pm »
Awesome!  Glad we could help.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf