/*

  • This program is for controlling a DC motor and a servo motor using MSP430F5529 microcontroller.
  • The program initializes the necessary ports and timers for the motors.
  • The DC motor is controlled using PWM signals generated by Timer_A2.
  • The servo motor is controlled using PWM signals generated by Timer_A1.
  • The program also uses Timer_A0 to periodically check the input from a keypad and adjust the servo motor accordingly. */

#include <msp430f5529.h>

#define CPU_F ((double)1000000) // CPU frequency in Hz

// Function to delay for a specified number of microseconds #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))

// Function to delay for a specified number of milliseconds #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))

unsigned int DC_motor = 100; // Duty cycle for DC motor PWM unsigned int i = 100; // Increment for servo motor PWM

// Function to initialize the servo motor void Duoji_init() { P2DIR |= BIT0; // Set P2.0 as output P2SEL |= BIT0; // Configure P2.0 for PWM output

TA1CCR0 = 20000; // Set PWM period to 20ms
TA1CCTL1 = OUTMOD_7; // Set CCR1 reset/set mode
TA1CCR1 = 1500; // Set CCR1 duty cycle to 1500us (neutral position)
TA1CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR

}

// Function to initialize the DC motor void DC_Motor_init() { P2DIR |= BIT4 + BIT5; // Set P2.4 and P2.5 as outputs P2SEL |= BIT4 + BIT5; // Configure P2.4 and P2.5 for PWM output

TA2CCR0 = 200; // Set PWM period to 0.2ms
TA2CCTL1 = OUTMOD_7; // Set CCR1 reset/set mode
TA2CCR1 = 0; // Set CCR1 duty cycle to 0 (motor off)
TA2CCTL2 = OUTMOD_7; // Set CCR2 reset/set mode
TA2CCR2 = 0; // Set CCR2 duty cycle to 0 (motor off)
TA2CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR

}

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer delay_ms(1000); // Delay for 1 second

Duoji_init(); // Initialize servo motor
DC_Motor_init(); // Initialize DC motor

TA0CCTL0 = CCIE; // Enable Timer0 A0 interrupt
TA0CCR0 = 5000; // Set Timer0 A0 period (adjust as needed)
TA0CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR

TA2CCR1 = DC_motor; // Set initial duty cycle for DC motor PWM
TA2CCR2 = DC_motor; // Set initial duty cycle for DC motor PWM

__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
__no_operation(); // For debugger

}

// Timer0 A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void TIMER0_A0_ISR(void) { switch(P6IN&0x1f) { // Read input from keypad (bits 0-4 of P6IN) case 0x0f: // If all keys are released TA1CCR1 = 1500+4i; // Set servo motor duty cycle to move right break; case 0x17: // If key 1 is pressed TA1CCR1 = 1500+2i; // Set servo motor duty cycle to move slightly right break; case 0x1b: // If key 2 is pressed TA1CCR1 = 1500; // Set servo motor duty cycle to center position break; case 0x1d: // If key 3 is pressed TA1CCR1 = 1500-2i; // Set servo motor duty cycle to move slightly left break; case 0x1e: // If key 4 is pressed TA1CCR1 = 1500-4i; // Set servo motor duty cycle to move left break; default: // If none of the keys match, do nothing break; }

#include msp430f5529h#define CPU_F double1000000#define delay_usx__delay_cycleslongCPU_Fdoublex10000000#define delay_msx__delay_cycleslongCPU_Fdoublex10000unsigned int DC_motor=100;unsigned int i=100;

原文地址: http://www.cveoy.top/t/topic/hNwD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录