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

Variable pulse-duty-cycle PWM
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Tue Aug 10, 2010 1:57 am     Reply with quote

Ok I give up.

Here is my program for the PIC16F877A:
Code:

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

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT                      //Power Up Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=8000000)


#define MAX_DUTY  500
int i=0;
long duty_cycle[21]={0,39,78,117,155,192,227,262,294,325,354,381,405,427,446,463,476,487,495,499,500};

#int_TIMER2
void  TIMER2_isr(void)
{
   static int8 increment = TRUE;

   if(increment)
   {
      i++;
      if(duty_cycle[i] == MAX_DUTY)
      increment = FALSE;
   }
   else
   {
      i--;
      if(duty_cycle[i] == 0)
      {
         increment = TRUE;
         output_toggle(PIN_C0);
         output_toggle(PIN_C1);
      }
   }
   set_pwm1_duty(duty_cycle[i]);
}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4,124,1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(0);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   clear_interrupt(INT_TIMER2);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);

   duty_cycle = 0;
   output_high(PIN_C0);
   output_low(PIN_C1);
   while(1);

}

This is working excellent and no problem at all.
Then I switch to PIC12F683 because it's all I need (16F877A too big).

Here's the program:
Code:

#include <12F683.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOCPD                    //No EE protection
#FUSES PROTECT                  //Code protected from reads
#FUSES MCLR                     //Master Clear pin enabled
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=8000000)

#define MAX_DUTY  500
int i=0;
long duty_cycle[21]={0,39,78,117,155,192,227,262,294,325,354,381,405,427,446,463,476,487,495,499,500};

#int_TIMER2
void  TIMER2_isr(void)
{
   static int8 increment = TRUE;

   if(increment)
   {
      i++;
      if(duty_cycle[i] == MAX_DUTY)
      increment = FALSE;
   }
   else
   {
      i--;
      if(duty_cycle[i] == 0)
      {
         increment = TRUE;
         output_toggle(PIN_A0);
         output_toggle(PIN_A1);
      }
   }
   set_pwm1_duty(duty_cycle[i]);
}

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,124,1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(0);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_8MHZ);
   clear_interrupt(INT_TIMER2);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);
     
   output_high(PIN_A0);
   output_low(PIN_A1);
   while(1);
}

It is acting weird. The duty cycle won't change as it does using the 16F877A.

I'm using Proteus 7.7 sp2 to test my programs.

Can anybody take a look at my program for the 12f683 and tell me what am I missing please.
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 2:31 am     Reply with quote

Hello

Anybody please help me, it's been a long time and I need to fix my problem.
pls
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 1:49 pm     Reply with quote

How do you know that Proteus even emulates the CCP correctly for
the 12F683 ? Try a very simple PWM program, with no interrupts.
Just switch between two different PWM duty cycles, such as 25% and
75%, in a loop, and see if that works. That will tell you if your version
of Proteus is working. In other words, trouble-shoot the problem.
Don't depend on us.
jbmiller



Joined: 07 Oct 2006
Posts: 73
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 2:19 pm     Reply with quote

Now you know WHY I do NOT like simulators !!

Nothing but real hardware will prove/show that the code you cut REALLY works !!!

Since your code works fine in the 877, I'd really suspect the 'simulator' that you're using.

Look for errata/errors/updates/etc. in Proteus' forum.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 5:20 pm     Reply with quote

I just tested your code right now in hardware with a 12F683 and vs. 1.04
and it worked. I changed the Timer2 postscaler to 16, so it would slow
down the update rate on the PWM changes. Then I connected the CCP1
pin to an LED with a 330 ohm series resistor. The LED brightness was
modulated up and down. I changed the postscaler just so I could test
the hardware easily:
Quote:

setup_timer_2(T2_DIV_BY_4,124,16);
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 11:14 pm     Reply with quote

Thanks PCM Programmer for help. This means that my code has no problem but the problem is in Proteus. If so, then I urge each and everyone of you people in this forum and everywhere to not take Proteus as a reference for the results they get. Why? Because this is the second time that Proteus does that to me. If you remember PCM Programmer, I once was trying to write to an lcd using PIC16F688 and it took me a month to find out that Proteus has a bug in this PIC because the same thing happened. I was testing using Proteus and then PCM Programmer tested on hardware directly and told me that the program is working. So I made the hardware and found that this was true. (a month + people).
Anyway, thanks PCM Programmer and I will now implement the hardware and try and if I had any troubles I will get back to you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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