Jump to content

Converting C code into Energia code

Recommended Posts


I have obtained a PWM code online written in C. What is the equivalent code if it is written in Energia - .ino file using Energia IDE libraries?

Here is the code:

#include <msp430g2553.h>

void setup()
  if (CALBC1_1MHZ==0xFF)       				 // If calibration constant erased
    while(1);                               // do not load, trap CPU!!
  DCOCTL = 0;                               // Select lowest DCOx and MODx settings
  BCSCTL1 = CALBC1_1MHZ;                    // Set range
  DCOCTL = CALDCO_1MHZ;                     // Set DCO step + modulation */

  P1DIR |= BIT6;
  P1SEL |= BIT6;
  TACCR0 = 20000-1; 					// PWM Period 20us (substract 1 because it's 0-based)
  TACCR1 = 10000; 						// CCR1 PWM duty cycle
  TACTL = TASSEL_2 + MC_1; 					// SMCLK, up mode
  _BIS_SR(CPUOFF); 							// Enter LPM0

Any help is much appreciated.

Link to post
Share on other sites

The setup as you have it will compile and work in Energia. You should omit the Stop watchdog and LPM0 part though if you plan to write anything in loop().

If you just want the code you have and not plan to use the loop you can rename void setup() to int main()

If you are asking how do I do PWM in Energia then have a look at the analogWrite() API (http://energia.nu/AnalogWrite.html). If you need to set the frequency of the signal to something else than the default (~490 Hz) use the setAnalogFrequency() API.

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.

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.

  • Create New...