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.

Share this post

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now