View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 07, 2010 12:50 pm |
|
|
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
|
|
Posted: Wed Dec 08, 2010 5:37 pm |
|
|
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
|
|
Posted: Wed Dec 08, 2010 6:10 pm |
|
|
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
|
|
Posted: Fri Dec 10, 2010 9:49 am |
|
|
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
|
|
Posted: Sat Dec 11, 2010 6:30 am |
|
|
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 |
|
|
|