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 Interrupt

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



Joined: 05 Dec 2010
Posts: 3

View user's profile Send private message

Comparator Interrupt
PostPosted: Sun Dec 05, 2010 7:14 pm     Reply with quote

Can tell me definition of comparator interrupt? or give me
suitable links... Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 10:44 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=26861
ppyote



Joined: 06 Dec 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon Dec 06, 2010 5:15 pm     Reply with quote

PCM programmer wrote:
http://www.ccsinfo.com/forum/viewtopic.php?t=26861


Could you give an example using the 18F2550 pic comparators? I do not get them to run, but in the range of 16f I have no problem in putting in place.
Sorry my English, but not all the good that I would like.
thanks
ogimy



Joined: 05 Dec 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon Dec 06, 2010 7:45 pm     Reply with quote

I have make this program to simulate the function of an comparator interrupt.

Situation:

a. PIC will continuously toggle LED at port RB1, RB2 and RB3, and toggle buzzer ON and OFF in sequence every 1 sec.

b. When interrupt occur, all LED and buzzer will automatically turn OFF, and only turn ON LED RB4.

c. After 2 sec, all sequence function back as normal.

Can someone check this code and show which part should put comparator interrupt?


Code:

#include <16f877a.h>
#device adc=8
#use delay (clock= 20000000)
#fuses hs,noprotect,nowdt,nolvp

#byte PORTa=5
#byte PORTb=6
#byte PORTc=7
#byte PORTd=8
#byte PORTe=9

