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 support@ccsinfo.com

18F2523 and PWM with CCP2 Module

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



Joined: 06 Dec 2005
Posts: 59

View user's profile Send private message

18F2523 and PWM with CCP2 Module
PostPosted: Fri Jul 11, 2014 7:31 am     Reply with quote

18F2523
PCWH 4.014

Dear,

I have a PWM signal at CCP1 Pin (RC2) that is running very well. To set the PWM, I use:

Code:

setup_timer_2(T2_DIV_BY_4, 255, 1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(value);


Now I want to add an other PWM (with same frequency) on CCP2 Pin (RC1) but it fails. I want to use:

Code:

setup_ccp2(CCP_PWM);
set_pwm2_duty(value);


but the compiler says "Undefined identifier set_pwm2_duty" and "Undefined identifier setup_ccp2".

Somebody with an idea how to activate PWM at CCP2 pin?

Thanks for any help
Nilsener
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Fri Jul 11, 2014 8:09 am     Reply with quote

I'd have to say 'get a new compiler'.

4.014, was when V4, was still beta at best. It was something like 4.050 before it started to work even remotely 'right'.

I kept 3.249, then the next one I felt worth retaining was in the high .060's for V4...

For many months, there was a 'sticky' at the top of this forum as people tried the new releases, and found successive problems.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 13, 2014 4:27 pm     Reply with quote

Here's a work-around if you don't want to upgrade. You should upgrade
because 4.014 isn't a great version, but if you don't want to, this will help.

Since setup_ccp2() and set_pwm2_duty() don't exist in vs. 4.014 for
the 18F2523, I just copied the ASM code and put it in a macro wrapper.
I included the feature of 8/10-bit pwm selection by checking the size of the
input parameter in the macro.

There is demo code to ramp the PWM duty cycle from 0 to 100% for both
8 and 10 bit modes. I didn't have an 18F2523 to test it, so I used an
18F4431 which is in the same family. It works. I did test it with vs. 4.014.
My goal was to get ASM code that looked the same as the CCS library
code. The demo shows PWM channels 1 and 2 so you can compare the
library code PWM (used on ch 1) to the macro code on ch 2. They produce
identical waveforms.
Code:

#include <18F2523.h>
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)

//----------------------------
// This macro creates the set_pwm2_duty() function
// which doesn't exist in vs. 4.014 for the 18F2523.

#byte CCPR2L = 0xFBB
#byte CCPR2H = 0xFBC
#byte CCP2CON = 0xFBA
int16 duty;
int8 temp;
#define BYTE_PTR(x) &(int8 *)(x)

#define set_pwm2_duty(value) \
duty = value;                \
if(sizeof(value) == 1)       \
   CCPR2L = duty;            \
else                         \
  {                          \
   #asm                      \
   RRCF   BYTE_PTR(duty)+1,F \
   RRCF   duty,F             \ 
   RRCF   BYTE_PTR(duty)+1,F \
   RRCF   duty,F             \
   RRCF   BYTE_PTR(duty)+1,F \
   MOVFF  01,CCPR2L          \
   RRCF   BYTE_PTR(duty)+1,F \
   RRCF   BYTE_PTR(duty)+1,W \
   ANDLW  30                 \
   MOVWF  temp               \
   MOVF   CCP2CON,W          \
   ANDLW  0xCF               \
   IORWF  temp,W             \
   MOVWF  CCP2CON            \
   #endasm                   \
  }                         


//----------------------------------------------------
// This macro creates the setup_ccp2() function which
// doesn't exist in vs. 4.014 for the 18F2523.

#define setup_ccp2(mode)   \ 
output_low(PIN_C1);        \ 
CCP2CON = mode   
       

//===================================
void main()
{
int8 duty8;
int16 duty16;
int16 i;

// Back off the PR2 value to 254 so we can get 100% duty cycle.
setup_timer_2(T2_DIV_BY_4, 254, 1);

setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

// Ramp the duty cycle from 0 to 100% for 10-bit and 8-bit pwm.

// 10-bit pwm
for(i = 0; i < 1024; i++)
   {
    duty16 = i;
    set_pwm1_duty(duty16);
    set_pwm2_duty(duty16);
    delay_ms(10);
   }

delay_ms(5000);

// 8-bit pwm

for(i=0; i < 256; i++)
   {
    duty8 = (int8)i;
    set_pwm1_duty(duty8);
    set_pwm2_duty(duty8);
    delay_ms(50);
   }


while(1);
}
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