View previous topic :: View next topic |
Author |
Message |
pop
Joined: 18 Sep 2004 Posts: 22
|
CCP Compare Interrupt Problem |
Posted: Mon Oct 25, 2004 7:23 pm |
|
|
I've been trying to debug this code for the entire evening. It is my first attempt at using compare and timer1 to fire a triac or in this case to activate an LED.
It seems to me that FireTriac() interrupt never happens. I know that zero crossing interrupt occurs correctly, but after that the program does not jump to FireTriac() interrupt when timer1==ccp_1.
Since I don't have access to an actual PIC right now, I am using PIC Simulator IDE 5.21 to test my code (maybe the simulator has bug....).
Can anybody tell me why the FireTriac() interrupt never occurs?
Thank you.
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT
#device PIC 16F877 ADC=8
#use delay(clock=20000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7)
#opt 9
// Reserve top 255 bytes of memory for the bootloader
#ORG 0x1F00,0x1FFFF{}
void blink()
{
output_high( PIN_B2 );
delay_us(10);
output_low( PIN_B2 );
}
#int_CCP1 //this interrupt occurs when "timer1 == ccp_1"
FireTriac()
{
output_high( PIN_B1 );
delay_us(20); //Note: the delays are small in order to ensure
//the simulator doesn't get stuck
output_low (PIN_B1);
}
#int_ext //pin RB0 EXT interrupt
void zeroCrossing_isr()
{
set_timer1(0);
ccp_1=100;
}
void init_PIC() //initialize PIC hardware
{
set_tris_b(0b00000001);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts( GLOBAL );
enable_interrupts( INT_EXT );
enable_interrupts( INT_CCP1 );
setup_ccp1(CCP_COMPARE_INT);
/*The following 2 lines ensure that no accidental
CCP interupt occurs*/
set_timer1(0);
ccp_1=65535;
ext_int_edge(L_TO_H);
output_low( PIN_B1 );
main()
{
init_PIC();
while(1)
{ //only an interrupt driven program for now
}
} |
|
|
dave
Joined: 21 Dec 2003 Posts: 7 Location: Sheffield, South Yorks, UK
|
|
Posted: Mon Oct 25, 2004 10:52 pm |
|
|
MPLAB ver 6.62
PCM 3.212
Found a missing brace and invalid address range for your #org, code compiled and ran using MPLAB sim.
Code: |
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT
//#device PIC 16F877 ADC=8
#use delay(clock=20000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7)
#opt 9
// Reserve top 255 bytes of memory for the bootloader
#ORG 0x1F00,0x1FFF{} //<you had ivalid address range
void blink()
{
output_high( PIN_B2 );
delay_us(10);
output_low( PIN_B2 );
}
#int_CCP1 //this interrupt occurs when "timer1 == ccp_1"
FireTriac()
{
output_high( PIN_B1 );
delay_us(20); //Note: the delays are small in order to ensure
//the simulator doesn't get stuck
output_low (PIN_B1);
}
#int_ext //pin RB0 EXT interrupt
void zeroCrossing_isr()
{
set_timer1(0);
ccp_1=100;
}
void init_PIC() //initialize PIC hardware
{
set_tris_b(0b00000001);
}//<<<<<<<<<<< Missing brace!!
main()
{
init_PIC();
set_timer1(0); //The following 2 lines ensure that no accidental
//CCP interupt occurs*/ a false interrupt occurred before the pic had fully initialised with your code due to the fact that on boot up some registers can have any value see the Microchip data sheet for the device.
ccp_1=65535;
ext_int_edge(L_TO_H);
output_low( PIN_B1 );
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts( INT_EXT );
enable_interrupts( INT_CCP1 );
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts( GLOBAL ); //set this up last!
while(1)
{
}
}
|
I know its a matter of personal preferance, but I only use an init routine to set the tris registers and clear the ports.
hope this helps
regards Dave |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Tue Oct 26, 2004 5:38 am |
|
|
Thank you
I'll try it immediately. I'm suprised the compiler never "noticed" the missing brace.... |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Tue Oct 26, 2004 6:07 am |
|
|
Thanks Dave. On MPSIM this works perfectly. I'll give it another shot with PIC simulator, but at least I know the code works.
Thanks again |
|
|
|