View previous topic :: View next topic |
Author |
Message |
iturtev Guest
|
CCP interrupt problem |
Posted: Mon Mar 01, 2010 11:48 am |
|
|
Hi,
I want to use CCP1 in compare mode with timer1 on PIC18F6620, but I have some problem and cant resolve it. Here is my code:
Code: |
#include "E:\f1\projects\Registrator\main.h"
#int_EXT
short a=0;
void EXT_isr(void)
{
}
#int_RB
void RB_isr(void)
{
}
#int_CCP1
void CCP1_isr(void)
{
disable_interrupts(INT_CCP1);
if (a==1)
{
OUTPUT_LOW(PIN_G3);
a=0;delay_ms(200);
}
else
{
OUTPUT_HIGH(PIN_G3);
a=1;delay_ms(200);
}
enable_interrupts(INT_CCP1);
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_ccp1(CCP_COMPARE_INT);
set_timer1(0);
CCP_1=0xFF;
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_EXT);
//enable_interrupts(INT_RB);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
ext_int_edge(0,H_TO_L);
SET_TRIS_B(0xFF);
SET_TRIS_E(0x00);
SET_TRIS_G(0x00);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
|
I am testing this code with Proteus. The problem is that there isnt any interrupt. And here is the interesting thing - I have button on RB0 and when I press this button it goes to ccp1 interrupt routine. Is this some sort of bug of Proteus or I am doing something wrong. I am new to CCS (I used BoostC till now) and I am still learning it. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 01, 2010 1:04 pm |
|
|
Try a more simple program, such as this CCS example file.
It uses the CCP in Compare mode:
Quote: |
c:\program files\picc\examples\ex_ccp1s.c
|
|
|
|
iturtev Guest
|
|
Posted: Mon Mar 01, 2010 1:26 pm |
|
|
PCM programmer wrote: | Try a more simple program, such as this CCS example file.
It uses the CCP in Compare mode:
Quote: |
c:\program files\picc\examples\ex_ccp1s.c
|
|
My code is simple too. I saw the example, but there is a function called "setup_compare" which I cant find anywhere. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 01, 2010 1:44 pm |
|
|
Quote: |
there is a function called "setup_compare" which I cant find anywhere. |
setup_compare() is used by the PCD compiler. You're using the PCH
compiler, so you can ignore that section of the code. Ignore the section
between the #if defined(__PCD__) and the #else statements.
Quote: |
My code is simple too.
|
It's not that simple. You have interrupt routines that are not required
to test the CCP compare mode. The #int_rb routine does not read
the PortB register, so if you get an interrupt, it will not be cleared, and
the #int_rb routine will be called continuously. The program will appear
to "lock up". Yes, I know that you have temporarily commented out
the enable_interrupts() statement for INT_RB. But if you uncomment it,
you will get the problem I described.
You have tons of Wizard code which is unnecessary and should be
deleted. All those modules are disable by default upon power-on reset
of the PIC. You don't need to disable them again.
You are setting the TRIS. You don't need to do that. The compiler will
automatically set the TRIS for you. |
|
|
iturtev Guest
|
|
Posted: Mon Mar 01, 2010 1:56 pm |
|
|
I didnt know these thing. In BoostC I must configure all TRIS-es, registers, bits manualy INT_RB and INT_EXT are necessary. I deleted the code temporary because of this problem with CCP. I am using them for other purposes. |
|
|
iturtev Guest
|
EDIT |
Posted: Mon Mar 01, 2010 2:00 pm |
|
|
I commented the unnecessary code just for test the CCP, but it didn't work. Only the problem with the button is solved, but the interrupt for CCP1 dont work.
Code: |
#include "E:\f1\projects\Registrator\main.h"
//#int_EXT
short a=0;
/*void EXT_isr(void)
{
}
#int_RB
void RB_isr(void)
{
}
*/
#int_CCP1
void CCP1_isr(void)
{
disable_interrupts(INT_CCP1);
if (a==1)
{
OUTPUT_LOW(PIN_G3);
a=0;delay_ms(200);
}
else
{
OUTPUT_HIGH(PIN_G3);
a=1;delay_ms(200);
}
enable_interrupts(INT_CCP1);
}
void main()
{
// setup_adc_ports(NO_ANALOGS|VSS_VDD);
// setup_adc(ADC_OFF);
// setup_psp(PSP_DISABLED);
//setup_spi(SPI_SS_DISABLED);
// setup_wdt(WDT_OFF);
//setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
//setup_timer_2(T2_DISABLED,0,1);
//setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
//setup_timer_4(T4_DISABLED,0,1);
setup_ccp1(CCP_COMPARE_INT);
CCP_1=0;
set_timer1(0);
CCP_1=0xFF;
//setup_comparator(NC_NC_NC_NC);
//setup_vref(FALSE);
// enable_interrupts(INT_EXT);
//enable_interrupts(INT_RB);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
// ext_int_edge(0,H_TO_L);
//SET_TRIS_B(0xFF);
//SET_TRIS_E(0x00);
//SET_TRIS_G(0x00);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 01, 2010 2:10 pm |
|
|
You are letting the program fall off the end of main(). It will then execute
a hidden SLEEP instruction that CCS places at that location. The PIC will
go to sleep and the program will not work. To fix this, put a continuous
loop at the end of main(). Example:
Quote: |
void main()
{
while(1);
}
|
|
|
|
iturtev Guest
|
|
Posted: Mon Mar 01, 2010 2:24 pm |
|
|
You are the man |
|
|
iturtev Guest
|
|
Posted: Tue Mar 02, 2010 1:58 am |
|
|
I have one question ... just for curiousity ... If I want to use CCP with timer3 not timer1, how can I tell CCS what timer to use? |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 02, 2010 5:34 am |
|
|
Look in the processor include file.
For most chips, there is a 'CCP_USE_TIMER3' define, you 'or' into the setup CCP. On the chip in this thread, there are instead three defines to OR into the setup_timer3 function, to enable this.
Best Wishes |
|
|
|