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

error using 2 interrupts

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



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

error using 2 interrupts
PostPosted: Tue Aug 09, 2005 1:38 pm     Reply with quote

Hi all,

I am trying to generate 2 PWM signals using 2 interrupts (timer0 and timer1). The PWM signal on PIN_B0 works fine (timer0), but the PWM signal on PIN_B1 (timer1) has the correct period but with a 500 mV amplitude (should be 5V). I don't know why this is happening because it's the same exact code (see below).

Any help is much appreciated,
-weg

Code:

#include <16F87.H>
#include <stdlib.h>
#include <math.h>

#define B0 PIN_B0
#define B1 PIN_B1

#fuses HS,NOWDT,NOPROTECT, PUT
#use delay(clock=10000000)

int rud_pwm, elev_pwm;

//Timer interrupt to generate PWM signals
#INT_TIMER0
void pwm_signal()
{
   int i;

   //One of the parameters used to cause the interrupt to occur every 22.5 msec
   set_timer0(37); // 61 for 20 msec

   /*--------RUDDER 0 (right), 255 (left) --------*/
   output_high(B0); // generates initial high time ie 1 msec
   delay_us(1000);
   i=0;
   for(i=0;i<rud_pwm;i++) // generates remaining high time, varied by variable "pwm"
   {
      //use this to tune duty cycle
      delay_us(1);
   }
   output_low(B0);
}

//Timer interrupt to read PWM signals
#INT_TIMER1
void pwm_signal2()
{
   int j;
   //One of the parameters used to cause the interrupt to occur every 22.5 msec
   set_timer1(37); // 61 for 20 msec

   /*--------RUDDER 0 (right), 255 (left) --------*/
   output_high(B1); // generates initial high time ie 1 msec
   delay_us(1000);
   j=0;
   for(j=0;j<elev_pwm;j++) // generates remaining high time, varied by variable "pwm"
   {
      //use this to tune duty cycle
      delay_us(1);
   }
   output_low(B1);
}




main()
{
   long m;

   delay_ms(1500);

   while(1)
   {
      // enable timer interrupts
      enable_interrupts(GLOBAL);
      enable_interrupts(INT_TIMER0);
      enable_interrupts(INT_TIMER1);

      //the other parameter used to cause interrupt to occur every 20 msec
      setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
      setup_timer_1(RTCC_INTERNAL|RTCC_DIV_256);

      rud_pwm = 128;
      elev_pwm = 128;

   }
} // end of main

kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

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

PostPosted: Tue Aug 09, 2005 2:09 pm     Reply with quote

It could be pure hardware problem rather then a firmware problem. Try the following:

* Make sure that your scope works as you expect. For example, you can have a 1x probe, while the scope expects a 10x probe.
* If you can, output_high(PIN_B1) for a short time. If it will still be 500mV instead of +5V, then you are probably grounding B1 somewhere. By the way, what is B1 connected to? Does your PIC get warm?
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Tue Aug 09, 2005 2:38 pm     Reply with quote

I don't think its hardware...here's why. If I change the timer0 loop to output the PWM signal on B1 and timer1 to B0 (just switching them), I see a signal on B1 and not B0. Whereas before, I saw a PWM signal on B0 and not B1. This comes back to the 2nd timer loop (timer1)...which I believe to be a software problem. Any suggestions?

Thanks,
-weg
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 09, 2005 2:49 pm     Reply with quote

There are several things about your code that don't look very good,
but here, you've got an 8-bit counter (Timer0) and a 16-bit counter
(Timer1), and you're loading them with the same number and expecting
them to both interrupt every 22.5 ms.
Code:
set_timer0(37);

set_timer1(37);
Ttelmah
Guest







PostPosted: Tue Aug 09, 2005 2:56 pm     Reply with quote

It is never actually reaching the second interrupt.
Your 'correct' signal, is noise being picked up because of poor grounding, or a problem with the scope probe, from the other line.
Timer1, is a 16bit counter. It does not support a 256 prescale. The values give no error, because they are legitimate values for timer0. You are sending timer1, the initialisation value for timer0, which probably corresponds (from a quick glance on the data sheet), to 'system clock from another source' (not the internal oscillator), prescale 1:1, do not synchronise external clock, use external clock on RB6, enable timer.
Without the external clock, this timer will never trigger.

Best
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