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

PWM I2C ... Timer2 ??

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



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PWM I2C ... Timer2 ??
PostPosted: Thu Apr 02, 2009 6:03 am     Reply with quote

Hi all.

I'm testing a circuit that needs an RTC and 3 PWM channels.
If I use RTC (DS1308) only, it works fine and I can read/write in DS1308 with no problem.
If I use PWM only (tested with CCP1, one channel) it works fine.
This is my init_pwm() :
Code:
setup_timer_2(T2_DIV_BY_4, 255, 8);
setup_ccp1(CCP_PWM);      // Configure CCP1 as a PWM
delay_us(10);                 // A small delay is necessary

When I join the RTC and PWM ... it doesn't work !!!
Why this happens ? Is because of my configuration of Timer2 ??
Can anybody help ?
Thank you
Jacob
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 6:07 am     Reply with quote

I'm sorry...
I'm using a 18F4525 with a 20MHz crystal.

Thank you
Jacob
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 12:37 pm     Reply with quote

Quote:
When I join the RTC and PWM ... it doesn't work !!!

What doesn't work ? The PWM or the RTC ? We are not mind readers.

Make sure you are not using PWM pins for the RTC device.
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 12:55 pm     Reply with quote

OK ...
You are right ...

But ... nothing works !!!
The RTC stop to work, i can't read/write the DS1307, and the PWM get crazy
with strange behaviors.

I'm using RC3 for SCL and RC4 for SDA in I2C communications.
I'm using RC2 to 'extract' the PWM from the PIC.

My 'ds1307.c' is based on a post in the 'ccs forum' and the PWM code is based on the 'EX_PWM.c'.

The code don't work simultaneous... but if i try it separated ... works !!!!!
Any ideia ???

Thank you
Jacob
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 1:00 pm     Reply with quote

Quote:
My 'ds1307.c' is based on a post in the 'ccs forum' and the PWM code is based on the 'EX_PWM.c'.

Post a link to the ds1307 code. (Do not post the code. Just post a link).

Then, post a small test program that shows the problem. Run this
program on your PIC and verify that it shows the problem.
The program must be very short. It must also compile with no errors.

Also post your compiler version.
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 1:10 pm     Reply with quote

Hello.

RTC:

the link is : http://www.ccsinfo.com/forum/viewtopic.php?t=23255&highlight=ds1307

Everything work Smile




My compiler version is : V4.057
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 2:14 pm     Reply with quote

Sorry ... instead of 'Preview' i 'Submit' !!!!

Hello.
My compiler version is : V4.057 and i'm using a 18F4525.

RTC : the link is : http://www.ccsinfo.com/forum/viewtopic.php?t=23255&highlight=ds1307

Everything works.


PWM :
Code:

void pwm_init(){
   setup_ccp1(CCP_PWM);
   // Configure CCP1 as a PWM
   setup_timer_2(T2_DIV_BY_4, 127, 1);
}

void main() {

   int16 i;

   pwm_init();

   while(1){
      for(i=50;i<800;i=i+50){
          set_pwm1_duty(i);
          printf("   %Lu    ", i);
          read_char();
      }   
   }

}


It works ... but one strange thing happens, when i observe the PWM output in my oscilloscope i have 100% of my PWM when i reach the 511, instead of 1023 (10 bits) and i have 50% with the value 256... in the manual says that 'set_pwm1_duty' receive an 8/16bits value but really writes a 10 bit. Why it only uses 9 bits (512) ? Am i doing something wrong ?

Now all together ... when i insert ds1307_init() and run 'ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)' the PWM goes crazy and it starts to write strange chars in the RS232 port. ??

Another question, how do i use P1A, P1B, P1C and P1D. I need 3 PWM channels. I've seen the topic : http://www.ccsinfo.com/forum/viewtopic.php?t=37206, but this code don't work with my PIC... I tried to make some changes... but it still don't compile ...

Thank you.
Jacob
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 2:27 pm     Reply with quote

Quote:
setup_timer_2(T2_DIV_BY_4, 127, 1);

I have 100% of my PWM when i reach the 511, instead of 1023.

That's because the Timer2 value is set to 127.
If you want 1024 steps, then change the Timer2 value to 255.
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 3:08 pm     Reply with quote

OK ...
That's logic ... 127 in Timer, of course that only reach 511.
Thank you ...

And what about PWM channels ?
How can I activate and control the 3 channels : P1A, P1B and P1C ?

Thank you in advance.
Jacob
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 3:38 pm     Reply with quote

Quote:
How can I activate and control the 3 channels : P1A, P1B and P1C ?

The 18F4525 has only 2 independent PWM channels. Each one can
have its own frequency and duty cycle.

One of the two channels is an ECCP channel. It can have up to 4 outputs
but they all run from the same PWM channel. They have the same
frequency, and the same duty cycle. The ECCP channel is intended
to drive a motor control circuit. It can be configured for Half-Bridge or
Full-Bridge mode.

With this PIC, you only have 2 independent, hardware PWM channels.
Do you understand this ?
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 4:35 am     Reply with quote

Unfortunately i understand ...

Quote:
The ECCP channel is intended to drive a motor control circuit.

With this sentence, I understand why they talk about bridge and half bridge in the datasheet.

I'll change the PIC to 18F4331, as mentioned in topic:
http://www.ccsinfo.com/forum/viewtopic.php?t=37206

If i use this PIC and similar code to the referred topic, then I can have 3 (or more) independent duty cycle PWM.

Thank you for your explanation.
I'm always learning in this forum Very Happy

Jacob
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