/\n * This program is for controlling a DC motor and a servo motor using MSP430F5529 microcontroller.\n * The program initializes the necessary ports and timers for the motors.\n * The DC motor is controlled using PWM signals generated by Timer_A2.\n * The servo motor is controlled using PWM signals generated by Timer_A1.\n * The program also uses Timer_A0 to periodically check the input from a keypad and adjust the servo motor accordingly.\n /\n\n#include <msp430f5529.h>\n\n#define CPU_F ((double)1000000) // CPU frequency in Hz\n\n// Function to delay for a specified number of microseconds\n#define delay_us(x) __delay_cycles((long)(CPU_F(double)x/1000000.0))\n\n// Function to delay for a specified number of milliseconds\n#define delay_ms(x) __delay_cycles((long)(CPU_F(double)x/1000.0))\n\nunsigned int DC_motor = 100; // Duty cycle for DC motor PWM\nunsigned int i = 100; // Increment for servo motor PWM\n\n// Function to initialize the servo motor\nvoid Duoji_init() {\n P2DIR |= BIT0; // Set P2.0 as output\n P2SEL |= BIT0; // Configure P2.0 for PWM output\n\n TA1CCR0 = 20000; // Set PWM period to 20ms\n TA1CCTL1 = OUTMOD_7; // Set CCR1 reset/set mode\n TA1CCR1 = 1500; // Set CCR1 duty cycle to 1500us (neutral position)\n TA1CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR\n}\n\n// Function to initialize the DC motor\nvoid DC_Motor_init() {\n P2DIR |= BIT4 + BIT5; // Set P2.4 and P2.5 as outputs\n P2SEL |= BIT4 + BIT5; // Configure P2.4 and P2.5 for PWM output\n\n TA2CCR0 = 200; // Set PWM period to 0.2ms\n TA2CCTL1 = OUTMOD_7; // Set CCR1 reset/set mode\n TA2CCR1 = 0; // Set CCR1 duty cycle to 0 (motor off)\n TA2CCTL2 = OUTMOD_7; // Set CCR2 reset/set mode\n TA2CCR2 = 0; // Set CCR2 duty cycle to 0 (motor off)\n TA2CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR\n}\n\nvoid main(void) {\n WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer\n delay_ms(1000); // Delay for 1 second\n\n Duoji_init(); // Initialize servo motor\n DC_Motor_init(); // Initialize DC motor\n\n TA0CCTL0 = CCIE; // Enable Timer0 A0 interrupt\n TA0CCR0 = 5000; // Set Timer0 A0 period (adjust as needed)\n TA0CTL = TASSEL_2 + MC_1 + TACLR; // Select SMCLK as clock source, set up mode as up mode, clear TAR\n\n TA2CCR1 = DC_motor; // Set initial duty cycle for DC motor PWM\n TA2CCR2 = DC_motor; // Set initial duty cycle for DC motor PWM\n\n __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts\n __no_operation(); // For debugger\n}\n\n// Timer0 A0 interrupt service routine\n#pragma vector=TIMER0_A0_VECTOR\n__interrupt void TIMER0_A0_ISR(void) {\n switch(P6IN&0x1f) { // Read input from keypad (bits 0-4 of P6IN)\n case 0x0f: // If all keys are released\n TA1CCR1 = 1500+4i; // Set servo motor duty cycle to move right\n break;\n case 0x17: // If key 1 is pressed\n TA1CCR1 = 1500+2i; // Set servo motor duty cycle to move slightly right\n break;\n case 0x1b: // If key 2 is pressed\n TA1CCR1 = 1500; // Set servo motor duty cycle to center position\n break;\n case 0x1d: // If key 3 is pressed\n TA1CCR1 = 1500-2i; // Set servo motor duty cycle to move slightly left\n break;\n case 0x1e: // If key 4 is pressed\n TA1CCR1 = 1500-4i; // Set servo motor duty cycle to move left\n break;\n default: // If none of the keys match, do nothing\n break;\n }\n

MSP430F5529 DC Motor & Servo Control with Keypad Input

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

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