Jump to content
Sign in to follow this  
dasnny

MSP432P401R PWM

Recommended Posts

I want to generate a low freqency PWM( 70Hz). How can i change the pwm frequency? The analogFrequency function is not working, I can`t compile if I write analogFrequency(800) for example. ( this is the error message that I get:" undefined reference to `analogFrequency' ")

Share this post


Link to post
Share on other sites

Hello @@dasnny

 

analogFrequency is not a documented function in the reference for Energia (or Arduino) so perhaps it is not surprising that it wasn't part of the MSP432 implementation.

 

The PWM implementation can be found in wiring_analog.c. I looked at it briefly and there appear to be quite a few differences between the MSP432 and what is used for the MSP430. You could modify it I suppose or better, implement your own separate variable frequency PWM function.  Perhaps someone else has done this for Energia but I haven't seen it.

 

EDIT:  70 Hz is very slow for a microcontroller. Can you use millis() and digitalWrite() to accomplish what you need?

Share this post


Link to post
Share on other sites

@@dasnny Unfortunately the Energia Library functions don't seem to be able to achieve this.

 

However you should be able to enable a timerA module clocked at 32kHz to generate a ~70Hz PWM.

 

32,768 / 70 = 468.11

 

Choose period to be 468 ticks. (CCR value)

Actual PWM freq = 32,768 / 468 = 70.01 Hz

 

Of course this can create a change on a pin, meaning the change is at 70Hz, but would create a 35Hz square wave.

Select a tick period of half to achieve 70Hz.

Share this post


Link to post
Share on other sites

 

EDIT:  70 Hz is very slow for a microcontroller. Can you use millis() and digitalWrite() to accomplish what you need?

This is one of those things I just had to try and it worked...

 

The neat thing about the MSP432 is that it is pretty fast and it has a RTOS.  So you can run the PWM in it's own little space and do other stuff at the same time easily.  The way I implemented it was to use the MultiBlink example as a starting place just to busy the MCU with other tasks.  Then I opened a new tab with the following code:

//#define PWM_PIN YELLOW_LED
#define PWM_PIN 2

// Calculate 30% duty periods in milliseconds for 70 Hz PWM
#define FREQ 70                        // frequency in Hz
#define PERIOD 1000000/FREQ            // total period in microseconds
#define DUTY 30                        // duty in %
#define PERIOD_ON (DUTY*PERIOD)/100
#define PERIOD_OFF ((100-DUTY)*PERIOD)/100

void setupYellowLed() {                
  // initialize the digital pin as an output.
  pinMode(PWM_PIN, OUTPUT);    
}

// the loop routine runs over and over again forever as a task.
void loopYellowLed() {
  digitalWrite(PWM_PIN, HIGH);        // turn the pin on (HIGH is the voltage level)
  delayMicroseconds(PERIOD_ON);       // wait for a duty on period
  digitalWrite(PWM_PIN, LOW);         // turn the pin off by making the voltage LOW
  delayMicroseconds(PERIOD_OFF);      // wait for a duty off period
}

I tried it first on LED1 which carries the strange name YELLOW_LED even though it isn't yellow.  Then I moved it over to Pin 2 and had a look on the oscilloscope with the duty set at 30%.  It actually makes a nice PWM signal at just over 70 Hz.

 

Meantime the MultiBlink is running without any apparent problem.  I'm sure you could overload it at some point though.

post-45284-0-96889300-1450602478_thumb.jpg

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...