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

PIC184620 Enabling INT_TIMER3 stops CCP2 PWM output

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



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PIC184620 Enabling INT_TIMER3 stops CCP2 PWM output
PostPosted: Tue Aug 26, 2014 11:46 pm     Reply with quote

Hi All

I am using the the CCP2 pin as a PWM output. The PWM signal disappears as soon as enable_interrupts(INT_TIMER3) is called.

The output of CCP2 has been changed from RC1 to RB3 with fuse CCP2B3.

The datasheet says that TMR2 is used.

Any clues would be welcome.

Thanks in advance.
Alex
_________________
I type therefore I press buttons
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 27, 2014 11:15 am     Reply with quote

The program below produces PWM output on Pin B3. Enabling
#int_timer3 interrupts doesn't affect it at all. It works.

Last night, you briefly posted that it failed if #int_rb was enabled. I tested
that, and it also works in that case.

If you want help, you need to post a test program that shows the failure.
You also need to post your CCS compiler version. Also tell us how you
know the PWM fails. ie., are you looking at Pin B3 with an oscilloscope ?
I'm testing it in hardware with a scope and with compiler vs. 5.026.

Test program:
Code:

#include <18F4620.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,CCP2B3
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#int_timer3
void timer3_isr(void)
{
static int8 count = 0;

count++;   // Increment a variable on each interrupt
}

 //=================================
void main()
{
// Setup for 3.9 KHz pwm frequency with 4 MHz oscillator.
setup_timer_2(T2_DIV_BY_1, 255, 1); 
setup_ccp2(CCP_PWM);
set_pwm2_duty(128);   // 50 % duty cycle

clear_interrupt(INT_TIMER3);
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);

while(1);
}
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 1:10 am     Reply with quote

Hi PCM programmer, Yes, that first post was a mistake. I became lost in testing and enabled some commands which led me down the wrong path.

I can confirm that the timer3 works okay in the reduced test program. When I introduce the code into the TMR3 ISR which is used for button polling and debounce that is where the problem resides.

Code:
#INT_TIMER3
void timer3_isr(void) {
   //poll key change and debounce
   if(kb_scan_count>=2){
      byte changedKeys;
      newKeys=input_b() & 0b01110000;        //read current keys B4,5,6 with input mask
      changedKeys=oldKeys ^ newKeys;         //XOR with oldKeys to identify changed keys
     
      //Test for the changed key no longer being set
      //SW1
      if (bit_test(changedKeys,4) && !bit_test(newKeys,4)) {
         keyPressed=1;
      }
      //SW2
      if (bit_test(changedKeys,5) && !bit_test(newKeys,5)) {
         keyPressed=2;
      }
      //SW3
      if (bit_test(changedKeys,6) && !bit_test(newKeys,6)) {
         keyPressed=3;
      }
      oldKeys=newKeys;                       //load current keys to old keys
      kb_scan_count=0;                       //reset count
   }
   

   //inc counters
   kb_scan_count++;
}


If I comment out the line with
Code:
newKeys=input_b() & 0b01110000;
All works
_________________
I type therefore I press buttons
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 9:11 am     Reply with quote

input_b() changes the TRIS to be all input pins on PortB. This is the
source of your problem. Pin B3 (PWM output) must be kept as an
output pin.

The 184620 data sheet says:
Quote:

15.4 PWM Mode

In Pulse-Width Modulation (PWM) mode, the CCPx pin
produces up to a 10-bit resolution PWM output. Since
the CCP2 pin is multiplexed with a PORTB or PORTC
data latch, the appropriate TRIS bit must be cleared to
make the CCP2 pin an output
.

and
Quote:
15.1.2 CCP2 PIN ASSIGNMENT

Changing the pin assignment of CCP2 does not
automatically change any requirements for configuring
the port pin. Users must always verify that the appropriate
TRIS register is configured correctly for CCP2
operation
, regardless of where it is located.


Solution:
Add the input_state_b() function (shown below) to your program and put
it above main(). Then call it in your #int_timer3 routine, instead of the
input_b() function:
Code:

// This function reads Port B without changing the TRIS.

int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB")
return(PortB);
}
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 7:43 pm     Reply with quote

Thank you PCM programmer Program works perfectly.

Your help has been valuable and high appreciated. Thank you again.

Alex
_________________
I type therefore I press buttons
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