Strange PWM results

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Strange PWM results

1,337 Views
elee40
Contributor I
I am working with a freescale MC9S12DT256 and attempting to get a pwm signal.  As a first approach processor expert was used and successfully produced a pwm signal.  Since this is for educational purposes the goal is define all memory locations manually.  The code below will sometimes produce a pwm signal and other times not.  I should also note that it compiles.  I have tested it on multiple processors and get the same result.  To ensure that it is correctly wired to the scope I have tested the code generated with processor expert and it works.  At this point I am stuck and not sure where my problem is.  Any help would be greatly appreciated. 
    Erik

#include <hidef.h>      /* common defines and macros */
#include <mc9s12dt256.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dt256b"

#define EPWMCNT (*((volatile unsigned int*) (0x000000AC)))
#define EPWMPER (*((volatile unsigned int*) (0x000000B4)))
#define EPWMDTY (*((volatile unsigned int*) (0x000000BC)))
#define EPWMPRCLKE (*((volatile unsigned int*) (0x000000A3)))
#define EPWMSCLAE (*((volatile unsigned long*) (0x000000A8)))
#define EPWMCLK_PCLK0E (*((volatile unsigned int*) (0x000000A2)))
#define EPWME_PWME0E (*((volatile unsigned int*) (0x000000A0)))
#define one (1<<0)

void pwmpercentage2(int percent){
     if (percent > 100) {
          percent = 100;
     }
     if (percent < 0) {
          percent = 0;
     }
     EPWMDTY=(byte)((percent*255)/100);
}

volatile static byte pwmChannel[1];
void main(void) {

EPWMCNT = 0;
EPWMPER=255; //sets the period
EPWMDTY = 25;//initialize duty cycle.
EPWMPRCLKE = 3; //set prescaler register
EPWMSCLAE = 187; //set scale register
EPWMCLK_PCLK0E = one; //select clock source
EPWME_PWME0E = one;  //run counter
  /* put your own code here */
  EnableInterrupts;
  pwmChannel[0]= PTP_PTP0;
pwmpercentage2(50);
for(;:smileywink:;

}
Labels (1)
Tags (1)
0 Kudos
2 Replies

418 Views
joeservo
Contributor III
when does it work?  over a certain percentage? under?  i think that casting, (byte)((percent*255)/100), may be the problem.
 
 
0 Kudos

418 Views
elee40
Contributor I
I would say its about 1 out of every 10 times that it works.  For sure it fails more than it works.  I think the casting portion is okay because I tested this portion of the code with bean generated code and had no problems.  I use that line to cast it to a byte because the period is only 255 so the dutycycle should never be any larger.  I will give the code a run without that function and just simply set the pwm dutycyle to a value and see if it works.  Thanks for the reply and am open for any other suggestions.
 Erik
0 Kudos