Void main()
{

Set_tris_b(0b0000000);
Setup_port_a(ALL_ANALOG);
Setup_adc(ADC_CLOCK_INTERNAL);

do
{

portb=0b00000010;
delay_ms(1000);
portb=0b00000100;
delay_ms(1000);
portb=0b00001000;
delay_ms(1000);
output_high(pin_D0);
delay_ms(2000);
output_low(pin_D0);
delay_ms(2000);

if (input(pin_D1)==1)
{
Portb=0b00010000;
delay_ms(2000);
}

else(input(pin_D1)==0)
{
portb=0b00000010;
delay_ms(1000);
portb=0b00000100;
delay_ms(1000);
portb=0b00001000;
delay_ms(1000);
output_high(pin_D0);
delay_ms(2000);
output_low(pin_D0);
delay_ms(2000);
}

}
While(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 07, 2010 12:50 pm     Reply with quote

Quote:

Could you give an example using the 18F2550 pic comparators?

Here's a comparator example for the 18F4550 with some hysteresis.
(I don't have an 18F2550, but the 18F4550 is in the same PIC family).
When you turn the trimpot on pin A0 (from 0v to 5v, slowly), the LED
will turn on when you go higher than 2.5 volts. When you then turn
the trimpot the other way (from 5v to 0v, slowly), the LED will go off
when you go below 2.3 volts. This hysteresis prevents noise on the
input signal (on pin A0) from causing the comparator to flip around
when it's near the threshold voltage. The hysteresis is created by
the code in the isr which changes the threshold level.
Code:

#include <18F4550.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)

#define VREF_LOW_THRESHOLD   11      // Vref = 2.3 volts
#define VREF_HIGH_THRESHOLD  12      // Vref = 2.5 volts

#define LED_PIN  PIN_B0

#byte CMCON = getenv("SFR:CMCON")

//------------------------------------
#INT_COMP
void isr()
{
int8 temp;

if(C1OUT == 1) 
  {
   output_high(LED_PIN);  // LED on
   setup_vref(VREF_LOW | VREF_LOW_THRESHOLD);
  }
else 
  {
   output_low(LED_PIN);  // LED off
   setup_vref(VREF_LOW | VREF_HIGH_THRESHOLD);
  }
 
// Read CMCON register to clear the "mismatch" condition.
temp = CMCON;   
}

//==================================================
void main()
{
int8 value;
   
setup_comparator(A0_VR_A1_VR | CP1_INVERT);

setup_vref(VREF_LOW | VREF_HIGH_THRESHOLD);

// Initialize the LED.
if(C1OUT == 1) 
   output_high(LED_PIN);
else 
   output_low(LED_PIN);

value = CMCON;  // Clear mismatch condition.   
clear_interrupt(INT_COMP);  // Clear interrupt flag
enable_interrupts(INT_COMP);  // Enable interrupts
enable_interrupts(GLOBAL);

while(1);
}
ppyote



Joined: 06 Dec 2010
Posts: 3

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 5:37 pm     Reply with quote

Hello again, I have the version of the 4104 ccs if I recuerto and the program has not since before I work ... is not in the simulator or the proboard
which compiler version are you?
will put the code below in my program ...
thanks again

Code:
#include <18f2550.h>
#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,NOVREGEN,NOPBADEN
#use delay(clock=20000000)
#word cncom=0x0fb4
#word portb=0x0f81
#word cvrcon = 0x0fb5
#use standard_io(a)


#int_comp
void interrupcion()
{
   if(!c1out){
      portb=0xff;
   }
   else portb=0x00;
}
void main(){
   set_tris_b(0x00);   
   set_tris_a(0xFF);
   setup_comparator(A0_A3_A1_A2);
   enable_interrupts(int_comp);
   enable_interrupts(global);   
   portb=0x00;
while(1){}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 6:10 pm     Reply with quote

I tested my program with versions 4.114 and 4.104 on a PicDem2-Plus
board and it works OK with both versions.

I don't want to test your program. I only wanted to post a working
demonstration program.
ogimy



Joined: 05 Dec 2010
Posts: 3

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 9:49 am     Reply with quote

I have rewrite the program. I try compile into ccs, its ok. But can the programmer check the code it will run on hardware and follow the situation above? Because I don't have hardware to check.
Code:

#include <16f877a.h>
#fuses hs,noprotect,nowdt,nolvp
#use delay(clock=20000000)

#define LEDPIN1  PIN_B1
#define LEDPIN2  PIN_B2
#define LEDPIN3  PIN_B3
#define LEDPIN4  PIN_B4

#byte CMCON = getenv("SFR:CMCON")

#INT_COMP
void isr()
{
int8 temp;

if(C1OUT == 1) 
     {
     
      output_high(LEDPIN1); // 1 LED ON
      output_high(LEDPIN2); // 1 LED ON
      output_high(LEDPIN3); // 1 LED ON
      output_high(pin_d7);  // BUZZER ON
      delay_ms(1000);
      }
else
  {
      output_low(LEDPIN1); // 1 LED Off
      output_low(LEDPIN2); // 1 LED Off
      output_low(LEDPIN3); // 1 LED Off
      output_low(pin_d7);  // BUZZER OFF
      delay_ms(1000);
     }
// Read CMCON register to clear the "mismatch" condition.
temp = CMCON;
}

void main()
{
int8 value;
   
setup_comparator(A0_VR_A1_VR);
 
// Initialize the LED.
if(C1OUT == 1)
   output_high(LEDPIN4);
else
   output_low(LEDPIN4);

value = CMCON;                // Clear mismatch condition.   
//clear_interrupts(INT_COMP);    // Clear interrupt flag
enable_interrupts(INT_COMP);  // Enable interrupts
enable_interrupts(GLOBAL);


while(1);
}
ppyote



Joined: 06 Dec 2010
Posts: 3

View user's profile Send private message

PostPosted: Sat Dec 11, 2010 6:30 am     Reply with quote

PCM programmer wrote:
I tested my program with versions 4.114 and 4.104 on a PicDem2-Plus
board and it works OK with both versions.

I don't want to test your program. I only wanted to post a working
demonstration program.


I know, I just wanted to ask you to revise it in case my code was wrong
as I said, my English is not very good. About what the version of ccs, you wondering if perhaps the version I was using had some bug ...
thanks again for your help
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