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

Trouble setting up PWM on 16F684

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
A small Guy
Guest







Trouble setting up PWM on 16F684
PostPosted: Sun Mar 08, 2009 1:23 pm     Reply with quote

Hy to all,

I'm puzzled, i can't make run the PWM on the 16F684.

The PIN is CCP1, port C5, PIN #5, clock is 8Mhz, PWM freq is 5Khz:

Code:

setup_timer_2(T2_DIV_BY_4, 99, 1);


And the simple program, just fading in/out a LED, is:

Code:

#include "C:\Documents and Settings\camp0s\Desktop\PIC\test\main.h"

long value=0;
 
void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4,99,1);
   
   setup_ccp1(CCP_PWM);
   //set_pwm1_duty(200); //0..200
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   setup_oscillator(OSC_8MHZ);

   while (true)
   {
      for (value=155;value<=200;value++)
      {
      set_pwm1_duty(value);  //value must be long
      delay_ms(25);
      }
      delay_ms(100);
      for (value=199;value>=156;value--)
      {
      set_pwm1_duty(value);  //value must be long
      delay_ms(25);
      }
   }

}


The LED is on pin #5 but it stays full on for all time, and i cannot see any frequency out of the pin.. where could be the mistake?

Tnx.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 08, 2009 1:55 pm     Reply with quote

1. Post the main.h file.
2. Post your compiler version.
3. Have you ever made anything work with this PIC ? For example,
can you blink an LED ?
Guest








PostPosted: Sun Mar 08, 2009 2:43 pm     Reply with quote

Hi!

It looks it was kind of false alarm. The compiler is 4.084 and the pic is working (not broken), the LED on/off works too.

Maybe i was hungry, after dinner, i double checked the setup and:

_ prescaler, timer2 and duty calculation where correct, but i missed the MAX value for the PWM which is 400 (5kHz, 8MHz clock), in the program i was doing up to 200
_ LED: i used a white led, bad, threshold voltage is rather high so i got full on, but full off for most of the time;
_ too bad, when i tried a fixed value it was high enough to light it.
_ too bad^2 when i tried the fading program the low threshold was too high again and i couldn't notice the dimming.

To get better debug i increased the delay_ms() between each step, removed the LED, downsized stepping to 10% increase/decrease at time and.. finally it worked!

One side note, while using the CCS setup wizard project, at the page labelled "other" where you setup the CCPX Settings functions there's a text saying "pulse width modulator output on pin C2", but for the 16f684, the correct pin is RC5.

Tnx.
Guest








PostPosted: Sun Mar 08, 2009 2:44 pm     Reply with quote

Just forgot, anyway, main.h:

Code:

#include <16F684.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES PUT                      //Power Up Timer
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled

#use delay(clock=8000000)
Guest








PostPosted: Sun Mar 08, 2009 3:15 pm     Reply with quote

Here again!

There's more!!

I found one more clue to why PWM was on it's own before, here's the code:

Code:

#include "C:\Documents and Settings\guest\Desktop\PIC\test\main.h"

#use fast_io (A)
//#use fast_io (C)

/*
#int_RA
void  RA_isr(void)
{
   delay_ms (50);
   if (input(PIN_A0)==1) output_toggle(PIN_C3);
}*/

long value=0;
 
void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4,99,1);
   
   setup_ccp1(CCP_PWM);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_tris_a (0x3F);
   enable_interrupts(INT_RA);
   enable_interrupts(GLOBAL);

   setup_oscillator(OSC_8MHZ);
   
   value=200;
   set_pwm1_duty(value);
   output_low(PIN_C3);
   
   while (true)
   {
   delay_ms(1000);
   }
}


With this code (and the previous main.h) the PWM output is not working:

_ the output pin is fixed at 5V
_ there is no frequency signal on it

BUT, if you comment these two lines:

/*
enable_interrupts(INT_RA);
enable_interrupts(GLOBAL);
*/

PWM is working again:

_ current setup, value=200 gives Vout_Avg of 2.5V, 50% duty
_ i can find the 5KHz signal out of the PIC pin

So.. kind of bug or enabling interrupts arise problems?

Tnx.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 08, 2009 3:41 pm     Reply with quote

Quote:
/*
#int_RA
void RA_isr(void)
{
delay_ms (50);
if (input(PIN_A0)==1) output_toggle(PIN_C3);
}
*/


enable_interrupts(INT_RA);
enable_interrupts(GLOBAL);

If you have the #int_ra interrupt service routine commented out (as
shown above), you must not enable interrupts. If you do, and you
get an INT_RA interrupt, the PIC will jump to the interrupt vector
address and there will be nothing there. The program will crash.
Guest








PostPosted: Sun Mar 08, 2009 4:47 pm     Reply with quote

Uhm, this appears to work now, thanks for pointing me at the right direction! i was thinking that without stimulating port_a nothing could happen.

I'm just doing a small homework for school but I'm confused by all the implications of "touching this and not specifying that one" could generate..

I just attached 5 buttons (if pressed level goes up) to the port_a of the PIC and i wanted to modify the PWM like +10% at a time (and -10%) depending on the pressed buttons.

In theory a small and straightforward flowchart.. in reality a pain, cause nothing is going right, I'm messing with fast_io, i need debounce but cannot pause too much to fully read input and clear int vector, and it's already late in the night..

Last time i used a PIC i did it all in ASM, it was a nice propeller clock.. but definitely took too much time (but debug was way easier, just keep track of the bits); this time trying first C coding i found little frustrating to understand why it hang/problem that arise, kind of my errors accumulate with implicit restrictions made by few lines of code setup.

"I feel" C coding, especially in peripheral setup, should be restricted to two condition:

_ either ALL requirements are satisfied, so code is assembled
_ if one statement or logic block miss, halt or signal to the user of half-state of "the machine"

.. but, don't take it too much serious :|

Tnx again.
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