View previous topic :: View next topic |
Author |
Message |
ariza
Joined: 16 Mar 2005 Posts: 13
|
set_timer0() |
Posted: Sun Jun 26, 2005 5:22 am |
|
|
Hi all
I want to learn that is it possible to set set_timer0(); function as in the code
Code: |
int8 count=0;
int8 const x3[]={0,1,2,4,6};
int8 data;
void choose()
{
if(x3[count]==1)
{
setup_timer_0(RTCC_INTERNAL|16);
enable_interrupts(INT_TIMER0);
set_timer0(100);
}
if(x3[count]==2)
{
setup_timer_0(RTCC_INTERNAL|8);
enable_interrupts(INT_TIMER0);
set_timer0(100);
}
if(x3[count]==4)
{
setup_timer_0(RTCC_INTERNAL|4);
enable_interrupts(INT_TIMER0);
set_timer0(100);
}
if(x3[count]==6)
{
setup_timer_0(RTCC_INTERNAL|4);
enable_interrupts(INT_TIMER0);
set_timer0(152);
}
}
#int_EXT1
EXT1_isr()
{
output_high(led0);
delay_ms(10);
count=count+1;
enable_interrupts(INT_EXT1);
output_low(led0);
delay_ms(10);
}
#int_TIMER0
TIMER0_isr()
{
int8 i;
do {
for(i=0; i<3; ++i)
{
if(i!=2)
{
set_adc_channel(i);
delay_us(10);
data = read_adc();
pd=data;
}
else
{
set_adc_channel(i+1);
delay_us(10);
data = read_adc();
pd=data;
}
}
}
while (TRUE);
}
void main()
{
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
enable_interrupts(INT_EXT1);
enable_interrupts(global);
for(;;){}
} |
Thanks for any advice
Best Regards
ARIZA |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Sun Jun 26, 2005 6:08 am |
|
|
You should initialise the timer (set_timer...) before enabling the timer interrrupt. You should also clear the timer interrupt flag before enabling the timer interrupt.
Code: |
setup_timer_0(RTCC_INTERNAL|16);
set_timer0(100);
clear_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER0);
|
Note that the compiler might automatically do some of this for you. I don't know, you could examine the assembler listing and see what it is doing however the steps above should be considered "best practice".
In your code, both interrupts are the same level (prioity in Microchip speak) so when one interrupt is being serviced the other is masked. This means that your delay_ms(xx) in the external interrupt handler will result in you losing timer interrupts. You should not re-enable the interrupt within the handler. The PIC interrupt handlers are likely non recursive and you will probably end up over-writing registers that were saved by the handler first interrupt event in the event another occurs while still in the handler. The PIC will automatically reenable the interrupt when the code executes the return from interrupt instruction.
In your external interrupt handler it would be "cleaner" to just set a flag (my_interrupt_occurred) then in the mainline pole for my_interrupt_occurred to be true then clear the flag and execute the code that you currently have inside the interrupt handler. This way the code still executes but you will not lose timer interrupts. In general you should avoid using delay_xx() in the interrupt handler. If you really have to do this, as per your ADC setting, them consider using delay_cycles() or use the same flag mechanism discussed above. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Jun 26, 2005 6:19 am |
|
|
also: You only have to enable the specific interrupts only once, they will stay enabled until disabled by you. |
|
|
|