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

set_timer0()

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



Joined: 16 Mar 2005
Posts: 13

View user's profile Send private message

set_timer0()
PostPosted: Sun Jun 26, 2005 5:22 am     Reply with quote

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: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sun Jun 26, 2005 6:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 26, 2005 6:19 am     Reply with quote

also: You only have to enable the specific interrupts only once, they will stay enabled until disabled by you.
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