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

Help on generating PWM on RB0/PWM0 pin !!

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







Help on generating PWM on RB0/PWM0 pin !!
PostPosted: Wed Dec 29, 2004 4:05 am     Reply with quote

Hi,

After searching for examples in setting PWM duty cycle in CCS forum, I decide to write this to seek for help.

Currently, I am using PIC18F2331 with 16Mhz crystal.
Can someone advise me on how to write the code for output a PWM on RB0/PWM0 pin with 50% dutycycle with any frequency??

Really appreciate that someone will help me.


Thanks very much. Question
Guest
Guest







2331/2431 EPWM example
PostPosted: Wed Dec 29, 2004 5:50 am     Reply with quote

This setup is a snip from working code. Don't forget to set up the TRIS register for the PWM output you're using.

This is a rough setup - you could refine it alot!

Code:

// Registers for the EPWM

#byte DFLTCON = 0xf60
#byte CAP3CON = 0xf61
#byte CAP2CON = 0xf62
#byte CAP1CON = 0xf63
#byte CAP3BUFL = 0xf64
#byte CAP3BUFH = 0xf65
#byte CAP2BUFL = 0xf66
#byte CAP2BUFH = 0xf67
#byte CAP1BUFL = 0xf68
#byte CAP1BUFH = 0xf69
#byte OVDCONS = 0xf6a
#byte OVDCOND = 0xf6b
#byte FLTCONFIG = 0xf6c
#byte DTCON = 0xf6d
#byte PWMCON1 = 0xf6e
#byte PWMCON0 = 0xf6f
#byte SEVTCMPH = 0xf70
#byte SEVTCMPL = 0xf71
#byte PDC3H = 0xf72
#byte PDC3L = 0xf73
#byte PDC2H = 0xf74
#byte PDC2L = 0xf75
#byte PDC1H = 0xf76
#byte PDC1L = 0xf77
#byte PDC0H = 0xf78
#byte PDC0L = 0xf79
#byte PTPERH = 0xf7a
#byte PTPERL = 0xf7b
#byte PTMRH = 0xf7c
#byte PTMRL = 0xf7d
#byte PTCON1 = 0xf7e
#byte PTCON0 = 0xf7f

// Don't forget to set up TRISB!!!!!



// Set up the PWM module....

   PDC0L = 0;
   PDC0H = 0;
   PDC1L = 0;
   PDC1H = 0;
   PDC2L = 0;
   PDC2H = 0;
   
   // LOOK AT THE DATA SHEET!!!!!!!!!!!!!!!!!!! //
   
   // Set Time Base (use defaults)
   PTCON0 = 0;
   
   // Enable PWM Time Base
   
   PTCON1 = 0x80;
   
   // Set for 9.8 KHz @ 40 MHz clock
   // You can set this where you like for your clock

   PTPERL = 0xff;
   PTPERH = 0x03;
   
   // Enable three PWM generators in independent mode
   
   PWMCON0 = 0x77;
   
   // Don't need any of these features....
   
   PWMCON1 = 0x00;
   
   
   // For PWM 0
   // You have two others independently available with this setup
   
   PDC0L = 0;
   PDC0H = 0x80; // 12 bit value, set to 0x0800 or 50%

   while(1);
   
   ....


Good luck! Smile
Guest
Guest







PostPosted: Wed Dec 29, 2004 5:53 am     Reply with quote

Small typo at the end of that code...

Should be

PDC0H = 0x08;

Not enough coffee yet! Rolling Eyes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Wed Dec 29, 2004 5:56 pm     Reply with quote

Guest wrote:
Small typo at the end of that code...

Should be

PDC0H = 0x08;

Not enough coffee yet! Rolling Eyes


Idea If you register then you can edit your post
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