CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

comparator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ElectroNick



Joined: 10 Oct 2005
Posts: 4

View user's profile Send private message

comparator
PostPosted: Fri Oct 14, 2005 11:41 pm     Reply with quote

Hi,

i want to use pic16f785's comparator module. VCC is 4.88V and I want comparator to control a led. But i can't success! The voltage on RA1 is about 4.7V but led is still passive. Could you please help me?

Here is my code:

Code:
#include "16F785.h"
#fuses INTRC_IO,PROTECT,NOWDT,NOMCLR
#use delay(clock=4000000)

#INT_COMP
void isr()  {
   
  disable_interrupts(GLOBAL);

   output_high(PIN_C6);  //led connected to RC6

   setup_vref(VREF_C1_HIGH | 15);
   setup_comparator(CP1_A1_VREF);
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);
}


void main()   {

  output_low(PIN_C6);

   delay_ms(500);

   setup_vref(VREF_C1_HIGH | 15);
   setup_comparator(CP1_A1_VREF);
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);

}
foothill
Guest







Dead code
PostPosted: Sat Oct 15, 2005 8:23 am     Reply with quote

Nick,

The code in main will run one pass, then stop if
the isr is not called. And If there are any problems in your isr,
the code will stall.

What's needed for troubleshooting is an infinite loop to
force main() to repeat endlessly. Try:

includes
.
variable declarations
.
comparator setups
.
main()

while (TRUE)
{

---Your main code body here----

}

Also, I often toggle an otherwise unused pin directly
somewhere in main, so I can see if main is running.
You can do the same in your isr, just to see if it's being
called. Try to get the whole structure running in some very
simple way, then add in the comparator function and isr.

Be aware that explicit function calls cannot be made from
within another function....they can't be nested. I always
put conditional comparator setup changes in the body of
main rather than in an isr, and if they're not to change, I
put them above main altogether...

Check the sample comparator program sent with the
compiler. Samples often give good tips.

Good luck!

Brian
Ttelmah
Guest







PostPosted: Sat Oct 15, 2005 8:31 am     Reply with quote

Ongoing to the above comment, _do not_ disable and enable the global interrupt in the interrupt handler. Doing this is a way to crash the PIC if the interrupt occurs a second time quickly. The interrupts are automatically disabled by the hardware, when an interrupt occurs, and automatically re-enabled when you return from an interrupt. If you re-enable before the return, and another interrupt is pending, the chip will interrupt inside the interrupt handler, and this results in a 'recursive' call, which the chip does not support.
You also don't need to re-enable the int_comp in the handler. It does not get disabled, and this is just uneccessary.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group