Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430.h>
- #include <MSP430F5529.h>
- #include <stdint.h>
- #include <math.h>
- #define Button BIT1 // Button P2.1
- #define PI 3.14159
- int counter = 0;
- int Duty = 0; // PWM duty cycle
- int Period = 200; // PWM period
- void main(void) {
- WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
- P1DIR |= BIT2; // Set P1.2 as output pin
- P1SEL |= BIT2; // Enable special function of P1.2
- TA0CTL |= MC__STOP; // Stop Timer A
- TA0CTL |= TASSEL__SMCLK; // Set SMCLK as timer clock source
- TA0CTL |= ID_0; // Set clock divider to 1
- TA0CTL |= TACLR; // Clear timer counter
- TA0CTL |= TACLR; // Clear timer counter
- TA0CCTL1 |= CCIE; // Enable interrupt for compare register 1
- TA0CCTL1 |= OUTMOD_6; // Set PWM output mode
- TA0CCR0 = Period; // Set PWM period
- TA0CCR1 = Duty; // Set initial PWM duty cycle
- TA0CTL |= MC__UP; // Start timer in UP mode
- __bis_SR_register(GIE); // Global interrupt enable
- // Configure button P2.1 for interrupt
- P2REN |= Button; // Enable pull-up resistor for button
- P2IE |= Button; // Enable interrupt for button
- P2IES |= Button; // Set interrupt on falling edge
- while(1) {
- }
- }
- #pragma vector=TIMER0_A1_VECTOR
- __interrupt void Timer_A(void) {
- counter++; // Increase counter for each PWM cycle
- double freq = sin(counter*2*PI/Period) + 1.0/5.0*sin(5*counter*2*PI/Period) + 1.0/9.0*sin(9*counter*2*PI/Period);
- int duty = (int) (freq*Period/2.0); // Calculate duty cycle from frequency
- if(duty < 0) duty = 0; // Limit duty cycle to positive values
- if(duty > Period) duty = Period; // Limit duty cycle to maximum period
- TA0CCR1 = duty; // Set new duty cycle
- TA0CTL &= ~TAIFG; // Clear timer interrupt flag
- }
- /*
- #pragma vector=PORT2_VECTOR
- __interrupt void Port_2(void) {
- P2IE &= ~Button; // Disable interrupt for button
- P2IFG &= ~Button; // Clear button interrupt flag
- if(P2IES & Button) // Rising edge
- {
- counter = 0; // Reset counter to start frequency equation from the beginning
- TA0CTL |= MC__STOP; // Stop timer
- TA0CCR1 = 0; // Set duty cycle to 0
- P2IES &= ~Button; // Set interrupt on rising edge for next time
- }
- else // Falling edge
- {
- TA0CTL |= TACLR; // Clear timer counter
- TA0CCR1 = Duty; // Set initial duty cycle
- P2IES |= Button; // Set interrupt on falling edge for next time
- }
- P2IE |= Button; // Enable interrupt for button
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement