Last active
November 15, 2023 17:05
-
-
Save aarmot/4547061 to your computer and use it in GitHub Desktop.
MSP430 PWM duty cycle drift demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "msp430.h" | |
/* | |
Simple PWM generator using Timer A | |
Output P1.0, PWM period about 2 ms | |
Duty cycle changes gradually from 5% to 95% | |
Compiled with msp430-gcc | |
aarmot 2013-01-16 | |
*/ | |
#define PIN BIT0 | |
#define PERIOD 2000 | |
#define MIN_DUTY 100 | |
#define MAX_DUTY 1900 | |
unsigned int duty = MIN_DUTY; | |
void main(void) | |
{ | |
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog | |
P1DIR |= PIN; // Output direction | |
P1OUT = PIN; // Out = 1 | |
TA0CCTL0 = CCIE; // CCR0 interrupt enabled | |
TA0CCR0 = duty; | |
TA0CTL = TASSEL_2 + MC_2; // SMCLK, contmode | |
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt | |
} | |
__attribute__((__interrupt__(TIMER0_A0_VECTOR))) | |
void Timer0_A(void) | |
{ | |
P1OUT ^= PIN; // Toggle output | |
if(P1OUT & PIN) { | |
duty++; | |
if(duty > MAX_DUTY) duty = MIN_DUTY; | |
TA0CCR0 += duty; | |
} else { | |
TA0CCR0 += PERIOD - duty; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment