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

#INT_RTCC based pwm will not work

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



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

#INT_RTCC based pwm will not work
PostPosted: Tue Nov 17, 2015 7:07 am     Reply with quote

Hello all
www.ccsinfo.com/forum/viewtopic.php?t=20050&postdays=0&postorder=asc&start=0

Quote:
They didn't show the setup code for the RTCC, so I've taken the liberty
of making a test program. For a PIC with a 4 MHz crystal, this demo
program creates a PWM signal on Pin B1, with a frequency of 100 Hz,
and a 25% duty cycle


I am trying to build pulse generator ( software pwm) as per PCM programmer provided code and working fine.

But, when I want to change the pwm rate using a switch control , its not working.
Any idea please ? I am using ver 4.124 and here is my code
Code:


   #include <16F877A.H>   
   
   #device *=16            // Use 16 bit pointers for Full RAM use (for 14 bit parts)
   #device adc=10          //Configures the read_adc return size
                         
   #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
   
   #use delay(clock = 20MHZ)       //Crystal Frequency  clock
   
   #use fast_io(A) // Port forwarding commands apply to A port
   #use fast_io(B) // Port forwarding commands apply to B port
   #use fast_io(C) // Port forwarding commands apply to C port
   #use fast_io(D) // Port forwarding commands apply to D port
   #use fast_io(E) // Port forwarding commands apply to E port
   
   #define  Speed_1            PIN_B7     
   #define  Speed_2            PIN_B6     

   #define  Stepper_pulse_out      PIN_C1   
   
   ////==============================================================================
   //#define LOOPCNT 39 // PWM Rate

   int8 width ;
   int8 loopcnt_valu ;
   int8 loopcnt ;
 //-------------------------------
#INT_RTCC
void tick_interrupt(void);

main()
{
width = 10;
loopcnt_valu = 30; // initial valu
loopcnt = loopcnt_valu ;

   set_tris_A(0b000001);   // configure input output
   set_tris_B(0b11111111);
   set_tris_C(0b10100000);
   set_tris_D(0b00010000);
   set_tris_E(0b000);     

setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_4);
      enable_interrupts (INT_TIMER0);
      enable_interrupts(global);
      set_timer0(6);
     
while(1)
   {
   if (input(Speed_1)==0 )
         {
          loopcnt_valu = 20 ;
         }
   if (input(Speed_2)==0 )
         {
          loopcnt_valu = 40 ;
         }
   }
}

//====================================
#INT_RTCC
void tick_interrupt(void)
{
static int8 loop = LOOPCNT;
static int8 pulse;

if(--loop == 0)
  {
   loop = LOOPCNT;
   pulse = width;
  }

if(pulse)
  {
   output_high(Stepper_pulse_out);
   pulse--;
  }
else
  {
   output_low(Stepper_pulse_out);
  }

}
[b]

Last edited by freedom on Tue Nov 17, 2015 9:02 am; edited 2 times in total
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Nov 17, 2015 7:22 am     Reply with quote

LOOPCNT is zero. Or did you want to use loopcnt_valu instead.

Regards
freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 17, 2015 8:04 am     Reply with quote

Quote:
LOOPCNT is zero. Or did you want to use loopcnt_valu instead.


I want to asign the value of LOOPCNT by loopcnt_valu which is generated in while loop if statement
Code:

if (input(Speed_1)==0 )
         {
          loopcnt_valu = 20 ;
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

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

PostPosted: Tue Nov 17, 2015 12:55 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=54585
try it
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 17, 2015 8:13 pm     Reply with quote

The main problem is that you are setting a variable called 'loopcnt_valu'
but you don't use it anywhere else. It's not used in the RTCC routine.
The problem is the lines in bold below:
Quote:
while(1)
{
if (input(Speed_1)==0 )
{
loopcnt_valu = 20 ;
}
if (input(Speed_2)==0 )
{
loopcnt_valu = 40 ;
}
}
}


To fix it, remove the '_valu' at the end, as shown below.
Code:
while(1)
   {
   if (input(Speed_1)==0 )
         {
          loopcnt = 20 ;
         }
   if (input(Speed_2)==0 )
         {
          loopcnt = 40 ;
         }
   }
}


There are a few other problems in your program such as:
1. Using the XT fuse with a 20 MHz crystal ? It won't work anywhere
except in Proteus. Real hardware needs the HS fuse.

2. CCS is not case sensitive by default. This is a strategy mistake in
my opinion. You should not use LOOPCNT and loopcnt in the same
program. It works in CCS, but it's just not good coding style.

3. The compiler warns you about main() and while(1). You can fix these
warnings as shown in bold below:
Quote:
void main()
{

while(TRUE)
{


}
freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 17, 2015 11:56 pm     Reply with quote

Thank Everyone. Now I understand my fault.
Now it is working
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