Jump to content
Sign in to follow this  

PWM led

Recommended Posts

HI there,



I'm playing around PWM, for now just with leds. I checked the teory, but I need to get a simple example. 

I'm working with msp430-gcc, this code was the easier that I found. They seem to use a shorter syntax (P1OUT_bit.P1OUT_0) that I don't get.

I also have few errors, mentioned below.




#include <msp430g2553.h>
#include <intrinsics.h> // Intrinsic functions
int main( void )
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL3 = LFXT1S_2; // Select ACLK from VLO (no crystal)
//  Initialize ports
P1OUT = 0; // Preset LED off (and everything else)
P1DIR = BIT0; // Configure P1.0 for LED output
P1REN = 0xFE; // Activate pullups on P1[7:1]
// Set up timer channel 1
TACCR1 = 0x2000; // Test compare action
TACCTL1 = CCIE; // Interrupts on TACCR1 compare
// Start timer: SMCLK, prescale/8, continuous mode, interrupts, clear
for (;;) {
LPM0; // LPM0 with interrupts (need SMCLK)
return 0; // Infinite loop
// Interrupt service routine for TACCR1.CCIFG, called when TAR -> TACCR1
//   and for TAIFG, called when TAR -> 0; share vector
// Interrupt flag cleared automatically with access to TAIV
#pragma vector = TIMER_A1_VECTOR
__interrupt void TIMER_A1_ISR (void) // ISR for TACCR1 CCIFG and TAIFG
// switch (TAIV) { // Standard switch
switch (__even_in_range(TAIV, 10)) { // Faster intrinsic fn
case 0: // No interrupt pending
break; // No action
case TAIV_CCIFG1: // Vector 2: CCIFG1
P1OUT_bit.P1OUT_0 = 0; // End of duty cycle: Turn off LED
case TAIV_TAIFG: // Vector A: TAIFG, last value possible
P1OUT_bit.P1OUT_0 = 1; // Start of PWM cycle: Turn on LED
default: // Should not be possible
for (;;) { // Disaster! Loop here for ever







blink3.c:30:9: warning: malformed

Share this post

Link to post
Share on other sites

I don't know what P1OUT_bit is, but it probably doesn't work with mspgcc.


FWIW there should be easier ways to drive a PWM signal from these chips, using Timer_A's dedicated TA0.x outputs with the requisite P1SEL/P1SEL2 bits set.  This is an autonomously-hardware-driven PWM system so you don't need any interrupts.  Your example is a software-driven PWM setup where IRQs/ISRs are used to enact the events.  Search around for examples.

Share this post

Link to post
Share on other sites

Sorry, but I wouldn't try bother anyone in this forum without doing some kind of research first. I found many tutorials on PWM theory, application letters....tons of examples on Arduino and Energia, but no single working example on PWM leds with "Native" code.

Of course I could be failing on the obvious, but I just cant find one.

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.

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...