Advertisement
LshySVK

MSP430 Sinus funkcia

Apr 20th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | Source Code | 0 0
  1. #include <msp430.h>
  2. #include <MSP430F5529.h>
  3. #include <stdint.h>
  4. #include <math.h>
  5.  
  6.  
  7. #define Button BIT1    // Button P2.1
  8. #define PI 3.14159
  9.  
  10.  
  11. int counter = 0;
  12. int Duty = 0;         // PWM duty cycle
  13. int Period = 200;    // PWM period
  14.  
  15.  
  16. void main(void) {
  17.     WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
  18.  
  19.     P1DIR |= BIT2;              // Set P1.2 as output pin
  20.     P1SEL |= BIT2;              // Enable special function of P1.2
  21.     TA0CTL |= MC__STOP;         // Stop Timer A
  22.  
  23.     TA0CTL |= TASSEL__SMCLK;    // Set SMCLK as timer clock source
  24.     TA0CTL |= ID_0;             // Set clock divider to 1
  25.     TA0CTL |= TACLR;            // Clear timer counter
  26.  
  27.     TA0CTL |= TACLR;            // Clear timer counter
  28.     TA0CCTL1 |= CCIE;           // Enable interrupt for compare register 1
  29.     TA0CCTL1 |= OUTMOD_6;       // Set PWM output mode
  30.     TA0CCR0 = Period;           // Set PWM period
  31.     TA0CCR1 = Duty;             // Set initial PWM duty cycle
  32.     TA0CTL |= MC__UP;           // Start timer in UP mode
  33.     __bis_SR_register(GIE);     // Global interrupt enable
  34.  
  35.     // Configure button P2.1 for interrupt
  36.     P2REN |= Button;            // Enable pull-up resistor for button
  37.     P2IE |= Button;             // Enable interrupt for button
  38.     P2IES |= Button;            // Set interrupt on falling edge
  39.  
  40.     while(1) {
  41.  
  42.     }
  43. }
  44.  
  45. #pragma vector=TIMER0_A1_VECTOR
  46. __interrupt void Timer_A(void) {
  47.     counter++;                 // Increase counter for each PWM cycle
  48.     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);
  49.     int duty = (int) (freq*Period/2.0);     // Calculate duty cycle from frequency
  50.     if(duty < 0) duty = 0;                  // Limit duty cycle to positive values
  51.     if(duty > Period) duty = Period;        // Limit duty cycle to maximum period
  52.     TA0CCR1 = duty;                         // Set new duty cycle
  53.     TA0CTL &= ~TAIFG;                       // Clear timer interrupt flag
  54. }
  55.  
  56.  
  57. /*
  58. #pragma vector=PORT2_VECTOR
  59. __interrupt void Port_2(void) {
  60.     P2IE &= ~Button;                         // Disable interrupt for button
  61.     P2IFG &= ~Button;                        // Clear button interrupt flag
  62.     if(P2IES & Button)                       // Rising edge
  63.     {
  64.         counter = 0;                         // Reset counter to start frequency equation from the beginning
  65.         TA0CTL |= MC__STOP;                  // Stop timer
  66.         TA0CCR1 = 0;                         // Set duty cycle to 0
  67.         P2IES &= ~Button;                    // Set interrupt on rising edge for next time
  68.     }
  69.     else                                     // Falling edge
  70.     {
  71.         TA0CTL |= TACLR;                     // Clear timer counter
  72.         TA0CCR1 = Duty;                      // Set initial duty cycle
  73.         P2IES |= Button;                     // Set interrupt on falling edge for next time
  74.     }
  75.     P2IE |= Button;                          // Enable interrupt for button
  76. }*/
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement