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 support@ccsinfo.com

PIC16F628 problem with comparator

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



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PIC16F628 problem with comparator
PostPosted: Tue Sep 14, 2010 2:46 pm     Reply with quote

Hi all,

This is my first post on this forum, although I've been reading some threads for quite some time.

I've never used PIC comparators, until today ... but for some reason I can't get it to work properly, even the simplest example of them all Question

PIC16F628
CCS v.4.084

Here's my code:
Code:

#include "main.h"

short safe_conditions=TRUE;

#int_RDA
void  RDA_isr(void)
{

}

#int_COMP
void  COMP_isr(void)
{
   safe_conditions=FALSE;
   printf("WARNING!!  Voltage level is above 3.6 V.   \r\n");
}



void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   
   setup_comparator(A0_VR_A1_VR);
   setup_vref(VREF_HIGH|15);
   
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);

   printf("\r\nRunning voltage test...\r\n\n");

   while(TRUE)
   {
      if(safe_conditions)
         printf("Voltage level is below 3.6 V.              \r\n");
      safe_conditions=TRUE;
      delay_ms(500);
   }

}


and the main.h

Code:

#include <16F628.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)


I'm guessing it can't go any simpler ... as far as the hardware goes, I only have the ICSP, the RS232, and both AN0 and AN1 connected to ground.

The problem is: the software is always stuck on the irq function, the only thing that cames out is:
"WARNING!! Voltage level is above 3.6 V."
... no matter what voltage I put on AN0 and AN1

Besides not working, I do have other questions:
1 - If I have 2 comparators on this chip, why I only have 1 irq function?
2 - From all the available options for the setup_comparator(xxxxxxx); would it be possible to configure it to use only one analogue input and compare it to Vref ? ... let's say ... on AN3 (having the output triggering the irq function) ?

Any help would be greatly appreciated
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 14, 2010 8:20 pm     Reply with quote

It worked for me. I installed vs. 4.084. I copied and pasted your code
into an MPLAB project with very few changes.

The one change that I made, was to add code inside the #int_rda isr.
If an rda interrupt occurs, you must get the character. If you don't, the
interrupt will keep on occuring over and over again. The progam will
appear to lock-up, as it spends most of its time executing the rda routine
repeatedly. Adding the lines shown in bold below, will prevent this
problem.
Quote:

#int_RDA
void RDA_isr(void)
{
char c;

c = getc();
}


After making those changes, I tested it on a PicDem2-Plus board (non-
Rohs version). Because the 16F628 has its hardware UART on pins B2
and B1, I jumpered those pins over to pins C6 and C7, so they would be
connected to the Max232A chip on the board. It worked. I turned the
trimpot on Pin A0 and I got the following output. It's a little difficult to do
because the main loop displays at 500 ms intervals, but when the
comparator isr executes, there's no delay. So I had to be quick and
turn the trimpot back the other way promptly so it wouldn't fill up the
TeraTerm screen with only one message type. Here's the output as I
turned the trimpot back and forth past the Vref threshold voltage:
Quote:

WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
Voltage level is below 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Wed Sep 15, 2010 6:22 am     Reply with quote

Hi PCM programmer,

Thanks for all the help!

Today I've started by add some code to the #int_RDA routine, and for some reason I've started to experience some errors while programming the PIC16F628 (Pickit 2 programmer) ... so as I didn't had any other 16F628, I used one 16F628A and also changed the device header file (which are pretty much the same!)

Finally got some things working ... I now get:

Quote:

(...)
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V.
WARNING!! Voltage level is above 3.6 V
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
Voltage level is below 3.6 V.
(...)


the problem is that I get the message of level below 3.6V when it is above 3.6V (regulated with a trimpot) and vice-versa ... strange, humm?

I also had to connect AN0 and AN1 to the same trimpot output, otherwise, if I leave some input floating, I always get "WARNING!! Voltage level is above 3.6 V."

Tell me please, did you also notice if the pic sw was reacting correctly to the threshold Vref ? ... or any of this, just doesn't make any sense Rolling Eyes

Thanks
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