Author Topic: PWM doesn't work  (Read 2434 times)

Bua

  • Sr. Member
  • ****
  • Posts: 34
    • View Profile
PWM doesn't work
« on: January 30, 2014, 05:39:35 AM »
Hi,

I have a problem that I think it could be a simple one.
I am trying to drive a buzzer via port X5 on Netcruzer board using PWM1 follows a demo code came with the webserver project.

I use the default webserver project. Below are the related code.

Code: [Select]
---------------------------------------main.c------------------------------------------------
int main(void)
{
      pwm1OpenDefault(PPS_OUT_X5, 0);

      pwm1SetPercent(PWM1_FREQ, 90);   //Set duty cycle (in percentage) of PWM channel 1
     
     //main loop
     while(1){
          //do nothing
     }

 }

It was just quiet.  If I set LAT_X5 =1, it works (very loud).  I tried

Code: [Select]
iPPSOutput(OUT_PIN_PPS_RP18,OUT_FN_PPS_OC1);    //Assign X5 to PWM1 (output)
And tried set X5 direction as output, but the result was the same.

Any ideas?

Cheers,
Bua
« Last Edit: February 01, 2014, 01:58:50 AM by Bua »

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: PWM doesn't work
« Reply #1 on: January 31, 2014, 04:40:53 PM »
Hi, you need a main loop in your code. Your code will enter the main function, execute the 2 lines, and go to some weird place. Strange things will happen! Use this code:

Code: [Select]
int main(void)
{
    pwm1OpenDefault(PPS_OUT_X5, 0);

    pwm1SetPercent(PWM1_FREQ, 90);    //Set duty cycle (in percentage) of PWM channel 1

    //Main Loop
    while(1) {
      //Do Nothing
    }
}

Also, please confirm you defined your PWM frequency in the projdefs.h file. For example:
Code: [Select]
#define PWM1_FREQ       PWM_FREQ_1600HZ     //PWM frequency is 1.6kHz

Bua

  • Sr. Member
  • ****
  • Posts: 34
    • View Profile
Re: PWM doesn't work
« Reply #2 on: February 01, 2014, 01:57:27 AM »
I had a main loop, sorry I forgot to post it previously.

I check projdefs.h file, I do have the freq set up exactly that, but it is still not working.

Thanks,

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: PWM doesn't work
« Reply #3 on: February 02, 2014, 04:30:16 PM »
I tested it with a LED (via 1k resistor) on that port, and it works. Please give full details of what you have connected to what port. Maybe include a photo.

Bua

  • Sr. Member
  • ****
  • Posts: 34
    • View Profile
Re: PWM doesn't work
« Reply #4 on: February 08, 2014, 10:02:06 PM »
Hi

I have attached the buzzer circuit and buzzer's datasheet.



My plan is to do the experiments with the resistor (R0), voltages, and frequencies. 

Thanks,